Little updated version of this
#!/usr/bin/env python2
# based on: https://steemit.com/nagios/@thomas-tiramisu/python-script-to-force-recheck-all-critical-services-in-nagios
import re
import time
STATUS_FILE="/home/nagios/local/var/status.dat"
CMD_FILE="/home/nagios/local/var/rw/nagios.cmd"
with open(STATUS_FILE) as file:
for line in file:
if "servicestatus {" in line:
for i in range(16):
myline=file.next().strip()
if re.match("host_name",myline):
host_name=myline.strip()
if re.match("service_description",myline):
service_description=myline.strip()
if re.match("current_state",myline):
current_state=myline.strip()
else:
current_state=""
if ("current_state=2" in current_state) or ("current_state=1" in current_state):
host_name = host_name.split("=",1)[1]
service_description = service_description.split("=",1)[1]
now=str(int(time.time()))
with open(CMD_FILE, "a") as cmd_file:
cmd = "["+now+"] SCHEDULE_FORCED_SVC_CHECK;"+host_name+";"+service_description+";"+now+"\n"
cmd_file.write(cmd)