2.ElasticSearch预警服务-Watcher详解

Watcher的核心组成部分:
1.Trigger:决定何时触发,必须对每个Watcher指定
2.Input:将数据加载到Context中
3.Condition:根据条件决定师傅执行Action操作,如不设置,默认总是执行
4.Transform:可选传输设置:处理及数据Load过程设置
5.Actions:动作:零个或者多个执行动作设定

完整的案例:

PUT _watcher/watch/log_event_watch
{
  "metadata" : { #附加可选metedata数据
    "color" : "red"
  },
  "trigger" : { #设定一个时间计划,每5分钟执行一次
    "schedule" : {
      "interval" : "5m" # Watcher使用系统时间,请注意保持服务器之间的时间同步
    }
  },
  "input" : { # 输入条件设定
    "search" : {
      "request" : { #查询log-events索引中status字段为error的数据
        "search_type" : "count",
        "indices" : "log-events",
        "body" : {
          "query" : { "match" : { "status" : "error" } }
        }
      }
    }
  },
  "condition" : { #判断 错误数据是否大于5
    "script" : "return ctx.payload.hits.total > 5"
  },
  "throttle_period" : "30m", #该设定用于节流控制,简单理解为每30分钟只执行一次Action动作,避免发送大量邮件等.
                             #默认值为5秒
  "transform" : { #由于上面设置了30M发送一次,而我们5分钟执行一次查询,所有这里的设置用于更新查询结果 
    "search" : {
      "indices" : "log-events",
      "body" : {
        "query" : { "match" : { "status" : "error" } }
      }
    }
  },
  "actions" : { #此处设置了2个Action动作
    "my_webhook" : {
      "webhook" : {#发送数据到ES指定的索引中
        "method" : "POST",
        "host" : "mylisteninghost",
        "port" : 9200,
        "path" : "/{{watch_id}}",
        "body" : "Encountered {{ctx.payload.hits.total}} errors"
      }
    },
    "email_administrator" : {
      "email" : { #发送数据到邮件中、
        "to" : "[email protected]",
        "subject" : "Encountered {{ctx.payload.hits.total}} errors",
        "body" : "Too many error in the system, see attached data",
        "attach_data" : true,
        "priority" : "high"
      }
    }
  }
}

猜你喜欢

转载自corejava2008.iteye.com/blog/2213291