Nagios通过MSN/飞信发送报警通知
1、msn报警
先在这里网上下载***的msn类文件,这个下载后其实是一套完整的msnbot的php程序,我们这里只是使用了其中的msn.class.php这个文件。
遵循nagios的标准结构,程序文件都会放在/usr/local/nagios/libexec文件夹下,所以我在此文件夹下创建phpmsnclass文件夹,将msn.class.php文件放置其中,并在此目录下创建用于发送msn报警的php程序,内容如下。
# cat sendmsnmsg.php
#!/usr/local/php529/bin/php -q
include_once('msn.class.php');
$msn_username ='YOUR_MSN_ID'; //消息发送人的msn帐号
$msn_password = "yourpassword"; //消息发送人msn密码
$msn_list = array('somebody1@hotmail.com','somebody2@hotmail.com'); //消息接收人msn地址,这里可选,因为会使用nagios传递过来的地址
$fp=fopen('/tmp/tmp/1','r'); //1这个文件是消息发送的内容,这个文件由nagios自动生成,路径根据自己实际自行修改
$file="";
while (! feof($fp)){
$cont=fgets($fp);
$file=$file.$cont; //拼接报警消息内容
}
$msn =new MSN('MSNP15'); //采用MSNP15协议,支持离线消息
if (!$msn->connect($msn_username,$msn_password)) {
echo "Error for connect to MSN networkn";
echo "$msn->errorn";
exit;
}
else {
$msn->sendMessage("$file",$argv[1]); //将$file内容发送给$argv[1]传递进来的msn消息接收者。
}
fclose($fp);
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
将此文件设置可执行权限,就可以进行测试了,首先在1文件中放入些内容比如
# cat 1
this is a test by wenzizone.cn
- 1.
- 2.
- 3.
- 4.
然后执行
./sendmsnmsg.php somebody@hotmail.com //后面跟的这个是接收人msn地址
- 1.
- 2.
可以看到下图:

说明msn发送是正常的。
2、飞信报警
首先到网上下载飞信的php写的api接口,解压后,同样在/usr/local/nagios/libexec创建phpfetion目录,将class_fetion.php放到此目录下,然后创建发送短信的php程序,如下
#!/usr/local/php529/bin/php -q
include_once('class_fetion.php');
$sms_username = "1381126xxxx"; //飞信帐号
$sms_password = "your password"; //飞信密码
$fp=fopen('/tmp/tmp/1','r'); //同msn内容
$file="";
while (! feof($fp)){
$cont=fgets($fp);
$file=$file.$cont;
}
$fetion = new fetion($sms_username,$sms_password);
$fetion->init() or die("fetion init failure!n");
$fetion->sent_sms('tel:'.$argv[1],$file); //根据传送进来的电话发送报警内容
fclose($fp);
?>
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
分配sendsms.php可执行权限,然后进行测试,执行
./sendsms.php 13810xxxxxx //后面这个是接受消息的手机号
- 1.
- 2.
用不了多一会就会收到消息,消息内容和msn那条一样,因为我们的测试内容不变。
3、和nagios的结合使用
首先编辑command.cfg文件,添加新的如下内容
# 'notify-host-by-msn' command definition
define command{
command_name notify-host-by-msn
command_line /usr/bin/printf "%b" "***** Nagios *****nNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOSTSTATE$nIP: $HOSTADDRESS$nInfo: HOSTOUTPUT$nDate/Time: $LONGDATETIME$n" >/tmp/tmp/1 | $USER$/phpmsnclass/sendmsnmsg.php $CONTACTEMAIL$
}
# 'notify-host-by-fetion' command definition
define command{
command_name notify-host-by-fetion
command_line /usr/bin/printf "%b" "***** Nagios *****nNotification Type: $NOTIFICATIONTYPE$nHost: $HOSTNAME$nState: $HOST
STATE$nIP: $HOSTADDRESS$nInfo: $HOSTOUTPUT$nDate/Time: $LONGDATETIME$n" >/tmp/tmp/1 | $USER1$/phpfetion/sendsms.php $CONTACTPAGER$
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
然后修改contacts.cfg文件,如下
define contact{
contact_name nagiosadmin
alias Nagios Admin
email http://www.wenzizone.cn/
pager 13810xxxxxx
host_notification_commands notify-host-by-msn
host_notification_options d,u
host_notification_period 24x7
host_notifications_enabled 1
service_notifications_enabled 1
service_notification_period 24x7
service_notification_options w,u,r
service_notification_commands notify-service-by-msn
}
define contact{
contact_name nagiosadmin01
alias Nagios Admin
email http://www.wenzizone.cn/
pager 13810xxxxxx
host_notification_command snotify-host-by-fetion
host_notification_options d,u
host_notification_period 24x7
host_notifications_enabled 1
service_notifications_enabled 1
service_notification_period 24x7
service_notification_options w,u,r
service_notification_commands notify-service-by-fetion
}
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
- 16.
- 17.
- 18.
- 19.
- 20.
- 21.
- 22.
- 23.
- 24.
- 25.
- 26.
- 27.
- 28.
- 29.
- 30.
- 31.
- 32.
- 33.
- 34.
- 35.
- 36.
- 37.
- 38.
- 39.
- 40.
- 41.
- 42.
- 43.
- 44.
- 45.
- 46.
- 47.
- 48.
- 49.
- 50.
- 51.
- 52.
- 53.
- 54.
- 55.
- 56.
接下来再host或者service上填上对应的contact然后就可以随时收到相应的报警了。
蚊子在这两天的测试用发现,飞信报警的送达率还是很高的,基本是每条都能收到,但是msn的送达率就令人担忧了,我发送10个能收到一个就算不错了,所以还请根据自己测试情况酌情考虑。
另外一点需要注意的是msn或者飞信发送报警,接收人都必须是发送人msn或飞信的好友才行,不然是不能送达的。以上就是Nagios通过MSN/飞信发送报警通知。
【编辑推荐】