#!/usr/bin/Envpython#-*-coding=utf-8-*-#Using GPL v2.7#Author: [email protected]126. Com#python Monitoring CPU Status"""1, the realization principle: obtains the system information through the SNMP protocol, then carries on the corresponding computation and the format, the final output result2, special note: SNMP is required on the monitored machine.Yum Install-Y net-snmp*installation"""#!/usr/bin/pythonimport osdef getallitems (Host, OID): SN1= Os.popen ('snmpwalk-v 2c-c Public'+ Host +' '+ OID +'|grep raw|grep cpu|grep-v Kernel'). Read ().Split('\ n')[:-1] Return SN1 def getDate (host): Items = Getallitems (Host,'. 1.3.6.1.4.1.2021.11') Date=[] rate=[] cpu_total=0#us= Us+ni, sy = sy + IRQ +SIRQ forIteminchItems:float_item=float(item.Split(' ')[3]) Cpu_total+=Float_itemifitem = = items[0]: Date. Append (float(item.Split(' ')[3]) +float(items[1].Split(' ')[3])) elifitem = = item[2]: Date. Append (float(item.Split(' ')[3] + items[5].Split(' ')[3] + items[6].Split(' ')[3])) Else: Date. Append (float_item) #calculate CPU usage Percen Tage forIteminch Date: Rate.append (item/cpu_total) * -) mean= ['%us','%ni','%sy','%id','%wa','%CPU_IRQ','%CPU_SIRQ'] #calculate CPU usage percentage result =map (None,rate,mean) return resultif__name__ = ='__main__': Hosts= ['192.168.10.1','192.168.10.2'] forHostinchHosts:print'=========='+ Host +'=========='result=getDate (host) Print'Cpu (s)', #print result forIinchRange5): Print'%.2f%s'% (result[i][0],result[i][1]), print print
"Python ops script" Python Monitors CPU status