转自http://blog.csdn.net/deccmtd/article/details/6063467

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访问如下:

Nagios+msn+fetion自定义时间发送报警消息-LMLPHP

Sign-in name: //发送消息的MSN帐号

Password: //发送消息的MSN密码

Recipient: //接收消息的MSN帐号

Message text: //消息内容

测试下能不能发送.能发送的话.说明程序没问题.

Nagios+msn+fetion自定义时间发送报警消息-LMLPHP

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上.下班后才发送到手机.方便多了.

来张报警效果图:

Nagios+msn+fetion自定义时间发送报警消息-LMLPHP

05-11 16:59