switchcat.py 4.5 KB
Newer Older
木子技术组's avatar
木子技术组 已提交
1 2 3 4 5 6 7
#!/usr/bin/env python
#encoding=utf-8

import subprocess
import time
import urllib2

木子技术组's avatar
木子技术组 已提交
8
monitor = [{'group':'switch',
木子技术组's avatar
木子技术组 已提交
9 10 11 12
            'type':'cisco',
            'public':'dianpingP@ssword',
            'version':'2c',
            'ip':'172.25.21.88',
木子技术组's avatar
木子技术组 已提交
13
            'port':[10101,10102,10601,10602]
木子技术组's avatar
木子技术组 已提交
14 15 16 17 18 19
          }]

CISCO_IN_TRAFFIC_OID = '.1.3.6.1.2.1.2.2.1.10.'
CISCO_IN_COUNT_OID = '.1.3.6.1.2.1.2.2.1.11.'
CISCO_OUT_TRAFFIC_OID = '.1.3.6.1.2.1.2.2.1.16.'
CISCO_OUT_COUNT_OID = '.1.3.6.1.2.1.2.2.1.17.'
木子技术组's avatar
木子技术组 已提交
20 21
CISCO_NAME_OID = '.1.3.6.1.2.1.1.5'
CISCO_PORT_NAME_OID = '.1.3.6.1.2.1.2.2.1.2.'
木子技术组's avatar
木子技术组 已提交
22 23 24 25 26 27
SLEEP_TIME = 8 #s

SERVER = '10.128.120.38:2281'

if __name__ == '__main__':
    while True:
u  
曾伟伟 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        for unit in monitor:
            if not unit.get('name'):
                try:
                    cmd = 'snmpwalk -c '+unit['public']+' -v '+unit['version']+' '+unit['ip']+' '+CISCO_NAME_OID
                    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
                    p.wait()
                    unit['name'] = p.stdout.read().split(': ')[1].strip()
                except Exception, e:
                    print str(e)
                    unit['name'] = 'unknown'
            cmd = 'snmpget -c '+unit['public']+' -v '+unit['version']+' '+unit['ip']+' '
            if unit['type'] == 'cisco':
                in_traffic_oid = CISCO_IN_TRAFFIC_OID
                out_traffic_oid = CISCO_OUT_TRAFFIC_OID
                in_count_oid = CISCO_IN_COUNT_OID
                out_count_oid = CISCO_OUT_COUNT_OID
            else:
                print 'The unit ' + unit['name'] + '\'s type can not resolved!! [' + unit['type'] + ']'
                continue
            if not unit.get('portname'):
                try:
                    cmd = 'snmpget -c '+unit['public']+' -v '+unit['version']+' '+unit['ip']+' '
                    if unit['type'] == 'cisco':
                        for p in unit['port']:
                            cmd += CISCO_PORT_NAME_OID + str(p) + ' '
                    else:
                        raise Exception('unknown device type!')
                    p = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
                    p.wait()
                    unit['portname'] = p.stdout.read().split(': ')[1].strip()
                except Exception, e:
                    print str(e)
                    unit['portname'] = unit['port']
            for port in unit['port']:
                if not port:
木子技术组's avatar
木子技术组 已提交
63
                    continue
u  
曾伟伟 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
                cmd += in_traffic_oid + str(port) + ' '
                cmd += out_traffic_oid + str(port) + ' '
                cmd += in_count_oid + str(port) + ' '
                cmd += out_count_oid + str(port) + ' '
            p = subprocess.Popen(cmd,stdout=subprocess.PIPE,shell=True)
            p.wait()
            result = [r.split(': ')[1] for r in p.stdout.read().split('\n') if r]
            if not unit.get('last'):
                unit['last'] = [int(r) for r in result]
            else:
                now = [int(r) for r in result]
                diff = []
                i = 0
                while i < len(now):
                    d = now[i]-unit['last'][i]
                    if d < 0:
                        d += 4294967296
                    diff.append(d)
                    i += 1
                unit['last'] = now
                print unit['last']
                i = 0
                while i < len(unit['port']):
木子技术组's avatar
木子技术组 已提交
87
                    try:
u  
曾伟伟 已提交
88 89 90 91 92 93 94 95 96 97
                        #in
                        print 'http://'+SERVER+'/cat/r/systemMonitor?group='+unit['group']+'&domain='+ \
                                unit['name']+'&key='+str(unit['portname'][i])+'-in&op=sum&sum='+str(diff[::4][i])
                        #urllib2.urlopen('http://'+SERVER+'/cat/r/systemMonitor?group='+unit['group']+'&domain='+
                        #            unit['name']+'&key='+unit['name']+'-'+group['key']+'/in-traffic'+'&op=sum&sum='+str(data),timeout=0)
                        #out
                        print 'http://'+SERVER+'/cat/r/systemMonitor?group='+unit['group']+'&domain='+ \
                                unit['name']+'&key='+str(unit['portname'][i])+'-in&op=sum&sum='+str(diff[1::4][i])
                        #urllib2.urlopen('http://'+SERVER+'/cat/r/systemMonitor?group='+unit['group']+'&domain='+
                        #            unit['name']+'&key='+unit['name']+'-'+group['key']+'/in-traffic'+'&op=sum&sum='+str(data),timeout=0)
木子技术组's avatar
木子技术组 已提交
98 99
                    except Exception, e:
                        print str(e)
u  
曾伟伟 已提交
100
                    i += 1
木子技术组's avatar
木子技术组 已提交
101 102

        time.sleep(SLEEP_TIME)