本帖最后由 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
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
0
已跪
错误信息
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
)
)
错误信息
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
stdClass Object
(
[base_resp] => stdClass Object
(
[ret] => 200002
[err_msg] => invalid args
)
)
测试发送消息 报这个消息 看不懂 ,消息收不到
(
[base_resp] => stdClass Object
(
[ret] => 200002
[err_msg] => invalid args
)
)
测试发送消息 报这个消息 看不懂 ,消息收不到
编辑回复