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