分类: LINUX
2009-12-29 11:13:21
Nagios使用飞信短信报警
1.下载安装飞信机器人
下载地址:
注意:ReaHat和CentOS用户应使用LINUX X86/32(REDHAT ES4X32)版。
其中支持库和安装包内容如下:
支持库:libraryrh4×32.tar.gz
最新飞信机器人为fetion20080522004-linrh4.tar.gz
cd /usr/local
# 将飞信解压到/usr/local下
tar zxvf fetion20080522004-linrh4.tar.gz
mv install fetion
# 创建飞信支持库目录
mkdir /usr/local/fetion/lib
tar zxvf libraryrh4×32.tar.gz
cd libraryrh4×32
cp lib*so* /usr/local/fetion/lib
ln -s /usr/local/fetion/lib/libcrypto.so.0.9.7a /usr/local/fetion/lib/libcrypto.so.4
ln -s /usr/local/fetion/lib/libssl.so.0.9.7a /usr/local/fetion/lib/libssl.so.4
vi /etc/ld.so.conf
# 加入下面行
/usr/local/fetion/lib
# 保存退出,并执行ldconfig命令使配置生效。
2.测试安装是否成功
[root@web74 fetion]# ./fetion
************************ IMPORTANT STATEMENT ************************
** **
** PLEASE DON’T USE THIS SOFTWARE TO SEND JUNK SHORT MESSAGES. **
** OTHERWISE PLEASE BEAR YOUR OWN CONSEQUENCES. **
** **
** Version:[20080522004-linrh4] **
*********************************************************************
This program is the console version of China Fetion!
It’s free for personal user.
Fetion official website:
This project website:
AUTHOR:KelvinH MSN/EMAIL:shichangguo@msn.com
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
3.编写发送脚本
vi /usr/local/fetion/sendsms.sh
#!/bin/sh
fetionDir=/usr/local/fetion
cd $fetionDir
DIR=`pwd`
# 设置发短信的号码和飞信登录密码
user=158xxxxxxxx
pwd=xxxxxx
for phone in `cat $DIR/phonelist.txt`
do
echo “$phone” | sed ‘/^[ ]*$/d’ | sed ’s/^[ ]*//’ | sed ’s/[ ]*$//’ | grep ‘^1[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]‘
if (($? == 0 ));then
if [[ -f $DIR/msg.txt ]];then
cat /dev/null > msg.txt
fi
phone=`echo “$phone” | sed ’s/^[ ]*//’ | sed ’s/[ ]*$//’`
echo “sms $phone $1″ >> $DIR/msg.txt
echo “quit” >> $DIR/msg.txt
$fetionDir/fetion -u $user -p $pwd -b $DIR/msg.txt
else
continue
fi
done
# phonelist.txt为接收短信号码列表,必须是发送飞信用户的好友
# 在此控制短信接收者而不是在nagios联系人中,好处是修改接收者后不需要重启nagios
# 可以使用#号注释接收者的号码以屏蔽接收者
vi /usr/local/nagios/fetion/phonelist.txt
# Phone NO. 1
138xxxxxxxx
# Phone NO. 2
158xxxxxxxx
# 修改飞信目录权限(否则运行Nagios的用户不能写入信息内容到/usr/local/fetion/msg.txt)
chown -R nagios.nagios /usr/local/fetion
chmod +x /usr/local/fetion/sendsms.sh
chmod +x /usr/local/fetion/fetion
4.配置Nagios
vi /usr/local/nagios/etc/objects/commands.cfg
################################################################################
#
# notify-host-by-sendmsg/notify-service-by-sendmsg //定义发送飞信报警的命令
#
################################################################################
define command {
command_name notify-host-by-sendmsg
command_line /usr/local/fetion/sendsms.sh “Host $HOSTSTATE$ alert for $HOSTNAME$($HOSTADDRESS$) on $TIME$.”
}
define command {
command_name notify-service-by-sendmsg
command_line /usr/local/fetion/sendsms.sh “”$TIME$”:$SERVICEDESC$($HOSTADDRESS$) is $SERVICESTATE$.”
}
定义联系人模板
define contact{
name sendmsg-contact ; The name of this contact template
service_notification_period 24×7 ; service notifications can be sent anytime
host_notification_period 24×7 ; host notifications can be sent anytime
service_notification_options w,u,c,s ; send notifications for all service states, flapping events, and scheduled downtime events
host_notification_options d,u,s ; send notifications for all host states, flapping events, and scheduled downtime events
service_notification_commands notify-service-by-sendmsg ; 使用定义的飞信报警命令
host_notification_commands notify-host-by-sendmsg ; 使用定义的飞信报警命令
register 0 ; DONT REGISTER THIS DEFINITION – ITS NOT A REAL CONTACT, JUST A TEMPLATE!
}
定义联系人及联系人组
vi /usr/local/nagios/etc/objects/contacts.cfg
define contact{
contact_name msgreceiver ; Short name of user
use sendmsg-contact ; 使用上面定义的联系人模板
alias MsgReceiver ; Full name of user
email
}
define contactgroup{
contactgroup_name admins-sendmsg
alias BaiHe Monitor
members msgreceiver
}
5.测试故障时是否能够触发短信报警