码迷,mamicode.com
首页 > 其他好文 > 详细

H3C交换机聚合端口状态扫描

时间:2016-05-09 23:44:27      阅读:540      评论:0      收藏:0      [点我收藏+]

标签:

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

 

H3C交换机聚合端口状态扫描

标签:

原文地址:http://www.cnblogs.com/OhLee/p/5475958.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!