ElasticSearch预警服务-Watcher安装指南及案例介绍

本文将介绍如何安装Watcher服务,并介绍相关的功能特征。

1.Watcher服务的安装

    目前Watcher服务处于Beta阶段,使用者可以申请最新的beta-key,然后通过插件的方式进行安装

 

Java代码   收藏代码
  1. bin/plugin -i elasticsearch/watcher/<watcher-beta-key>  

 

 2.验证服务

  

Java代码   收藏代码
  1. curl -XGET 'http://192.168.100.72:9200/_watcher/stats?pretty'  
  2. 或者直接通过浏览器访问也是可以的  

3.版本要求

   Watcher需要ElasticSearchV1.5.0+版本

 

案例介绍一:

扫描二维码关注公众号,回复: 537540 查看本文章

   监控错误数据案例,每10秒搜索一次数据,发现错误后,记录一条错误记录。

 

   配置流程:

   1.设置定时器和输入源(错误数据的查询条件)

   2.设置触发条件(是否查询到了错误数据)

   3.设置触发动作(发现错误后执行Action)

 

   设置详情:

   1.每10秒搜索一次日志数据  

Java代码   收藏代码
  1. curl -XPUT 'http://192.168.100.72:9200/_watcher/watch/log_error_watch' -d '{  
  2.   "trigger" : {  
  3.     "schedule" : { "interval" : "10s" }   
  4.   },  
  5.   "input" : {  
  6.     "search" : {  
  7.       "request" : {  
  8.         "indices" : [ "logs" ],  
  9.         "body" : {  
  10.           "match" : { "message""error" }  
  11.         }  
  12.       }  
  13.     }  
  14.   }  
  15. }'  

 

   2.数据加载情况历史记录  

Java代码   收藏代码
  1. curl -XGET 'http://192.168.100.72:9200/.watch_history*/_search?pretty' -d '{  
  2.   "sort" : [  
  3.     { "execution_result.execution_time" : "desc" }  
  4.   ]  
  5. }'  

    从返回结果中可以看到每10秒触发一次查询操作,如果历史记录中无数据,表示没有搜索到数据。

   3.如何设置条件

Java代码   收藏代码
  1. curl -XPUT 'http://192.168.100.72:9200/_watcher/watch/log_error_watch' -d '{  
  2.   "trigger" : { "schedule" : { "interval" : "10s" } },  
  3.   "input" : {  
  4.     "search" : {  
  5.       "request" : {  
  6.         "indices" : [ "logs" ],  
  7.         "body" : {  
  8.           "query" : {  
  9.             "match" : { "message""error" }  
  10.           }  
  11.         }  
  12.       }  
  13.     }  
  14.   },  
  15.   "condition" : { #设置条件,结果数据是否大于0!即是否查询到了数据  
  16.     "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}   
  17.   }  
  18. }'  

 

  3.查询触发条件执行状态 

Java代码   收藏代码
  1. #查询Watcher的历史记录,验证触发条件是否被触发了  
  2. curl -XGET 'http://192.168.100.72:9200/.watch_history*/_search?pretty' -d '{  
  3.   "query" : {  
  4.     "bool" : {  
  5.       "must" : [  
  6.         { "match" : { "execution_result.condition.met" : true }},  
  7.         { "range" : { "execution_result.execution_time" : { "from" : "now-10s"}}}  
  8.       ]  
  9.     }  
  10.   }  
  11. }'  

   4.设置动作

  

Java代码   收藏代码
  1. curl -XPUT 'http://192.168.100.72:9200/_watcher/watch/log_error_watch' -d '{  
  2.   "trigger" : { "schedule" : { "interval" : "10s" } },  
  3.   "input" : {  
  4.     "search" : {  
  5.       "request" : {  
  6.         "indices" : [ "logs" ],  
  7.         "body" : {  
  8.           "query" : {  
  9.             "match" : { "message""error" }  
  10.           }  
  11.         }  
  12.       }  
  13.     }  
  14.   },  
  15.   "condition" : {  
  16.     "compare" : { "ctx.payload.hits.total" : { "gt" : 0 }}  
  17.   },  
  18.   "actions" : { #触发动作,这里可以使 记录日志,发送邮件,发送到第三方的WebHook等等。  
  19.     "log_error" : {  
  20.       "logging" : {  
  21.         "text" : "Found {{ctx.payload.hits.total}} errors in the logs"  
  22.       }  
  23.     }  
  24.   }  
  25. }'  

 

 

案例介绍二:

    监控ElasticSearch集群状态:每10秒检测一次集群状态,如果集群状态错误,则发送邮件给运维

    配置流程就不过多介绍了,直接来结果。

  

Java代码   收藏代码
  1. curl -XPUT 'http://192.168.100.72:9200/_watcher/watch/cluster_health_watch' -d '{  
  2.   "trigger" : {  
  3.     "schedule" : { "interval" : "10s" }  
  4.   },  
  5.   "input" : {  
  6.     "http" : {  
  7.       "request" : {  
  8.        "host" : "192.168.100.72",  
  9.        "port" : 9200,  
  10.        "path" : "_cluster/health"  
  11.       }  
  12.     }  
  13.   },  
  14.   "condition" : {  
  15.     "compare" : {  
  16.       "ctx.payload.status" : { "eq" : "red" }  
  17.     }  
  18.   },  
  19.   "actions" : {  
  20.     "send_email" : {  
  21.       "email" : {  
  22.         "to" : "[email protected]",  
  23.         "subject" : "ES Cluster Status Warning",  
  24.         "body" : "ES Cluster status is RED"  
  25.       }  
  26.     }  
  27.   }  
  28. }'  
  29. 请注意,如果配置邮件发送,需要在ElasticSearch配置文件中配置以下信息  
  30. watcher.actions.email.service.account:  
  31.   work:  
  32.     profile: gmail  
  33.     email_defaults:  
  34.       from: <email>   
  35.     smtp:  
  36.       auth: true  
  37.       starttls.enable: true  
  38.       host: smtp.gmail.com  
  39.       port: 587  
  40.       user: <username>   
  41.       password: <password>   

http://corejava2008.iteye.com/blog/2213102

猜你喜欢

转载自aoyouzi.iteye.com/blog/2213145