1. 下载微信公众平台私有接口
目前创新互联已为1000多家的企业提供了网站建设、域名、雅安服务器托管、网站托管、服务器托管、企业网站设计、网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。我们拥有完善的网络基础设施服务,能够为企业或个人提供域名注册、雅安服务器托管、企业邮局、网站加速、数据库、云主机等网络基础服务。
# 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 文件,只保留如下几行即可:
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{...} 部分,将以下两行:
service_notification_commands notify-service-by-email
host_notification_commands notify-host-by-email
改为:service_notification_commands notify-service-by-email,service-notify-by-weixin
host_notification_commands notify-host-by-email,host-notify-by-weixin
2) 增加调用命令: commands.cfg
修改 /usr/local/nagios/etc/objects/commands.cfg
在该文件的最后增加以下部分:
##### host-notify-by-weixin command definition
define command{
command_name host-notify-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
}
##### service-notify-by-weixin command definition
define command{
command_name service-notify-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{...} 部分增加如下一行
address1 1000000000 #微信好友 ID
4) 重载 nagios 配置
# service nagios reload