[i=s] 本帖最后由 sincethen 于 2016-4-13 16:35 编辑 [/i] 首先感觉 [color=#ff00][backcolor=rgb(229, 237, 242)][size=12px][url=http://www./bbs/space-uid-3328.html]So Long[/url][/size][/backcolor][/color] 同学,根据他发布的笔记 修正而来。Nagios整合微信订阅号报警 [url=http://www./bbs/fo ... 3D542]http://www./bbs/foru ... id=7917&fromuid=542[/url] 正常运行 一个星期,完美监控 。 前面步骤,类似于 [color=#ff00][backcolor=rgb(229, 237, 242)][size=12px][url=http://www./bbs/space-uid-3328.html]So Long[/url][/size][/backcolor][/color] 同学的方法。 1 微信公众平台账号注册,订阅号 订阅号比较好申请,我自己几分钟就成功了。其它账号没有试,欢迎申请其它的同学反馈。 2 微信公众平台web私有接口 [color=#ff0000]cd /usr/local/src/[/color] [color=#ff0000]yum install -y git[/color][color=#ff0000]git clone https://github.com/lealife/WeiXin-Private-API[/color]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 即为报警调用的参数。 添加调用报警命令有[size=3][color=#000000][b]两种[/b][/color][/size]方法: contacts.cfg配置中: define contact{ contact_name nagiosadmin ; Short name of user use generic-contact [color=#ff0000]service_notification_commands notify-service-by-weixin[/color] [color=#ff0000]host_notification_commands notify-service-by-weixin[/color] } 也可是在templates.cfg模板库中添加 define contact{ name generic-contact service_notification_commands notify-service-by-email, [color=#ff0000]notify-service-by-weixin[/color] ; send service notifications via email host_notification_commands notify-host-by-email,[color=#ff0000]notify-service-by-weixin[/color] ; send host notifications via email } 下面就是报警的命令: 在commands.cfg中添加: [color=#ff0000]####notify-service-by-weixin[/color] [color=#ff0000]define command{[/color] [color=#ff0000] command_name notify-service-by-weixin[/color] [color=#ff0000] 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 [/color] [color=#ff0000] }[/color] [color=#ff0000] [/color] [color=#ff0000] ####notify-host-by-weixin [/color] [color=#ff0000]define command{[/color] [color=#ff0000] command_name notify-host-by-weixin[/color] [color=#ff0000] 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 [/color] [color=#ff0000] }[/color] 下面是模拟微信网页发送消息: 根据命令: /usr/bin/php /usr/local/nagios/libexec/weixin/test.php $CONTACTADDRESS1$ 添加$CONTACTADDRESS1$ 参数: 在contacts.cfg中: [color=#ff0000]define contact{[/color] [color=#ff0000] contact_name nagiosadmin [/color] [color=#ff0000] address1 opGJIs_I3OYfGxbyPGzP4YrfkfDc[/color] [color=#ff0000] }[/color] address1 参数来历:登陆微信公众平台,在用户管理下面,有关注你的微信号。 比如我的,我另一个微信号关注自己。{:4_103:} 选择用户 查看属性链接,以我自己为例啊。 [code]https://mp.weixin.qq.com/misc/ ... zh_CN[/code] 其中fakeid=到&token= 之间一串字母 是用户加密过的地址。 然后是修改微信调用网页: 先将文件保存在nagios目录下面: [color=#ff0000]cd /usr/local/src/WeiXin-Private-API[/color] [color=#ff0000]mkdir /usr/local/nagios/libexec/weixin/[/color] [color=#ff0000]cp -ar cache/ config.php include/ test.php /usr/local/nagios/libexec/weixin/[/color] [color=#ff0000]chown -R nagios.nagios /usr/local/nagios/libexec/weixin/[/color] [color=#ff0000]cd /usr/local/nagios/libexec/weixin/[/color] 首先修改config.php ,添加你的微信订阅号账号和密码: [color=#ff0000]vim config.php[/color] [color=#ff0000] 'account' => '公众平台账号',[/color] [color=#ff0000] 'password' => '密码',[/color] 然后:修改 test.php文件, 其中:添加 发送消息用户的地址 testFakeId 也是$CONTACTADDRESS1$ 传送过来的参数,添加 msg 消息地址,添加发送消息 最终保留如下: [color=#ff0000]<?php[/color] [color=#ff0000]require 'config.php';[/color] [color=#ff0000]require 'include/WeiXin.php';[/color] [color=#ff0000] [/color] [color=#ff0000]$weiXin = new WeiXin($G_CONFIG['weiXin']);[/color] [color=#ff0000] [/color] [color=#ff0000]$testFakeId = '$argv[1]';[/color] [color=#ff0000] [/color] [color=#ff0000]$msg = cat /usr/local/nagios/var/nagios.msg ;[/color][color=#ff0000] [/color] [color=#ff0000]print_r($weiXin->send($testFakeId, '$msg'));[/color] 然后检测nagios 配置,重新载入配置: [color=#ff0000]/usr/local/nagios/bin/nagios -v /usr/local/nagios/etc/nagios.cfg[/color] [color=#ff0000]service nagios reload[/color] --------------------------------------------------------------------------------------------------------------------------------- 以上是 [color=#ff00][backcolor=rgb(229, 237, 242)][size=12px][url=http://www./bbs/space-uid-3328.html]So Long[/url][/size][/backcolor][/color] 同学 的原版介绍。说说我遇到的一些问题。 首先,当订阅号发送过一次信息后,就不能再发送消息。这很坑人了。难道只能用一次。 俺经过千思百虑,发现问题出现在这里: 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 在写入报警消息时,最好先删除之前的报警消息。 [color=#ff0000]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 [/color] 为便于测试调用微信的结果消息,把输出信息记录在文档中 [color=#ff00]vim /usr/local/nagios/etc/object/commands.cfg[/color] [color=#ff00] ####notify-service-by-weixin[/color] [color=#ff0000]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 [/color][color=#ff0000] } ####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 [/color][color=rgb(255, 0, 0)] }[/color] [color=#ff0000] [/color][color=#000000]当天测试,一切正常,然后第二天。再去测试发现又不能发送消息。[/color] [color=#000000] 这次[/color][color=#000]的主要原因,是因为cookie的问题。我就不介绍如何排查。主要是查看 weixin_serv.txt和weixin_host.txt文件[/color] [color=#000]处理的方法,就是定期删除cookie[/color] [color=#ff0000]crontab -e [/color] [color=#ff0000]0 0 * * * /bin/rm -rf /usr/local/nagios/libexec/weixin/cache/{cookie,webToken} [/color] 最后一个重点: 就是[b][color=#000080]关注号每隔两天要向订阅号发消息[/color][/b],否则,订阅号不能再向关注的微信号发消息。 欢迎大家批评指正,俺 微信号: sincethen_yl [i=s] 本帖最后由 sincethen 于 2016-4-13 16:35 编辑 [/i] 首先感觉 [color=#ff00][backcolor=rgb(229, 237, 242)][size=12px][url=http://www.apelea...
|
|