"Python ops script" Python Monitors CPU status

Source: Internet
Author: User
Tags print print snmp

#!/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

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.