Skip to content

Commit b303d5a

Browse files
committed
Add logging when invalid (negative) value is encountered
1 parent dcbab15 commit b303d5a

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

netflowbot.py

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ def get_traffic_for_entity(interval_label, last_used_ts, max_ts, time_between, d
282282
# returns cumulative traffic for the whole entity, and traffic per interface for this entity
283283
with get_db_cursor() as c:
284284

285-
c.execute(f"""
285+
sql = f"""
286286
SELECT
287287
f.{'input_snmp' if direction == DIRECTION_INGRESS else 'output_snmp'},
288288
sum(f.in_bytes)
@@ -295,15 +295,23 @@ def get_traffic_for_entity(interval_label, last_used_ts, max_ts, time_between, d
295295
f.direction = %s
296296
GROUP BY
297297
f.{'input_snmp' if direction == DIRECTION_INGRESS else 'output_snmp'}
298-
""", (entity_ip, last_used_ts, max_ts, direction))
298+
"""
299+
c.execute(sql, (entity_ip, last_used_ts, max_ts, direction))
299300

300301
values = []
301302
sum_traffic = 0
302303
for if_index, traffic_bytes in c.fetchall():
303304
output_path = NetFlowBot.construct_output_path_prefix(interval_label, direction, entity_id, interface=if_index)
305+
v = traffic_bytes / time_between
306+
if v < 0:
307+
# invalid condition - to find the cause we need some additional logging:
308+
log.error(f"Sum of positive numbers should never be negative! " +
309+
"{v} {traffic_bytes} {time_between} {sql} {entity_ip} {last_used_ts} {max_ts} {direction}")
310+
continue # this is never ok - skip this value
311+
304312
values.append({
305313
'p': output_path,
306-
'v': traffic_bytes / time_between,
314+
'v': v,
307315
})
308316
sum_traffic += traffic_bytes
309317

0 commit comments

Comments
 (0)