nagios的MSN和飞信PHP类API报警实践
感谢Eric的帮助!!!
感谢Eric的帮助!!!
一.MSN的报警
1.下载php类,所有的php程序都在msn的目录下
2.解压文件放到 /opt/lammp/htdocs/msn 下 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.创建报警打印信息文件
Cd /usr/local/nagios/
Vi msnservice.out
测试
Chown nagios.nagios msnservice.out
5.发送测试信息
wget -qO- http://localhost/simple.php
MSN 收到信息
6.将MSN报警整合到nagios下
1).创建 MSN 脚本
cd /usr/local/nagios/libexec
vi msn.sh
#!/bin/bash
wget -qO- http://localhost/msn/simple.php
2).修改nagios command.cfg 配置文件
cd /usr/local/nagios/etc/
vi 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
}
3).修改 contacts.cfg ,在 service_notification_commands参数后面添加上notify-service-by-msn,在host_notification_commands参数后添加notify-host-by-msn
define contact {
contact_name sery
alias system administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email,notify-service-by-sms,notify-service-by-msn
host_notification_commands notify-host-by-email,notify-host-by-sms, notify-host-by-msn
email [email protected]
}
7.重新启动nagios即可实现MSN报警
1.下载php类,所有的php程序都在msn的目录下
2.解压文件放到 /opt/lammp/htdocs/msn 下 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.创建报警打印信息文件
Cd /usr/local/nagios/
Vi msnservice.out
测试
Chown nagios.nagios msnservice.out
5.发送测试信息
wget -qO- http://localhost/simple.php
MSN 收到信息
6.将MSN报警整合到nagios下
1).创建 MSN 脚本
cd /usr/local/nagios/libexec
vi msn.sh
#!/bin/bash
wget -qO- http://localhost/msn/simple.php
2).修改nagios command.cfg 配置文件
cd /usr/local/nagios/etc/
vi 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
}
3).修改 contacts.cfg ,在 service_notification_commands参数后面添加上notify-service-by-msn,在host_notification_commands参数后添加notify-host-by-msn
define contact {
contact_name sery
alias system administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email,notify-service-by-sms,notify-service-by-msn
host_notification_commands notify-host-by-email,notify-host-by-sms, notify-host-by-msn
email [email protected]
}
7.重新启动nagios即可实现MSN报警
二.飞信php报警 (首先双方加为飞信好友,即发飞信的手机加要收到飞信手机为好友即可)
1.下载php类
2.解压到 /opt/lammp/htdocs/sms/ apache 访问目录
3.创建 发信 PHP 文件
cd /var/www
vi 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('138106077xx');//$_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->sendSMS_toPhone($tophone,iconv('UTF-8', 'gb2312', $message));//$_CFG['sms_shop_mobile']
$sms->sendSMS_toPhone($tophone,$message);//$_CFG['sms_shop_mobile']
$sms->sip_logout();
?>
4.创建临时报警打印信息文件
Cd /usr/local/nagios/
vi smservice.out
测试 测试 测试
chown nagios.nagios smservice.out ### 一会就不用在改权限了,因为这个文件是nagios来操作的
5.发短信测试
wget -qO- http://localhost/sms/send.php?re=136012089**
6.整合到nagios下
1).创建SMS脚本
cd /usr/local/nagios/libexec
vi sms.sh 内容如下:
#!/bin/bash
re=13611031238
wget -qO- http://localhost/send.php?re=$re >> /dev/null
注意:re=13611031238 为要收到短信的手机号码,如果是多个手机号每个手机号用逗号分开。
2).修改nagios command.cfg 配置文件
cd /usr/local/nagios/etc/
vi command.cfg
##添加如下
#### 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
}
1.下载php类
2.解压到 /opt/lammp/htdocs/sms/ apache 访问目录
3.创建 发信 PHP 文件
cd /var/www
vi 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('138106077xx');//$_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->sendSMS_toPhone($tophone,iconv('UTF-8', 'gb2312', $message));//$_CFG['sms_shop_mobile']
$sms->sendSMS_toPhone($tophone,$message);//$_CFG['sms_shop_mobile']
$sms->sip_logout();
?>
4.创建临时报警打印信息文件
Cd /usr/local/nagios/
vi smservice.out
测试 测试 测试
chown nagios.nagios smservice.out ### 一会就不用在改权限了,因为这个文件是nagios来操作的
5.发短信测试
wget -qO- http://localhost/sms/send.php?re=136012089**
6.整合到nagios下
1).创建SMS脚本
cd /usr/local/nagios/libexec
vi sms.sh 内容如下:
#!/bin/bash
re=13611031238
wget -qO- http://localhost/send.php?re=$re >> /dev/null
注意:re=13611031238 为要收到短信的手机号码,如果是多个手机号每个手机号用逗号分开。
2).修改nagios command.cfg 配置文件
cd /usr/local/nagios/etc/
vi command.cfg
##添加如下
#### 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 在 service_notification_commands参数后面添加上notify-service-by-sms,在host_notification_commands参数后添加notify-host-by-sms
define contact {
contact_name sery
alias system administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email,notify-service-by-sms,notify-service-by-msn
host_notification_commands notify-host-by-email,notify-host-by-sms, notify-host-by-msn
email [email protected]
}
define contact {
contact_name sery
alias system administrator
service_notification_period 24x7
host_notification_period 24x7
service_notification_options w,u,c,r
host_notification_options d,u,r
service_notification_commands notify-service-by-email,notify-service-by-sms,notify-service-by-msn
host_notification_commands notify-host-by-email,notify-host-by-sms, notify-host-by-msn
email [email protected]
}
7.重新启动nagios即可实现飞信php报警