Nagios整合微信订阅号报警(修订版)

回复 收藏
本帖最后由 sincethen 于 2016-4-13 16:53 编辑

首先感觉 So Long 同学,根据他发布的笔记 修正而来。Nagios整合微信订阅号报警 http://www.apelearn.com/bbs/foru ... id=7917&fromuid=542
正常运行 一个星期,完美监控 。



前面步骤,类似于 So Long 同学的方法。
1 微信公众平台帐号注册,订阅号
订阅号比较好申请,我自己几分钟就成功了。其它帐号没有试,欢迎申请其它的同学反馈。

2 微信公众平台web私有接口
cd /usr/local/src/
# yum install -y git
# git clone https://github.com/lealife/WeiXin-Private-API

3 nagios添加报警方法步骤:
客户机配置 -->联系人或组,---> 报警命令--->使用php 调用网页发送报警信息。  

客户机配置 /usr/local/nagios/etc/object/下,
有些define service 检测配置会添加以下参数:
contact_groups          admins           
也就是报警联系分组admins。
如果没有,那么大家会看到:
define host{
use linux-server
}
在 templates.cfg 模板配置中,我们找linux-server配置
define host{
name                  linux-server
contact_groups        admins
}
也可以看到联系分组。

再在contacts.cfg 配置,看联系分组设置:
define contactgroup{
        contactgroup_name       admins
        members                 nagiosadmin
}
define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact
                }               
在templates.cfg配置中:
define contact{
        name                            generic-contact
        service_notification_commands   notify-service-by-email          ; send service notifications via email
        host_notification_commands      notify-host-by-email             ; send host notifications via email
                }
service_notification_commands和 host_notification_commands   即为报警调用的参数。


添加调用报警命令有两种方法:
contacts.cfg配置中:
define contact{
        contact_name                    nagiosadmin             ; Short name of user
        use                             generic-contact
        service_notification_commands   notify-service-by-weixin
        host_notification_commands      notify-service-by-weixin
}

也可是在templates.cfg模板库中添加
define contact{
        name                            generic-contact
        service_notification_commands   notify-service-by-email, notify-service-by-weixin         ; send service notifications via email
        host_notification_commands      notify-host-by-email,notify-service-by-weixin            ; send host notifications via email
}
        
下面就是报警的命令:
在commands.cfg中添加:
        ####notify-service-by-weixin
define command{
                command_name notify-service-by-weixin
                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/var/nagios.msg && /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$  &>/dev/null        
        }
        
        ####notify-host-by-weixin        
define command{
                command_name notify-host-by-weixin
                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/var/nagios.msg && /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$  &>/dev/null        
        }
        
        
下面是模拟微信网页发送消息:
根据命令: /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$  

添加$CONTACTADDRESS1$ 参数:
在contacts.cfg中:
define contact{
        contact_name                    nagiosadmin
                address1                        opGJIs_I3OYfGxbyPGzP4YrfkfDc
                }
address1 参数来历:登陆微信公众平台,在用户管理下面,有关注你的微信号。
比如我的,我另一个微信号关注自己。{:4_103:}
选择用户 查看属性链接,以我自己为例啊。
  1. https://mp.weixin.qq.com/misc/getheadimg?fakeid=opGJIs_I3OYfGxbyPGzP4YrfkfDc&token=714996826〈=zh_CN
其中fakeid=到&token= 之间一串字母 是用户加密过的地址。
   
QQ图片20160413150832.png
            
        

然后是修改微信调用网页:
先将文件保存在nagios目录下面:
cd /usr/local/src/WeiXin-Private-API
mkdir /usr/local/nagios/libexec/weixin/
cp -ar cache/ config.php include/ test.php  /usr/local/nagios/libexec/weixin/
chown -R nagios.nagios /usr/local/nagios/libexec/weixin/
cd /usr/local/nagios/libexec/weixin/
首先修改config.php ,添加你的微信订阅号帐号和密码:

