zabbix 监控主机并钉钉报警

1、安装客户端

# yum install zabbix-agent-5.0.32-1.el8.x86_64.rpm

2、配置客户端

# egrep  -v '^$|^#'   /etc/zabbix/zabbix_agentd.conf
  PidFile=/var/run/zabbix/zabbix_agentd.pid
  LogFile=/var/log/zabbix/zabbix_agentd.log
  LogFileSize=0
  Server=116.205.129.152
  ServerActive=116.205.129.152
  Hostname=cocoai-1
  Include=/etc/zabbix/zabbix_agentd.d/*.conf

3、启动客户端

# systemctl start  zabbix-agent

4、创建主机模板

 5、创建web页面usl监控

1)创建监控模板

2)创建web监控场景

6、创建触发器

 7、绑定主机

 8、创建钉钉群和机器人

9、编写钉钉告警脚本

脚本位置

# grep ^Aler /etc/zabbix/zabbix_server.conf
# cd /usr/lib/zabbix/alertscripts
# vim dingding.py

1)python 3 脚本

#!/usr/bin/python3
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
import time
headers = {'Content-Type': 'application/json;charset=utf-8'}
time=time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
api_url = "您的 Webhook 地址"
def msg(text,user):
  json_text= {
     "msgtype": "markdown",
        "markdown": {
            "title": "Kasar",
            "text": text   + "\n \n @"+ user
        },
        "at": {
            "atMobiles": [
                user
 ],
            "isAtAll": False
        }
    }
  r = requests.post(api_url,data=json.dumps(json_text),headers=headers).json()
  code = r["errcode"]
if __name__ == '__main__':
    text = sys.argv[2]
    user = sys.argv[1]
    msg(text,user)

2)python 2 脚本

#!/usr/bin/python2
# -*- coding: utf-8 -*-
import requests
import json
import sys
import os
headers = {'Content-Type': 'application/json;charset=utf-8'}
api_url = "https://oapi.dingtalk.com/robot/send?access_token=c8a8336f237f35bc7efe68bcb4f8a71344950397b338f9fef1ed6019bbb4ef04"
def msg(text,user):
    json_text= {
     "msgtype": "markdown",
        "markdown": {
            "title":"IT小白Kasar",
            "text": text + "\n \n @"+ user
        },
        "at": {
            "atMobiles": [
                user
 ],
            "isAtAll": False
        }
    }
    r=requests.post(api_url,data=json.dumps(json_text),headers=headers).json()
    code = r["errcode"]
if __name__ == '__main__':
    text = sys.argv[2]
    user = sys.argv[1]
    msg(text,user)

3)测试脚本

# pip3 install requests
# chown -R zabbix.zabbix /usr/lib/zabbix/alertscripts/ # chmod +x dingding.py # ./dingding.py 告警 test
备注:告警是钉钉的关键字,test是内容,requests模块

 10、创建报警媒介

 11、创建动作

1)创建动作

2)创建触发器

 3、创建告警操作和恢复告警操作

告警主机:{HOST.NAME}

告警地址:{HOST.IP}

监控项目:{ITEM.NAME}

监控取值:{ITEM.LASTVALUE}

当前状态:{TRIGGER.STATUS}

告警时间:{EVENT.DATE} {EVENT.TIME}

12、给用户添加报警媒介

12、钉钉webhook方式告警

 脚本

var dingding = { 
  key: null, 
  message: null, 
  msgtype: "markdown", 
  proxy: null, 
sendMessage: function () { 
    var params = { 
       msgtype: dingding.msgtype, 
       markdown: { 
          title: "告警", //钉钉关键字
          text: dingding.message 
        }, 
      }, 
      data, 
      response, 
      request = new CurlHttpRequest(), 
      url = 
        "https://oapi.dingtalk.com/robot/send?access_token=" + 
        dingding.key; 
    if (dingding.proxy) { 
      request.setProxy(dingding.proxy); 
    } 
    request.AddHeader("Content-Type: application/json"); 
    data = JSON.stringify(params); 
    // Remove replace() function if you want to see the exposed key in the log file. 
    Zabbix.Log( 
      4, 
      "[dingding Webhook] URL: " + url.replace(dingding.key, "<BOT KEY>") 
    ); 
    Zabbix.Log(4, "[dingding Webhook] params: " + data); 
    response = request.Post(url, data); 
    Zabbix.Log(4, "[dingding Webhook] HTTP code: " + request.Status()); 
    try { 
      response = JSON.parse(response); 
    } catch (error) { 
      response = null; 
    } 
    if (request.Status() !== 200 || response.errcode !== 0) { 
      if (typeof response.errmsg === "string") { 
        throw response.errmsg; 
      } else { 
        throw "Unknown error. Check debug log for more information."; 
      } 
    } 
  }, 
}; 
try { 
  var params = JSON.parse(value); 
  if (typeof params.Key === "undefined") { 
    throw 'Incorrect value is given for parameter "Key": parameter is missing'; 
  } 
  dingding.key = params.Key; 
  if (params.HTTPProxy) { 
    dingding.proxy = params.HTTPProxy; 
  } 
  dingding.to = params.To; 
  dingding.message = params.Subject + "\n" + params.Message; 
  dingding.sendMessage(); 
  return "OK"; 
} catch (error) { 
  Zabbix.Log(4, "[dingding Webhook] notification failed: " + error); 
  throw "Sending failed: " + error + "."; 
}

 

posted on 2023-05-29 14:03  五光十色  阅读(1036)  评论(0编辑  收藏  举报

导航