Skip to content

Commit ca5a8ad

Browse files
authored
Set Prometheus metric to 0 if SNMP metric not found (#121)
1 parent 3e6d6e4 commit ca5a8ad

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

collector/server.py

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,6 @@ def work(ip_list):
7373

7474
def get_snmp_data(ip):
7575
for oid in SnmpOid:
76-
# set the error to zero by default, this is because
77-
# SNMP OIDs related to errors often dissappear when
78-
# the associated issue that the metric refers to is
79-
# no longer present (i.e. an empty tray now has
80-
# paper). To avoid leaving an error metric as 1
81-
# which would create a false positive, set the error
82-
# as zero before reading anything.
83-
if oid.is_error:
84-
snmp_error.labels(name=oid.metric_name, ip=ip).set(0)
8576
with snmp_req_duration.time():
8677
errorIndication, errorStatus, errorIndex, varBinds = next(
8778
getCmd(SnmpEngine(),
@@ -90,22 +81,30 @@ def get_snmp_data(ip):
9081
ContextData(),
9182
ObjectType(ObjectIdentity(oid.metric_value)))
9283
)
93-
if errorIndication:
94-
logging.error(f"Error indication from {ip} for metric {oid.metric_value}: {errorIndication}")
95-
device_unreachable.set(1)
96-
continue
97-
if errorStatus:
98-
logging.error(f"Error status from {ip} for metric {oid.metric_value}: {errorStatus.prettyPrint()}")
99-
continue
100-
101-
device_unreachable.set(0)
102-
if not varBinds:
103-
continue
104-
res = varBinds[0]
84+
if errorIndication:
85+
logging.error(f"Error indication from {ip} for metric {oid.metric_value}: {errorIndication}")
86+
device_unreachable.set(1)
87+
continue
88+
if errorStatus:
89+
logging.error(f"Error status from {ip} for metric {oid.metric_value}: {errorStatus.prettyPrint()}")
90+
# SNMP OIDs related to errors often dissappear when
91+
# the associated issue that the metric refers to is
92+
# no longer present (i.e. an empty tray now has
93+
# paper). To avoid leaving an error metric as 1
94+
# which would create a false positive, set the metric
95+
# to zero if the associated SNMP OID was not found
10596
if oid.is_error:
106-
snmp_error.labels(name=oid.metric_name, ip=ip).set(1)
107-
continue
108-
snmp_metric.labels(name=oid.metric_name, ip=ip).set(res[1])
97+
snmp_error.labels(name=oid.metric_name, ip=ip).set(0)
98+
continue
99+
100+
device_unreachable.set(0)
101+
if not varBinds:
102+
continue
103+
res = varBinds[0]
104+
if oid.is_error:
105+
snmp_error.labels(name=oid.metric_name, ip=ip).set(1)
106+
continue
107+
snmp_metric.labels(name=oid.metric_name, ip=ip).set(res[1])
109108

110109
@app.get("/metrics")
111110
async def metrics():

0 commit comments

Comments
 (0)