LinuxEye - Linux系统教程

LinuxEye - Linux系统教程

当前位置: 主页 > 脚本编程 >

python的kvm虚拟机状态值监控

时间:2013-03-13 21:12来源:未知 编辑:admin 点击:
python获取kvm虚拟机状态值 测试代码,其中只包含了基本的信息,所有的信息都是通过libvirt的api来获取的。 具体的实现代码可以优化。 import libvirt import memcache import os import time from xml.e
python获取kvm虚拟机状态值
测试代码,其中只包含了基本的信息,所有的信息都是通过libvirt的api来获取的。

具体的实现代码可以优化。
import libvirt 
import memcache 
import os 
import time 
from xml.etree import ElementTree as ET 

mc = memcache.Client(['192.168.0.112:11211']) 
while True: 
    conn = libvirt.open('qemu:///system') 
 
    dom = conn.listDomainsID() 
    for i in dom: 
        vmm_id = conn.lookupByID(i) 
        vmm_name = vmm_id.name() 
        if "wrksp" in vmm_name: 
            try: 
        #    vmm_namevmm_name_id = vmm_name.split('-')[1] 
                vmm_id_str = str(vmm_name) 
                vmmid = vmm_id_str.split('-')[1] 
                print vmmid 
                vmm_dom_xml = vmm_id.XMLDesc(0) 
                vmm_xml_name = vmm_id_str + '.' + 'xml' 
                vmm_xml_path = '/tmp/' + vmm_xml_name 
                print vmm_xml_path 
                if os.path.exists(vmm_xml_path): 
                    pass 
                else : 
                    os.mknod(vmm_xml_path) 
             
                xml_open = open(vmm_xml_path,'w') 
                for lines in vmm_dom_xml: 
                    xml_open.write(lines) 
                xml_open.close() 
                 
                vmm_xml_open = ET.parse(vmm_xml_path) 
                path_list=[] 
                path = vmm_xml_open.findall('//source') 
                for i in path: 
                    file1 = i.attrib 
                    filename=file1.get('file') 
                    if filename: 
                        path_list.append(filename) 
                 
    #    print path_list 
                interface_list = [] 
                interface = vmm_xml_open.findall('//target') 
                for j in interface: 
                    interface_network = j.attrib 
                    dev1 = interface_network.get('dev') 
                    dev3 = 'vnet' 
                    dev2 = str(dev1) 
                    if dev3 in dev2: 
                        interface_list.append(dev1) 
                 
        #    print interface_list 
                 
                totalrd = vmmid + 'rd' 
                totalrd_byte = 0 
                totalwr = vmmid + 'wr' 
                totalwr_byte = 0 
                totalrx = vmmid + 'rx' 
                totalrx_byte = 0 
                totaltx = vmmid + 'tx' 
                totaltx_byte = 0 
                totalrp = vmmid + 'rp' 
                totalrx_packet = 0 
                totaltp = vmmid + 'tp' 
                totaltx_packet = 0 
             
                for image_file in  path_list:   
        #        print image_file 
        #        size = vmm_id.blockInfo(image_file,0) 
                    block_status = vmm_id.blockStatsFlags(image_file,0) 
         
                    totalrd_bytetotalrd_byte = totalrd_byte + block_status['rd_bytes'] 
                    totalwr_bytetotalwr_byte = totalwr_byte + block_status['wr_bytes'] 
                print totalrd_byte    
                print totalwr_byte 
        #        print totalwr_byte 
             
             
                for interfaceinfo_path in interface_list: 
                    interfaceinfo = vmm_id.interfaceStats(interfaceinfo_path) 
                    totalrx_bytetotalrx_byte = totalrx_byte + interfaceinfo[0] 
                    totalrx_packettotalrx_packet = totalrx_packet + interfaceinfo[1] 
                    totaltx_bytetotaltx_byte = totaltx_byte + interfaceinfo[4] 
                    totaltx_packettotaltx_packet = totaltx_packet + interfaceinfo[5] 
             
                totalmem = vmmid + 'actual' 
                totalmem_data = 0 
                rssmem = vmmid + 'rss' 
                rssmem_data = 0 
                mem_status = vmm_id.memoryStats() 
                totalmem_data = mem_status['actual'] 
                rssmem_data = mem_status['rss']   
        #    print totalmem_data 
        #    print rssmem_data    
             
                totalcpu = vmmid + 'cpu' 
                totalcpu_usage = 0 
                cpu_time =  vmm_id.info()[4] 
                timetime1 = time.time() 
                time.sleep(3) 
                timetime2 = time.time() 
                cpu_new_time = vmm_id.info()[4] 
                totalcpu_usage = int(((cpu_new_time - cpu_time)/((time2 - time1)*10000000))*100) 
                print totalcpu_usage 
             
                mc.set('centosliran', '1234', 20) 
                mc.set('centos_clusterliran','345',20) 
                mc.set_multi({totalrd:totalrd_byte,totalwr:totalwr_byte,totalrx:totalrx_byte,totaltx:totaltx_byte,totalrp:totalrx_packet,totaltp:totaltx_packet,totalmem:totalmem_data,rssmem:rssmem_data,totalcpu:totalcpu_usage},60)   
                    #value = mc.get('key') 
                   # print value 
            except: 
                pass

转载请保留固定链接: http://www.linuxeye.com/program/1351.html

------分隔线----------------------------
标签:PythonKVM
评论列表(网友评论仅供网友表达个人看法,并不表明本站同意其观点或证实其描述)
栏目列表
推荐内容