huijial 发表于 2018-8-13 12:11:38

python监控系统资源

  #!/usr/bin/python
  import re
  import os
  def getAllitems(host,oid):
        sn1 = os.popen('snmpwalk -v 2c -c public ' + host + ' ' + oid).read().split('\n')[:-1]
        return sn1
                                                              
  def getDate(source,newitem):
        for item in source:
                  newitem.append(item.split(':').strip())
        return newitem
                                                              
  def getRealDate(item1,item2,listname):
        for i in range(len(item1)):
                  listname.append(int(item1)*int(item2)/1024)
        return listname
                                                              
  def caculateDiskUsedRate(host):
        hrStorageDescr = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageDescr')
        hrStorageUsed = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageUsed')
        hrStorageSize = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageSize')
        hrStorageAllocationUnits = getAllitems(host, 'HOST-RESOURCES-MIB::hrStorageAllocationUnits')
                                                              
        disk_list = []
        hrsused = []
        hrsize = []
        hrsaunits = []
                                                              
        #get disk_list
        for item in hrStorageDescr:
                  if re.search('/',item):
                          disk_list.append(item.split(':'))
        #print disk_list      
                                                              
        getDate(hrStorageUsed,hrsused)
        getDate(hrStorageSize,hrsize)
        #print getDate(hrStorageAllocationUnits,hrsaunits)
                                                              
        #get hrstorageAllocationUnits
        for item in hrStorageAllocationUnits:
                  hrsaunits.append(item.split(':').strip().split(' '))
        #caculate the result
        #disk_used = hrStorageUsed * hrStorageAllocationUnits /1024 (KB)
        disk_used = []
        total_size = []
        disk_used = getRealDate(hrsused,hrsaunits,disk_used)
        total_size = getRealDate(hrsize,hrsaunits,total_size)
                                                              
        diskused_rate = []
        for i in range(len(disk_used)):
                  diskused_rate.append(str(round((float(disk_used)/float(total_size)*100), 2)) + '%')
                                                              
        return diskused_rate,disk_list
                                                              
  if __name__ == '__main__':
        hosts = ['192.168.30.111','192.168.30.112']
        for host in hosts:
                  result = caculateDiskUsedRate(host)
                  diskused_rate = result
                  partition = result
                  print "==========",host,'=========='
                  for i in range(len(diskused_rate)):
                          print '%-20s used: %s' % (partition,diskused_rate)
                  print
页: [1]
查看完整版本: python监控系统资源