source: genesis/tools/get-link-traffic.py@ 9987

Last change on this file since 9987 was 9987, checked in by rick, 13 years ago

Poor-mans Nagios/MRTG data gattering.

  • Property svn:executable set to *
File size: 2.9 KB
Line 
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#
7import glob
8import logging
9import subprocess
10import sys
11import yaml
12logging.basicConfig(level=logging.DEBUG)
13logger = logging.getLogger()
14
15DATASTORE='store.yaml'
16
17try:
18 store = yaml.load(open(DATASTORE,'r'))
19except IOError:
20 store = { 'snmp' : {}, 'traffic' : {}, 'uptime' : {}}
21 pass
22
23class ConnectError(Exception):
24 pass
25
26def 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
41def 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
56def 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
68try:
69 ff = sys.argv[1]
70except IndexError:
71 ff = ''
72
73try:
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
107except KeyboardInterrupt:
108 pass
109
110yaml.dump(store,open(DATASTORE,'w'))
111
112
Note: See TracBrowser for help on using the repository browser.