Nagios整合微信订阅号报警

回复 收藏
本帖最后由 So Long 于 2015-5-6 12:54 编辑

Nagios 整合微信订阅号报警

环境:rhel6.5 selinux 和 iptables 关闭。要求能上外网的(虚拟机亲测可用)
以下是nagios服务与微信订阅号的整合过程,最终实现当服务或主机出现故障,自动调用微信报警。
重点讲述如何配置微信公众平台私有接口,至于nagios服务的配置请大家参照nagios官方文档进行,此处不再赘述。(www.nagios.org

1. 下载微信公众平台私有接口
# yum install -y git
# git clone https://github.com/lealife/WeiXin-Private-API

2. 修改微信公众平台私有接口代码,以配合nagios报警

# cp -r WeiXin-Private-API /usr/local/nagios/libexec/weixin
# chown -R nagios.nagios /usr/local/nagios/libexec/weixin
# cd /usr/local/nagios/libexec/weixin

修改config.php文件:

$G_CONFIG["weiXin"] = array(

        'account' => '微信公众平台登录帐号',#填写你注册的微信订阅号的帐号和密码

        'password' => '微信公众平台登录密码',

修改test.php文件,只保留如下几行即可:

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

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

$testFakeId = "$argv[1]";#微信好友ID号,这里通过nagios传入

$msg = `cat /usr/local/nagios/var/nagios.msg`;#要发送的报警信息,由nagios传入

print_r($weiXin->send($testFakeId, "$msg"));#给微信好友发送信息

3. 整合nagios和微信公共平台私有接口

1) 增加微信报警选项: templates.cfg

修改 /usr/local/nagios/etc/objects/templates.cfg

在 define contact{...} 部分,将以下两行:
host_notification_commands notify-host-by-email
service_notification_commands notify-service-by-email

改为:

host_notification_commands notify-host-by-email,notify-host-by-weixin
service_notification_commands notify-service-by-email,notify-service-by-weixin


2) 增加调用命令: commands.cfg

修改 /usr/local/nagios/etc/objects/commands.cfg

在该文件的最后增加以下部分:

##### notify-host-by-weixin command definition
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
}

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

3) 修改联系人选项: contact.cfg

修改 /usr/local/nagios/etc/objects/contact.cfg
在 define contact{...} 部分增加如下一行

address     11206***#微信好友ID,登录微信公众平台网页版,在用户管理中点击你要发微信的好友,此时在地址上显示的fakeid就是微信好友的ID。

4) 重载nagios配置

# service nagios reload
http://C:\\Users\\THINKPAD\\Desktop\\tupian\\图片20
图片20.png 图片20.png
2015-05-06 12:51 举报
已邀请:
0

疾风

赞同来自:

给力!回头做一个
0

田埂上的梦

赞同来自:

{:4_91:} 好东西
0

lyhabc

赞同来自:

貌似要开通一个微信公众号
0

疾风

赞同来自:

收藏了
0

tornado

赞同来自:

好东西,谢谢分享
0

乌贼的帽子

赞同来自:

address  添加不进去报错误啊
0

starry

赞同来自:

乌贼的帽子 发表于 2015-10-30 02:36
address  添加不进去报错误啊

改成address1
0

starry

赞同来自:

已跪
错误信息
PHP Notice:  Undefined index: cookiePath in /etc/nagios/libexec/weixin/include/WeiXin.php on line 41
PHP Notice:  Undefined index: webTokenPath in /etc/nagios/libexec/weixin/include/WeiXin.php on line 42
PHP Warning:  file_get_contents(): Filename cannot be empty in /etc/nagios/libexec/weixin/include/WeiXin.php on line 95
PHP Warning:  file_get_contents(): Filename cannot be empty in /etc/nagios/libexec/weixin/include/WeiXin.php on line 96
PHP Notice:  Undefined index: HTTP_USER_AGENT in /etc/nagios/libexec/weixin/include/LeaWeiXinClient.php on line 33
PHP Warning:  file_put_contents(): Filename cannot be empty in /etc/nagios/libexec/weixin/include/WeiXin.php on line 64
PHP Warning:  file_put_contents(): Filename cannot be empty in /etc/nagios/libexec/weixin/include/WeiXin.php on line 85
PHP Notice:  Undefined index: HTTP_USER_AGENT in /etc/nagios/libexec/weixin/include/LeaWeiXinClient.php on line 33
stdClass Object
(
    [base_resp] => stdClass Object
        (
            [ret] => 0
            [err_msg] => ok
        )

)
0

369666951

赞同来自:

stdClass Object
(
    [base_resp] => stdClass Object
        (
            [ret] => 200002
            [err_msg] => invalid args
        )

)
测试发送消息 报这个消息  看不懂 ,消息收不到
0

方琪

赞同来自:

1
0

ankang

赞同来自:

写的不错挺好的学习了{:4_91:}
0

sincethen

赞同来自:

不错。赞一下先。
0

balich

赞同来自:

谢谢分享!
0

wanglixing

赞同来自:

resulted in a return code of 126.  Make sure the script or binary you are trying to execute actually exists...我现在遇到nagios调用微信提示这个信息,那个大哥大姐可以帮帮忙。
0

sincethen

赞同来自:

这个代码有很多坑,过几天,我给大家整理下。我已经处理过那些坑 了。
0

sincethen

赞同来自:

俺 已 另发新贴,修改些问题,欢迎楼主前往指正。http://www.apelearn.com/bbs/thread-12117-1-1.html
0

summer123

赞同来自:

0

riverxyz

赞同来自:

本帖最后由 riverxyz 于 2016-9-9 17:46 编辑

address 这个直接加的这个格式不对,会报错。得用address1这样。
0

riverxyz

赞同来自:

/usr/local/nagios/var/nagios.msg这个文件是编译安装 的才有的吗?我yum安装的好像没有,这个文件的格式是什么样的呢?
0

小贤

赞同来自:

那你很棒很棒哦

回复帖子,请先登录注册

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