Skip to content

Commit

Permalink
fibrechannel.py now calls SNMP queries as a separate thread.
Browse files Browse the repository at this point in the history
  • Loading branch information
evanjfraser committed Mar 20, 2014
1 parent 75d9a59 commit 2964aed
Showing 1 changed file with 17 additions and 6 deletions.
23 changes: 17 additions & 6 deletions fibrechannel/fibrechannel.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@
# Author: Evan Fraser evan.fraser@trademe.co.nz
# Date: August 2012
# Copyright: GPL
# Updated 21/03/2014 to do SNMP calls threaded.

import sys
import os
import re
import time
import threading
import pprint
from pysnmp.entity.rfc3413.oneliner import cmdgen
NIPARAMS = {}
Expand All @@ -18,7 +20,8 @@
'data' : {}
}
LAST_NIMETRICS = dict(NIMETRICS)
NIMETRICS_CACHE_MAX = 5
NIMETRICS_CACHE_MAX = 10
SNMPTABLE = {}

descriptors = list()

Expand All @@ -33,18 +36,21 @@
'ifOutErrors' : (1,3,6,1,2,1,2,2,1,20),
}


def get_metrics():
"""Return all metrics"""

global NIMETRICS, LAST_NIMETRICS
global NIMETRICS, LAST_NIMETRICS, SNMPTABLE

# 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('switch_'):
ipaddr,name = NIPARAMS[para].split(':')
snmpTable = runSnmp(oidDict,ipaddr)
#snmpTable = runSnmp(oidDict,ipaddr)
threading.Thread(runSnmp(oidDict,ipaddr))
snmpTable = SNMPTABLE[ipaddr]
newmetrics = buildDict(oidDict,snmpTable,name)
metrics = dict(newmetrics, **metrics)

Expand Down Expand Up @@ -75,7 +81,7 @@ def get_delta(name):

# Separate routine to perform SNMP queries and returns table (dict)
def runSnmp(oidDict,ip):

global SNMPTABLE
# cmdgen only takes tuples, oid strings don't work

# 'ifIndex' : (1,3,6,1,2,1,2,2,1,1),
Expand Down Expand Up @@ -111,7 +117,8 @@ def runSnmp(oidDict,ip):
errorStatus.prettyPrint(), errorIndex and varBindTable[-1][int(errorIndex)-1] or '?'
)
else:
return(varBindTable)
#return(varBindTable)
SNMPTABLE[ip] = varBindTable

def buildDict(oidDict,t,switch): # passed a list of tuples, build's a dict based on the alias name
builtdict = {}
Expand Down Expand Up @@ -146,7 +153,11 @@ def buildDict(oidDict,t,switch): # passed a list of tuples, build's a dict based
# 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, switch):
snmpTable = runSnmp(oidDict,ipaddr)
global SNMPTABLE
snmpThread = threading.Thread(runSnmp(oidDict,ipaddr))
snmpTable = SNMPTABLE[ipaddr]

#snmpTable = runSnmp(oidDict,ipaddr)
aliasdict = buildDict(oidDict,snmpTable,switch)
spoof_string = ipaddr + ':' + switch
#print newdict
Expand Down

0 comments on commit 2964aed

Please sign in to comment.