Python operations script Python monitors network card traffic

Source: Internet
Author: User
Tags snmp

#!/usr/bin/Envpython#-*-coding=utf-8-*-#Using GPL v2.7#Author: [email protected]126. Com#python Monitor network card traffic"""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 reimport os#get SNMP-MIB2 of the Devicesdef Getallitems (host,oid): SN1= Os.popen ('snmpwalk-v 2c-c Public'+ Host +' '+ OID). Read ().Split('\ n')[:-1] return SN1 #get Network Devicedef getDevices (Host): Device_mib= Getallitems (Host,'RFC1213-MIB::IFDESCR') Device_list= []         forIteminchDevice_mib:ifRe.search ('ETH', Item): Device_list.append (item.Split(':')[3].strip ()) return device_list #get NetworkDatedef getDate (host,oid): Date_mib= Getallitems (host,oid) [1:]        Date= []         forIteminchDate_mib:byte=float(item.Split(':')[3].strip ())Date. Append (Str (round (byte/1024x768,2)) +'KB') returnDate                                                                                      if__name__ = ='__main__': Hosts= ['192.168.10.1','192.168.10.2']         forHostinchhosts:device_list=GetDevices (Host) Inside= GetDate (Host,'if-mib::ifinoctets') outside= GetDate (Host,'if-mib::ifoutoctets') Print'=========='+ Host +'=========='                 forIinchRange (len (inside)): Print'%s:rx:%-15s TX:%s'%(Device_list[i], inside[i], outside[i]) print

Python operations script Python monitors network card traffic

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.