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

kvm

相关推荐