nagios msn 飛信PHP類API 報警

          昨天晚上下班沒有收到nagios發來的短信,回到家裏登錄服務器發送測試短信,sms 沒有報錯,也沒有返回OK,很奇怪,而後在網上搜索也沒有搜索到什麼,無心中看到CSDN上有 飛信官方的PHP類提供下載,趕快下載下來看了下,不錯,比那些提供PHP API 的要好的多吧,由於用別人的免費API的話,你的手機號和密碼不怕被別人利用嗎?可都是明文哦?廢話很少說了看我是如何設置的吧,我也順便把MSN報警也貼上。
 
 
MSN報警
1. 下載php類
2. 解壓文件放到 /var/www 下 apache 訪問目錄
3. 修改 simple.php
<?php
error_reporting(E_ALL);
include('sendMsg.php');
$fh = file_get_contents('/usr/local/nagios/msnservice.out');   ###報警打印信息文件
$sendMsg = new sendMsg();
$sendMsg->simpleSend('eric@edgora.com', 'password', 'yangzi2008@126.com',$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
<?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
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
相關文章
相關標籤/搜索