nagios微信报警(版本V2)

http://www.icanwen.com/?p=716

随着移动互联网技术的高速发展,微信被大家广泛使用,被应用到很多领域。本文将介绍如何让监控神器集成微信报警。

如果要使用微信报警需要用到微信的公众平台。这里写了一个模拟微信公众平台登陆的接口。

只要关注你注册的这个公众号,出现在好友列表中,Nagios就可以发送告警信息到你指定的微信帐号。
微信公众平台的开发模式下有订阅号和服务号,如果升级到服务号当然是方便多了,但不幸的是微信升级到服务号要公司的营业执照代码之类的东东,制造了很多麻烦。模拟网页发送是迫不得而为之^_^(其实使用腾讯提供微信API当然是最好了)。
下面是具体的操作步骤
一.注册微信公众平台。注册地址 https://mp.weixin.qq.com/
二.下载nodejs程序,在程序config/config.coffee中填写刚注册的公众号和密码。
node_wx
本程序有两个功能:1.是模拟登陆 2.是获取好友列表
1.获取好友信息
http://IP:8080/wx/friend
  • {




    • id: 5726745,
    • nick_name: "user1",
    • remark_name: "",
    • group_id: 0

    },



  • {

    • id: 618272308,
    • nick_name: "user2",
    • remark_name: "",
    • group_id: 0

    },



  • {

    • id: 2026561000,
    • nick_name: "user3",
    • remark_name: "",
    • group_id: 0

    },



2.安装nodejs
wget  http://nodejs.org/dist/v0.10.26/node-v0.10.26-linux-x64.tar.gz
tar xvzf node-v0.10.26-linux-x64.tar.gz
cp -Rp node-v0.10.26 /usr/local/
PATH=$PATH:/usr/local/node-v0.10.26/bin
(或者用nvm安装很方便)
3.运行接口程序
cd node_wx
node . 运行nodejs程序
(可以用pm2 启用微信接口程序 pm2 start index.js 然后设置开机启动)
三.写一个调用微信接口的SHELL,在此我写了几行SHELL在这里奉上。
weixin.rar
四.Nagios的配置
1.在 objects/commands.cfg中增加如下信息

define command{

command_name    notify-host-by-weixin

command_line    /usr/bin/printf "%b" "*****ERS monitor Robot*****\n\n Notification Type: $NOTIFICATIONTYPE$\n Host: $HOSTNAME$\nState: $HOSTSTATE$\n Address: $HOSTADDRESS$\n Info: $HOSTOUTPUT$\n\n Date/Time: $LONGDATETIME$\n" >/usr/local/nagios/var/weixin.out | /usr/local/bin/weixin.sh -f $CONTACTADDRESS1$

      }

# 'notify-service-by-weixin' command definition

define command{

command_name    notify-service-by-weixin



command_line    /usr/bin/printf "%b" "***** ERS monitor Robot*****\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/weixin.out | /usr/local/bin/weixin.sh -f $CONTACTADDRESS1$

                                }



注意:command_line后面的是在一行中
2.以objects/contacts.cfg中示例信息如下:
define contact{
contact_name              nagiosadmin             ; Short name of user
alias                           Nagios Admin            ; Full name of user
service_notification_period     24×7
host_notification_period        24×7
service_notification_options    w,u,c,r,f,s
host_notification_options       d,u,r,f,s
service_notification_commands    notify-host-by-email,notify-host-by-sms ,notify-service-by-weixin
host_notification_commands      notify-host-by-email,notify-host-by-sms ,notify-service-by-weixin
pager                          134123456               ;pager number
email                           [email protected]           ;Email message
address1                     2447905640               ;微信好友ID号
}


保存重启nagios 测试一下 大功告成 !现在Nagios就可以用微信预警了
注明:笔者水平有限,有错误之处欢迎指正!
weixin.jpg


你可能感兴趣的:(技术,程序,移动互联网,friend,皇太子)