Elastic Service中Watcher在日志EFK系统中告警应用
日志告警,是基于EFK日志系统中的es(es版本是7.10.1) watcher。wathcer 是es中支持的一种定时任务调度引擎,可以对现有的索引内容进行监控,告警支持告警记录、发送通知、通知第三方(webhook)。架构图如下:
watcher 告警示例
告警示例是针对日志索引中error日志出现的频次做的告警,watcher 告警示例创建语句如下:
PUT _watcher/watch/123456
{
"trigger": {
"schedule": {
"interval": "1m" ## 执行任务周期
}
},
"input": { ## watcher检测的内容的输入
"search": {
"request": { ## es中DLS语句
"body": {
"size": 0,
"query": {
"bool": {
"filter": {
"range": {
"@timestamp": {
"gte": "{{ctx.trigger.scheduled_time}}||-50m", ## 从定时任务触发开始的前50分钟
"lte": "{{ctx.trigger.scheduled_time}}",
"format": "strict_date_optional_time||epoch_millis"
}
}
},
"must": [{"term":{"loglevel":"ERROR"}}]
}
}
},
"indices": [
"<paas-cloud-log-consumer-staging-{now/d}>" ## 索引表达式 ,最新一天日志索引
]
}
}
},
"condition" : {
"compare" : { "ctx.payload.hits.total" : { "gt" : 5 }} ## DLS 查询语句命中的数据数大于5,ctx.payload.hits.total watcher执行器上下文,请参考wathcer官网文档
},
"actions" : {
"my_webhook" : {
"throttle_period" : "5m", ## 静默时间 ,静默时间最好是需要跟 执行周期匹配,避免无效的执行
"webhook" : {
"method" : "POST",
"scheme" : "http",
"host" : "host",
"port" : 8008,
"path": "/robot/send",
"headers" : {
"Content-Type" : "application/json"
},
"params" : {
"access_token" : "" ## 机器人id
},
"body" : """{ ## 钉钉认证方式 采用的是关键字认证,需要在内容中带机器人配置的关键字
"msgtype": "markdown",
"markdown": { ## 参考钉钉机器人api文档
"title":"bdtp alert",
"text": "##log alert \n #### business alert @18708450002 \n - content:log error of paas-cloud-log-consumer-staging service happended 5 time last 5 minutes \n - business owner:机器人\n"
},
"at": {
"atMobiles": [
"152222222222"
],
"atUserIds": [
"user123"
],
"isAtAll": false
}
}"""
}
}
}
}
通过kibana可视化界面配置,然后查看watcher定时任务的执行情况如下图:
第一步:找到下图的功能导航栏中:
第二步:进入Stack Management界面以后:
第三步:选中对应watcher 对应的id:
示例告警结果展示: