Python调用snmpwalk获取交换机带宽

一个之前写的获取交换机流量的程序:

import commands
import time
import datetime

INTERFACES = ['504','505','506','507','508','509','510',
              '511','512','513','514','515','516','517','518',
              '519','520','521','522','523','524','525','526',
              '527','528','529','530','531','532','533','534',
              '535','536','537','538','539','540','541','542',
              '543','544','545','546','547','548','549','550',
              '551','561','562','563','564','565','566','567',
              '568','569','570','571','572','573','574','575',
              '576','577','578','579','580','581','582','583',
              '584','586','587','588','589','590','591','592',
              '593','594','595','596','597','598','599','600',
              '601','602','603','604','605','606','607','608']
DEVICEIP = '10.10.100.253'
COMMUNITY = 'Suzhou_iMC\&KUO'

IF_DESC_OID = '1.3.6.1.2.1.31.1.1.1.1'
IF_IN_OID = '1.3.6.1.2.1.31.1.1.1.6'
IF_OUT_OID = '1.3.6.1.2.1.31.1.1.1.10'

SAMPLE_INTERVAL = 5

COMMAND = 'snmpwalk -c %s -v 2c ' % (COMMUNITY,)

DESCS = []
IN_TOTALS_01 = []
IN_TOTALS_02 = []
OUT_TOTALS_01 = []
OUT_TOTALS_02 = []
IN_TOTALS_TIME_01 = []
IN_TOTALS_TIME_02 = []
OUT_TOTALS_TIME_01 = []
OUT_TOTALS_TIME_02 = []
IN_SPEEDS = []
OUT_SPEEDS = []

def get_if_desc(interface):
    desc = commands.getoutput(COMMAND + DEVICEIP + ' ' + IF_DESC_OID + '.' + interface)
    return desc.split(' ')[3]

def get_if_in_total(interface):
    in_total = commands.getoutput(COMMAND + DEVICEIP + ' ' + IF_IN_OID + '.' + interface)
    return long(in_total.split(' ')[3])

def get_if_out_total(interface):
    out_total = commands.getoutput(COMMAND + DEVICEIP + ' ' + IF_OUT_OID + '.' + interface)
    return long(out_total.split(' ')[3])

for interface in INTERFACES:
    DESCS.append(get_if_desc(interface))
    
for interface in INTERFACES:
    IN_TOTALS_01.append(get_if_in_total(interface))
    IN_TOTALS_TIME_01.append(datetime.datetime.now())
    
for interface in INTERFACES:
    OUT_TOTALS_01.append(get_if_out_total(interface))
    OUT_TOTALS_TIME_01.append(datetime.datetime.now())
    
time.sleep(SAMPLE_INTERVAL)

for interface in INTERFACES:
    IN_TOTALS_02.append(get_if_in_total(interface))
    IN_TOTALS_TIME_02.append(datetime.datetime.now())
    
for interface in INTERFACES:
    OUT_TOTALS_02.append(get_if_out_total(interface))
    OUT_TOTALS_TIME_02.append(datetime.datetime.now())
    
    
for _, value in enumerate(DESCS): 
    IN_SPEEDS.append(str((IN_TOTALS_02[_] - IN_TOTALS_01[_]) / (IN_TOTALS_TIME_02[_] - IN_TOTALS_TIME_01[_]).seconds))
    OUT_SPEEDS.append(str((OUT_TOTALS_02[_] - OUT_TOTALS_01[_]) / (OUT_TOTALS_TIME_02[_] - OUT_TOTALS_TIME_01[_]).seconds))

#HOST   IF_NAME    IF_SPEED_IN     IF_SPEED_OUT
for _, value in enumerate(DESCS):
    print DEVICEIP + ";" + DESCS[_] + ";" + IN_SPEEDS[_] + ";" + OUT_SPEEDS[_]

发表评论

电子邮件地址不会被公开。 必填项已用*标注

*