阿里云日志报警

版权声明:未经许可不得转载 https://blog.csdn.net/qq_35299863/article/details/79542226

这里简单介绍下基于阿里云日志服务的报警设置

1:首先通过loghub输出到阿里云日志服务

2:基于日志服务这里简单介绍两种报警

    2.1:基于阿里云本身提供的报警设置

        2.1.1:进入需要查询的标签页面,在输入框输入查询语句。例如:

            *|select inst_id,request_uri,requestId,duration where duration > 8000

            这样就查询出来符合条件的日志啦,当然查询的条件必须加入到索引里

        2.1.2:右上角 另存为快速查询,起个名字就完事啦

        2.1.3:右上角 另存为报警,快递查询名就选刚才2.1.2的名字,报警动作这里 自己选择,我一般都是通过钉钉,写入钉钉机器人的地址即可

    2.2:基于阿里云API

        如果你想尝试通过API去写代码实现以上步骤也完全ok

def get_log_duration():
    topic = ""
    query = "duration"
    From = int(time.time()) - 60
    To = int(time.time())

    endpoint = 'cn-hangzhou.log.aliyuncs.com'       # 选择与上面步骤创建Project所属区域匹配的Endpoint
    accessKeyId = ''    # 使用您的阿里云访问密钥AccessKeyId
    accessKey = ''      # 使用您的阿里云访问密钥AccessKeySecret
    project = ''        # 上面步骤创建的项目名称
    logstore = ''       # 上面步骤创建的日志库名称
    # 构建一个client
    client = LogClient(endpoint, accessKeyId, accessKey)

    req4 = GetLogsRequest(project, logstore, From, To, topic, query)
    res4 = client.get_logs(req4)
    # if res4 is not None and res4.is_completed():
    #     print "is_completed"
    if res4 is not None:
        return map(lambda x: x.contents, res4.get_logs())
def get_max_duration():
    info = get_log_duration()
    date = {
        "duration": 4000,
        "requestUri": "",
        "requestId": "",
        "inst_id":"",
        "timestamp":""
    }

    for line in info:
        dura = line.get("duration")
        Uri = line.get("requestUri")
        reqid = line.get("requestId")
        inst_id = line.get("inst_id")
        timestamp = line.get("@timestamp")

        if int(dura) > int(date["duration"]):
            date["duration"] = dura
            date["requestUri"] = Uri
            date["requestId"] = reqid
            date["inst_id"] = inst_id
            date["timestamp"] = timestamp

    if date["duration"] > 4000:
        return date


date = get_max_duration()

下面就是报警代码,在上篇 阿里云统计每日消费  已有钉钉报警,这里就不做介绍啦,当然必须在数据存在的情况下再做报警,否者就退出。


猜你喜欢

转载自blog.csdn.net/qq_35299863/article/details/79542226