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