昨天晚上下班没有收到nagios发来的短信,回到家里登陆服务器发送测试短信,sms 没有报错,也没有返回OK,很奇怪,然后在网上搜索也没有搜索到什么,无意中看到CSDN上有 飞信官方的PHP类提供下载,赶快下载下来看了下,不错,比那些提供PHP API 的要好的多吧,因为用别人的免费API的话,你的手机号和密码不怕被别人利用吗?可都是明文哦?废话不多说了看我是如何设置的吧,我也顺便把MSN报警也贴上。
MSN报警
1.
下载php类
2.
解压文件放到 /var/www 下 apache 访问目录
3.
修改 simple.php
error_reporting(E_ALL);
include('sendMsg.php');
$fh = file_get_contents('/usr/local/nagios/msnservice.out'); ###报警打印信息文件
$sendMsg = new sendMsg();
$sendMsg->simpleSend('[email protected]', 'password', '[email protected]',$fh);
echo $sendMsg->result.'fffff '.$sendMsg->error;
?>
4.
创建报警打印信息文件
shell $> cd /usr/local/nagios/
shell $> vim msnservice.out
测试
shell $> chown nagios.nagios msnservice.out
5.发送测试信息
shell $> wget -qO-
http://localhost/simple.php
MSN 收到信息
飞信php报警
1.
下载php类
2.
解压到 /var/www apache 访问目录
3.
创建 发信 PHP 文件
shell $> cd /var/www
shell $> vim send.php
require_once('class.fetion.php');//上面的那些代码
$fh = file_get_contents('/usr/local/nagios/smservice.out'); //报警打印信息
$tophone=trim($_GET['re']);
$message=trim($fh);
$sms = new Fetion;
$sms->phone_num = trim('13611031***');//$_CFG['sms_shop_mobile']
$sms->password = trim('password');//$_CFG['sms_fetion_password']
$sms->sip_login();
$sms->sendSMS_toPhone($tophone,$message);//$_CFG['sms_shop_mobile']
$sms->sip_logout();
?>
4.
创建临时报警打印信息文件
shell $> cd /usr/local/nagios/
shell $> vim smservice.out
测试 测试 测试
shell $>chown nagios.nagios smservice.out ### 一会就不用在改权限了,因为这个文件是nagios来操作的
5.
发短信测试
wget -qO-
http://localhost/send.php?re=
13611031***
整合到nagios下
1.
创建 MSN 脚本 与 SMS脚本
shell $>cd /usr/local/nagios/libexec
shell $>vim msn.sh
#!/bin/bash
wget -qO-
http://localhost/simple.php
Vim sms.sh
#!/bin/bash
re=13611031238 ##接收人电话号码,必须是你的好友
wget -qO- http://localhost/send.php?re=$re >> /dev/null
2.
修改nagios command.cfg 配置文件
shell $> cd /usr/local/nagios/etc/objects
shell $> vim command.cfg
##添加如下
# 'notify-host-by-msn' command definition
define command{
command_name notify-host-by-msn
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\nHost: $HOSTNAME$\nState: $HOSTSTATE$\nAddress: $HOSTADDRESS$\nInfo: $HOSTOUTPUT$\n\nDate/Time: $LONGDATETIME$\n" > /usr/local/nagios/msnservice.out | $USER1$/msn.sh
}
# 'notify-service-by-msn' command definition
define command{
command_name notify-service-by-msn
command_line /usr/bin/printf "%b" "***** Nagios *****\n\nNotification Type: $NOTIFICATIONTYPE$\n\nService: $SERVICEDESC$\nHost: $HOSTALIAS$\nAddress: $HOSTADDRESS$\nState: $SERVICESTATE$\n\nDate/Time: $LONGDATETIME$\n\nAdditional Info:\n\n$SERVICEOUTPUT$" > /usr/local/nagios/msnservice.out | $USER1$/msn.sh
}
#### notify-host-sms ####
define command {
command_name notify-host-sms
command_line /usr/bin/printf "%s" "Host $HOSTSTATE$ alert for $HOSTNAME$ on $DATETIME$ $CONTACTPAGER$" > /usr/local/nagios/smservice.out | $USER1$/sms.sh
}
###### notify-service-sms ######
define command {
command_name notify-service-sms
command_line /usr/bin/printf "%s" "$HOSTADDRESS$ $HOSTALIAS$/$SERVICEDESC$ is $SERVICESTATE$ $CONTACTPAGER$" > /usr/local/nagios/smservice.out | $USER1$/sms.sh
}
3.
修改 contacts.cfg 如果用的是 templates.cfg 模版的话就修改 templates.cfg
define contact{
name generic-contact
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r,f,s
host_notification_options d,u,r,f,s
service_notification_commands notify-service-by-email,notify-service-by-msn,notify-service-sms
host_notification_commands notify-host-by-email,notify-host-by-msn,notify-host-sms
register 0
### 这样就可以 三重保险了~~呵呵~~
我也顺便 把我的 PHP 文件都上传上来大家下载自己看看吧。
最后 谢谢 我公司同事,帮我看了一下PHP,我PHP有点菜~~~呵呵。
PS:一些朋友在使用飞信PHP API 出错,我在这里补充一下,
飞信PHP 源码需要用到 socket curl 扩展模块~
php-net-socket
php-curl