#!/usr/bin/env python
python 获取资源信息
原创swq499809608 博主文章分类:Python Shell GO ©著作权
©著作权归作者所有:来自51CTO博客作者swq499809608的原创作品,谢绝转载,否则将追究法律责任
#-*-coding:utf8-*-
"""
@Author : Villiam Sheng
@Group : Linux Group
@Date : 2012-07-18
@Funtion:
Update kvm host status ...
get_nic: Get a week network flow
1,Get Seven days before flow,get maximum value!
2,Get Seven days before flow,get average value!
"""
import os,sys,libvirt,socket,shutil,re
from statvfs import F_BLOCKS,F_BAVAIL,F_BSIZE
class kvm_os_status(object):
def __init__(self):
self.vmm = {}
try:
self.conn = libvirt.open(None)
except libvirt.libvirtError,e:
print e
def get_mem(self):
try:
f = open('/proc/meminfo','r')
for i in f.readlines():
if i.find('MemTotal:') != -1:
total_mem=int(i.split(':')[1].split('kB')[0])/1024
continue
try:
exec_command = """ grep "memory" /data*/domains/*/*.xml 2>/dev/null |awk -F "<memory>" '{print $2}'|awk -F "</memory>" '{print $1}' """
mem = os.popen(exec_command).readlines()
act_mem = 0
for m in mem:
act_mem += int(m) / 1024
self.vmm['free_mem'] = total_mem - act_mem
except Exception,e:
pass
except Exception,e:
pass
def get_mip(self):
try:
exec_command = """cat /etc/sysconfig/network-scripts/ifcfg-br0 |grep "IPADDR"|awk -F"=" '{print $2}' 2>/dev/null"""
mip = os.popen(exec_command).read().strip()
sock=socket.socket(socket.AF_INET,socket.SOCK_DGRAM)
try:
sock.connect((mip,0))
except socket.error,e:
exec_command = """cat /etc/sysconfig/network-scripts/ifcfg-br1 |grep "IPADDR"|awk -F"=" '{print $2}'"""
nip = os.popen(exec_command).read().strip()
self.vmm['mip'] = nip
self.vmm['mip'] = sock.getsockname()[0]
except:
pass
def get_disk(self):
extends = []
disk={}
try:
ext_disk = os.popen("df -h |awk '{print $1,$6}'|grep -v 'tmpfs'|grep -v 'Filesystem'|grep -v 'sda1'|grep -v 'mfs'|grep -v 'T'").readlines()
except Exception,e:
print e
if ext_disk == "":
self.vmm['free_disk'] = vfs[F_BLOCKS]*vfs[F_BSIZE]/1024/1024/1024
else:
free_disk = 0
for disk in ext_disk:
try:
vfs = os.statvfs(disk.split()[1])
except Exception,e:
print e
full_space = vfs[F_BLOCKS]*vfs[F_BSIZE]/1024/1024/1024
free_space = vfs[F_BAVAIL]*vfs[F_BSIZE]/1024/1024/1024
imgs = os.popen("ls -ls %s/domains/vm*/data*.img 2>/dev/null | awk '{print $1,$6}'" % disk.split()[1]).readlines()
if imgs:
for i in imgs:
t_size, f_size = i.strip().split()
free_space -= int(f_size)/1024/1024/1024
if int(t_size) != 0:
free_space += int(t_size)/1024/1024
disk={disk.split()[1]:free_space}
for i in disk.keys():
free_disk += disk[i]
self.vmm['free_disk'] = free_disk
a = os.popen("cat /etc/issue|awk '{print $7}'|grep -v 'Kernel'|grep -v '^$'").readline()
self.vmm['os_type'] = 'RHEL%sx64'%a.strip()
def count(self):
self.vmm['vmm_count'] = 0
try:
for id in self.conn.listDomainsID():
dom = self.conn.lookupByID(id)
self.vmm['vmm_count'] = self.vmm['vmm_count'] + 1
except libvirt.libvirtError,e:
pass
print self.vmm
def work(self):
self.get_mem()
self.get_disk()
self.get_mip()
self.count()
if __name__ == "__main__":
st = kvm_os_status()
st.work()
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
python 获取k8s集群资源信息 python获取key
#python打卡##Python数学编程##python# 什么是高阶函数? 高阶函数是一种将函数作为参数,或者把函数作为结果返回的函数,map函数、sorted函数就是高阶函数的典型例子。map函数在小编以前的文章中做过相应的知识分享。sorted函数是python的内置函数,它的可选参数key用于提供一个函数,它可以将函数应用到各个元素上进行排序。根据单词长
python 获取k8s集群资源信息 c++ map 获取key列表 map 长度 map根据key排序 python apply函数