66from pytz import utc
77from colors import color
88import requests
9+ import redis
910
1011from easysnmp import Session , SNMPVariable
1112from mathjspy import MathJS
@@ -26,18 +27,19 @@ class NoValueForOid(Exception):
2627 pass
2728
2829
29- previous_counter_values = {}
30+ REDIS_HOST = os .environ .get ('REDIS_HOST' , '127.0.0.1' )
31+ r = redis .Redis (host = REDIS_HOST )
3032
3133
3234def _get_previous_counter_value (counter_ident ):
33- prev_value = previous_counter_values . get (counter_ident )
34- if prev_value is None :
35+ prev_value = r . hgetall (counter_ident )
36+ if not prev_value : # empty dict
3537 return None , None
36- return prev_value
38+ return int ( prev_value [ b'v' ]), float ( prev_value [ b't' ])
3739
3840
3941def _save_current_counter_value (new_value , now , counter_ident ):
40- previous_counter_values [ counter_ident ] = ( new_value , now )
42+ r . hmset ( counter_ident , { b'v' : new_value , b't' : now } )
4143
4244
4345def _convert_counters_to_values (results , now , counter_ident_prefix ):
@@ -52,7 +54,7 @@ def _convert_counters_to_values(results, now, counter_ident_prefix):
5254 # counter - deal with it:
5355 counter_ident = counter_ident_prefix + f'/{ i } /{ v .oid } /{ v .oid_index } '
5456 old_value , t = _get_previous_counter_value (counter_ident )
55- new_value = float (v .value )
57+ new_value = int ( float (v .value ) )
5658 _save_current_counter_value (new_value , now , counter_ident )
5759 if old_value is None :
5860 new_results .append (SNMPVariable (oid = v .oid , oid_index = v .oid_index , value = None , snmp_type = 'COUNTER_PER_S' ))
@@ -233,7 +235,7 @@ def do_snmp(*args, **job_info):
233235 walk_indexes = [r .oid_index for r in result ]
234236 log .info ("Results: {}" .format (list (zip (oids , methods , results ))))
235237
236- counter_ident_prefix = f'{ job_info ["entity_id" ]} /{ sensor ["sensor_details" ][ "id " ]} '
238+ counter_ident_prefix = f'{ job_info ["entity_id" ]} /{ sensor ["sensor_id " ]} '
237239 results_no_counters = _convert_counters_to_values (results , time .time (), counter_ident_prefix )
238240
239241 # We have SNMP results and expression - let's calculate value(s). The trick here is that
0 commit comments