#!/usr/bin/env python#-*-coding=utf-8-*-#Using GPL v2.7#Author: [email protected]#python monitoring Memory (swap) Utilization" "1, the realization principle: obtains the system information through the SNMP protocol, then carries on the corresponding computation and the format, the final output result 2, the special attention: the monitoring machine needs to support SNMP. Yum install-y net-snmp* Installation" "#!/usr/bin/pythonImportOSdefGetallitems (Host, OID): SN1= Os.popen ('snmpwalk-v 2c-c Public'+ Host +' '+ OID). Read (). Split ('\ n') [:-1] returnSN1defGetswaptotal (Host): Swap_total= Getallitems (Host,'ucd-snmp-mib::memtotalswap.0') [0].split (' ') [3] returnSwap_totaldefgetswapused (Host): Swap_avail= Getallitems (Host,'ucd-snmp-mib::memavailswap.0') [0].split (' ') [3] Swap_total=getswaptotal (host) swap_used= Str (Round ((float (swap_total)-float (swap_avail)/float (swap_total)) *100, 2)) +'%' returnswap_useddefGetmemtotal (Host): Mem_total= Getallitems (Host,'ucd-snmp-mib::memtotalreal.0') [0].split (' ') [3] returnMem_totaldefgetmemused (Host): Mem_total=getmemtotal (host) Mem_avail= Getallitems (Host,'ucd-snmp-mib::memavailreal.0') [0].split (' ') [3] mem_used= Str (Round ((float (mem_total)-float (mem_avail)/float (mem_total)) *100, 2)) +'%' returnmem_usedif __name__=='__main__': Hosts= ['192.168.10.1','192.168.10.2'] Print "monitoring Memory Usage" forHostinchhosts:mem_used=getmemused (host) swap_used=getswapused (host)Print '=========='+ Host +'==========' Print 'mem_used =%-15s swap_used =%-15s'%(mem_used, swap_used)Print
Python operational Script python Monitoring memory (swap) utilization