watch告警参考
{
"trigger": {
"schedule": {
# 检查频率
"interval": "60s"
}
},
"input": {
"search": {
"request": {
"search_type": "query_then_fetch",
"indices": [
# 检查的索引,{now/d}为当天的日志格式:例如:2019.01.29
"<prod-planet-match-bj-{now/d}>"
],
"types": [],
"body": {
"query": {
"bool": {
"must": [
{
"range": {
"@timestamp": {
# 查询近一分钟内的数据,这个值一般和检查频率相同
"gte": "now-1m",
"lte": "now"
}
}
},
{
"query_string": {
"default_field": "message",
# 查询条件
"query": "ERROR"
}
}
],
# 过滤条件
"must_not": [
{
"query_string": {
"default_field": "message",
"query": "has no valid user in shell"
}
}
]
}
}
}
}
}
},
"condition": {
"compare": {
"ctx.payload.hits.total": {
# 报警数量阀值
"gte": 1
}
}
},
"actions": {
# 执行操作,这里为发送邮件。
"send_email": {
"email": {
"profile": "standard",
"attachments": {
"data.txt": {
"data": {
"format": "json"
}
}
},
"to": [
# 一般只需要修改收件人信息。其他不需要修改。
"948793841@qq.com:
],
# 这里引用 watch id
"subject": "{{ ctx.watch_id }} 报警",
"body": {
"text": "报错日志条数: {{ctx.payload.hits.total}}\n 日志路径{{ ctx.payload.hits.hits.0._source.source }}\n 实例ID:{{ ctx.payload.hits.hits.0._source.meta.cloud.instance_id }}\n 报错日志:\n{{#ctx.payload.hits.hits}}{{_source.message}}\n{{/ctx.payload.hits.hits}}"
}
}
}
}
}
参考文档:
模版语法:http://www.cnblogs.com/DF-fzh/p/5979093.html
引用变量参考:https://www.elastic.co/guide/en/watcher/2.4/condition.html
watcher配置参考:https://www.elastic.co/guide/en/x-pack/current/xpack-alerting.html