• zabbix告警 邮件告警 钉钉告警


    邮件告警

    添加主机组

    在这里插入图片描述

    添加模板

    在这里插入图片描述

    添加主机

    在这里插入图片描述

    在模板中添加监控项

    在这里插入图片描述

    在模板中添加触发器

    在这里插入图片描述

    添加动作,远程执行命令

    [root@client ~]# vim /etc/sudoers  #给zabbix用户提权
    
    • 1

    在这里插入图片描述

    [root@client ~]# vim /etc/zabbix/zabbix_agentd.conf
    
    • 1

    在这里插入图片描述

    [root@client ~]# systemctl restart zabbix-agent
    
    • 1

    在这里插入图片描述
    在这里插入图片描述### 创建告警媒介并测试

    在这里插入图片描述
    在这里插入图片描述

    给用户绑定告警媒介类型

    在这里插入图片描述

    动作配置

    在这里插入图片描述
    在这里插入图片描述

    钉钉告警

    在钉钉中创建群聊

    在这里插入图片描述

    在这里插入图片描述

    安装python依赖模块python-requests
    [root@zabbix-server ~]# yum -y install python3 python3-requests
    
    • 1
    配置钉钉告警配置脚本zabbix_ding.conf
    [root@zabbix-server ~]# vim /etc/zabbix/zabbix_ding.conf
    [config]
    log_path=/var/log/zabbix/zabbix_ding.log
    webhook=https://oapi.dingtalk.com/robot/send?access_token=c1d11ecf6704f008eb25de644ed5cead752c065c0d11f9f89b58273cf0c78a96
    secret=SECc26e5bc55c64d454f15a8bdcce6825b7e5bb4f6fecc38025d2845837415192d7
    
    • 1
    • 2
    • 3
    • 4
    • 5
    在目录/var/log/zabbix中创建钉钉告警日志文件zabbix_ding.log
    [root@zabbix-server ~]# touch /var/log/zabbix/zabbix_ding.log
    
    • 1
    设置钉钉告警日志文件zabbix_ding.log的所有权
    [root@zabbix-server ~]# chown zabbix.zabbix /var/log/zabbix/zabbix_ding.log 
    
    • 1
    在/usr/lib/zabbix/alertscripts目录中执行的脚本dingding.py的内容
    [root@zabbix-server ~]# cd /usr/lib/zabbix/alertscripts
    [root@zabbix-server alertscripts]# vim dingding.py
    #!/usr/bin/env python3
    # coding:utf8
    #
    import configparser
    import os
    import time
    import hmac
    import hashlib
    import base64
    import urllib.parse
    import requests
    import json
    import sys
    
    config = configparser.ConfigParser()
    config.read('/etc/zabbix/zabbix_ding.conf', encoding='utf-8')
    log_path = config.get('config', 'log_path')
    api_url = config.get('config', 'webhook')
    api_secret = config.get('config', 'secret')
    log_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
    
    
    # 钉钉机器人文档说明
    # https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
    def get_timestamp_sign():
        timestamp = str(round(time.time() * 1000))
        secret = api_secret
        secret_enc = secret.encode('utf-8')
        string_to_sign = '{}\n{}'.format(timestamp, secret)
        string_to_sign_enc = string_to_sign.encode('utf-8')
        hmac_code = hmac.new(secret_enc, string_to_sign_enc, digestmod=hashlib.sha256).digest()
        sign = urllib.parse.quote_plus(base64.b64encode(hmac_code))
        return timestamp, sign
    
    # 获取加签后的链接
    def get_signed_url():
        timestamp, sign = get_timestamp_sign()
        webhook = api_url + "×tamp=" + timestamp + "&sign=" + sign
        return webhook
    
    # 定义消息模式
    def get_webhook(mode):
        if mode == 0:  # only 关键字
            webhook = api_url
        elif mode == 1 or mode == 2:  # 关键字和加签 或 # 关键字+加签+ip
            webhook = get_signed_url()
        else:
            webhook = ""
            print("error! mode:   ", mode, "  webhook :  ", webhook)
        return webhook
    
    
    def get_message(text, user_info):
        # 和类型相对应,具体可以看文档 :https://ding-doc.dingtalk.com/doc#/serverapi2/qf2nxq
        # 可以设置某个人的手机号,指定对象发送
        message = {
            "msgtype": "text",  # 有text, "markdown"、link、整体跳转ActionCard 、独立跳转ActionCard、FeedCard类型等
            "text": {
                "content": text  # 消息内容
            },
            "at": {
                "atMobiles": [
                    user_info,
                ],
                "isAtAll": False  # 是否是发送群中全体成员
            }
        }
        return message
    
    
    # 消息发送日志
    def log(info):
        if os.path.exists(log_path):
            log_file = open(log_path, "a+")
        else:
            log_file = open(log_path, "w+")
        log_file.write(info)
    
    
    def send_ding_message(text, user_info):
        # 请求的URL,WebHook地址
        # 主要模式有 0 : 关键字 1:# 关键字 +加签 3:关键字+加签+IP
        webhook = get_webhook(1)
        # 构建请求头部
        header = {
            "Content-Type": "application/json",
            "Charset": "UTF-8"
        }
        # 构建请求数据
        message = get_message(text, user_info)
        # 对请求的数据进行json封装
        message_json = json.dumps(message)
        # 发送请求
        info = requests.post(url=webhook, data=message_json, headers=header).json()
        code = info["errcode"]
        errmsg = info["errmsg"]
        if code == 0:
            log(log_time + ":消息已发送成功 返回信息:%s %s\n" % (code, errmsg))
        else:
            log(log_time + ":消息发送失败 返回信息:%s %s\n" % (code, errmsg))
            print(log_time + ":消息发送失败 返回信息:%s %s\n" % (code, errmsg))
            exit(3)
    
    
    if __name__ == "__main__":
        text = sys.argv[3]
        user_info = sys.argv[1]
        send_ding_message(text, user_info)
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    设置脚本目录的所有权
    [root@zabbix-server alertscripts]# chown -R zabbix.zabbix /usr/lib/zabbix/alertscripts/
    [root@zabbix-server alertscripts]# chmod +x dingding.py 
    [root@zabbix-server alertscripts]# ./dingding.py  user subject mesages
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    配置脚本告警

    在这里插入图片描述
    在这里插入图片描述

    为用户添加报警媒介

    在这里插入图片描述

    创建动作

    在这里插入图片描述

    [root@client ~]# systemctl stop sshd
    
    • 1

    在这里插入图片描述

  • 相关阅读:
    第九章 JDBC
    Kubernetes: kube-apiserver 之认证
    数据分析回头看2——重复值检查/元素替换/异常值筛选
    C++学习——继承
    试图替代 Python 的下一代AI编程语言:Mojo
    智慧公厕:改变公共厕所管理与运营的未来
    C1. k-LCM (easy version)-Codeforces Round #708 (Div. 2)
    C# 文件 文件夹 解除占用
    配置anaconda+安装cuda和cudnn+将cuda和cudnn配置到指定目录+使用cuda命令安装cuda和cudnn
    隆云通吸顶多参数传感器
  • 原文地址:https://blog.csdn.net/2301_78315274/article/details/134479323