#!/usr/bin/python #__*__ coding:utf-8 __*__ import os import time import commands from multiprocessing import Queue,Event,Process,Pool,Manager #指定交换机snmp community和日志文件路径 community=‘public‘ logfile=‘/root/switch_bagg_monitor/switch_bagg_monitor.log‘ #判断交换机所属厂家 def switch_provider(ip,community): global provider 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‘ in result: provider=‘Cisco‘ elif ‘H3C‘ in result: provider=‘H3C‘ #定义一个写日志的函数,供后面调用 def log_write(msg): file=open(logfile,‘a+‘) file.write(msg) file.close() def BAGG_Monitor(ip,community): switch_provider(ip,community) if provider==‘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 for item in result if ‘INTEGER: 0‘ not in item] #print bagg_conf_int for item in bagg_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 in result: #print result #print bagg_index bagg_mem_count=0 for i in bagg_conf_int: if bagg_index in i: bagg_mem_count += 1 if bagg_mem_count >= 2: int_index=item.split(‘=‘)[0].split(‘.‘)[-1] #print item #print int_index bagg_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‘ in result: 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 in result: 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端口聚合异常\n‘ %(current_time,ip,error_int) #print msg log_write(msg) elif provider == ‘Cisco‘: pass if __name__ == ‘__main__‘: pool = Pool(30) for ip in open("/root/switch_bagg_monitor/switch_ip.txt") : ip = ip.strip() result = pool.apply_async(BAGG_Monitor,(ip,community,)) pool.close() pool.join()
时间: 2024-10-11 10:17:47