真实的国产乱ⅩXXX66竹夫人,五月香六月婷婷激情综合,亚洲日本VA一区二区三区,亚洲精品一区二区三区麻豆

成都創(chuàng)新互聯(lián)網(wǎng)站制作重慶分公司

KVM虛擬機監(jiān)控的示例分析

小編給大家分享一下KVM虛擬機監(jiān)控的示例分析,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

10余年專注成都網(wǎng)站制作,企業(yè)網(wǎng)站設(shè)計,個人網(wǎng)站制作服務(wù),為大家分享網(wǎng)站制作知識、方案,網(wǎng)站設(shè)計流程、步驟,成功服務(wù)上千家企業(yè)。為您提供網(wǎng)站建設(shè),網(wǎng)站制作,網(wǎng)頁設(shè)計及定制高端網(wǎng)站建設(shè)服務(wù),專注于企業(yè)網(wǎng)站設(shè)計,高端網(wǎng)頁制作,對成都鑿毛機等多個行業(yè),擁有豐富的網(wǎng)站制作經(jīng)驗。

    1 對CPU監(jiān)控

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)#根據(jù)OpenStack創(chuàng)建的Instance ID得到相應(yīng)的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數(shù)量信息

#CPU利用率計算公式-CPU時間差/時間間隔/1000000000/核的數(shù)量*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 對內(nèi)存的監(jiān)控

python代碼

def get_memory(pid):#定義獲取當前已使用的內(nèi)存的函數(shù)
mem=0

#linux下 /proc/pid(進程ID)/smaps 下保存的是進程內(nèi)存映像信息,比同一目錄下的maps文件更詳細些

for line in file('/proc/%d/smaps' % int(pid),'r'):
if re.findall('Private_',line):

  #統(tǒng)計Private內(nèi)存信息量
mem+=int(re.findall('(\d+)',line)[0])
return mem

#根據(jù)實例名獲取進程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到相應(yīng)的虛擬實例上,如果是Linux 系統(tǒng)可以用free -m指令查看內(nèi)存使用率

   3 對磁盤的監(jiān)控

   創(chuàng)建虛擬機應(yīng)用實例時,會生成相應(yīng)的XML文件來表明實例的信息

   def get_devices(dom,path,devs):#該函數(shù)用于獲取XML中某節(jié)點的值

tree=ElementTree.fromstring(dom.XMLDesc(0))#將XML文件轉(zhuǎn)換為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):#獲取磁盤狀態(tài)信息函數(shù) 包含磁盤讀入的總比特數(shù)和寫出的總比特數(shù)
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為默認傳入的參數(shù)
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到相應(yīng)的虛擬實例上,如果是Linux 系統(tǒng)可以用df -h指令查看磁盤使用率

    4 對網(wǎng)絡(luò)的監(jiān)控

def get_nicInfo(nics):#獲取網(wǎng)絡(luò)信息包括Receive的總比特數(shù)和Transmit的總比特數(shù)
net_status={}

#通過 cat /proc/net/dev 命令查看網(wǎng)絡(luò)信息
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={}

#獲取網(wǎng)卡名稱
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 官網(wǎng)API定義

virDomainBlockInfo

struct virDomainBlockInfo {
unsigned long longcapacity

logical size in bytes of the block device backing image

unsigned long longallocation

highest allocated extent in bytes of the block device backing image

unsigned long longphysical

physical size in bytes of the container of the backing image

}

virDomainBlockStatsStruct

struct virDomainBlockStatsStruct {
long longrd_req

number of read requests

long longrd_bytes

number of read bytes

long longwr_req

number of write requests

long longwr_bytes

number of written bytes

long longerrs

In Xen this returns the mysterious 'oo_req'.

}

以上是“KVM虛擬機監(jiān)控的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!


標題名稱:KVM虛擬機監(jiān)控的示例分析
本文URL:http://weahome.cn/article/gpoghe.html

其他資訊

在線咨詢

微信咨詢

電話咨詢

028-86922220(工作日)

18980820575(7×24)

提交需求

返回頂部