Nagios通过企业微信报警


主要分两部分进行:

  • 注册企业微信,自建应用,获取与发送消息相关的信息;
  • 编写调用微信API脚本(bash),配置Nagios微信报警;

一、企业微信

  1、注册企业微信:https://work.weixin.qq.com/,登录后在“我的企业”找到CorpID

  2、创建应用,获取应用里,AgentIdSecret

 

  

  3、在“通讯录”里,查看需要接受报警的部门id或者成员账号(非昵称)

二、服务器端配置

  1、编写调用微信API脚本(bash)命名为set_to_weixin.sh,放在Nagios插件目录下,默认:/usr/local/nagios/libexec,添加可执行权限

 1 #!/bin/bash
 2 get_access_token () {
 3 curl -s https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=CorpID\&corpsecret=Secret >/tmp/$$.txt
 4 access_token=`awk -F "\"" '{print $10}' /tmp/$$.txt`
 5 }
 6 
 7 sed_to_weixin () {
 8 curl https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token=$access_token -d  "{ \
 9    \"touser\": \"xxxxxx\", \
10    \"msgtype\": \"text\", \
11    \"agentid\": 1000000, \
12    \"text\": { \
13        \"content\": \"$1\" \
14    }, \
15    \"safe\":\"0\" \
16 }"
17 }
18 
19 main () {
20 get_access_token
21 sed_to_weixin "$*"
22 }
23 
24 main "$*"

  注:脚本第3行,替换为自己的CorpIDSecret

    脚本第9行,替换为自己的成员账号;或者改为toparty后面接部门id

    脚本第11行,替换为自己的AgentId

  2、修改commands.cfg,在末尾添加如下配置

1 # For weixin
2 define command{
3 command_name notify-host-by-weixin
4 command_line /usr/local/nagios/libexec/set_to_weixin.sh "host\n-@@-$NOTIFICATIONTYPE$-@@-$HOSTNAME$-@@-$HOSTSTATE$-@@-$HOSTADDRESS$-@@-$HOSTOUTPUT$-@@-$CONTACTALIAS$"
5 }
6 define command{
7 command_name notify-service-by-weixin
8 command_line /usr/local/nagios/libexec/set_to_weixin.sh "service\n-@@-$NOTIFICATIONTYPE$-@@-$SERVICEDESC$-@@-$HOSTALIAS$-@@-$HOSTADDRESS$-@@-$SERVICESTATE$-@@-$SERVICEOUTPUT$-@@-$CONTACTALIAS$"
9 }

  3、修改templates.cfg,在contact名为generic-contact的模板里修改如下两行

1 service_notification_commands   notify-service-by-email,notify-service-by-weixin
2 host_notification_commands      notify-host-by-email,notify-host-by-weixin

  4、重启Nagios

  [root@nagios ~]# /etc/init.d/nagios reload

三、测试

  1、登录Nagios,点击hosts或者services,点击,发送当前通知

  2、登陆企业微信客户端,查看是否收到报警消息。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM