首页 > 代码库 > 实战Nagios网络监控(3)——Nagios 微信报警
实战Nagios网络监控(3)——Nagios 微信报警
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{...} 部分,将以下两行:
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
本文出自 “技术人生,简单不简单” 博客,请务必保留此出处http://willis.blog.51cto.com/11907152/1866098
实战Nagios网络监控(3)——Nagios 微信报警