码迷,mamicode.com
首页 > 移动开发 > 详细

nagios 自动恢复告警通知

时间:2017-01-25 23:28:06      阅读:502      评论:0      收藏:0      [点我收藏+]

标签:nagios 自动化 告警恢复 脚本

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中添加个例行任务就可以了


本文出自 “月童” 博客,谢绝转载!

nagios 自动恢复告警通知

标签:nagios 自动化 告警恢复 脚本

原文地址:http://littlefive.blog.51cto.com/6440205/1894162

(0)
(0)
   
举报
评论 一句话评论(0
0条  
登录后才能评论!
© 2014 mamicode.com 版权所有 京ICP备13008772号-2
迷上了代码!