1414import psycopg2
1515
1616from grafoleancollector import Collector
17- from dbutils import get_db_cursor , DB_PREFIX , migrate_if_needed , db_disconnect
17+ from dbutils import get_db_cursor , DB_PREFIX , initial_wait_for_db , migrate_if_needed , db_disconnect , DBConnectionError
1818
1919
2020logging .basicConfig (format = '%(asctime)s | %(levelname)s | %(message)s' ,
@@ -40,6 +40,8 @@ class InvalidOutputPath(Exception):
4040
4141def _get_previous_counter_value (counter_ident ):
4242 with get_db_cursor () as c :
43+ if c is None :
44+ raise DBConnectionError ()
4345 try :
4446 c .execute (f'SELECT value, ts FROM { DB_PREFIX } bot_counters WHERE id = %s;' , (counter_ident ,))
4547 rec = c .fetchone ()
@@ -54,6 +56,8 @@ def _get_previous_counter_value(counter_ident):
5456
5557def _save_current_counter_value (new_value , now , counter_ident ):
5658 with get_db_cursor () as c :
59+ if c is None :
60+ raise DBConnectionError ()
5761 c .execute (f"INSERT INTO { DB_PREFIX } bot_counters (id, value, ts) VALUES (%s, %s, %s) ON CONFLICT (id) DO UPDATE SET value = %s, ts = %s;" ,
5862 (counter_ident , new_value , now , new_value , now ))
5963
@@ -67,24 +71,28 @@ def _convert_counters_to_values(results, now, counter_ident_prefix):
6771 if v .snmp_type not in ['COUNTER' , 'COUNTER64' ]:
6872 new_results .append (v )
6973 continue
74+
7075 # counter - deal with it:
71- counter_ident = counter_ident_prefix + f'/{ i } /{ v .oid } /{ v .oid_index } '
72- old_value , t = _get_previous_counter_value (counter_ident )
7376 new_value = int (float (v .value ))
74- _save_current_counter_value (new_value , now , counter_ident )
75- if old_value is None :
76- new_results .append (SNMPVariable (oid = v .oid , oid_index = v .oid_index , value = None , snmp_type = 'COUNTER_PER_S' ))
77- continue
77+ counter_ident = counter_ident_prefix + f'/{ i } /{ v .oid } /{ v .oid_index } '
78+ try :
79+ old_value , t = _get_previous_counter_value (counter_ident )
80+ _save_current_counter_value (new_value , now , counter_ident )
81+ if old_value is None :
82+ new_results .append (SNMPVariable (oid = v .oid , oid_index = v .oid_index , value = None , snmp_type = 'COUNTER_PER_S' ))
83+ continue
7884
79- # it seems like the counter overflow happened, discard result:
80- if new_value < old_value :
81- new_results .append (SNMPVariable (oid = v .oid , oid_index = v .oid_index , value = None , snmp_type = 'COUNTER_PER_S' ))
82- log .warning (f"Counter overflow detected for oid { v .oid } , oid index { v .oid_index } , discarding value - if this happens often, consider using OIDS with 64bit counters (if available) or decreasing polling interval." )
83- continue
85+ # it seems like the counter overflow happened, discard result:
86+ if new_value < old_value :
87+ new_results .append (SNMPVariable (oid = v .oid , oid_index = v .oid_index , value = None , snmp_type = 'COUNTER_PER_S' ))
88+ log .warning (f"Counter overflow detected for oid { v .oid } , oid index { v .oid_index } , discarding value - if this happens often, consider using OIDS with 64bit counters (if available) or decreasing polling interval." )
89+ continue
8490
85- dt = now - t
86- dv = (new_value - old_value ) / dt
87- new_results .append (SNMPVariable (oid = v .oid , oid_index = v .oid_index , value = dv , snmp_type = 'COUNTER_PER_S' ))
91+ dt = now - t
92+ dv = (new_value - old_value ) / dt
93+ new_results .append (SNMPVariable (oid = v .oid , oid_index = v .oid_index , value = dv , snmp_type = 'COUNTER_PER_S' ))
94+ except DBConnectionError :
95+ log .error (f"Could not convert counter due to DB error: { counter_ident } / { new_value } " )
8896 return new_results
8997
9098
@@ -443,6 +451,7 @@ def wait_for_grafolean(backend_url):
443451if __name__ == "__main__" :
444452 dotenv .load_dotenv ()
445453
454+ initial_wait_for_db ()
446455 migrate_if_needed ()
447456 db_disconnect () # each worker should open their own connection pool
448457
0 commit comments