系统监控:msn在线机器人实时报警
一、实际需求
在应用各种监控软件(比如:cacti、nagios、sitescope等)的时候,我一般都会用到它的email阀值报警功能。如果这时候再加上一个msn在线监控机器人为你把关,第一时间给你发出msn报警信息,是不是能让你更快的处理问题呢。以下我为大家介绍一个msn command line 的小程序来实现这个功能。
软件下载地址:sendMsg
二、运行环境
一个支持php的系统环境就可以啦,当然要能上网,不然怎么发消息呢。
我的做法是和cacti监控服务器放一起,不需要额外的设备和资源投入。
注册一个msn的帐号用于监控机器人。比如:test@test.com 密码:123456
需要收到消息的msn帐号必须加监控帐号test@test.com为好友,不然收不到消息。
三、sendMsg用法
sendMsg.zip包中所有文件如下:
-rw-r--r-- 1 root root 1213 Jul 29 2007 index.php //测试页面,web中打开开始测试;很容易做。
-rw-r--r-- 1 root root 3894 Jul 29 2007 msnpauth-1.1.3.php
-rw-r--r-- 1 root root 3372 Jul 29 2007 msnpauth.php
-rw-r--r-- 1 root root 4586 Jul 29 2007 sendMsg.php
-rw-r--r-- 1 root root 223 Jul 29 2007 simple.php
-rw-r--r-- 1 root root 1424 Jul 29 2007 template.tpl
该程序也是通过登录msn服务器、建立IM会话,发送消息;
基本PHP语法如下:
$sendMsg->login('test@test.com', '123456');
//刚才建立的用于举例的msn监控机器人帐号
$sendMsg->createSession('recipient@hotmail.com');
//接受信息人的msn帐号
$sendMsg->sendMessage('message', 'Times New Roman', 'FF0000');
//第一个是具体信息内容,后面可以设定字体和颜色;
$sendMsg->sendMessage(iconv("GBK", "UTF-8", 测试), 'Times New Roman', '008000');
//也利用iconv转换gbk到utf8来发送中文信息;
四、实际应用
这里是我自己写的一个应用发送msn信息的php脚本:仅供参考,如果大家有更好请和我交流。
<?
if ($argc != 3) {
die("Usage: send_cndmonitor.php <msn-address> <messages>\n");
}
array_shift($argv);
$msnaddr = $argv[0];
$messages = $argv[1];
include('sendMsg.php');
$sendMsg = new sendMsg();
$sendMsg->login('test@test.com', '123456');
$sendMsg->createSession($msnaddr);
$sendMsg->sendMessage($messages, 'Times New Roman', '008000');
?>
主要是为了能被其他脚本调用,用于发送一个报警信息。缺点是不能判定错误,所以实际运用中存在故障,需要网络流畅的环境下使用。
我们的生产环境已经存在大量的监控系统,所以针对错误信息已经整理到数据库中,因此我只需要从数据库导出目前存在error信息的文本文件,然后根据节点位置发送给相关维护负责人即可。
为了能判定和确保发送正确,我利用sendMsg中的index.php的页面和shell脚本相结合来循环发送,实在抱歉本人PHP程度有限;
脚本如下:(这是我实例中使用的一个工作脚本,仅供大家借鉴)
wget --user=monitor --password=123456 http://127.0.0.1/monitor/msn.txt -O /var/www/html/sendMsg/msn.txt.1 >/dev/null 2>&1
#下载msn要发送的信息,因为页面都是认证的所以用了wget的user和password;
now=`date +%Y-%m-%d-%H:%M`
[ -f /var/www/html/sendMsg/msn.txt ] && oldmd5=`md5sum var/www/html/sendMsg/msn.txt |awk '{print $1}' |tee /var/log/cdn_status_old.md5` || exit 0
[ -f /var/www/html/sendMsg/msn.txt.1 ] && newmd5=`md5sum var/www/html/sendMsg/msn.txt.1 |awk '{print $1}' |tee /var/log/cdn_status_new.md5` || exit 0
SA=(admin1 admin2 admin3 admin4)
# 相关负责人列表和下载的msn信息的中的名字对应;
msnaddr=(admin1@msn.com admin2@msn.com admin3@msn.com admin4@msn.com)
# 相关负责人的msn帐号和SA变量中的的名字顺序一一对应;
sendMsg()
{
num=0
while [ $num -lt 1 ];
do
wget --post-data "sender=test@test.com&password=123456&recipient=${1}&message=${2}" http://127.0.0.1/sendMsg/index.php -O /var/www/html/sendMsg/index.php.1 >/dev/null 2>&1
# 使用wget post-data发送post参数给index.php页面,用以发送msn信息。
if [ -f /var/www/html/sendMsg/index.php.1 ]; then
if cat /var/www/html/sendMsg/index.php.1 |grep -i successfully >/dev/null 2>&1;then
num=1 #判断信息发送成功
elif cat /var/www/html/sendMsg/index.php.1 |grep -i "The user appears to be offline" >/dev/null 2>&1;then
num=1 #判断msn接受人为是否在线状态
echo "The user is offline."
exit 0
elif cat /var/www/html/sendMsg/index.php.1 |grep -i "Something went wrong trying to connect to the server" >/dev/null 2>&1;then
num=1 #判断msn 服务器存在连接问题
echo "MSN server is wrong."
exit 0
else
num=0 #除了以上三种情况退出循环外,其他情况重试。
fi
rm -f /var/www/html/sendMsg/index.php.1
else
num=0
fi
done
}
if [[ $oldmd5 == $newmd5 ]];then #校验msn的信息是否是已经发送过的,主要是为了不重复发送错误信息。
rm -f /var/www/html/sendMsg/msn.txt.1
exit 0
else
mv /var/www/html/sendMsg/msn.txt /var/www/html/sendMsg/bak/msn$now.txt -f
mv /var/www/html/sendMsg/msn.txt.1 /var/www/html/sendMsg/msn.txt -f
#备份已发送的msn错误信息
fi
for i in `seq 0 1 3` #根据维护人员的数量进行判断和循环
do
if cat /var/www/html/sendMsg/msn.txt |grep -i ${SA[$i]}; then
messages=`cat /var/www/html/sendMsg/msn.txt |grep -i ${SA[$i]}`
sendMsg "${msnaddr[$i]}" "$messages"
else
continue
fi
done
加入到crontab每5分钟执行一次;考虑维护人员的上线时间(最好是24H值班msn,那就不存在这个问题)设定执行时间为每周1到5的9点到18点;
实际应用大家可以根据自己的情况进行调整,我这里只是告诉大家怎么使用sendmsg,举了一个简单的实例帮助大家理解和应用。
如果懂php语言的可以把这个作为cacti的一个插件使用,那样效果就更棒啦~

