WHEREIS

#!/usr/bin/env python

import sys, time

import XenAPI


username = 'root'

password = '1'


def main(session):

        """

        Lists inventory of XenServer virtual machines, grouped by their network names.

        This allows a vm being reported in multiple groups (result of having multiple NICs)

        :return:

        Ansible pluggable dynamic inventory, as a Python json serializable dictionary.

        """


print ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>   " + "xen server :" , url


        try:

            all_vms = session.xenapi.VM.get_all()


            for vm in all_vms:

                record = session.xenapi.VM.get_record(vm)

                if not record["is_control_domain"] and\

                   not (record["is_a_template"]) and\

                   record["power_state"] == "Running":


                    for i in record['VIFs']:

    name = record["name_label"].encode('utf8')

                        net_ref = session.xenapi.VIF.get_record(i)['network']

                        network = session.xenapi.network.get_record(net_ref)

                        #inventory.setdefault(network['name_label'], []).append(record['name_label'])

                        # xen_tools don't provide an API reporting FQDN / hostname.

                        # Thus we need to define ansible_host with the reported IP of a vm,

                        # since using a vm name, and enforcing naming conventions through XenCenter

                        #  could cause more trouble...

                        # Let's use first assigned IP.

                        ip = session.xenapi\

                            .VM_guest_metrics.get_record(record['guest_metrics'])\

                            .get("networks", {}).get('0/ip')

print name, "\t", ip

        except XenAPI.Failure as e:

            print("[Error] : " + str(e))

            exit(1)


if __name__ == "__main__":

    if len(sys.argv) <> 2:

        print "Usage:"

        print sys.argv[0], " <ip>"

        sys.exit(1)

    url = sys.argv[1]

    # First acquire a valid session by logging in:

    session = XenAPI.Session('https://%s' % url)

    session.xenapi.login_with_password(username, password, "2.3",

                                   "Example migration-demo v0.1")

    try:

            main(session)

    finally:

        session.xenapi.session.logout()

'Language > Python' 카테고리의 다른 글

python 주식정보 가져오기  (0) 2019.02.08
코딩 연습  (0) 2018.04.18

이 글을 공유합시다

facebook twitter kakaoTalk kakaostory naver band
loading