From 8286b8f6c83235c0f498cc30ea29d897ca731215 Mon Sep 17 00:00:00 2001 From: Adam Williamson Date: Mon, 14 Aug 2023 08:58:54 -0700 Subject: [PATCH] Port check_nagios_notifications.py to Python 3 Saw from one of the emails this morning that this isn't running because there's no python2 on whatever system it was trying to run on. This ports it to Python 3 (thanks, 2to3) and cleans up the formatting (thanks, black). I tested it with a random sample file I found lying around the internet - https://github.com/bahamas10/node-nagios-status-parser/blob/master/status.dat and it seems to do what it's supposed to do. Signed-off-by: Adam Williamson --- .../scripts/check_nagios_notifications.py | 125 +++++++++++------- 1 file changed, 77 insertions(+), 48 deletions(-) diff --git a/roles/nagios_server/files/nagios/scripts/check_nagios_notifications.py b/roles/nagios_server/files/nagios/scripts/check_nagios_notifications.py index 08b0e751ab..4d2190d6e9 100755 --- a/roles/nagios_server/files/nagios/scripts/check_nagios_notifications.py +++ b/roles/nagios_server/files/nagios/scripts/check_nagios_notifications.py @@ -1,10 +1,11 @@ -#!/usr/bin/python2 +#!/usr/bin/python3 # # A script to read the Nagios status file and send email for notifications # off, but have recovered. # # Written by Athmane Madjoudj , 2011-11-15 # based on tummy.com's work , 2010-11-16 +# Minimally ported to Python 3 by Adam Williamson , 2023-08-14 # Released under the GPLv2. import re @@ -14,10 +15,11 @@ from socket import gethostname # Settings debug = 0 -EMAIL_FROM="nagios@fedoraproject.org" -EMAIL_TO="sysadmin-noc-members@fedoraproject.org" -#EMAIL_TO="athmane@fedoraproject.org" -nagios_status_file = '/var/spool/nagios/status.dat' +EMAIL_FROM = "nagios@fedoraproject.org" +EMAIL_TO = "sysadmin-noc-members@fedoraproject.org" +# EMAIL_TO="athmane@fedoraproject.org" +nagios_status_file = "/var/spool/nagios/status.dat" + class NagiosStatus: def __init__(self, filename): @@ -26,79 +28,106 @@ class NagiosStatus: self.load_status_file() def load_status_file(self): - fp = open(self.filename, 'r') + fp = open(self.filename, "r") while True: line = fp.readline() - if not line: break + if not line: + break - m = re.match(r'^hoststatus\s+{\s*$', line) + m = re.match(r"^hoststatus\s+{\s*$", line) if m: - if debug >= 2: print 'START OF HOST' - data = { 'services' : [] } + if debug >= 2: + print("START OF HOST") + data = {"services": []} while True: line = fp.readline() - if not line: break - if debug >= 2: print 'host: %s' % line.rstrip() - m2 = re.match(r'^\s+([^=]+)=(\S.*)*$', line.rstrip()) - if not m2: break + if not line: + break + if debug >= 2: + print("host: %s" % line.rstrip()) + m2 = re.match(r"^\s+([^=]+)=(\S.*)*$", line.rstrip()) + if not m2: + break data[m2.group(1)] = m2.group(2) - self.hosts[data['host_name']] = data - if debug >= 2: print 'END OF HOST' + self.hosts[data["host_name"]] = data + if debug >= 2: + print("END OF HOST") - m = re.match(r'^servicestatus\s+{\s*$', line) + m = re.match(r"^servicestatus\s+{\s*$", line) if m: - if debug >= 2: print 'START OF SERVICE' + if debug >= 2: + print("START OF SERVICE") data = {} while True: line = fp.readline() - if not line: break - if debug >= 2: print 'service: %s' % line.rstrip() - m2 = re.match(r'^\s+([^=]+)=(.*)$', line.rstrip()) - if not m2: break + if not line: + break + if debug >= 2: + print("service: %s" % line.rstrip()) + m2 = re.match(r"^\s+([^=]+)=(.*)$", line.rstrip()) + if not m2: + break data[m2.group(1)] = m2.group(2) - self.hosts[data['host_name']]['services'].append(data) - if debug >= 2: print 'END OF SERVICE' + self.hosts[data["host_name"]]["services"].append(data) + if debug >= 2: + print("END OF SERVICE") + def main(): status = NagiosStatus(nagios_status_file) output = "" for host in sorted(status.hosts.keys()): host = status.hosts[host] - if host.get('notifications_enabled', None) == None: - output+= 'Host %s has no notifications_enabled line \n' % host['host_name'] + if host.get("notifications_enabled", None) == None: + output += "Host %s has no notifications_enabled line \n" % host["host_name"] continue # are there any hard states that aren't 0 or 1? - hard_states = [ x for x in - [ int(x['last_hard_state']) for x in host['services'] ] - if not x in [0,1] ] + hard_states = [ + x + for x in [int(x["last_hard_state"]) for x in host["services"]] + if not x in [0, 1] + ] need_newline = False - if host['notifications_enabled'] == '0' and not hard_states: - output += ('Host %s has notifications disabled and all services ok \n' - % host['host_name']) + if host["notifications_enabled"] == "0" and not hard_states: + output += ( + "Host %s has notifications disabled and all services ok \n" + % host["host_name"] + ) need_newline = True - for service in host['services']: - if debug: print '%s@%s' % ( service['check_command'], host['host_name'] ) - if debug: print ' notifications_enabled: %(notifications_enabled)s last_hard_state: %(last_hard_state)s' % service - if (int(service['notifications_enabled']) == 0 - and int(service['last_hard_state']) in [0,1]): - output+= (('Service %(check_command)s@%(host_name)s\n' - ' has notifications disabled, but is ok\n') % service) + for service in host["services"]: + if debug: + print("%s@%s" % (service["check_command"], host["host_name"])) + print( + " notifications_enabled: %(notifications_enabled)s last_hard_state: %(last_hard_state)s" + % service + ) + if int(service["notifications_enabled"]) == 0 and int( + service["last_hard_state"] + ) in [0, 1]: + output += ( + "Service %(check_command)s@%(host_name)s\n" + " has notifications disabled, but is ok\n" + ) % service need_newline = True - if need_newline: output+="\n\n" - - if output.strip() != '': - msg_body = "List of notifications off for recovered hosts/services: \n\n"+output + if need_newline: + output += "\n\n" + + if output.strip() != "": + msg_body = ( + "List of notifications off for recovered hosts/services: \n\n" + output + ) msg = MIMEText(msg_body) - msg['Subject']="Notifications status on %s" % gethostname() - msg['From']=EMAIL_FROM - msg['To']=EMAIL_TO + msg["Subject"] = "Notifications status on %s" % gethostname() + msg["From"] = EMAIL_FROM + msg["To"] = EMAIL_TO smtp_conn = SMTP() - smtp_conn.connect('localhost') + smtp_conn.connect("localhost") smtp_conn.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string()) smtp_conn.quit() -if __name__ == '__main__': + +if __name__ == "__main__": main()