一、实际需求
在应用各种监控软件(比如:cacti、nagios、sitescope等)的时候,我一般都会用到它的email阀值报警功能。如果这时候再加上一个 msn在线监控机器人为你把关,第一时间给你发出msn报警信息,是不是能让你更快的处理问题呢。以下我为大家介绍一个msn command line 的小程序来实现这个功能。
软件下载地址:sendMsg
二、运行环境
一个支持php的系统环境就可以啦,当然要能上网,不然怎么发消息呢。
我的做法是和cacti监控服务器放一起,不需要额外的设备和资源投入。
注册一个msn的帐号用于监控机器人。比如:test@test.com 密码:123456
需要收到消息的msn帐号必须加监控帐号test@test.com为好友,不然收不到消息。
三、sendMsg用法
sendMsg.zip包中所有文件如下:
# ls -l sendMsg/
-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 = new sendMsg();
$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脚本:仅供参考,如果大家有更好请和我交流。
#!/usr/bin/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程度有限;
脚本如下:(这是我实例中使用的一个工作脚本,仅供大家借鉴)
#!/bin/sh
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点;
*/5 9-18 * * 1-5 /var/www/html/sendMsg/send_report.sh
实际应用大家可以根据自己的情况进行调整,我这里只是告诉大家怎么使用sendmsg,举了一个简单的实例帮助大家理解和应用。
如果懂php语言的可以把这个作为cacti的一个插件使用,那样效果就更棒啦~
一、软件环境(centos 5为例)
这里向大家推荐的是飞信机器人的软件,支持linux、命令行下的飞信、完全免费(目前为止);
官网地址:http://www.it-adv.net/
支持库安装:
需要Glibc2.4以上的版本;centos5默认安装的Glibc2.5,所不需要额外升级;
目前网上的安装文档只有ubuntu的,至于redhat、fedora、centos都没有相关安装文档,我在安装的过程中遇到很多库的问题,所以在这 里一并给大家进行介绍;
二、安装过程
下载包地址 LINUX X86/32(REDHAT ES4X32):支 持库 最 新程序fetion20080522004-linrh4.tar.gz
其中支持库和安装包内容如下:
# tar zxvf libraryrh4x32.tar.gz
libACE-5.6.5.so
libACE_SSL-5.6.5.so
libcrypto.so.0.9.7a
libssl.so.0.9.7a
# tar zxvf fetion20080522004-linrh4.tar.gz
./install/
./install/www/
./install/www/index.html
./install/www/css/
./install/www/css/scheme.css
./install/www/css/main.css
./install/www/css/print.css
./install/www/css/main-msie.css
./install/www/images/
./install/www/js/
./install/www/js/jquery.js
./install/socket_interface/
./install/socket_interface/socket_demo.php
./install/Readme.txt
./install/sound/
./install/sound/smile8.wav
./install/sound/message.wav
./install/sound/smile1.wav
./install/sound/login.wav
./install/sound/openchatwin.wav
./install/sound/closechatwin.wav
./install/fetion
./install/plugins/
./install/plugins/plugin_contact_update
./install/plugins/plugin_buddy_application
./install/plugins/plugin_message
./install/plugins/plugin_invite
./install/plugins/plugin_timer
./install/conf/
把支持库复制到/usr/lib/目录下,并作软链接如下:
cp lib*so* /usr/lib/
ln -s /usr/lib/libcrypto.so.0.9.7a /usr/lib/libcrypto.so.4
ln -s /usr/lib/libssl.so.0.9.7a /usr/lib/libssl.so.4
设定lib库配置文件
#vi /etc/ld.so.conf
#增加一条
/usr/lib/
#保存退出后,执 行
#ldconfig
fetion的目录结构:
.\Readme.txt: 本说明文件
.\fetion:主程序
.\sound\*.wav:各种音效文件,您可以用自己喜欢的文件替换
.\plugins\plugin_*: 插件程序,您可以修改插件以达到自己的各性化需求
我在安装中遇到缺少krb5的库问题,这里写一下解决方法:
错误如下:
./fetion: error while loading shared libraries: libgssapi_krb5.so.2: cannot open shared object file: No such file or directory
安装一下krb5-libs包就可以解决啦:
完成之后测试,安装是否成功;执行
#cd install
# ./fetion -h
Usage:
fetion -h
-h: help
fetion -u mobile -p pwd [-b batchfile] [-EN] [-d]
fetion -u mobile -p pwd [-b batchfile] [-EN] [-d]
-u: Fetion user account(only supports mobile phone No.)
-p: Account password
-b: Batch file name
-d: Debug on and write logs to [mobile]-debug.log
-EN: English
三、实际测试
登录飞信默认是中文utf8的,如果linux系统不支持utf8会出现乱码:我使用english的方式来登录啦。
测试帐号:13713718888
密码:123456 (如果密码中含有特殊字符,请使用单引号)
./fetion -u 13713718888 -p 123456 -EN
登录后输入?获得帮助
****************************************************************
指 令 指令说明
list 好友列表: list
sms 发送短信: sms 飞信号/手机号/编号 短信内容(换行请用\n代替)
chat 在线消息: chat 飞信号/手机号/编号 短信内容(换行请用\n代替)
status 在线状态: status online/busy/away/hidden [个性化信息]
add 添加好友: add 飞信号/手机号 申请信息 本地呢称
del 删除好友: del 飞信号/手机号/编号
invite 邀请好友: invite 手机号 您的姓名(邀请好友开通飞信)
autoaccept 自动接受: autoaccept on/off/status 自动接受加为好友申请
myinfo 修改资料: myinfo nickname/impresa 内容(呢称/心情)
help 帮助菜单: help/?
exit 退出系统: quit/exit
****************************************************************
sms 137******** "this is test message" # 这个就是我们需要的发送短信的指令啦!
脚本测试:
可以使用fetion的-b参数来实现p处理;
举例如下:
# vi p.sh
sms 13712312311 “msyqlserver is down”
sms 13712312312 “db1 is not running”
sms 13712312313 “warning db2 disk3 is full”
保存后执行:
./fetion -u 13713718888 -p 123456 -b p.sh
就是一次给3个admin发出报警信息。
还有许多插件的功能,我会在下次实际应用中写出日志和大家一起研究;
在初步接触中,发现可以实现一些交互信息的查询,比如直接向短信机器人发指令得到某台服务器的运行状态等。
前几天给 Nova 的缓存系统添加了 Memcache 支持。但是这玩意儿对个人博客系统来说可能不太合适(哪天 XKLog 推出多用户版本了可能会派上用场),事实上,就我自己试用而言,单机情况下使用 Memcache 反而可能使性能下降(使用 SHMOP 时运行时间在 20 微秒左右,使用 Memcache 时运行时间在 40 微秒左右)。于是又准备鼓捣一下如上三个 PHP 加速器。
一、PHP加速器介绍
PHP加速器是一个为了提高PHP执行效率,从而缓存起PHP的操作码,这样PHP后面执行就不用解析转换了,可以直接调用PHP操作码,这样速度上就提 高了不少。
Apache中使用mod_php的请求、响应执行流程:
1、Apache接收请求。
2、Apache传递请求给mod_php。
3、mod_php定位磁盘文件,并加载到内存中。
4、 mod_php编译源代码成为opcode树。
5、mod_php执行opcode树。
PHP加速器相应的就是第四步,它的目的就是防止PHP每次请求都重复编译PHP代码,因为在高访问量的网站上,大量的编译往往没有执行速度快呢?所以这 里面有个瓶颈就是PHP的重复编译既影响了速度又加载了服务器负载,为了解决此问题,PHP加速器就这样诞生了。
二、PHP加速器安装与配置
1、安装配置APC
APC全称是Alternative PHP Cache,官方翻译叫”可选PHP缓存”,它是PHP PECL中的一个扩展,好像是facebook在使用它,下面开始安装(ubuntu环境):
$wget http://pecl.php.net/get/APC-3.0.19.tgz
$tar xvzf APC-3.0.19.tgz
$cd APC-3.0.19/APC-3.0.19
$/usr/local/php/bin/phpize
$./configure –enable-apc –enable-apc-mmap –with-php-config=/usr/local/php/bin/php-config
$make
$sudo make install
下面我们再配置APC,因为我的PECL扩展路径改变了,所以我得移动下编译好的文件:
$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/apc.so /usr/local/php/lib/php/extensions/PECL
然后我们再编辑php.ini文件进行配置,请把下面的代码加入到php.ini中即 可:
extension_dir = “/usr/local/php/lib/php/extensions/PECL”
extension = apc.so
; APC
apc.enabled = 1
apc.shm_segments = 1
apc.shm_size = 64
apc.optimization = 1
apc.num_files_hint = 0
apc.ttl = 0
apc.gc_ttl = 3600
apc.cache_by_default = on
这样重启apache就会在phpinfo()信息中显示。
2、安装配置eAccelerator
eAccelerator的前身其实是truck-mmcache,因为开发truk-mmcache的人被Zend给招安了,所以开发 eAccelerator的人继承了truk-mmcache的一些特性,设计出eAccelerator加速器。安装如下:
$wget http://jaist.dl.sourceforge.net/sourceforge/eaccelerator/eaccelerator-0.9.5.tar.bz2
$tar -jxf eaccelerator-0.9.5.tar.bz2
$cd eaccelerator-0.9.5
$/usr/local/php/bin/phpize
$./configure –enable-eaccelerator=shared –with-php-config=/usr/local/php/bin/php-config
$make
$sudo make install
$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/eaccelerator.so /usr/local/php/lib/php/extensions/PECL
将下面代码加入php.ini文件中
extension = eaccelerator.so
; eAccelerator
eaccelerator.shm_size = “16″
eaccelerator.cache_dir = “/tmp/eaccelerator”
eaccelerator.enable = “1″
eaccelerator.optimizer = “1″
eaccelerator.check_mtime = “1″
eaccelerator.debug = “0″
eaccelerator.filter = “”
eaccelerator.shm_max = “0″
eaccelerator.shm_ttl = “0″
eaccelerator.prune_period = “0″
eaccelerator.shm_only = “0″
eaccelerator.compress = “1″
eaccelerator.compress_level = “9″
创建缓存目录,重启apache
$sudo mkdir /tmp/eaccelerator
$sudo chmod 777 /tmp/eaccelerator
$sudo /usr/local/apache/apachectl restart
在phpinfo()检查是否安装成功.
3、安装配置XCache
XCache作为国人自己开发的东西,做小菜鸟的我也感到骄傲,而且XCache无论在速度还是性能上都做的不错。下面就赶紧让我们品尝它吧!
$wget http://xcache.lighttpd.net/pub/Releases/1.2.2/xcache-1.2.2.tar.gz
$tar xvzf xcache-1.2.2.tar.gz
$cd xcache-1.2.2
$/usr/local/php/bin/phpize
$./configure –enable-xcache –enable-xcache-coverager –with-php-config=/usr/local/php/php-config
$make
$sudo make install
$sudo mv /usr/local/php/lib/php/extensions/no-debug-non-zts-20060613/xcache.so /usr/local/php/lib/php/extensions/PECL
在php.ini添加配置信息:
extension = xcache.so
; xcache
xcache.admin.user = “admin”
xcache.admin.pass = “(执行) echo ’(你的密码)’|md5sum(得出的密文)”
;
xcache.size = 24M
xcache.shm_scheme = “mmap”
xcache.count = 2
xcache.slots = 8k
xcache.ttl = 0
xcache.gc_interval = 0
xcache.var_size = 8M
xcache.var_count = 1
xcache.var_slots = 8k
xcache.var_ttl = 0
xcache.var_maxttl = 0
xcache.var_gc_interval = 300
xcache.test = Off
xcache.readonly_protection = On
xcache.mmap_path = “/tmp/xcache”
xcache.coredump_directory = “”
xcache.cacher = On
xcache.stat = On
xcache.optimizer = Off
;
xcache.coverager = On
xcache.coveragedump_directory = “”
创建缓存目录,重启apache
$sudo mkdir /tmp/xcache
$sudo chmod 777 /tmp/xcache
$sudo /usr/local/apache/bin/apachectl restart
去查看phpinfo()信息吧!
三、PHP加速器测试
1、测试环境
硬件: AMD Athlon 64 X2 Dual Core Processor 4400+ @ 2.2GHz CPU, 2GB 内存. 160GB SATA 硬盘
软件: Linux Ubuntu server Gutsy 7.10, Apache 2.2.4, MySQL 5.0.45 和 PHP 5.2.3
测试指令: ab -c5 -n3000 http://example.com/ (我们使用的是Apache Benchmark (ab) 工具,并发连接为5,3000次请求)
2、测试结果
无任何加速器:
Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 288.255212 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 10.41 [#/sec] (mean)
Time per request: 480.425 [ms] (mean)
Time per request: 96.085 [ms] (mean, across all concurrent requests)
Transfer rate: 226.23 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.5 0 19
Processing: 181 479 186.0 444 1822
Waiting: 166 461 184.7 427 1708
Total: 181 479 186.0 444 1822
Percentage of the requests served within a certain time (ms)
50% 444
66% 525
75% 577
80% 619
90% 732
95% 819
98% 946
99% 1012
100% 1822 (longest request)
APC加速器:
Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 98.530068 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 30.45 [#/sec] (mean)
Time per request: 164.217 [ms] (mean)
Time per request: 32.843 [ms] (mean, across all concurrent requests)
Transfer rate: 661.84 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 58 163 71.2 155 2452
Waiting: 53 158 69.6 150 2329
Total: 58 163 71.2 155 2452
Percentage of the requests served within a certain time (ms)
50% 155
66% 178
75% 193
80% 204
90% 235
95% 258
98% 285
99% 302
100% 2452 (longest request)
eAccelerator加速器:
Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 95.983986 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 31.26 [#/sec] (mean)
Time per request: 159.973 [ms] (mean)
Time per request: 31.995 [ms] (mean, across all concurrent requests)
Transfer rate: 679.39 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.1 0 3
Processing: 57 159 91.3 148 3830
Waiting: 50 152 89.8 142 3704
Total: 57 159 91.3 148 3830
Percentage of the requests served within a certain time (ms)
50% 148
66% 174
75% 193
80% 205
90% 239
95% 263
98% 289
99% 309
100% 3830 (longest request)
XCache加速器:
Document Path: /
Document Length: 21757 bytes
Concurrency Level: 5
Time taken for tests: 99.76300 seconds
Complete requests: 3000
Failed requests: 0
Write errors: 0
Total transferred: 66777000 bytes
HTML transferred: 65271000 bytes
Requests per second: 30.28 [#/sec] (mean)
Time per request: 165.127 [ms] (mean)
Time per request: 33.025 [ms] (mean, across all concurrent requests)
Transfer rate: 658.19 [Kbytes/sec] received
Connection Times (ms)
min mean[+/-sd] median max
Connect: 0 0 0.0 0 2
Processing: 59 164 83.4 155 3367
Waiting: 52 156 66.4 148 1802
Total: 59 164 83.4 155 3367
Percentage of the requests served within a certain time (ms)
50% 155
66% 178
75% 196
80% 206
90% 237
95% 263
98% 287
99% 305
100% 3367 (longest request)
3、结果摘要
| | 请求时间(秒) | 单次请求时间(毫秒) | 最大内存占用(MB) | 最小内存占用(MB) |
| None | 10.41 | 96.08 | 24 | 24 |
| APC | 30.45 | 32.84 | 21 | 21 |
| eAccelerator | 31.26 | 31.99 | 23 | 18 |
| XCache | 30.28 | 33.02 | 29 | 19 |
四、PHP加速器比较结果总结
1、通过测试得出eAccelerator在请求时间和内存占用综合方面是最好的。
2、通过测试得出使用加速器比无加速器在请求时间快了3倍左右。
3、通过各个官方观察,XCache是更新最快的,这也说明最有发展的。
以上是总结结果,你也许会问我到底用那个加速器好呢?我只能告诉你,首先,用一定比不用好,其次每个加速器还有一些可以调优的参数,所以要根据你的系统环 境而定,然后,我个人觉得你可以详细研究下eAccelerator和XCache,这两款潜力还是很大的,最后我从比较专业的测试网站搞了一张结果图: