提供不同的连接方式,注意连接使用过后需要关闭。
from __future__ import print_function
import sys
import libvirt
# conn = libvirt.open('qemu+ssh://IP/system'
conn = libvirt.open('qemu:///system')
if conn == None:
print('Failed to open connection to qemu:///system', file=sys.stderr)
exit(1)
conn.close()
2.2 列出Domains
conn.listAllDomains(type)方法返回指定类型的domains列表,type参数可以设置以下类型
VIR_CONNECT_LIST_DOMAINS_ACTIVE
VIR_CONNECT_LIST_DOMAINS_INACTIVE
VIR_CONNECT_LIST_DOMAINS_PERSISTENT
VIR_CONNECT_LIST_DOMAINS_TRANSIENT
VIR_CONNECT_LIST_DOMAINS_RUNNING
VIR_CONNECT_LIST_DOMAINS_PAUSED
VIR_CONNECT_LIST_DOMAINS_SHUTOFF
VIR_CONNECT_LIST_DOMAINS_OTHER
VIR_CONNECT_LIST_DOMAINS_MANAGEDSAVE
VIR_CONNECT_LIST_DOMAINS_NO_MANAGEDSAVE
VIR_CONNECT_LIST_DOMAINS_AUTOSTART
VIR_CONNECT_LIST_DOMAINS_NO_AUTOSTART
VIR_CONNECT_LIST_DOMAINS_HAS_SNAPSHOT
VIR_CONNECT_LIST_DOMAINS_NO_SNAPSHO
如果设置为0,则返回所有活动和关机的domain
2.2 获取监控数据
VM的监控信息主要是CPU使用率,内存使用率,Disk使用率,Disk I/O,Network I/O。其中,CPU的使用率,Disk I/O,Network I/O并不能直接获取,需要经过计算获得。
2.2.1 CPU使用率
libvirt中并不能直接获取到虚拟机的CPU使用率,但是可以通过CPUTIME来计算出实际使用率。计算的公式为:
首先得到一个周期差:cpu_time_diff = cpuTimenow — cpuTimet seconds ago
然后根据这个差值计算实际使用率:%CPU = 100 × cpu_time_diff / ((now- seconds ago) × vcpus × 109).CPUtime可以使用domain的info函数获得,如下:
from __future__ import print_function
import sys
import libvirt
from xml.dom import minidom
conn = libvirt.open('qemu:///system')
dom = conn.lookupByName(domName)
dominfo = dom.info()
print('The state is ' + str(dominfo[0]))
print('The max memory is ' + str(dominfo[1]))
print('The memory is ' + str(dominfo[2]))
print('The number of cpus is ' + str(dominfo[3]))
print('The cpu time is ' + str(dominfo[4]))
conn.close()
info()函数返回一个数组,下标为4的值即为CPUTime。
2.2.2 内存使用率
内存的使用情况可以函数dom.memoryStats()获得,如下:
from __future__ import print_function
import sys
import libvirt
domName = 'Fedora22-x86_64-1'
conn = libvirt.open('qemu:///system')
if conn == None:
print('Failed to open connection to qemu:///system', file=sys.stderr)
exit(1)
dom = conn.lookupByID(5)
if dom == None:
print('Failed to find the domain '+domName, file=sys.stderr)
exit(1)
stats = dom.memoryStats()
print('memory used:')
for name in stats:
print(' '+str(stats[name])+' ('+name+')')
conn.close()
exit(0)
可以在Virsh命令行中验证获取的值是否正确。
2.2.3 Disk使用率
对于一个VM的DISK,在DOMAIN XML的device根据标示获取,有关Disk部分的XML如下所示。
<devices>
<disk type='file' snapshot='external'>
<driver name="tap" type="aio" cache="default"/>
<source file='/var/lib/xen/images/fv0' startupPolicy='optional'>
<seclabel relabel='no'/>
</source>
<target dev='hda' bus='ide'/>
/*这个'hda'就是需要的参数 */
<iotune>
<total_bytes_sec>10000000</total_bytes_sec>
<read_iops_sec>400000</read_iops_sec>
<write_iops_sec>100000</write_iops_sec>
</iotune>
<boot order='2'/>
<encryption type='...'>
...
</encryption>
<shareable/>
<serial>
...
</serial>
</disk>
...
</devices>
使用函数domain.blockInfo(disk,0)获得磁盘的总量和已使用量
2.2.4 Disk I/O
可以使用 domain. blockStats ()方法,该方法需要传递一个Disk的参数,这个参数可以通过XML文件取得:
<devices>
<disk type='file' snapshot='external'>
<driver name="tap" type="aio" cache="default"/>
<source file='/var/lib/xen/images/fv0' startupPolicy='optional'>
<seclabel relabel='no'/>
</source>
<target dev='hda' bus='ide'/>
/*这个'hda'就是需要的参数 */
<iotune>
<total_bytes_sec>10000000</total_bytes_sec>
<read_iops_sec>400000</read_iops_sec>
<write_iops_sec>100000</write_iops_sec>
</iotune>
<boot order='2'/>
<encryption type='...'>
...
</encryption>
<shareable/>
<serial>
...
</serial>
</disk>
...
</devices>
2.2.5 Network I/O
可以使用 domain.interfaceStats()方法,该方法需要传递一个虚拟网卡接口的参数,这个参数可以通过XML文件取得:
<interface type='bridge'>
<mac address='00:16:3e:74:03:53'/>
<source bridge='xenbr0'/>
<script path='vif-bridge'/>
<target dev='vif1.0 '/>
/*这个vif1.0就是需要的参数 */
</interface>
网卡的I/O值返回是一个数组,根据自己需要获取对应的值。
3. 总结
通过对VM监控信息的获取,可以实时的了解Cloud环境中各个VM的负载和使用情况,合理的调配计算资源,或者关闭长时间没有使用的VM,做到Cloud资源的合理利用和运行环境的健康可靠性。