From 4fa49cc5416ac71cc19f29e68a91b0cc89bf82e2 Mon Sep 17 00:00:00 2001
From: Leonardo Rossetti <me@lrossetti.com>
Date: Fri, 20 Apr 2018 00:01:48 -0300
Subject: [PATCH] koji_wellness plugin

Signed-off-by: Leonardo Rossetti <me@lrossetti.com>
---
 roles/nagios_server/files/nagios/commands/koji.cfg |  6 ++
 .../files/nagios/plugins/check_koji_wellness.py    | 86 ++++++++++++++++++++++
 roles/nagios_server/files/nagios/services/koji.cfg |  8 ++
 3 files changed, 100 insertions(+)
 create mode 100755 roles/nagios_server/files/nagios/plugins/check_koji_wellness.py

diff --git a/roles/nagios_server/files/nagios/commands/koji.cfg b/roles/nagios_server/files/nagios/commands/koji.cfg
index 03ab1d33f..ae3ead6db 100644
--- a/roles/nagios_server/files/nagios/commands/koji.cfg
+++ b/roles/nagios_server/files/nagios/commands/koji.cfg
@@ -27,3 +27,9 @@ define command{
         command_line    $USER1$/check_koji
 }
 
+# 'check_koji_wellness'
+define command {
+        command_name    check_koji_wellness.py
+        command_line    $USER1$/check_koji_wellness
+}
+
diff --git a/roles/nagios_server/files/nagios/plugins/check_koji_wellness.py b/roles/nagios_server/files/nagios/plugins/check_koji_wellness.py
new file mode 100755
index 000000000..08a576bb6
--- /dev/null
+++ b/roles/nagios_server/files/nagios/plugins/check_koji_wellness.py
@@ -0,0 +1,86 @@
+#!/usr/bin/env python
+import sys
+import re
+
+import requests
+
+koji_host = sys.argv[1]
+try:
+    koji_pkg = sys.argv[2]
+except IndexError:
+    koji_pkg = 'koji'
+
+NAGIOS = {
+    'OK': 0,
+    'WARN': 1,
+    'CRIT': 2
+}
+
+TARGETS = {
+    'web': 'https://%s/koji/' % koji_host,
+    'hub': 'https://%s/kojihub/' % koji_host,
+    'pkg': 'https://%s/packages/%s/' % (koji_host, koji_pkg)
+}
+
+
+def do_request(url, method='GET', headers=None, body=None):
+    try:
+        res = getattr(requests, method.lower())(url, headers=headers, data=body)
+    except requests.exceptions.ConnectionError as e:
+        return (500, e.message)
+    return (res.status_code, res.text)
+
+
+def check_web():
+    url = TARGETS['web']
+    (status_code, data) = do_request(url)
+    if status_code != 200:
+        return (NAGIOS['CRIT'], 'Unable to access "%s"' % url)
+    if not re.search(r'<div class="pageHeader">[a-zA-Z\s]+</div>', data):
+        return (NAGIOS['WARN'], 'Unable to match content from "%s"' % url)
+    return (NAGIOS['OK'], '')
+
+
+def check_hub(method='system.listMethods'):
+    url = TARGETS['hub']
+    xmlrpc = '''<?xml version="1.0" encoding="utf-8"?>
+    <methodCall>
+        <methodName>%s</methodName>
+        <params></params>
+    </methodCall>''' % method
+    headers = {
+        'Content-Type': 'text/xml'
+    }
+    (status_code, data) = do_request(url, method='POST', headers=headers, body=xmlrpc)
+    if status_code != 200:
+        return (NAGIOS['CRIT'], 'Unable to access "%s"' % url)
+    if len(re.findall(r'<value><string>[a-zA-Z0-9\._]+</string></value>', data)) == 0:
+        return (NAGIOS['WARN'], 'Unable to parse content from "%s"' % url)
+    return (NAGIOS['OK'], None)
+
+
+def check_pkg():
+    url = TARGETS['pkg']
+    (status_code, data) = do_request(url)
+    if status_code != 200:
+        return (NAGIOS['CRIT'], 'Unable to access "%s"' % url)
+    if len(re.findall(r'<img src="/icons/folder.gif" alt="\[DIR\]"> <a href="[0-9.\/]+">[0-9.\/]+</a>', data)) == 0:
+        return (NAGIOS['WARN'], 'Unable to match content from "%s"' % url)
+    return (NAGIOS['OK'], None)
+
+
+def main():
+    ok = NAGIOS['OK']
+    for (code, msg) in (check_web(), check_hub(), check_pkg()):
+        if code != ok:
+            return (code, msg)
+    return (ok, None)
+
+
+if __name__ == '__main__':
+    (code, reason) = main()
+    if code != NAGIOS['OK']:
+        sys.stderr.write('%s\n' % reason)
+    else:
+        sys.stdout.write('All checks passed.\n')
+    sys.exit(code)
diff --git a/roles/nagios_server/files/nagios/services/koji.cfg b/roles/nagios_server/files/nagios/services/koji.cfg
index e18f35c59..851e9b211 100644
--- a/roles/nagios_server/files/nagios/services/koji.cfg
+++ b/roles/nagios_server/files/nagios/services/koji.cfg
@@ -6,3 +6,11 @@ define service {
   use                   criticaltemplate
 }
 
+
+define service {
+  host_name     koji.fedoraproject.org
+  service_description   Check Koji wellness
+  check_command         check_koji_wellness!koji.fedoraproject.org!koji
+  max_check_attempts    5
+  use                   criticaltemplate
+}
-- 
2.14.3