vim config.php
        'account' => '公众平台账号',
       'password' => '密码',

然后:修改 test.php文件,
其中:添加 发送消息用户的地址 testFakeId 也是$CONTACTADDRESS1$ 传送过来的参数,添加 msg 消息地址,添加发送消息 最终保留如下:

<?php
require "config.php";
require "include/WeiXin.php";


$weiXin = new WeiXin($G_CONFIG['weiXin']);


$testFakeId = "$argv[1]";


$msg = `cat /usr/local/nagios/var/nagios.msg`;


print_r($weiXin->send($testFakeId, "$msg"));

然后检测nagios 配置,重新载入配置:
/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg
service nagios reload



---------------------------------------------------------------------------------------------------------------------------------
以上是 So Long 同学 的原版介绍。说说我遇到的一些问题。
首先,当订阅号发送过一次信息后,就不能再发送消息。这很坑人了。难道只能用一次。
俺经过千思百虑,发现问题出现在这里:
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/var/nagios.msg
在写入报警消息时,最好先删除之前的报警消息。
command_line /bin/rm -f /usr/local/nagios/var/nagios.msg &&/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/var/nagios.msg && /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$  &>/dev/null

为便于测试调用微信的结果消息,把输出信息记录在文档中
vim /usr/local/nagios/etc/object/commands.cfg
   ####notify-service-by-weixin
define command{
                command_name notify-service-by-weixin
                command_line  /bin/rm -f /usr/local/nagios/var/nagios.msg &&/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/var/nagios.msg && /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$  &>/usr/local/nagios/var/weixin_serv.txt        
        }

        ####notify-host-by-weixin        
define command{
                command_name notify-host-by-weixin
                command_line  /bin/rm -f /usr/local/nagios/var/nagios.msg &&/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/var/nagios.msg && /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$  &>/usr/local/nagios/var/weixin_host.txt        
        }

当天测试,一切正常,然后第二天。再去测试发现又不能发送消息。
这次的主要原因,是因为cookie的问题。我就不介绍如何排查。主要是查看 weixin_serv.txt和weixin_host.txt文件
处理的方法,就是定期删除cookie
crontab -e
0 0 * * * /bin/rm -rf /usr/local/nagios/libexec/weixin/cache/{cookie,webToken}


最后一个重点:
就是关注号每隔两天要向订阅号发消息,否则,订阅号不能再向关注的微信号发消息。
QQ图片20160413163115.png


欢迎大家批评指正,俺 微信号: sincethen_yl
QQ图片20160413163115.png
2016-04-13 16:52 举报
已邀请:
0

sincethen

赞同来自:


向多个微信号发送消息:修改contacts.cfg,再加入一个地址:
define contact{
        contact_name                    nagiosadmin
                address1                        opGJIs_I3OYfGxbyPGzP4YrfkfDc
                address2                       #关注号加密地址
                }

修改commands.cfg,再传递一个参数:
        ####notify-service-by-weixin
define command{
                command_name notify-service-by-weixin
                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/var/nagios.msg && /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$  $CONTACTADDRESS2$ &>/dev/null        
        }

####notify-host-by-weixin        
define command{
                command_name notify-host-by-weixin
                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/var/nagios.msg && /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$ $CONTACTADDRESS2$ &>/dev/null        
        }

再编辑 微信的test.php 档案:
$testFakeId = "$argv[1]";
$testFakeId2 = "$argv[2]";
print_r($weiXin->send($testFakeId, "$msg"));
print_r($weiXin->send($testFakeId2, "$msg"));
我这里没有使用批量发送的接口。



附加一些  weixin_serv.txt   反馈回来的消息:
stdClass Object
(
    [base_resp] => stdClass Object
        (
            [ret] => 0
            [err_msg] => ok
        )
)


OK:正常发送。
fans not receive: 用户拒绝接收消息。
customer block: 用户两天内没互动,发送失败。

回复帖子,请先登录注册

退出全屏模式 全屏模式 回复
评分
可选评分理由: