OpenStack实战篇之序章--KVM虚拟机监控

初识OpenStack两周,于它已没有初见时的神秘感,褪去层层面纱之后,渐识它的强大,决心以彼为刀剑,征战云计算的疆场,今夜,号角吹响,且奏序章。

    用OpenStack 创建虚拟实例后,需要对这些实例进行监控,本文所讲的是KVM环境下利用OpenStack集成的Libvirt API对虚拟机CPU,内存,磁盘使用率以及网络上传下载速度进行监控

    闲话少说,直接正题。

    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>0else 0.0
cpu_usage=cpu_usage if (cpu_usage<100else 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)

 

OpenStack实战篇之序章--KVM虚拟机监控OpenStack实战篇之序章--KVM虚拟机监控初次写博,请各位大神手下留情,请轻喷!

参考:

Libvirt 官网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'.

}

 

http://libvirt.org/html/libvirt-libvirt.html

转载于:https://my.oschina.net/u/2285247/blog/1589787

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值