[9987] | 1 | #!/usr/bin/env python
|
---|
| 2 | #
|
---|
| 3 | # XXX: Parsing snmpwalk is soo wrong todo, use a proper python library.
|
---|
| 4 | #
|
---|
| 5 | # Rick van der Zwet <info@rickvanderzwet.nl>
|
---|
| 6 | #
|
---|
| 7 | import glob
|
---|
| 8 | import logging
|
---|
| 9 | import subprocess
|
---|
| 10 | import sys
|
---|
| 11 | import yaml
|
---|
| 12 | logging.basicConfig(level=logging.DEBUG)
|
---|
| 13 | logger = logging.getLogger()
|
---|
| 14 |
|
---|
| 15 | DATASTORE='store.yaml'
|
---|
| 16 |
|
---|
| 17 | try:
|
---|
| 18 | store = yaml.load(open(DATASTORE,'r'))
|
---|
| 19 | except IOError:
|
---|
| 20 | store = { 'snmp' : {}, 'traffic' : {}, 'uptime' : {}}
|
---|
| 21 | pass
|
---|
| 22 |
|
---|
| 23 | class ConnectError(Exception):
|
---|
| 24 | pass
|
---|
| 25 |
|
---|
| 26 | def find_right_snmp_ip(data):
|
---|
| 27 | for k,v in data.iteritems():
|
---|
| 28 | if k.startswith('iface_'):
|
---|
| 29 | ip = v['ip'].split('/')[0]
|
---|
| 30 | logger.info("Trying ip %s", ip)
|
---|
| 31 | p = subprocess.Popen("snmpwalk -t 1 -r 1 -Oq -c public -v2c %s uptime" % ip,
|
---|
| 32 | shell=True,
|
---|
| 33 | stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
---|
| 34 | (stdout, stderr) = p.communicate()
|
---|
| 35 | if p.returncode == 0:
|
---|
| 36 | return ip
|
---|
| 37 | else:
|
---|
| 38 | logger.error(stderr.strip())
|
---|
| 39 | raise None
|
---|
| 40 |
|
---|
| 41 | def get_snmp_stats(ip,target):
|
---|
| 42 | p = subprocess.Popen("snmpwalk -t 1 -r 1 -Oq -c public -v2c %s %s" % (ip, target),
|
---|
| 43 | shell=True,
|
---|
| 44 | stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
---|
| 45 | (stdout, stderr) = p.communicate()
|
---|
| 46 | if p.returncode != 0:
|
---|
| 47 | logger.error(stderr.strip())
|
---|
| 48 | raise ConnectError
|
---|
| 49 | r = {}
|
---|
| 50 | for line in stdout.strip().split('\n'):
|
---|
| 51 | index = line.split()[0][len(target)+1:]
|
---|
| 52 | value = line.split()[1]
|
---|
| 53 | r[index] = value
|
---|
| 54 | return r
|
---|
| 55 |
|
---|
| 56 | def get_snmp_value(ip, target):
|
---|
| 57 | p = subprocess.Popen("snmpget -t 1 -r 1 -Ovt -c public -v2c %s %s" % (ip, target),
|
---|
| 58 | shell=True,
|
---|
| 59 | stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
---|
| 60 | (stdout, stderr) = p.communicate()
|
---|
| 61 | if p.returncode != 0:
|
---|
| 62 | logger.error(stderr.strip())
|
---|
| 63 | raise ConnectError
|
---|
| 64 | return stdout.strip()
|
---|
| 65 |
|
---|
| 66 |
|
---|
| 67 |
|
---|
| 68 | try:
|
---|
| 69 | ff = sys.argv[1]
|
---|
| 70 | except IndexError:
|
---|
| 71 | ff = ''
|
---|
| 72 |
|
---|
| 73 | try:
|
---|
| 74 | for nf in sorted(glob.glob('nodes/*%s*/wleiden.yaml' % ff)):
|
---|
| 75 | data = yaml.load(open(nf,'r'))
|
---|
| 76 | nodename = data['nodename']
|
---|
| 77 |
|
---|
| 78 | if store['snmp'].has_key(nodename):
|
---|
| 79 | ip = store['snmp'][nodename]
|
---|
| 80 | else:
|
---|
| 81 | logger.info("Running discovery for %s", nodename)
|
---|
| 82 | ip = find_right_snmp_ip(data)
|
---|
| 83 | store['snmp'][nodename] = ip
|
---|
| 84 |
|
---|
| 85 | if ip == None:
|
---|
| 86 | logger.error("No valid ip found for node %s", nodename)
|
---|
| 87 | continue
|
---|
| 88 |
|
---|
| 89 | logger.info("Processing %s via %s", nodename, ip)
|
---|
| 90 | target = 'IF-MIB::ifDescr'
|
---|
| 91 |
|
---|
| 92 | try:
|
---|
| 93 | iface = get_snmp_stats(ip, 'IF-MIB::ifDescr')
|
---|
| 94 | ifout = get_snmp_stats(ip, 'IF-MIB::ifOutOctets')
|
---|
| 95 | ifin = get_snmp_stats(ip, 'IF-MIB::ifInOctets')
|
---|
| 96 |
|
---|
| 97 | uptime = get_snmp_value(ip, 'DISMAN-EVENT-MIB::sysUpTimeInstance')
|
---|
| 98 | store['uptime'][nodename] = int(uptime)
|
---|
| 99 |
|
---|
| 100 | traffic = {}
|
---|
| 101 | for i,f in iface.iteritems():
|
---|
| 102 | traffic[f] = (int(ifin[i]), int(ifout[i]))
|
---|
| 103 | store['traffic'][nodename] = traffic
|
---|
| 104 | except ConnectError:
|
---|
| 105 | logger.error("Unable to get all data")
|
---|
| 106 | pass
|
---|
| 107 | except KeyboardInterrupt:
|
---|
| 108 | pass
|
---|
| 109 |
|
---|
| 110 | yaml.dump(store,open(DATASTORE,'w'))
|
---|
| 111 |
|
---|
| 112 |
|
---|