In Nagios when the Nagios alarm, sometimes disable the alarm notification, monitoring restore normal When the disable notification is not canceled, resulting in the next monitoring is not normal when no alarm notification; To prevent this, a recovery alarm notification script was written and a routine task to restore such alert notifications was added.
The code is as follows:
#!/usr/bin/env python#desc: to change services ' Notification status (0 or 1) if current state of service is ok while enable_notification is falseimport osimport reimport sysimport timenagios_home= "/usr/local/nagios" Cmd_file=nagios_home+ "/var/rw/nagios.cmd" status_file=nagios_home+ "/var/status.dat" host_list=[]service_list=[] Def send_nagios_command (*args): "Send a simple command to our local Nagios server. ' global Cmd_file if len (args) < 2: return False arg = ' [%d] ' % int (Time.time ()) + '; Join (J.decode (' Utf-8 ') for j in args) arg = arg.encode (' Utf-8 ') &Nbsp; print (' sending command: %s ' % arg) try: fd = os.open (Cmd_file, os. o_wronly) os.write (fd, arg + ' \ n ') os.close (FD) except exception as e: print e return false return truedef init_data (datafile): "initial data read From statusfile and store in list while each object store in dict " hoststatus={} servicestatus={} for line in datafile: line=line.strip () if line.endswith (' {'): type=line.split (' ', 1) [0] elif ' host_name ' in line: key,val=line.split (' = ', 1) if type== ' Hoststatus ': hoststatus[key]=val if type== ' ServiceStatus ': servicestatus[key]=val elif ' Service_description ' in line: key,val= Line.split (' = ', 1) if type== ' Hoststatus ': hoststatus[key]=val if type== ' ServiceStatus ': servicestatus[key]=val elif ' Current_state ' in line: key,val=line.split (' = ', 1) if type== ' Hoststatus ' : hoststatus[key]=val if type== ' ServiceStatus ': servicestatus[key]=val elif ' notifications_enabled ' in line: key,val=line.split (' = ', 1) if type== ' Hoststatus ': hoststatus[key]=val if type== ' ServiceStatus ': servicestatus[key]=val elif line== '} ': if type== ' Hoststatus ': host_list.append (Hoststatus) hoststatus={} if type== ' ServiceStatus ': service_list.apPend (ServiceStatus) servicestatus={} ' Main code ' With open (status_file, ' R ') as f: init_data (f) " for each_hoststatus in host_list: print each_hoststatus for each_servicestatus in service_list: print each_servicestatus ' #print need to change host: "&Nbsp; for each_hoststatus in host_list: if each_hoststatus[' Current_state ']== ' 0 ' and each_hoststatus[' notifications_enabled ']== ' 0 ': print each_hoststatus send_nagios_command (' ENABLE_HOST_ Notifications ', each_hoststatus[' host_name ']) #print "need to change service: " for each_servicestatus in service_list: if each_servicestatus[' current_state ']== ' 0 ' and each_servicestatus[' notifications_enabled ']== ' 0 ': print each_servicestatus send_nagios_command ( ' Enable_svc_notifications ', each_servicestatus[' host_name '],each_servicestatus[' service_description ']) sys.exit ( 0)
Then add a routine to the crontab.
This article from "Month Child" blog, declined reprint!
Nagios Automatic Recovery Alarm Notification