zoukankan      html  css  js  c++  java
  • AWS告警优化

    (1)AWS添加sns,略过

    (2)添加自定义webhook钉钉机器人,略过。

    (3)编写lambda函数脚本,利用cloudwatch触发告警。

    # -*- coding: utf-8 -*-
    import json
    import os
    import re
    import datetime
    from botocore.vendored import requests
    
    def time_format(str_time):
        ''' 时间的时区转换为东8区 '''
        Ymd = str_time.split('T')[0]
        HMS = str_time.split('T')[1].split('.')[0]
        str_time = '%s %s' % (Ymd, HMS)
        time = datetime.datetime.strptime(str_time, "%Y-%m-%d %H:%M:%S")
        format_time = time + datetime.timedelta(hours=8)
        return format_time
    
    
    def size_b_to_other(size):
        """用于转换容量单位"""
        units = ['B', 'KB', 'MB', 'GB', 'TB']
        # 处理异常
        if size < 1024:
            return size
    
        # 遍历单位的位置并用取整除赋值
        for unit in units:
            if size >= 1024:
                size //= 1024
            else:
                size_h = '{} {}'.format(size, unit)
                return size_h
    
        size_h = '{} {}'.format(size, unit)
        return size_h
        
        
        
    def lambda_handler(event, context):
       #钉钉机器人url
        url = "xxxxxxxx"
        # 解析要使用的字段
        Sns = event['Records'][0]['Sns']
        Subject = Sns['Subject']
        if "ALARM" in Subject:
            title = "<font color=#FF0000 size=3>AWS 报警触发</font>"
            tag_word = " 连续"
        elif "OK" in Subject:
            title = "<font color=#008000 size=3>AWS 报警恢复</font>"
            tag_word = " 未连续"
        elif "INSUFFICIENT_DATA" in Subject:
            title = "AWS 报警异常(数据不足)"
            tag_word = " , Insufficient Data 未连续"        
       
        Timestamp = Sns['Timestamp']
        Message = Sns['Message']
        Message = json.loads(Message)
        
        try:
            
            Region = Message['Region']
            AlarmName = Message['AlarmName']
            service_key = Message['Trigger']['Dimensions'][0]['name']
            service_value = Message['Trigger']['Dimensions'][0]['value']
            MetricName = Message['Trigger']['MetricName']
            Namespace = Message['Trigger']['Namespace']
            Period = Message['Trigger']['Period']
            EvaluationPeriods = Message['Trigger']['EvaluationPeriods']
            Threshold = Message['Trigger']['Threshold']
            StateChangeTime = Message['StateChangeTime']
            NewStateReason = Message['NewStateReason']
            AlarmDescription = Message['AlarmDescription']
    
            if "INSUFFICIENT_DATA" not in Subject:
                    
                # 转换cloudwatch单位为友好单位
                lastpoint = re.findall(r'[[](.*?)[)]', NewStateReason)
                lastpoint_data = size_b_to_other(float('%.2f' % float(lastpoint[0].split(' (')[0])))
                lastpoint_time = datetime.datetime.strptime(lastpoint[0].split(' (')[1],"%d/%m/%y %H:%M:%S")
                lastpoint_time = lastpoint_time + datetime.timedelta(hours=8)
            else:
                lastpoint_data = 'null'
                lastpoint_time = time_format(StateChangeTime)
    
        except:
            Message = json.dumps(Message, sort_keys=True, indent=2)
            content = title + "
    
    >报警主题:"  + Subject +  
                "
    
    >详细信息:"  + Message +  
                "
    
    >备注信息:消息解析异常"
    
    
        pagrem = {
        "msgtype":"markdown",
            "markdown": {
                  "title": "AWS告警" + "...."  ,
                 "text":"报警主题 :" + title  +
                    "
    
    >监控指标:"  + AlarmName + 
                    "
    
    >报警时间:"  + str(lastpoint_time)   +
                    "
    
    >报警资源:"  + str(service_key) + " : " + str(service_value)  +
                    "
    
    >报警信息:"  + "当前值=" + str(lastpoint_data) + tag_word + str(EvaluationPeriods) + "次达到 " + "阀值=" + str(size_b_to_other(Threshold))  +
                    "
    
    >业务备注:"  + str(AlarmDescription) 
            },
            "at":{
                "atMobiles":[
                    "15527453712"
                ]
        },
            "isAtAll": "False"
        }
        headers={
            'Content-Type':'application/json'
        }    
        requests.post(url, data=json.dumps(pagrem), headers=headers)

    效果图:

  • 相关阅读:
    PHP之旅3 php数组以及遍历数组 以及each() list() foreach()
    NSSetUncaughtExceptionHandler
    runtime
    Objective-C中的instancetype和id区别
    tableView 局部刷新
    CGAffineTransform
    iOS中文本属性Attributes
    ios 相机 自定义 相片的截取
    php程序的生命周期
    PHP代码执行流程
  • 原文地址:https://www.cnblogs.com/5sdba-notes/p/13166450.html
Copyright © 2011-2022 走看看