switchcat.py 4.9 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 28 29
SLEEP_TIME = 8 #s

SERVER = '10.128.120.38:2281'

if __name__ == '__main__':
    while True:
        try:
            for unit in monitor:
木子技术组's avatar
木子技术组 已提交
30 31 32 33 34 35 36 37 38 39
                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']+' '
木子技术组's avatar
木子技术组 已提交
40 41 42 43 44 45 46 47
                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
木子技术组's avatar
木子技术组 已提交
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
                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
木子技术组 已提交
64
                        continue
木子技术组's avatar
木子技术组 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
                    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']):
                        try:
                            #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)
                        except Exception, e:
                            print str(e)
                        i += 1
木子技术组's avatar
木子技术组 已提交
102
        except Exception, e:
木子技术组's avatar
木子技术组 已提交
103
            print 'exception!!! ' + str(e)
木子技术组's avatar
木子技术组 已提交
104 105

        time.sleep(SLEEP_TIME)