2020-10-05 15:17:25 +01:00
|
|
|
#!/usr/bin/python
|
2017-01-05 00:55:16 +00:00
|
|
|
""" 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.
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
Requires: python-dateutil
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
Usage:
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
$ check_datanommer_timesince CATEGORY WARNING_THRESH CRITICAL_THRESH
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
:Author: Ralph Bean <rbean@redhat.com>
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
"""
|
2020-10-05 15:17:25 +01:00
|
|
|
from __future__ import print_function
|
|
|
|
|
|
|
|
from builtins import str
|
2017-01-05 00:55:16 +00:00
|
|
|
import dateutil.relativedelta
|
|
|
|
import subprocess
|
|
|
|
import sys
|
|
|
|
import json
|
2020-10-05 15:17:25 +01:00
|
|
|
|
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
def query_timesince(identifier):
|
|
|
|
# If it has a '.', then assume it is a topic.
|
|
|
|
if '.' in identifier:
|
|
|
|
cmd = 'datanommer-latest --topic %s --timesince' % identifier
|
|
|
|
else:
|
|
|
|
cmd = 'datanommer-latest --category %s --timesince' % identifier
|
|
|
|
sys.stderr.write("Running %r\n" % cmd)
|
|
|
|
process = subprocess.Popen(cmd.split(), shell=False,
|
|
|
|
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
|
|
|
|
stdout, stderr = process.communicate()
|
|
|
|
prefix, stdout = stdout.split("INFO] ", 1)
|
|
|
|
data = json.loads(stdout)
|
|
|
|
return float(data[0])
|
2020-10-05 15:17:25 +01:00
|
|
|
|
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
def main():
|
|
|
|
identifier, warning_threshold, critical_threshold = sys.argv[-3:]
|
|
|
|
timesince = query_timesince(identifier)
|
|
|
|
warning_threshold = int(warning_threshold)
|
|
|
|
critical_threshold = int(critical_threshold)
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
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))
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
string = ", ".join(time_strings)
|
|
|
|
reason = "datanommer has not seen a %r message in %s" % (identifier, string)
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
if timesince > critical_threshold:
|
2020-10-05 15:17:25 +01:00
|
|
|
print("CRIT: ", reason)
|
2017-01-05 00:55:16 +00:00
|
|
|
sys.exit(2)
|
2020-10-05 15:17:25 +01:00
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
if timesince > warning_threshold:
|
2020-10-05 15:17:25 +01:00
|
|
|
print("WARN: ", reason)
|
2017-01-05 00:55:16 +00:00
|
|
|
sys.exit(1)
|
2020-10-05 15:17:25 +01:00
|
|
|
|
|
|
|
print("OK: ", reason)
|
2017-01-05 00:55:16 +00:00
|
|
|
sys.exit(0)
|
2020-10-05 15:17:25 +01:00
|
|
|
|
|
|
|
|
2017-01-05 00:55:16 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
try:
|
|
|
|
main()
|
|
|
|
except Exception as e:
|
2020-10-05 15:17:25 +01:00
|
|
|
print("UNKNOWN: ", str(e))
|
2017-01-05 00:55:16 +00:00
|
|
|
sys.exit(3)
|