Nagios+msn+fetion自定義時間發送報警消息

Nagios+fetion發送手機報警使用了幾個月.每次報警短信來都要看下手機.感覺麻煩.上網找了下.發現Nagios也可以跟MSN結合起來.如果是這樣.上班時間發送報警消息到MSN上.下班後再發送到手機上豈不是更好.

下面是我的安裝過程.寫得比較簡單:

首先下載MSN linux客戶端工具:

[root@yunwei tmp]# wget http://downloads.fanatic.net.nz/dev/php/sendMsg.zip

解壓.、移至web工作目錄.(發送報警消息的腳本需要通過web路徑訪問這個程序)

[root@yunwei tmp]# unzip sendMsg.zip

[root@yunwei tmp]# mv sendMsg /www/msn

[root@yunwei ~]# cd /www/msn/

[root@yunwei msn]# ls

index.php  msnpauth-1.1.3.php  msnpauth.php  sendMsg.php  simple.php  template.tpl

修改sendMsg.php

[root@yunwei msn]# head sendMsg.php

<?php

require_once('msnpauth.php');  //將msnpauth.php改爲msnpauth-1.1.3.php  msnpauth.php需要SSL支持.所以我們用msnpauth-1.1.3.php就好了

修改後通過web訪問如下:

wps_clip_image-16407

Sign-in name: //發送消息的MSN帳號

Password: //發送消息的MSN密碼

Recipient: //接收消息的MSN帳號

Message text: //消息內容

測試下能不能發送.能發送的話.說明程序沒問題.

wps_clip_image-29334

OK .程序沒問題了.現在跟Nagios結合起來:

在Nagios的commands.cfg中加入如下內容.

[root@yunwei msn]# cat /usr/local/nagios/etc/objects/commands.cfg | grep msn

# 'host-notify-by-msn' command definition

        command_name    host-notify-by-msn

        command_line   /usr/bin/printf "%b" "***** Nagios *****/n/nNotification Type: $NOTIFICATIONTYPE$/nHost: $HOSTNAME$/nState: $HOSTSTATE$/nAddress: $HOSTADDRESS$/nInfo: $HOSTOUTPUT$/n/nDate/Time: $LONGDATETIME$/n" > /tmp/msnhost.out | $USER1$/check_msn_host.sh

        command_name    service-notify-by-msn

        command_line   /usr/bin/printf "%b" "***** Nagios *****/n/nNotification Type: $NOTIFICATIONTYPE$/n/nService: $SERVICEDESC$/nHost: $HOSTALIAS$/nAddress: $HOSTADDRESS$/nState: $SERVICESTATE$/n/nDate/Time: $LONGDATETIME$/n/nAdditional Info:/n/n$SERVICEOUTPUT$/n" > /tmp/msnservice.out | $USER1$/check_msn_service.sh

在contacts.cfg加入如下內容:

[root@yunwei msn]# cat /usr/local/nagios/etc/objects/contacts.cfg  | grep -v "#"

define contact{

         contact_name                    msn

         alias                           Nagios_msn

         service_notification_period      24*7

         host_notification_period          24*7

         service_notification_options     w,u,c,r

         host_notification_options        d,r

         service_notification_commands    service-notify-by-msn

         host_notification_commands       host-notify-by-msn

        }

define contactgroup{

        contactgroup_name       admins

        alias                   Nagios Administrators

        members            nagiosadmin,msn    //添加msn contact.

        }

創建發送報警的腳本:

[root@yunwei msn]cat /usr/local/nagios/libexec/check_msn_service.sh

#!/bin/bash

[email protected]

PASSWD=test

[email protected]

[email protected]

MESSAGE=`cat /tmp/msnservice.out`

wget -O - -q --post-data="sender=$SENDER&password=$PASSWD&recipient=$TO1&message=$MESSAGE" http://localhost/msn/index.php

wget -O - -q --post-data="sender=$SENDER&password=$PASSWD&recipient=$TO2&message=$MESSAGE" http://localhost/msn/index.php

[root@yunwei msn]cat /usr/local/nagios/libexec/check_msn_host.sh

#!/bin/bash

[email protected]

PASSWD=test

[email protected]

[email protected]

MESSAGE=`cat /tmp/msnhost.out`

wget -O - -q --post-data="sender=$SENDER&password=$PASSWD&recipient=$TO1&message=$MESSAGE" http://localhost/msn/index.php

wget -O - -q --post-data="sender=$SENDER&password=$PASSWD&recipient=$TO2&message=$MESSAGE" http://localhost/msn/index.php

記得chmod +x ..  不然沒執行權限... 

原本想MSN給羣發送消息.這樣效率會快一些,但是實驗不成功...發送多人消息又不能以逗號隔開.只好多寫幾條命令.達到的效果是一樣的. 如果發送的消息是空的或者發送失敗.注意下./tmp/msnhost.out跟 /tmp/msnservice.out的權限.

現在Nagios+MSN報警已經完成了.

還有最後一步. 上班時間發MSN消息 .下班發送手機報警 .

修改Nagios的timeperiods.cfg

自己定義下時間.我這裏定義了兩個.workhours 跟free_times

define timeperiod{

        timeperiod_name workhours

        alias           Normal Work Hours

        monday        09:00-18:00

        tuesday         09:00-18:00

        wednesday      09:00-18:00

        thursday        09:00-18:00

        friday          09:00-18:00

        }

define timeperiod{

        timeperiod_name free_times

        alias           Normal Work Hours

        sunday         18:01-08:59

        monday        18:01-08:59

        tuesday         18:01-08:59

        wednesday      18:01-08:59

        thursday        18:01-08:59

        friday          18:01-08:59

        saturday        18:01-08:59

        }

修改飛信的報警時間參數(我們的飛信報警是設置在templates.cfg配置文件裏)

service_notification_period     24x7   // 改爲free_times             

host_notification_period       24x7   // 改爲free_times

修改MSN的報警時間參數(這個是在contacts.cfg ,我們剛剛新加的)

service_notification_period     24x7   // 改爲workhours             

host_notification_period       24x7   // 改爲workhours

這樣上班時間的報警就發送到MSN上.下班後才發送到手機.方便多了.

來張報警效果圖:

wps_clip_image-15791

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章