#!/usr/bin/python#__*__ coding:utf-8 __*__ImportOSImport TimeImportcommands fromMultiprocessingImportQueue,event,process,pool,manager#Specify switch SNMP community and log file Pathscommunity=' Public'logfile='/root/switch_bagg_monitor/switch_bagg_monitor.log'#determine the manufacturer the switch belongs todefSwitch_provider (ip,community):Globalprovider Provider_oid='snmpv2-mib::sysdescr.0'cmd='snmpwalk-c%s-v 2c%s%s'%(community,ip,provider_oid) Code,result=commands.getstatusoutput (cmd)if 'Cisco' inchResult:provider='Cisco' elif 'H3C' inchResult:provider='H3C'#defines a function that writes a log for later invocationdeflog_write (msg): File=open (LogFile,'A +') File.write (msg) file.close ()defBagg_monitor (ip,community): Switch_provider (ip,community)ifprovider=='H3C': Bagg_conf_oid='iso.2.840.10006.300.43.1.2.1.1.13'cmd='snmpwalk-c%s-v 2c%s%s'%(community,ip,bagg_conf_oid) Code,result=commands.getstatusoutput (cmd) result=result.split ('\ n') Bagg_conf_int=[item forIteminchResultif 'integer:0' not inchItem]#Print Bagg_conf_int forIteminchBagg_conf_int:bagg_index=item.split ('=') [-1].strip () bagg_int_status_oid='if-mib::ifoperstatus.%s'% (Bagg_index.split (':') [-1].strip ()) cmd='snmpwalk-c%s-v 2c%s%s'%(community,ip,bagg_int_status_oid) Code,result=commands.getstatusoutput (cmd)if 'Integer:down (2)' not inchResult:#Print Result #Print Bagg_indexBagg_mem_count=0 forIinchBagg_conf_int:ifBagg_indexinchI:bagg_mem_count+ = 1ifBagg_mem_count >= 2: Int_index=item.split ('=') [0].split ('.') [-1] #Print Item #Print Int_indexBagg_run_oid='iso.2.840.10006.300.43.1.2.1.1.12.%s'%int_index cmd='snmpwalk-c%s-v 2c%s%s'%(community,ip,bagg_run_oid) Code,result=commands.getstatusoutput (cmd)if 'integer:0' inchresult:int_adminstatus_oid='if-mib::ifadminstatus.%s'%int_index cmd='snmpwalk-c%s-v 2c%s%s'%(community,ip,int_adminstatus_oid) Code,result=commands.getstatusoutput (cmd)if ' Down' not inchresult:int_desc_oid='if-mib::ifdescr.%s'%int_index cmd='snmpwalk-c%s-v 2c%s%s'%(community,ip,int_desc_oid) Code,result=commands.getstatusoutput (cmd) error_int=result.split (':') [-1].strip () current_time=time.strftime ('%y-%m-%d%h:%m:%s') msg='%s\t%s\t\t%s\t\t Port aggregation exception \ n'%(Current_time,ip,error_int)#Print msglog_write (msg)elifProvider = ='Cisco': Passif __name__=='__main__': Pool= Pool (30) forIpinchOpen"/root/switch_bagg_monitor/switch_ip.txt"): IP=Ip.strip () result=Pool.apply_async (Bagg_monitor, (ip,community,)) Pool.close () Pool.join ()
H3C Switch aggregation port status scan