update nagios client checks to py3

This commit is contained in:
Mark O'Brien 2020-10-05 15:17:25 +01:00
parent 8a8b1731b3
commit 32330de141
4 changed files with 87 additions and 78 deletions

View file

@ -1,26 +1,28 @@
#!/usr/bin/python2
#!/usr/bin/python
""" NRPE check for datanommer/fedmsg health.
Given a category like 'bodhi', 'buildsys', or 'git', return an error if
datanommer hasn't seen a message of that type in such and such time.
You can alternatively provide a 'topic' which might look like
org.fedoraproject.prod.bodhi.update.comment.
Requires: python-dateutil
Usage:
$ check_datanommer_timesince CATEGORY WARNING_THRESH CRITICAL_THRESH
:Author: Ralph Bean <rbean@redhat.com>
"""
from __future__ import print_function
from builtins import str
import dateutil.relativedelta
import subprocess
import sys
import json
def query_timesince(identifier):
# If it has a '.', then assume it is a topic.
if '.' in identifier:
@ -34,39 +36,39 @@ def query_timesince(identifier):
prefix, stdout = stdout.split("INFO] ", 1)
data = json.loads(stdout)
return float(data[0])
def main():
identifier, warning_threshold, critical_threshold = sys.argv[-3:]
timesince = query_timesince(identifier)
warning_threshold = int(warning_threshold)
critical_threshold = int(critical_threshold)
time_strings = []
rd = dateutil.relativedelta.relativedelta(seconds=timesince)
for denomination in ['years', 'months', 'days', 'hours', 'minutes', 'seconds']:
value = getattr(rd, denomination, 0)
if value:
time_strings.append("%d %s" % (value, denomination))
string = ", ".join(time_strings)
reason = "datanommer has not seen a %r message in %s" % (identifier, string)
if timesince > critical_threshold:
print "CRIT: ", reason
print("CRIT: ", reason)
sys.exit(2)
if timesince > warning_threshold:
print "WARN: ", reason
print("WARN: ", reason)
sys.exit(1)
print "OK: ", reason
print("OK: ", reason)
sys.exit(0)
if __name__ == '__main__':
try:
main()
except Exception as e:
print "UNKNOWN: ", str(e)
print("UNKNOWN: ", str(e))
sys.exit(3)