實戰Nagios網絡監控(3)——Nagios 微信報警


1. 下載微信公衆平臺私有接口php

# yum install -y gitios

# git clone https://github.com/lealife/WeiXin-Private-APIgit


2. 修改微信公衆平臺私有接口代碼,以配合 nagios 報警github

# cp -r WeiXin-Private-API /usr/local/nagios/libexec/weixin微信

# chown -R nagios.nagios /usr/local/nagios/libexec/weixin微信公衆平臺

# cd /usr/local/nagios/libexec/weixinide

修改 config.php 文件:ui

$G_CONFIG["weiXin"] = array(spa

'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

相關文章
相關標籤/搜索