Пример #1
0
 def getVmInstanceBaseInfo(self, server_ip, server_id):
     '''获取所有实例的基本信息'''
     dataList = []
     for ins in self.conn.listAllDomains():
         raw_xml = ins.XMLDesc(0)
         xml = minidom.parseString(raw_xml)
         try:
             cpu = xml.getElementsByTagName('vcpu')[0].getAttribute(
                 'current')
             if len(cpu) == 0:
                 cpu = xml.getElementsByTagName(
                     'vcpu')[0].childNodes[0].data
         except:
             cpu = 0
         try:
             mem = ins.info()[2] / 1024
         except:
             mem = 0
         #获取vnc端口信息
         try:
             vnc_port = xml.getElementsByTagName(
                 'graphics')[0].getAttribute("port")
         except:
             vnc_port = 0
         ntkList = []
         #获取主机Mac地址
         for nk in xml.getElementsByTagName('interface'):
             if nk.getElementsByTagName('mac') and nk.getElementsByTagName(
                     'target'):
                 ntkData = dict()
                 mac = nk.getElementsByTagName('mac')[0].getAttribute(
                     'address')
                 name = nk.getElementsByTagName('target')[0].getAttribute(
                     "dev")
                 ntkData['name'] = name
                 ntkData['mac'] = mac
                 ntkList.append(ntkData)
         data = dict()
         data["name"] = ins.name()
         data["status"] = ins.state()[0]
         data["cpu"] = cpu
         data["server_ip"] = server_ip
         data["server_id"] = server_id
         data["mem"] = mem
         data["vnc"] = vnc_port
         data['token'] = TokenUntils.makeToken(str=server_ip + data["name"])
         data['netk'] = ntkList
         dataList.append(data)
     return dataList
Пример #2
0
    def getVmInstanceInfo(self, instance, server_ip, vMname):
        '''查询单个实例信息'''
        vmPoolList = []
        pools = self.conn.listAllStoragePools(0)
        #获取存储池里面的卷
        for pls in pools:
            for vol in pls.listVolumes():
                data = {}
                data[pls.name()] = pls.storageVolLookupByName(vol).path()
                vmPoolList.append(data)
        if instance:
            domData = {}
            status = instance.state()
            domData['status'] = status[0]
            raw_xml = instance.XMLDesc(0)
            xml = minidom.parseString(raw_xml)
            diskList = []
            #获取实例的磁盘信息
            for disk in xml.getElementsByTagName('disk'):
                #判断设备类型是不是磁盘
                if disk.getAttribute("device") == 'disk':
                    if disk.getElementsByTagName('source'):
                        data = {}
                        try:
                            disk_name = disk.getElementsByTagName(
                                'source')[0].getAttribute("file")
                        except:
                            disk_name = disk.getElementsByTagName(
                                'source')[0].getAttribute("dev")
                        #判断卷存在那个存储池里面
                        for vol in vmPoolList:
                            for p, v in vol.iteritems():
                                if disk_name == v: data['disk_pool'] = p
                        data['disk_path'] = disk_name
                        data['disk_sn'] = disk.getElementsByTagName(
                            'target')[0].getAttribute("dev")
                        try:
                            data['disk_size'] = instance.blockInfo(
                                disk_name)[0] / 1024 / 1024 / 1024
                            data['disk_capacity'] = instance.blockInfo(
                                disk_name)[1] / 1024 / 1024 / 1024
                            data['disk_per'] = round(
                                float(data['disk_capacity']) /
                                data['disk_size'] * 100, 2)
                        except:
                            data['disk_size'] = 0
                            data['disk_capacity'] = 0
                            data['disk_per'] = 0
                        diskList.append(data)
            #获取虚拟机实例的网卡名称
            nkList = []
            for nk in xml.getElementsByTagName('interface'):
                if nk.getElementsByTagName('target'):
                    nk_name = nk.getElementsByTagName(
                        'target')[0].getAttribute("dev")
                    nkList.append(nk_name)
            #获取虚拟机实例内存的容量信息
            try:
                mem = instance.info()[2] / 1024
            except:
                mem = 0
            #mem利用率与ip地址
            try:
                if status[0] == 5: domData['mem_per'] = 0
                else:
                    mem_per = round(
                        float(instance.memoryStats().get('rss')) /
                        instance.memoryStats().get('actual') * 100, 2)
                    if mem_per > 100: domData['mem_per'] = 100
                    else: domData['mem_per'] = mem_per

            except Exception, e:
                domData['mem_per'] = 0
            #获取虚拟机实例CPU信息
            try:
                cpu = xml.getElementsByTagName('vcpu')[0].getAttribute(
                    'current')
                if len(cpu) == 0:
                    cpu = xml.getElementsByTagName(
                        'vcpu')[0].childNodes[0].data
            except:
                cpu = 0
            #获取vnc端口信息
            try:
                vnc_port = xml.getElementsByTagName(
                    'graphics')[0].getAttribute("port")
            except:
                vnc_port = 0

            domData['disks'] = diskList
            domData['netk'] = nkList
            domData['mem'] = mem
            domData['cpu'] = cpu
            domData['vnc'] = vnc_port
            domData['name'] = vMname
            #生成noVNC需要的token
            domData['token'] = TokenUntils.makeToken(str=server_ip + vMname)
            return domData