python的kvm虚拟机状态值监控

 python获取kvm虚拟机状态值

测试代码,其中只包含了基本的信息,所有的信息都是通过libvirt的api来获取的。

具体的实现代码可以优化。

 

 
 
  1. import libvirt 
  2. import memcache 
  3. import os 
  4. import time 
  5. from xml.etree import ElementTree as ET 
  6.  
  7.  
  8. mc = memcache.Client(['192.168.0.112:11211']) 
  9. while True: 
  10.     conn = libvirt.open('qemu:///system') 
  11.  
  12.  
  13.     dom = conn.listDomainsID() 
  14.     for i in dom: 
  15.         vmm_id = conn.lookupByID(i) 
  16.         vmm_name = vmm_id.name() 
  17.         if "wrksp" in vmm_name: 
  18.             try: 
  19.         #    vmm_namevmm_name_id = vmm_name.split('-')[1] 
  20.                 vmm_id_str = str(vmm_name) 
  21.                 vmmid = vmm_id_str.split('-')[1] 
  22.                 print vmmid 
  23.                 vmm_dom_xml = vmm_id.XMLDesc(0) 
  24.                 vmm_xml_name = vmm_id_str + '.' + 'xml' 
  25.                 vmm_xml_path = '/tmp/' + vmm_xml_name 
  26.                 print vmm_xml_path 
  27.                 if os.path.exists(vmm_xml_path): 
  28.                     pass 
  29.                 else : 
  30.                     os.mknod(vmm_xml_path) 
  31.              
  32.                 xml_open = open(vmm_xml_path,'w') 
  33.                 for lines in vmm_dom_xml: 
  34.                     xml_open.write(lines) 
  35.                 xml_open.close() 
  36.                  
  37.                 vmm_xml_open = ET.parse(vmm_xml_path) 
  38.                 path_list=[] 
  39.                 path = vmm_xml_open.findall('//source') 
  40.                 for i in path: 
  41.                     file1 = i.attrib 
  42.                     filename=file1.get('file') 
  43.                     if filename: 
  44.                         path_list.append(filename) 
  45.                  
  46.     #    print path_list 
  47.                 interface_list = [] 
  48.                 interface = vmm_xml_open.findall('//target') 
  49.                 for j in interface: 
  50.                     interface_network = j.attrib 
  51.                     dev1 = interface_network.get('dev') 
  52.                     dev3 = 'vnet' 
  53.                     dev2 = str(dev1) 
  54.                     if dev3 in dev2: 
  55.                         interface_list.append(dev1) 
  56.                  
  57.         #    print interface_list 
  58.                  
  59.                 totalrd = vmmid + 'rd' 
  60.                 totalrd_byte = 0 
  61.                 totalwr = vmmid + 'wr' 
  62.                 totalwr_byte = 0 
  63.                 totalrx = vmmid + 'rx' 
  64.                 totalrx_byte = 0 
  65.                 totaltx = vmmid + 'tx' 
  66.                 totaltx_byte = 0 
  67.                 totalrp = vmmid + 'rp' 
  68.                 totalrx_packet = 0 
  69.                 totaltp = vmmid + 'tp' 
  70.                 totaltx_packet = 0 
  71.              
  72.                 for p_w_picpath_file in  path_list:   
  73.         #        print p_w_picpath_file 
  74.         #        size = vmm_id.blockInfo(p_w_picpath_file,0) 
  75.                     block_status = vmm_id.blockStatsFlags(p_w_picpath_file,0) 
  76.          
  77.                     totalrd_bytetotalrd_byte = totalrd_byte + block_status['rd_bytes'] 
  78.                     totalwr_bytetotalwr_byte = totalwr_byte + block_status['wr_bytes'] 
  79.                 print totalrd_byte    
  80.                 print totalwr_byte 
  81.         #        print totalwr_byte 
  82.              
  83.              
  84.                 for interfaceinfo_path in interface_list: 
  85.                     interfaceinfo = vmm_id.interfaceStats(interfaceinfo_path) 
  86.                     totalrx_bytetotalrx_byte = totalrx_byte + interfaceinfo[0] 
  87.                     totalrx_packettotalrx_packet = totalrx_packet + interfaceinfo[1] 
  88.                     totaltx_bytetotaltx_byte = totaltx_byte + interfaceinfo[4] 
  89.                     totaltx_packettotaltx_packet = totaltx_packet + interfaceinfo[5] 
  90.              
  91.                 totalmem = vmmid + 'actual' 
  92.                 totalmem_data = 0 
  93.                 rssmem = vmmid + 'rss' 
  94.                 rssmem_data = 0 
  95.                 mem_status = vmm_id.memoryStats() 
  96.                 totalmem_data = mem_status['actual'] 
  97.                 rssmem_data = mem_status['rss']   
  98.         #    print totalmem_data 
  99.         #    print rssmem_data    
  100.              
  101.                 totalcpu = vmmid + 'cpu' 
  102.                 totalcpu_usage = 0 
  103.                 cpu_time =  vmm_id.info()[4] 
  104.                 timetime1 = time.time() 
  105.                 time.sleep(3) 
  106.                 timetime2 = time.time() 
  107.                 cpu_new_time = vmm_id.info()[4] 
  108.                 totalcpu_usage = int(((cpu_new_time - cpu_time)/((time2 - time1)*10000000))*100) 
  109.                 print totalcpu_usage 
  110.              
  111.                 mc.set('centosliran', '1234', 20) 
  112.                 mc.set('centos_clusterliran','345',20) 
  113.                 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)   
  114.                     #value = mc.get('key') 
  115.                    # print value 
  116.             except: 
  117.                 pass 

 

    原文作者:liran728729
    原文地址: https://blog.51cto.com/liran728729/1152863
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