七月 24th, 2008 at 8:44 上午
http://user.qzone.qq.com/56802890/blog/1216772013
我写了一个专门在nagios下发MSN报警的文章,跟博主类似!
有空多向你请教啦!
七月 24th, 2008 at 8:49 上午
这个sendmsg使用MSN9协议,不支持发离线报警消息!我对照msnp的修改成MSN15版本协议不成功!
七月 24th, 2008 at 11:03 上午
对的,这样很不方便,所以我们使用的时候一般都会再在24H值班工程师的msn上。
以后还需要改进,我也在看其他的msn发送工具。
希望有相关技术,也请告诉我们。
七月 28th, 2008 at 12:19 下午
博主请教一个问题,我在使用sendmsg发送消息时报错:
[root@TS263 msn]# wget -qO- http://10.167.26.3/msn/index.php?SendMess=“$message”
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://msnialogin.passport.com:443 (Unable to find the socket transport "ssl" – did you forget to enable it when you configured PHP?) in /usr/local/apache/htdocs/msn/msnpauth.php on line 67
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 82
Warning: feof(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 91
Warning: feof(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 91
我是在apache下使用sendmsg的,请问这是什么原因引起的呢?
七月 28th, 2008 at 3:23 下午
一般你要是想做到24小时 最好是发手机短信 弄个短信猫 就可以做到了
不过最好别这样 要是真的半夜给你发短信 我的妈啊 受不了
七月 28th, 2008 at 3:48 下午
其实我一直有个想法,而且是免费的,就是电信的飞信软件,添加为好友后,就可以使用啦。不用添加自己的短信发送设备啦~
不过要用到.net来开发短信接口,这玩样不开源。
还有一个就是邮件+短信的方式;这方面的应用就比较多啦。
七月 28th, 2008 at 6:04 下午
飞信9月份开发好了计费模块
免费的日子不长了
七月 28th, 2008 at 6:48 下午
楼上的楼上的楼上的楼上,你大概没有编译安装php_openssl模块,如果是手工编译的话,需要重新编译php openssl支持,如果是用rpm包的话,查看一下有没有类似php_openssl.so 的模块
七月 28th, 2008 at 7:01 下午
To myfreeke:
这个问题我也遇到过,先确认是否php编译的有问题;
然后测试网络发送是否有问题,主要是测试连接一下msn的服务器;msnialogin.passport.com,loginnet.passport.com;
还有就是一些单双引号的引用也要注意;(我遇到过尤其是单引号的应用)
你的index.php文件就是sendMsg里面的吗?
我用了你的方法测试通过。
多测试几次或者使用wget –post-data再试试;
七月 29th, 2008 at 11:10 上午
我已经重新编译了SSL啦,感谢
七月 29th, 2008 at 11:13 上午
其实飞信我已经安装好了,就还差一个发送的脚本不会写。
七月 29th, 2008 at 7:40 下午
你装了那个飞信,是不是我今天写的blog里面的
http://www.hiadmin.com/%E7%B3%BB%E7%BB%9F%E7%9B%91%E6%8E%A7%EF%BC%9Alinux%E5%91%BD%E4%BB%A4%E8%A1%8C-%E9%A3%9E%E4%BF%A1%E5%AE%A2%E6%88%B7%E7%AB%AF%E5%8F%91%E9%80%81%E5%85%8D%E8%B4%B9%E6%8A%A5%E8%AD%A6%E7%9F%AD%E4%BF%A1/
这个脚本我最近正好要写一下~
七月 31st, 2008 at 1:43 下午
就是这个版本啦
八月 3rd, 2008 at 9:48 上午
博主请教一个问题,我在使用sendmsg发送消息时报错:
[root@TS263 msn]# wget -qO- http://10.167.26.3/msn/index.php?SendMess=“$message”
Warning: fsockopen() [function.fsockopen]: unable to connect to ssl://msnialogin.passport.com:443 (Unable to find the socket transport “ssl” – did you forget to enable it when you configured PHP?) in /usr/local/apache/htdocs/msn/msnpauth.php on line 67
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 79
Warning: fputs(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 82
Warning: feof(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 91
Warning: feof(): supplied argument is not a valid stream resource in /usr/local/apache/htdocs/msn/msnpauth.php on line 91
================
我的php编译是这样的
./configure –prefix=/usr/local/php –with-mysql=/usr/local/mysql/ –with-apxs=/usr/local/apache2/bin/apxs –with-openssl-dir=/usr/local/openssl –with-expat-dir=/usr/lib –enable-xslt –with-gd=/usr/local/gd2/ –with-jpeg-dir=/usr/local/jpeg6/ –with-zlib-dir=/usr/local/zlib/ –with-png-dir=/usr/local/libpng2/ –with-freetype-dir=/usr/local/freetype2/ –with-mime-magic –with-png –with-xml
我已经加了openssl了,而且在phpinfo里能看到了:
Apache/2.2.4 (Unix) mod_ssl/2.2.4 OpenSSL/0.9.8g DAV/2 PHP/5.2.3
请问,为什么,还报上面的错误呢?请帮助。。。
八月 4th, 2008 at 4:11 下午
请问myfreeke兄,是如何编译的?能否指教下,我的问题和你的一样,可我安装了openssl,在apache与php时,都添加了ssl这个功能….为什么,和你出现的一样错误呢….郁闷啊!!!
八月 4th, 2008 at 5:58 下午
wget –user=monitor –password=123456 http://127.0.0.1/monitor/msn.txt -O /var/www/html/sendMsg/msn.txt.1 >/dev/null 2>&1
这是什么意思呀?
八月 4th, 2008 at 6:02 下午
msn.txt 是不是就可以换成别的日志文件来读取??
message.log ??
八月 4th, 2008 at 6:26 下午
wget是从web server上下载监控报告,msn.txt是我们每5分钟会更新的所有服务器错误信息的日志文件,其格式是供sendMsg发送给系统管理的报警信息; 这里做了-O主要是为了和原来已经存在的文件做比较,免得一直重复发送同样的错误,报警信息;
当然文件名是可以修改的;
如果你要来发送message.log的话,需要加上分析日志的工具或者脚本;
十月 4th, 2008 at 2:03 上午
呵呵,留个联系方式,我好请教下嘛
十月 14th, 2008 at 11:56 上午
不知道kittyboy 兄弟的php加ssl功能搞定没?去我的博客看看,总结在那里了
五月 1st, 2009 at 10:12 上午
1hlvhkbn71za6q5w
系统监控