nagios中在nagios告警时,有时候会禁用告警通知,监控恢复正常时禁用通知没有取消,导致下次监控不正常时没有告警通知;为防止这种情况,编写了恢复告警通知脚本,添加了恢复此类告警通知的例行任务
代码如下:
#!/usr/bin/env python #Desc: to change services‘notification status(0 or 1) if current state of service is ok while enable_notification is False import os import re import sys import time nagios_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‘) 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 True def 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:" 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)
之后在crontab中添加个例行任务就可以了
时间: 2024-10-12 16:21:25