|
4 | 4 | # Author: Evan Fraser [email protected] |
5 | 5 | # Date: August 2012 |
6 | 6 | # Copyright: GPL |
| 7 | +# Updated 21/03/2014 to do SNMP calls threaded. |
7 | 8 |
|
8 | 9 | import sys |
9 | 10 | import os |
10 | 11 | import re |
11 | 12 | import time |
| 13 | +import threading |
12 | 14 | import pprint |
13 | 15 | from pysnmp.entity.rfc3413.oneliner import cmdgen |
14 | 16 | NIPARAMS = {} |
|
18 | 20 | 'data' : {} |
19 | 21 | } |
20 | 22 | LAST_NIMETRICS = dict(NIMETRICS) |
21 | | -NIMETRICS_CACHE_MAX = 5 |
| 23 | +NIMETRICS_CACHE_MAX = 10 |
| 24 | +SNMPTABLE = {} |
22 | 25 |
|
23 | 26 | descriptors = list() |
24 | 27 |
|
|
33 | 36 | 'ifOutErrors' : (1,3,6,1,2,1,2,2,1,20), |
34 | 37 | } |
35 | 38 |
|
| 39 | + |
36 | 40 | def get_metrics(): |
37 | 41 | """Return all metrics""" |
38 | 42 |
|
39 | | - global NIMETRICS, LAST_NIMETRICS |
| 43 | + global NIMETRICS, LAST_NIMETRICS, SNMPTABLE |
40 | 44 |
|
41 | 45 | # if interval since last check > NIMETRICS_CACHE_MAX get metrics again |
42 | 46 | if (time.time() - NIMETRICS['time']) > NIMETRICS_CACHE_MAX: |
43 | 47 | metrics = {} |
44 | 48 | for para in NIPARAMS.keys(): |
45 | 49 | if para.startswith('switch_'): |
46 | 50 | ipaddr,name = NIPARAMS[para].split(':') |
47 | | - snmpTable = runSnmp(oidDict,ipaddr) |
| 51 | + #snmpTable = runSnmp(oidDict,ipaddr) |
| 52 | + threading.Thread(runSnmp(oidDict,ipaddr)) |
| 53 | + snmpTable = SNMPTABLE[ipaddr] |
48 | 54 | newmetrics = buildDict(oidDict,snmpTable,name) |
49 | 55 | metrics = dict(newmetrics, **metrics) |
50 | 56 |
|
@@ -75,7 +81,7 @@ def get_delta(name): |
75 | 81 |
|
76 | 82 | # Separate routine to perform SNMP queries and returns table (dict) |
77 | 83 | def runSnmp(oidDict,ip): |
78 | | - |
| 84 | + global SNMPTABLE |
79 | 85 | # cmdgen only takes tuples, oid strings don't work |
80 | 86 |
|
81 | 87 | # 'ifIndex' : (1,3,6,1,2,1,2,2,1,1), |
@@ -111,7 +117,8 @@ def runSnmp(oidDict,ip): |
111 | 117 | errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?' |
112 | 118 | ) |
113 | 119 | else: |
114 | | - return(varBindTable) |
| 120 | + #return(varBindTable) |
| 121 | + SNMPTABLE[ip] = varBindTable |
115 | 122 |
|
116 | 123 | def buildDict(oidDict,t,switch): # passed a list of tuples, build's a dict based on the alias name |
117 | 124 | builtdict = {} |
@@ -146,7 +153,11 @@ def buildDict(oidDict,t,switch): # passed a list of tuples, build's a dict based |
146 | 153 | # define_metrics will run an snmp query on an ipaddr, find interfaces, build descriptors and set spoof_host |
147 | 154 | # define_metrics is called from metric_init |
148 | 155 | def define_metrics(Desc_Skel, ipaddr, switch): |
149 | | - snmpTable = runSnmp(oidDict,ipaddr) |
| 156 | + global SNMPTABLE |
| 157 | + snmpThread = threading.Thread(runSnmp(oidDict,ipaddr)) |
| 158 | + snmpTable = SNMPTABLE[ipaddr] |
| 159 | + |
| 160 | + #snmpTable = runSnmp(oidDict,ipaddr) |
150 | 161 | aliasdict = buildDict(oidDict,snmpTable,switch) |
151 | 162 | spoof_string = ipaddr + ':' + switch |
152 | 163 | #print newdict |
|
0 commit comments