Skip to content

Commit ee6561c

Browse files
committed
Added netiron SNMP modules
1 parent 90a588a commit ee6561c

2 files changed

Lines changed: 257 additions & 0 deletions

File tree

network/netiron/netiron.py

Lines changed: 232 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,232 @@
1+
#!/usr/bin/python
2+
# Name: netiron.py
3+
# Desc: Ganglia module for polling netirons via snmnp (probably work with any snmp capable device)
4+
# Author: Evan Fraser [email protected]
5+
# Date: April 2012
6+
# Copyright: GPL
7+
8+
import sys
9+
import os
10+
import re
11+
import time
12+
from pysnmp.entity.rfc3413.oneliner import cmdgen
13+
NIPARAMS = {}
14+
15+
NIMETRICS = {
16+
'time' : 0,
17+
'data' : {}
18+
}
19+
LAST_NIMETRICS = dict(NIMETRICS)
20+
NIMETRICS_CACHE_MAX = 5
21+
22+
descriptors = list()
23+
24+
oidDict = {
25+
'ifIndex' : (1,3,6,1,2,1,2,2,1,1),
26+
'ifName' : (1,3,6,1,2,1,31,1,1,1,1),
27+
'ifAlias' : (1,3,6,1,2,1,31,1,1,1,18),
28+
'ifHCInOctets' : (1,3,6,1,2,1,31,1,1,1,6),
29+
'ifHCOutOctets' : (1,3,6,1,2,1,31,1,1,1,10),
30+
'ifInUcastPkts' : (1,3,6,1,2,1,2,2,1,11),
31+
'ifOutUcastPkts' : (1,3,6,1,2,1,2,2,1,17),
32+
}
33+
34+
def get_metrics():
35+
"""Return all metrics"""
36+
37+
global NIMETRICS, LAST_NIMETRICS
38+
39+
# if interval since last check > NIMETRICS_CACHE_MAX get metrics again
40+
if (time.time() - NIMETRICS['time']) > NIMETRICS_CACHE_MAX:
41+
metrics = {}
42+
for para in NIPARAMS.keys():
43+
if para.startswith('netiron_'):
44+
ipaddr,name = NIPARAMS[para].split(':')
45+
snmpTable = runSnmp(oidDict,ipaddr)
46+
newmetrics = buildDict(oidDict,snmpTable,name)
47+
metrics = dict(newmetrics, **metrics)
48+
49+
# update cache
50+
LAST_NIMETRICS = dict(NIMETRICS)
51+
NIMETRICS = {
52+
'time': time.time(),
53+
'data': metrics
54+
}
55+
56+
return [NIMETRICS, LAST_NIMETRICS]
57+
58+
def get_delta(name):
59+
"""Return change over time for the requested metric"""
60+
61+
# get metrics
62+
[curr_metrics, last_metrics] = get_metrics()
63+
try:
64+
delta = float(curr_metrics['data'][name] - last_metrics['data'][name])/(curr_metrics['time'] - last_metrics['time'])
65+
#print delta
66+
if delta < 0:
67+
print "Less than 0"
68+
delta = 0
69+
except StandardError:
70+
delta = 0
71+
72+
return delta
73+
74+
# Separate routine to perform SNMP queries and returns table (dict)
75+
def runSnmp(oidDict,ip):
76+
77+
# cmdgen only takes tuples, oid strings don't work
78+
# ifIndex = (1,3,6,1,2,1,2,2,1,1)
79+
# ifName = (1,3,6,1,2,1,31,1,1,1,1)
80+
# ifAlias = (1,3,6,1,2,1,31,1,1,1,18)
81+
# ifHCInOctets = (1,3,6,1,2,1,31,1,1,1,6)
82+
# ifHCOutOctets = (1,3,6,1,2,1,31,1,1,1,10)
83+
84+
#Runs the SNMP query, The order that oid's are passed determines the order in the results
85+
errorIndication, errorStatus, errorIndex, varBindTable = cmdgen.CommandGenerator().nextCmd(
86+
# SNMP v2
87+
cmdgen.CommunityData('test-agent', 'public'),
88+
cmdgen.UdpTransportTarget((ip, 161)),
89+
oidDict['ifAlias'],
90+
oidDict['ifIndex'],
91+
oidDict['ifName'],
92+
oidDict['ifHCInOctets'],
93+
oidDict['ifHCOutOctets'],
94+
oidDict['ifInUcastPkts'],
95+
oidDict['ifOutUcastPkts'],
96+
)
97+
# Check for SNMP errors
98+
if errorIndication:
99+
print errorIndication
100+
else:
101+
if errorStatus:
102+
print '%s at %s\n' % (
103+
errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
104+
)
105+
else:
106+
return(varBindTable)
107+
108+
def buildDict(oidDict,t,netiron): # passed a list of tuples, build's a dict based on the alias name
109+
builtdict = {}
110+
111+
for line in t:
112+
# if t[t.index(line)][2][1] != '':
113+
string = str(t[t.index(line)][2][1])
114+
match = re.search(r'ethernet', string)
115+
if match and t[t.index(line)][0][1] != '':
116+
alias = str(t[t.index(line)][0][1])
117+
index = str(t[t.index(line)][1][1])
118+
name = str(t[t.index(line)][2][1])
119+
hcinoct = str(t[t.index(line)][3][1])
120+
builtdict[netiron+'_'+alias+'_bitsin'] = int(hcinoct) * 8
121+
hcoutoct = str(t[t.index(line)][4][1])
122+
builtdict[netiron+'_'+alias+'_bitsout'] = int(hcoutoct) * 8
123+
hcinpkt = str(t[t.index(line)][5][1])
124+
builtdict[netiron+'_'+alias+'_pktsin'] = int(hcinpkt)
125+
hcoutpkt = str(t[t.index(line)][6][1])
126+
builtdict[netiron+'_'+alias+'_pktsout'] = int(hcoutpkt)
127+
128+
return builtdict
129+
130+
# define_metrics will run an snmp query on an ipaddr, find interfaces, build descriptors and set spoof_host
131+
# define_metrics is called from metric_init
132+
def define_metrics(Desc_Skel, ipaddr, netiron):
133+
snmpTable = runSnmp(oidDict,ipaddr)
134+
aliasdict = buildDict(oidDict,snmpTable,netiron)
135+
spoof_string = ipaddr + ':' + netiron
136+
#print newdict
137+
138+
for key in aliasdict.keys():
139+
if "bitsin" in key:
140+
descriptors.append(create_desc(Desc_Skel, {
141+
"name" : key,
142+
"units" : "bits/sec",
143+
"description" : "received bits per sec",
144+
"groups" : "Throughput",
145+
"spoof_host" : spoof_string,
146+
}))
147+
elif "bitsout" in key:
148+
descriptors.append(create_desc(Desc_Skel, {
149+
"name" : key,
150+
"units" : "bits/sec",
151+
"description" : "transmitted bits per sec",
152+
"groups" : "Throughput",
153+
"spoof_host" : spoof_string,
154+
}))
155+
elif "pktsin" in key:
156+
descriptors.append(create_desc(Desc_Skel, {
157+
"name" : key,
158+
"units" : "pkts/sec",
159+
"description" : "received packets per sec",
160+
"groups" : "Packets",
161+
"spoof_host" : spoof_string,
162+
}))
163+
elif "pktsout" in key:
164+
descriptors.append(create_desc(Desc_Skel, {
165+
"name" : key,
166+
"units" : "pkts/sec",
167+
"description" : "transmitted packets per sec",
168+
"groups" : "Packets",
169+
"spoof_host" : spoof_string,
170+
}))
171+
172+
173+
return descriptors
174+
175+
def metric_init(params):
176+
global descriptors, Desc_Skel, _Worker_Thread, Debug, newdict
177+
178+
print '[netiron] Received the following parameters'
179+
print params
180+
181+
#Import the params into the global NIPARAMS
182+
for key in params:
183+
NIPARAMS[key] = params[key]
184+
185+
Desc_Skel = {
186+
'name' : 'XXX',
187+
'call_back' : get_delta,
188+
'time_max' : 60,
189+
'value_type' : 'double',
190+
'format' : '%0f',
191+
'units' : 'XXX',
192+
'slope' : 'both',
193+
'description' : 'XXX',
194+
'groups' : 'netiron',
195+
}
196+
197+
# Find all the netiron's passed in params
198+
for para in params.keys():
199+
if para.startswith('netiron_'):
200+
#Get ipaddr + name of netirons from params
201+
ipaddr,name = params[para].split(':')
202+
# pass skel, ip and name to define_metrics to create descriptors
203+
descriptors = define_metrics(Desc_Skel, ipaddr, name)
204+
#Return the descriptors back to gmond
205+
return descriptors
206+
207+
def create_desc(skel, prop):
208+
d = skel.copy()
209+
for k,v in prop.iteritems():
210+
d[k] = v
211+
return d
212+
213+
214+
def metric_cleanup():
215+
'''Clean up the metric module.'''
216+
pass
217+
218+
# For CLI Debuging:
219+
if __name__ == '__main__':
220+
params = {
221+
'netiron_1' : '192.168.1.1:switch1',
222+
'netiron_2' : '192.168.1.2:switch2',
223+
}
224+
descriptors = metric_init(params)
225+
print len(descriptors)
226+
while True:
227+
for d in descriptors:
228+
v = d['call_back'](d['name'])
229+
print 'value for %s is %u' % (d['name'], v)
230+
print 'Sleeping 5 seconds'
231+
time.sleep(5)
232+
#exit(0)

network/netiron/netiron.pyconf

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
modules {
2+
module {
3+
name = "netiron"
4+
language = "python"
5+
param netiron_1 {
6+
# ip:hostname
7+
value = '192.168.1.1:switch1'
8+
}
9+
#param netiron_2 {
10+
# value = '192.168.1.2:switch2'
11+
#}
12+
}
13+
}
14+
#/* Collection groups for the
15+
# example python module */
16+
collection_group {
17+
collect_every = 20
18+
time_threshold = 50
19+
metric {
20+
name_match = "(.+)in"
21+
}
22+
metric {
23+
name_match = "(.+)out"
24+
}
25+
}

0 commit comments

Comments
 (0)