xuanhao 发表于 2017-5-21 10:04:23

6.ElasticSearch预警服务-Watcher详解-Transform设置

ElasticSearch预警服务-Watcher详解-Transform设置
Transform(数据转换),在Watcher服务的Action执行前执行。
可以执行Transform的位置有两次,分别是:
1.全局定义(Watch Level),将对所有的Watch动作生效
2.局部定义(Action Level),只对指定的Watch动作生效

设置案例:

{
"trigger" : { ...}
"input" : { ... },
"condition" : { ... },
"transform" : { #此处为全局的
"search" : {
"body" : { "query" : { "match_all" : {} } }
}
}
"actions" : {
"tyler_webhook": {
"transform" : { #次数为局部
"script" : "return ctx.payload.hits"
}
"webhook" : {
"host" : "host.domain",
"port" : 8089,
"path" : "/notify/{{ctx.watch_id}}"
}
}
]
...
}
目前支持的三种定义类型为search, script ,chain
1.Search Transform
执行一个查询,并将返回结果替换Context中的内容。
来看一下局部定义的Search Transform:

{
...
"transform" : {
"search" : {
"body" : { "query" : { "match_all" : {} } }
}
}
...
}
 #带参数定义

{
"transform" : {
"search" : {
"search_type" : "count", #执行Count操作
"indices" : [ "logstash-*" ], #查询所有以logstash开头的索引
"body" : {
"query" : {
"match" : { "priority" : "error"} #查询proiority为Error的数据
}
}
}
}
}
 #支持ES的全部搜索API格式...

{
"transform" : {
"search" : {
"search_type" : "count",
"index" : [ "logstash-*" ],
"type" : "event",
"body" : {
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"must" : [
{
"range" : {
"@timestamp" : {
"from" : "{{ctx.trigger.scheduled_time}}||-30s",
"to" : "{{ctx.trigger.triggered_time}}"
}
}
},
{
"query" : {
"match" : { "priority" : "error"}
}
}
]
}
}
}
}
}
}
}
}
 #支持 inline template查询

{
"transform" : {
"search" : {
"search_type" : "count",
"index" : [ "logstash-*" ],
"type" : "event",
"body" : {
"template" {
"inline" : {
"query" : {
"filtered" : {
"filter" : {
"bool" : {
"must" : [
{
"range" : {
"@timestamp" : {
"from" : "{{ctx.trigger.scheduled_time}}||-30s",
"to" : "{{ctx.trigger.triggered_time}}"
}
}
},
{
"query" : {
"match" : { "priority" : "{{priority}}"}
}
}
]
}
}
}
},
"params" : {
"priority" : "error"
}
}
}
}
}
}
}
2.Script Transform
需要注意的是,从ESv1.4.3开始, inline groovy scripts功能是默认关闭的,请注意开启
简单的脚本设置定义:

{...
"transform" : {
"script" : "return [ time : ctx.trigger.scheduled_time ]" #Groovy脚本
}
...
}
3.Chain Transform
链式数据转换,指可以指定多个Transform组成数据量,一个Transform的输出作为下一个Transfrom的输入.

{
...
"transform" : {
"chain" : [ #定义格式
{
"search" : {#搜索Transform,执行count查询
"search_type" : "count",
"indices" : [ "logstash-*" ],
"body" : {
"query" : {
"match" : { "priority" : "error"}
}
}
}
},
{ #脚本Transform,将count结果赋值给Error_count字段
"script" : "return [ error_count : ctx.payload.hits.total ]"
}
]
}
...
}
 
 
页: [1]
查看完整版本: 6.ElasticSearch预警服务-Watcher详解-Transform设置