蚊子最近一直在弄公司的nagios,最近进行的监控报警这部分了,主要锁定在了msn和短信报警上,下面我把我这边实现的方法整理出来,有需要的朋友可以参考。
1,msn报警
先在这里http://code.google.com/p/phpmsnclass/downloads/list下载最新的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
<?php include_once(‘msn.class.php’); $msn_username =’YOUR_MSN_ID’; //消息发送人的msn帐号 $msn_password = "yourpassword"; //消息发送人msn密码 $msn_list = array(‘[email protected]’,’[email protected]’); //消息接收人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文件中放入些内容比如
# cat 1 this is a test by wenzizone.cn |
然后执行
可以看到下图

说明msn发送是正常的。
2,飞信报警
首先到这里http://code.google.com/p/phpfetionapi/downloads/list下载飞信的php写的api接口,解压后,同样在/usr/local/nagios/libexec创建phpfetion目录,将class_fetion.php放到此目录下,然后创建发送短信的php程序,如下
#!/usr/local/php529/bin/php -q
<?php 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); ?>
|
分配sendsms.php可执行权限,然后进行测试,执行
./sendsms.php 13810xxxxxx //后面这个是接受消息的手机号 |
用不了多一会就会收到消息,消息内容和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$ } |
然后修改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 24×7 host_notifications_enabled 1 service_notifications_enabled 1 service_notification_period 24×7 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 24×7 host_notifications_enabled 1 service_notifications_enabled 1 service_notification_period 24×7 service_notification_options w,u,r service_notification_commands notify-service-by-fetion } |
接下来再host或者service上填上对应的contact然后就可以随时收到相应的报警了。
蚊子在这两天的测试用发现,飞信报警的送达率还是很高的,基本是每条都能收到,但是msn的送达率就令人担忧了,我发送10个能收到一个就算不错了,所以还请根据自己测试情况酌情考虑。
另外一点需要注意的是msn或者飞信发送报警,接收人都必须是发送人msn或飞信的好友才行,不然是不能送达的。
蚊子完成本篇文章参考了如下两篇文章
1.http://blog.s135.com/post/390/,主要参考了msn的php脚本
2.http://www.ritto.cn/?p=308,受到博主使用class.fetion.php的启发,去google上找到飞信的php-api