Python's KVM virtual machine status value monitoring

Source: Internet
Author: User


Python Gets the KVM virtual machine status value

Test the code, which contains only basic information, all of which are obtained through the 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


This article is from the "Target: India" blog, please be sure to keep this source http://shunzi115.blog.51cto.com/5184443/1826170

Python's KVM virtual machine status value monitoring

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.