首页 > 其他 > 详细

AWS告警优化

时间:2020-06-19 21:26:39      阅读:83      评论:0      收藏:0      [点我收藏+]

(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 + "\n\n>报警主题:"  + Subject +              "\n\n>详细信息:"  + Message +              "\n\n>备注信息:消息解析异常"


    pagrem = {
    "msgtype":"markdown",
        "markdown": {
              "title": "AWS告警" + "...."  ,
             "text":"报警主题 :" + title  +
                "\n\n>监控指标:"  + AlarmName + 
                "\n\n>报警时间:"  + str(lastpoint_time)   +
                "\n\n>报警资源:"  + str(service_key) + " : " + str(service_value)  +
                "\n\n>报警信息:"  + "当前值=" + str(lastpoint_data) + tag_word + str(EvaluationPeriods) + "次达到 " + "阀值=" + str(size_b_to_other(Threshold))  +
                "\n\n>业务备注:"  + str(AlarmDescription) 
        },
        "at":{
            "atMobiles":[
                "15527453712"
            ]
    },
        "isAtAll": "False"
    }
    headers={
        Content-Type:application/json
    }    
    requests.post(url, data=json.dumps(pagrem), headers=headers)

效果图:

技术分享图片

AWS告警优化

原文:https://www.cnblogs.com/5sdba-notes/p/13166450.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!