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 <awilliam@redhat.com>
This commit is contained in:
parent
6af2bb2619
commit
8286b8f6c8
1 changed files with 77 additions and 48 deletions
|
@ -1,10 +1,11 @@
|
||||||
#!/usr/bin/python2
|
#!/usr/bin/python3
|
||||||
#
|
#
|
||||||
# A script to read the Nagios status file and send email for notifications
|
# A script to read the Nagios status file and send email for notifications
|
||||||
# off, but have recovered.
|
# off, but have recovered.
|
||||||
#
|
#
|
||||||
# Written by Athmane Madjoudj <athmane@fedoraproject.org>, 2011-11-15
|
# Written by Athmane Madjoudj <athmane@fedoraproject.org>, 2011-11-15
|
||||||
# based on tummy.com's work <jafo@tummy.com>, 2010-11-16
|
# based on tummy.com's work <jafo@tummy.com>, 2010-11-16
|
||||||
|
# Minimally ported to Python 3 by Adam Williamson <adamwill@fedoraproject.org>, 2023-08-14
|
||||||
# Released under the GPLv2.
|
# Released under the GPLv2.
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
@ -14,10 +15,11 @@ from socket import gethostname
|
||||||
|
|
||||||
# Settings
|
# Settings
|
||||||
debug = 0
|
debug = 0
|
||||||
EMAIL_FROM="nagios@fedoraproject.org"
|
EMAIL_FROM = "nagios@fedoraproject.org"
|
||||||
EMAIL_TO="sysadmin-noc-members@fedoraproject.org"
|
EMAIL_TO = "sysadmin-noc-members@fedoraproject.org"
|
||||||
#EMAIL_TO="athmane@fedoraproject.org"
|
# EMAIL_TO="athmane@fedoraproject.org"
|
||||||
nagios_status_file = '/var/spool/nagios/status.dat'
|
nagios_status_file = "/var/spool/nagios/status.dat"
|
||||||
|
|
||||||
|
|
||||||
class NagiosStatus:
|
class NagiosStatus:
|
||||||
def __init__(self, filename):
|
def __init__(self, filename):
|
||||||
|
@ -26,79 +28,106 @@ class NagiosStatus:
|
||||||
self.load_status_file()
|
self.load_status_file()
|
||||||
|
|
||||||
def load_status_file(self):
|
def load_status_file(self):
|
||||||
fp = open(self.filename, 'r')
|
fp = open(self.filename, "r")
|
||||||
while True:
|
while True:
|
||||||
line = fp.readline()
|
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 m:
|
||||||
if debug >= 2: print 'START OF HOST'
|
if debug >= 2:
|
||||||
data = { 'services' : [] }
|
print("START OF HOST")
|
||||||
|
data = {"services": []}
|
||||||
while True:
|
while True:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line: break
|
if not line:
|
||||||
if debug >= 2: print 'host: %s' % line.rstrip()
|
break
|
||||||
m2 = re.match(r'^\s+([^=]+)=(\S.*)*$', line.rstrip())
|
if debug >= 2:
|
||||||
if not m2: break
|
print("host: %s" % line.rstrip())
|
||||||
|
m2 = re.match(r"^\s+([^=]+)=(\S.*)*$", line.rstrip())
|
||||||
|
if not m2:
|
||||||
|
break
|
||||||
data[m2.group(1)] = m2.group(2)
|
data[m2.group(1)] = m2.group(2)
|
||||||
self.hosts[data['host_name']] = data
|
self.hosts[data["host_name"]] = data
|
||||||
if debug >= 2: print 'END OF HOST'
|
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 m:
|
||||||
if debug >= 2: print 'START OF SERVICE'
|
if debug >= 2:
|
||||||
|
print("START OF SERVICE")
|
||||||
data = {}
|
data = {}
|
||||||
while True:
|
while True:
|
||||||
line = fp.readline()
|
line = fp.readline()
|
||||||
if not line: break
|
if not line:
|
||||||
if debug >= 2: print 'service: %s' % line.rstrip()
|
break
|
||||||
m2 = re.match(r'^\s+([^=]+)=(.*)$', line.rstrip())
|
if debug >= 2:
|
||||||
if not m2: break
|
print("service: %s" % line.rstrip())
|
||||||
|
m2 = re.match(r"^\s+([^=]+)=(.*)$", line.rstrip())
|
||||||
|
if not m2:
|
||||||
|
break
|
||||||
data[m2.group(1)] = m2.group(2)
|
data[m2.group(1)] = m2.group(2)
|
||||||
self.hosts[data['host_name']]['services'].append(data)
|
self.hosts[data["host_name"]]["services"].append(data)
|
||||||
if debug >= 2: print 'END OF SERVICE'
|
if debug >= 2:
|
||||||
|
print("END OF SERVICE")
|
||||||
|
|
||||||
|
|
||||||
def main():
|
def main():
|
||||||
status = NagiosStatus(nagios_status_file)
|
status = NagiosStatus(nagios_status_file)
|
||||||
output = ""
|
output = ""
|
||||||
for host in sorted(status.hosts.keys()):
|
for host in sorted(status.hosts.keys()):
|
||||||
host = status.hosts[host]
|
host = status.hosts[host]
|
||||||
if host.get('notifications_enabled', None) == None:
|
if host.get("notifications_enabled", None) == None:
|
||||||
output+= 'Host %s has no notifications_enabled line \n' % host['host_name']
|
output += "Host %s has no notifications_enabled line \n" % host["host_name"]
|
||||||
continue
|
continue
|
||||||
|
|
||||||
# are there any hard states that aren't 0 or 1?
|
# are there any hard states that aren't 0 or 1?
|
||||||
hard_states = [ x for x in
|
hard_states = [
|
||||||
[ int(x['last_hard_state']) for x in host['services'] ]
|
x
|
||||||
if not x in [0,1] ]
|
for x in [int(x["last_hard_state"]) for x in host["services"]]
|
||||||
|
if not x in [0, 1]
|
||||||
|
]
|
||||||
need_newline = False
|
need_newline = False
|
||||||
if host['notifications_enabled'] == '0' and not hard_states:
|
if host["notifications_enabled"] == "0" and not hard_states:
|
||||||
output += ('Host %s has notifications disabled and all services ok \n'
|
output += (
|
||||||
% host['host_name'])
|
"Host %s has notifications disabled and all services ok \n"
|
||||||
|
% host["host_name"]
|
||||||
|
)
|
||||||
need_newline = True
|
need_newline = True
|
||||||
|
|
||||||
for service in host['services']:
|
for service in host["services"]:
|
||||||
if debug: print '%s@%s' % ( service['check_command'], host['host_name'] )
|
if debug:
|
||||||
if debug: print ' notifications_enabled: %(notifications_enabled)s last_hard_state: %(last_hard_state)s' % service
|
print("%s@%s" % (service["check_command"], host["host_name"]))
|
||||||
if (int(service['notifications_enabled']) == 0
|
print(
|
||||||
and int(service['last_hard_state']) in [0,1]):
|
" notifications_enabled: %(notifications_enabled)s last_hard_state: %(last_hard_state)s"
|
||||||
output+= (('Service %(check_command)s@%(host_name)s\n'
|
% service
|
||||||
' has notifications disabled, but is ok\n') % 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
|
need_newline = True
|
||||||
|
|
||||||
if need_newline: output+="\n\n"
|
if need_newline:
|
||||||
|
output += "\n\n"
|
||||||
if output.strip() != '':
|
|
||||||
msg_body = "List of notifications off for recovered hosts/services: \n\n"+output
|
if output.strip() != "":
|
||||||
|
msg_body = (
|
||||||
|
"List of notifications off for recovered hosts/services: \n\n" + output
|
||||||
|
)
|
||||||
msg = MIMEText(msg_body)
|
msg = MIMEText(msg_body)
|
||||||
msg['Subject']="Notifications status on %s" % gethostname()
|
msg["Subject"] = "Notifications status on %s" % gethostname()
|
||||||
msg['From']=EMAIL_FROM
|
msg["From"] = EMAIL_FROM
|
||||||
msg['To']=EMAIL_TO
|
msg["To"] = EMAIL_TO
|
||||||
smtp_conn = SMTP()
|
smtp_conn = SMTP()
|
||||||
smtp_conn.connect('localhost')
|
smtp_conn.connect("localhost")
|
||||||
smtp_conn.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
|
smtp_conn.sendmail(EMAIL_FROM, EMAIL_TO, msg.as_string())
|
||||||
smtp_conn.quit()
|
smtp_conn.quit()
|
||||||
|
|
||||||
if __name__ == '__main__':
|
|
||||||
|
if __name__ == "__main__":
|
||||||
main()
|
main()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue