nagios 自動回復警示通知

來源:互聯網
上載者:User

標籤: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 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‘)    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:"        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 自動回復警示通知

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.