您好,登錄后才能下訂單哦!
小編給大家分享一下KVM虛擬機監控的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!
1 對CPU的監控
Python 代碼
import libvirt
import os
import time
conn=libvirt.open("qemu:///system")
if conn==None:
print "fail to connect hypervisor"
sys.exit(1)
try:
dom0=conn.lookupByID(85)#根據OpenStack創建的Instance ID得到相應的Domain對象
except:
print "fail to find the domain by ID"
sys.exit(1)
Pstart_time=time.time() #取當前時間
Dstart_time=dom0.info()[4]#直接獲取DomainInfo中的CPU時間信息
time.sleep(2)
Dstop_time=dom0.info()[4]
Pstop_time=time.time()
core_num=int(dom0.info()[3])#獲取DomainIndo中的core數量信息
#CPU利用率計算公式-CPU時間差/時間間隔/1000000000/核的數量*100=CPU利用率
cpu_usage=(Dstart_time-Dstop_time)/(Pstart_time-Pstop_time)/1000000000/core_num*100
cpu_usage=cpu_usage if (cpu_usage>0) else 0.0
cpu_usage=cpu_usage if (cpu_usage<100) else 100.0
print cpu_usage
2 對內存的監控
python代碼
def get_memory(pid):#定義獲取當前已使用的內存的函數
mem=0
#linux下 /proc/pid(進程ID)/smaps 下保存的是進程內存映像信息,比同一目錄下的maps文件更詳細些
for line in file('/proc/%d/smaps' % int(pid),'r'):
if re.findall('Private_',line):
#統計Private內存信息量
mem+=int(re.findall('(\d+)',line)[0])
return mem
#根據實例名獲取進程ID
pid=(os.popen("ps aux|grep "+dom0.name()+" | grep -v 'grep' | awk '{print $2}'").readlines()[0])
memstatus=get_memory(pid)
memusage='%.2f' % (int(memstatus)*100.0/int(dom0.info()[2]))
print memusage
驗證方法: 可以SSH到相應的虛擬實例上,如果是Linux 系統可以用free -m指令查看內存使用率
3 對磁盤的監控
創建虛擬機應用實例時,會生成相應的XML文件來表明實例的信息
def get_devices(dom,path,devs):#該函數用于獲取XML中某節點的值
tree=ElementTree.fromstring(dom.XMLDesc(0))#將XML文件轉換為XML樹對象
devices=[]
for target in tree.findall(path):
dev=target.get(devs)
if not dev in devices:
devices.append(dev)
return devices
def get_blockStats(dom):#獲取磁盤狀態信息函數 包含磁盤讀入的總比特數和寫出的總比特數
block_status={}
disks=get_devices(dom,"devices/disk/target","dev")
for block in disks:
block_status[block]=dom.blockStats(block)
return block_status
block_status0={}
block_status1={}
block_status0=get_blockStats(dom0)
time.sleep(2)
block_status1=get_blockStats(dom0)
block_info=[]
for block in get_devices(dom0,"devices/disk/source","file"):
block_info.append(dom0.blockInfo(block,0))#獲取磁盤信息 其中0為默認傳入的參數
for domBlockInfo in block_info:
print "logical size in bytes :%s" % domBlockInfo[0]
print "highest allocated extent in bytes :%s" % domBlockInfo[1]
print "physical size in bytes :%s" % domBlockInfo[2]
print "disk usage :%s" % str(domBlockInfo[1]/1.0/domBlockInfo[0]*100)[:5]
for block in get_devices(dom0,"devices/disk/target","dev"):
print "rd_speed :%s" % str((block_status1[block][1]-block_status0[block][1])/2048)
print "wr_speed :%s" % str((block_status1[block][3]-block_status0[block][3])/2048)
驗證方法: 可以SSH到相應的虛擬實例上,如果是Linux 系統可以用df -h指令查看磁盤使用率
4 對網絡的監控
def get_nicInfo(nics):#獲取網絡信息包括Receive的總比特數和Transmit的總比特數
net_status={}
#通過 cat /proc/net/dev 命令查看網絡信息
for nic in nics:
net_status[nic]=[os.popen("cat /proc/net/dev |grep -w '"+nic+"' |awk '{print $10}'").readlines()[0][:-1],os.popen("cat /proc/net/dev |grep -w '"+nic+"' |awk '{print $2}'").readlines()[0][:-1]]
return net_status
net_status0={}
net_status1={}
#獲取網卡名稱
nics=get_devices(dom0,"devices/interface/target","dev")
net_status0=get_nicInfo(nics)
time.sleep(2)
net_status1=get_nicInfo(nics)
for nic in nics:
print "netcard_name :%s" % nic
print "transmit_speed :%s" % str((int(net_status1[nic][0])-int(net_status0[nic][0]))/2048)
print "receive_speed :%s" % str((int(net_status1[nic][1])-int(net_status0[nic][1]))/2048)
參考:
Libvirt 官網API定義
virDomainBlockInfo
struct virDomainBlockInfo {
unsigned long long | capacity | logical size in bytes of the block device backing image |
unsigned long long | allocation | highest allocated extent in bytes of the block device backing image |
unsigned long long | physical | physical size in bytes of the container of the backing image |
}
virDomainBlockStatsStruct
struct virDomainBlockStatsStruct {
long long | rd_req | number of read requests |
long long | rd_bytes | number of read bytes |
long long | wr_req | number of write requests |
long long | wr_bytes | number of written bytes |
long long | errs | In Xen this returns the mysterious 'oo_req'. |
}
以上是“KVM虛擬機監控的示例分析”這篇文章的所有內容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。