Python implements Linux KVM system monitoring metrics, written many years ago to help colleagues, quickly learn Python technology

Source: Internet
Author: User

#!/usr/bin/env python#-*-coding:utf8-*-"" "@Author  :  villiam sheng@group  :   Linux Group@Date   :  2011-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,refrom  statvfs  import f_blocks,f_bavail,f_bsize class kvm_os_status (object):     def  __init__ (self): &NBSP;&NBsp;      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  $ ' "" "                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 ()


This article from "Welcome comments, Welcome to like" blog, please be sure to keep this source http://swq499809608.blog.51cto.com/797714/1549222

Python implements Linux KVM system monitoring metrics, written many years ago to help colleagues, quickly learn Python technology

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.