ElasticSearch预警服务-Watcher详解-Condition设置 - 猫猫得天空♂

/ 0评 / 0

ElasticSearch预警服务-Watcher详解-Condition设置

Condition用于在Watcher触发后决定是否去执行Action动作.
目前支持的类型有4种,分别是always,never,script,compare.
如果没有进行设置,则默认为always类型。
当Condition执行时,可以访问Context中的数据,即Script和Compare可以使用其中的数据来决定是否满足条件。

1.Always Condition
设置为Always后,Action动作总会执行除非遇到throttled(节流)的情况.

  1. PUT _watcher/watch/my-watch
  2. {
  3. ...
  4. "condition" : {
  5. "always" : {}
  6. }
  7. ...
  8. }

2.Never Condition
设置为Never后,Action动作永远不会执行.

  1. PUT _watcher/watch/my-watch
  2. {
  3. ...
  4. "condition" : {
  5. "never" : {}
  6. }
  7. ...
  8. }

 
3.Script Condition
默认脚本语言是Groovy。这里可以设置ElasticSearch支持的所有编程语言。

  1. {
  2. ...
  3. "condition" : {
  4. "script" : "return true" #默认Groovy
  5. }
  6. ...
  7. }

3.1.InLineScript

  1. {
  2. ...
  3. "condition" : {
  4. "script" : {
  5. "inline" : "return true";
  6. }
  7. }
  8. ...
  9. }

带参数的设置

  1. {
  2. ...
  3. "condition" : {
  4. "script" : {
  5. "inline" : "return result";
  6. "lang" : "groovy",
  7. "params" : {
  8. "result" : true #参数设置
  9. }
  10. }
  11. }
  12. ...
  13. }

3.2.File Script 使用脚本文件,脚本文件必须放置到config/scripts目录下

  1. {
  2. ...
  3. "condition" : {
  4. "script" : {
  5. "file" : "my_script";
  6. }
  7. }
  8. ...
  9. }
  10. 使用脚本文件并带参数设置
  11. {
  12. ...
  13. "condition" : {
  14. "script" : {
  15. "file" : "my_script";
  16. "lang" : "groovy",
  17. "params" : {
  18. "result" : true
  19. }
  20. }
  21. }
  22. ...
  23. }

3.3.Indexed  Scripts

  1. {
  2. ...
  3. "condition" : {
  4. "script" : {
  5. "id" : "my_script"
  6. }
  7. }
  8. ...
  9. }
  10. 使用索引脚本并带参数设置
  11. {
  12. ...
  13. "condition" : {
  14. "script" : {
  15. "id" : "my_script",
  16. "lang" : "javascript",
  17. "params" : {"color": "red"}
  18. }
  19. }
  20. ...
  21. }

3.4使用Script获取Context中的数据

  1. {
  2. "input" : {
  3. "search" : {
  4. "search_type" : "count",
  5. "indices" : "log-events",
  6. "body" : {
  7. "query" : { "match" : { "status" : "error" } }
  8. }
  9. }
  10. },
  11. "condition" : {
  12. "script" : {#获取命中数,并判断大于节流阀值
  13. "script" : "return ctx.payload.hits.total > threshold",
  14. "params" : {
  15. "threshold" : 5
  16. }
  17. }
  18. }
  19. ...
  20. }

4.Compare Condition

通过与Watcher Context中的模型数据进行比较,即可以使用eq,not_eq,gt,gte,lt,lte等判断条件。

  1. {
  2. ...
  3. "condition" : {
  4. "compare" : {
  5. "ctx.payload.hits.total" : {
  6. "gte" : 5
  7. }
  8. }
  9. ...
  10. }

对于日期格式的数据,可以使用表达式。

  1. {
  2. ...
  3. "condition" : {
  4. "compare" : {
  5. "ctx.execution_time" : {
  6. "gte" : "<{now-5m}>"
  7. }
  8. }
  9. ...
  10. }

#Context中数据进行比较

  1. {
  2. ...
  3. "condition" : {
  4. "compare" : {
  5. "ctx.payload.aggregations.status.buckets.error.doc_count" : {
  6. "not_eq" : "{{ctx.payload.aggregations.handled.buckets.true.doc_count}}"
  7. }
  8. }
  9. ...
  10. }

 

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注