ElasticSearch预警服务-Watcher详解-Condition设置
Condition用于在Watcher触发后决定是否去执行Action动作.
目前支持的类型有4种,分别是always,never,script,compare.
如果没有进行设置,则默认为always类型。
当Condition执行时,可以访问Context中的数据,即Script和Compare可以使用其中的数据来决定是否满足条件。
1.Always Condition
设置为Always后,Action动作总会执行除非遇到throttled(节流)的情况.
PUT _watcher/watch/my-watch
{
...
"condition" : {
"always" : {}
}
...
}
2.Never Condition
设置为Never后,Action动作永远不会执行.
PUT _watcher/watch/my-watch
{
...
"condition" : {
"never" : {}
}
...
}
3.Script Condition
默认脚本语言是Groovy。这里可以设置ElasticSearch支持的所有编程语言。
{
...
"condition" : {
"script" : "return true" #默认Groovy
}
...
}
3.1.InLineScript
{
...
"condition" : {
"script" : {
"inline" : "return true";
}
}
...
}
带参数的设置
{
...
"condition" : {
"script" : {
"inline" : "return result";
"lang" : "groovy",
"params" : {
"result" : true #参数设置
}
}
}
...
}
3.2.File Script 使用脚本文件,脚本文件必须放置到config/scripts目录下
{
...
"condition" : {
"script" : {
"file" : "my_script";
}
}
...
}
使用脚本文件并带参数设置
{
...
"condition" : {
"script" : {
"file" : "my_script";
"lang" : "groovy",
"params" : {
"result" : true
}
}
}
...
}
3.3.Indexed Scripts
{
...
"condition" : {
"script" : {
"id" : "my_script"
}
}
...
}
使用索引脚本并带参数设置
{
...
"condition" : {
"script" : {
"id" : "my_script",
"lang" : "javascript",
"params" : {"color": "red"}
}
}
...
}
3.4使用Script获取Context中的数据
{
"input" : {
"search" : {
"search_type" : "count",
"indices" : "log-events",
"body" : {
"query" : { "match" : { "status" : "error" } }
}
}
},
"condition" : {
"script" : {#获取命中数,并判断大于节流阀值
"script" : "return ctx.payload.hits.total > threshold",
"params" : {
"threshold" : 5
}
}
}
...
}
4.Compare Condition
通过与Watcher Context中的模型数据进行比较,即可以使用eq,not_eq,gt,gte,lt,lte等判断条件。
{
...
"condition" : {
"compare" : {
"ctx.payload.hits.total" : {
"gte" : 5
}
}
...
}
对于日期格式的数据,可以使用表达式。
{
...
"condition" : {
"compare" : {
"ctx.execution_time" : {
"gte" : "<{now-5m}>"
}
}
...
}
#Context中数据进行比较
{
...
"condition" : {
"compare" : {
"ctx.payload.aggregations.status.buckets.error.doc_count" : {
"not_eq" : "{{ctx.payload.aggregations.handled.buckets.true.doc_count}}"
}
}
...
}