1. Xpack-Watchs基本使用:

首先看一个watcher的例子:

  1. ## 创建一个watcher,比如定义一个trigger 每个10s钟看一下input里的数据
  2. PUT _xpack/watcher/watch/school_watcher
  3. {
  4. "trigger": {
  5. "schedule": {
  6. "interval": "10s"
  7. }
  8. },
  9. ## 查看任务信息
  10. "input": {
  11. "search": {
  12. "request": {
  13. ## 监控具体索引
  14. "indices": ["school*"],
  15. ## body里面具体些搜索语句
  16. "body": {
  17. "size": 0,
  18. "query": {
  19. "match": {
  20. ## 比如索引里面name 有 hello 则进行报警
  21. "name": "hello"
  22. }
  23. }
  24. }
  25. }
  26. }
  27. },
  28. ## 对于上面的查询结果进行比较:
  29. "condition": {
  30. ## compare进行比较
  31. "compare": {
  32. ## 上面的query查询的结果会放入到ctx.payload中:
  33. ## 比如获取 ctx.payload.hits.total ctx.payload._shards.total 等等
  34. "ctx.payload.hits.total": {
  35. "gt": 0
  36. }
  37. }
  38. },
  39. ## transform作用:重新查询出文档内容赋值给ctx.payload
  40. "transform": {
  41. "search": {
  42. "request": {
  43. "indices": ["school*"],
  44. "body": {
  45. "size": 10,
  46. "query": {
  47. "match": {
  48. "name": "hello"
  49. }
  50. }
  51. }
  52. }
  53. }
  54. },
  55. ## 根据上面的查询、比较结果,执行actions里面定义的动作(定义多种报警类型)
  56. "actions": {
  57. ## 报警名字
  58. "log_hello": {
  59. ## 防止报警风暴: 设置阈值 15m内曾经报警过, 则不报警
  60. "throttle_period": "15m",
  61. ## 报警方式:logging、mail、http等
  62. "logging": {
  63. ## 报警具体内容:使用 {{ 查询参数 }} 进行赋值:
  64. "text": "Found {{ctx.payload.hits.total}} hello in the school"
  65. }
  66. }
  67. }
  68. }
  1. ctx.payload取值规范:

比如我们进行search搜索school里面name=zhangsan的数据:

  1. ## payload取值规范:比如我们进行search搜索school:
  2. GET school/_search
  3. {
  4. "query": {
  5. "match": {
  6. "name": "zhangsan"
  7. }
  8. }
  9. }

查询结果如下:

  1. {
  2. "took": 14,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 2,
  6. "successful": 2,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 1,
  12. "max_score": 1.5404451,
  13. "hits": [
  14. {
  15. "_index": "school",
  16. "_type": "student",
  17. "_id": "1",
  18. "_score": 1.5404451,
  19. "_source": {
  20. "name": "zhangsan",
  21. "age": 25,
  22. "course": "elasticsearch",
  23. "study_date": "2018-06-15T20:30:50",
  24. "mark": "today is a good day"
  25. }
  26. }
  27. ]
  28. }
  29. }

表示查询:ctx.payload结果集:

  1. ## 表示查询:ctx.payload结果集:
  2. {{#ctx.payload.hits.hits}} {{_source.name}} {{_source.course}} {{/ctx.payload.hits.hits}}

比如我们进行search搜索school并采用聚合的方式来查询terms course数据:

  1. GET school/_search
  2. {
  3. "size": 0,
  4. "aggs": {
  5. "myterms": {
  6. "terms": {
  7. "field": "course",
  8. "size": 10
  9. }
  10. }
  11. }
  12. }

查询结果:

  1. {
  2. "took": 11,
  3. "timed_out": false,
  4. "_shards": {
  5. "total": 2,
  6. "successful": 2,
  7. "skipped": 0,
  8. "failed": 0
  9. },
  10. "hits": {
  11. "total": 10,
  12. "max_score": 0,
  13. "hits": []
  14. },
  15. "aggregations": {
  16. "myterms": {
  17. "doc_count_error_upper_bound": 0,
  18. "sum_other_doc_count": 0,
  19. "buckets": [
  20. {
  21. "key": "elasticsearch",
  22. "doc_count": 7
  23. },
  24. {
  25. "key": "good",
  26. "doc_count": 1
  27. },
  28. {
  29. "key": "spring",
  30. "doc_count": 1
  31. },
  32. {
  33. "key": "spring elasticsearch",
  34. "doc_count": 1
  35. }
  36. ]
  37. }
  38. }
  39. }

payload取值使用:现在想取得上面的hits.hits里面的数据内容,就可以使用如下方式:

  1. ## 表示查询:ctx.payload结果集:
  2. {{#ctx.payload.aggregations.aggsname.buckets}} {{key}} {{doc_count}} {{/ctx.payload.aggregations.aggsname.buckets}}
  3. ## 针对这里内容就是:
  4. {{#ctx.payload.aggregations.myterms.buckets}} {{key}} {{doc_count}} {{/ctx.payload.aggregations.myterms.buckets}}
  1. watcher API使用: ```shell

    查看一个watcher

    GET _xpack/watcher/watch/school_watcher

删除一个watcher

DELETE _xpack/watcher/watch/school_watcher

执行watcher

POST _xpack/watcher/watch/school_watcher/_execute

查看执行结果

GET /.watcher-history*/_search?pretty { “sort” : [ { “result.execution_time” : “desc” } ], “query”: { “match”: { “watch_id”: “school_watcher” } } }

  1. 4. triggers的几种类型
  2. ```shell
  3. #--------------------triggers的几种类型--------------------
  4. #hourly、daily、weekly、monthly、yearly、cron、interval
  5. #hourly:每小时执行
  6. #例如:12:00, 12:15, 12:30, 12:45, 1:00, 1:15
  7. {
  8. "trigger" : {
  9. "schedule" : {
  10. "hourly" : { "minute" : [ 0, 15, 30, 45 ] }
  11. }
  12. }
  13. }
  14. #daily:每天执行
  15. #每天00:00, 12:00, and 17:00
  16. {
  17. "trigger" : {
  18. "schedule" : {
  19. "daily" : { "at" : [ "midnight", "noon", "17:00" ] }
  20. }
  21. }
  22. }
  23. #每天00:00, 00:30, 12:00, 12:30, 17:00 and 17:30
  24. {
  25. "trigger" : {
  26. "schedule" : {
  27. "daily" : {
  28. "at" {
  29. "hour" : [ 0, 12, 17 ],
  30. "minute" : [0, 30]
  31. }
  32. }
  33. }
  34. }
  35. }
  36. #weekly:指定星期几
  37. #周二12:00,周五17:00
  38. {
  39. "trigger" : {
  40. "schedule" : {
  41. "weekly" : [
  42. { "on" : "tuesday", "at" : "noon" },
  43. { "on" : "friday", "at" : "17:00" }
  44. ]
  45. }
  46. }
  47. }
  48. #周二、周五的17:00
  49. {
  50. "trigger" : {
  51. "schedule" : {
  52. "weekly" : {
  53. "on" : [ "tuesday", "friday" ],
  54. "at" : [ "noon", "17:00" ]
  55. }
  56. }
  57. }
  58. }
  59. #monthly:指定每月哪天执行
  60. #每月10号中午、每月20号17:00
  61. {
  62. "trigger" : {
  63. "schedule" : {
  64. "monthly" : [
  65. { "on" : 10, "at" : "noon" },
  66. { "on" : 20, "at" : "17:00" }
  67. ]
  68. }
  69. }
  70. }
  71. #每月10号、20号的00:00,12:00
  72. {
  73. "trigger" : {
  74. "schedule" : {
  75. "monthly" : {
  76. "on" : [ 10, 20 ],
  77. "at" : [ "midnight", "noon" ]
  78. }
  79. }
  80. }
  81. }
  82. #yearly-指定月、日、时
  83. #每年的1月10日12:00,每年的7月20日17:00
  84. {
  85. "trigger" : {
  86. "schedule" : {
  87. "yearly" : [
  88. { "in" : "january", "on" : 10, "at" : "noon" },
  89. { "in" : "july", "on" : 20, "at" : "17:00" }
  90. ]
  91. }
  92. }
  93. }
  94. #每年1月10日,1月20日,12月10日,12月20日的12:00,00:00
  95. {
  96. "trigger" : {
  97. "schedule" : {
  98. "yearly" : {
  99. "in" : [ "jan", "dec" ],
  100. "on" : [ 10, 20 ],
  101. "at" : [ "midnight", "noon" ]
  102. }
  103. }
  104. }
  105. }
  106. #cron-表达式
  107. <seconds> <minutes> <hours> <day_of_month> <month> <day_of_week> [year]
  108. 0 5 9 * * ?
  109. 0 0-5 9 * * ?
  110. 0 0/15 9 * * ?
  111. #interval-周期的
  112. #间隔单位:s:秒、m:分钟、h:小时、d:天、w:星期
  1. input的几种类型:
    ```shell

    ——————————Inputs的几种类型——————————

    Simple、Search、HTTP、Chain

    Simple Input-静态数据

    每天12点触发

    { “trigger” : {

    1. "schedule" : {
    2. "daily" : { "at" : "noon" }
    3. }

    }, “input” : {

    1. "simple" : {
    2. "name" : "John"
    3. }

    }, “actions” : {

    1. "reminder_email" : {
    2. "email" : {
    3. "to" : "to@host.domain",
    4. "subject" : "Reminder",
    5. "body" : "Dear {{ctx.payload.name}}, by the time you read these lines, I'll be gone"
    6. }
    7. }

    } }

Search-搜索

{ “input” : { “search” : { “request” : { “indices” : [ “logs” ], “body” : { “query” : { “match_all” : {} } } } } }, “condition” : { “compare” : { “ctx.payload.hits.total” : { “gt” : 5 }} } … }

Http-请求

request.host

request.port

request.path

request.headers

request.params

request.url:request.scheme, request.host, request.port and request.params

request.method:head、get、post、put、delete

request.auth

request.body

request.proxy.host

request.proxy.port

request.connection_timeout

request.read_timeout

response_content_type:json, yaml and text

extract

get请求

{ “input” : { “http” : { “request” : { “host” : “example.com”, “port” : 9200, “path” : “/idx/_search” } } } }

含有body体内容

{ “input” : { “http” : { “request” : { “host” : “host.domain”, “port” : 9200, “path” : “/idx/_search”, “body” : “{\”query\” : { \”match\” : { \”category\” : \”event\”}}}” } } } }

含有参数的

{ “input” : { “http” : { “request” : { “host” : “host.domain”, “port” : “9200”, “path” : “/_cluster/stats”, “params” : { “human” : “true” } } } } }

含有用户密码

{ “input” : { “http” : { “request” : { “host” : “host.domain”, “port” : “9200”, “path” : “/myservice”, “auth” : { “basic” : { “username” : “user”, “password” : “pass” } } } } } }

直接请求url的

{ “input” : { “http” : { “request” : { “url” : “http://api.openweathermap.org/data/2.5/weather“, “params” : { “lat” : “52.374031”, “lon” : “4.88969”, “appid” : ““ } } } } }

Chain-input-同时设置多个input,串行

{ “input” : { “chain” : { “inputs” : [

  1. ## 第一步input
  2. {
  3. "first" : {
  4. "simple" : { "path" : "/_search" }
  5. }
  6. },
  7. ## 第二步input (可以去使用第一步input返回的结果)
  8. {
  9. "second" : {
  10. "http" : {
  11. "request" : {
  12. "host" : "localhost",
  13. "port" : 9200,
  14. "path" : "{{ctx.payload.first.path}}"
  15. }
  16. }
  17. }
  18. }
  19. ]
  20. }
  21. }

}

  1. 6. condition条件设置:如果condition条件返回true 则会触发action 如果返回 false 则就停止,不执行action
  2. ```shell
  3. #--------------------条件设置--------------------
  4. #Always Condition
  5. "condition" : {
  6. "always" : {}
  7. }
  8. #Never Condition
  9. "condition" : {
  10. "never" : {}
  11. }
  12. #Compare Condition (进行和查询的结果进行比较语法如下:)
  13. # eq:、not_eq、gt、gte、lt、lte
  14. ## 比如错误条数超过了5条进行报警、响应长时间超过多少毫秒进行报警等
  15. {
  16. "condition" : {
  17. "compare" : {
  18. "ctx.payload.hits.total" : {
  19. "gte" : 5
  20. }
  21. }
  22. }
  23. #<{expression}> 正则表达式 使用 <> 中写正则表达式: 比如 当前时间 - 5分钟 进行比较,如下:
  24. {
  25. "condition" : {
  26. "compare" : {
  27. "ctx.execution_time" : {
  28. "gte" : "<{now-5m}>"
  29. }
  30. }
  31. }
  32. #{{path}} 比较,这个就是最开始的示例里面的获取参数方式,如下:
  33. {
  34. "condition" : {
  35. "compare" : {
  36. "ctx.payload.aggregations.status.buckets.error.doc_count" : {
  37. "not_eq" : "{{ctx.payload.aggregations.handled.buckets.true.doc_count}}"
  38. }
  39. }
  40. }
  41. #Array Compare Condition 数组比较: 比如当前的doc_count大于25 就进行报警
  42. {
  43. "condition": {
  44. "array_compare": {
  45. "ctx.payload.aggregations.top_tweeters.buckets" : {
  46. "path": "doc_count" ,
  47. "gte": {
  48. "value": 25,
  49. }
  50. }
  51. }
  52. }
  53. }
  54. #Script Condition 脚本方式
  55. {
  56. "input" : {
  57. "search" : {
  58. "indices" : "log-events",
  59. "body" : {
  60. "size" : 0,
  61. "query" : { "match" : { "status" : "error" } }
  62. }
  63. }
  64. },
  65. "condition" : {
  66. "script" : {
  67. ## 当前返回的条数是否大于阈值,进行报警
  68. "inline" : "return ctx.payload.hits.total > threshold",
  69. "params" : {
  70. "threshold" : 5
  71. }
  72. }
  73. }
  74. }
  1. Action 触发器 ```shell

    ——————————Actions——————————

    Email Action—发送邮件

如果使用发送邮件的报警,则需要在elasticsearch.yml中配置发送邮件服务的信息

xpack.notification.email: default_account: gmail_account account: gmail_account: profile: gmail smtp: auth: true starttls.enable: true host: smtp.gmail.com port: 587 user: password: outlook_account: profile: outlook smtp: auth: true starttls.enable: true host: smtp-mail.outlook.com port: 587 user: password: : exchange_account: profile: outlook email_defaults: from: smtp: auth: true starttls.enable: true host: port: 587 user: password:

发送邮件

“actions” : {

  1. ## actions名字
  2. "send_email" : {
  3. "email" : {
  4. "to" : "'Recipient Name <recipient@example.com>'",
  5. #"to" : ['Personal Name <user1@host.domain>', 'user2@host.domain'],
  6. "subject" : "Watcher Notification",
  7. "body" : "{{ctx.payload.hits.total}} error logs found"
  8. }
  9. }

}

发送含有附件信息的邮件

“actions” : { “email_admin” : { “email”: { “to”: “‘John Doe john.doe@example.com‘“, “attachments” : {

  1. ## 附件方式
  2. "my_image.png" : {
  3. "http" : {
  4. "content_type" : "image.png",
  5. "request" : {
  6. "url": "http://example.org/foo/my-image.png"
  7. }
  8. }
  9. },
  10. ## xpack reporting插件生成方式:
  11. "dashboard.pdf" : {
  12. "reporting" : {
  13. "url": "http://example.org:5601/api/reporting/generate/dashboard/Error-Monitoring"
  14. }
  15. },
  16. ## 自定义附件
  17. "data.yml" : {
  18. "data" : {
  19. "format" : "yaml"
  20. }
  21. }
  22. }
  23. }
  24. }

}

Webhook Action,发送一个http请求

发送github的issue

“actions” : { “create_github_issue” : {

  1. ## 因为发邮件到达率不是特别高,所以可以使用外部的接口调用方式
  2. ## 比如这里调用url为外部的手机短信接口进行发送
  3. "webhook" : {
  4. ## 请求方式
  5. "method" : "POST",
  6. ## 外部请求地址
  7. "url" : "https://api.github.com/repos/<owner>/<repo>/issues",
  8. ## 请求报文
  9. "body" : "{
  10. \"title\": \"Found errors in 'contact.html'\",
  11. \"body\": \"Found {{ctx.payload.hits.total}} errors in the last 5 minutes\",
  12. \"assignee\": \"web-admin\",
  13. \"labels\": [ \"bug\", \"sev2\" ]
  14. }",
  15. ## 用户名密码
  16. "auth" : {
  17. "basic" : {
  18. "username" : "<username>",
  19. "password" : "<password>"
  20. }
  21. }
  22. }
  23. }

}

带有url参数的请求

“actions” : { “my_webhook” : { “webhook” : { “method” : “POST”, “host” : “mylisteningserver”, “port” : 9200, “path”: “:/alert”, “params” : { “watch_id” : “{{ctx.watch_id}}” } } } }

自定义header

“actions” : { “my_webhook” : { “webhook” : { “method” : “POST”, “host” : “mylisteningserver”, “port” : 9200, “path”: “:/alert/{{ctx.watch_id}}”, “headers” : { “Content-Type” : “application/yaml” }, “body” : “count: {{ctx.payload.hits.total}}” } } }

Index Action—创建索引文档

“actions” : { “index_payload” : { “index” : { “index” : “my-index”, “doc_type” : “my-type”, “doc_id”: “my-id” } } }

Logging Action—记录日志

level:error, warn, info, debug and trace

日志种类:

category:xpack.watcher.actions.logging

“actions” : { “log” : { “transform” : { … },

  1. ## 日志报警
  2. "logging" : {
  3. "text" : "executed at {{ctx.execution_time}}",
  4. ## 日志级别
  5. "level": "info"
  6. }
  7. }

}

Jira Action 与jira集成

HipChat Action

Slack Action

PagerDuty Action

  1. 8. 使用接口的形式创建一个watcher, 进行模拟:
  2. 1. watcher脚本:
  3. ```shell
  4. ## 查询school
  5. GET school/student/_search
  6. {
  7. "query": {
  8. "match_all":{}
  9. }
  10. }
  11. ## 创建school_watcher
  12. PUT _xpack/watcher/watch/school_watcher
  13. {
  14. "trigger": {
  15. "schedule": {
  16. "interval": "10s"
  17. }
  18. },
  19. "input": {
  20. "search": {
  21. "request": {
  22. "indices": ["school*"],
  23. "body": {
  24. "size": 0,
  25. "query": {
  26. "match": {
  27. "name": "hello"
  28. }
  29. }
  30. }
  31. }
  32. }
  33. },
  34. "condition": {
  35. "compare": {
  36. "ctx.payload.hits.total": {
  37. "gt": 0
  38. }
  39. }
  40. },
  41. "transform": {
  42. "search": {
  43. "request": {
  44. "indices": ["school*"],
  45. "body": {
  46. "size": 10,
  47. "query": {
  48. "match": {
  49. "name": "hello"
  50. }
  51. }
  52. }
  53. }
  54. }
  55. },
  56. "actions": {
  57. "log_hello": {
  58. "throttle_period": "15m",
  59. "logging": {
  60. "text": "Found {{ctx.payload.hits.total}} hello in the school"
  61. }
  62. }
  63. }
  64. }
  65. ## 查看watcher执行结果
  66. GET /.watcher-history*/_search?pretty
  67. {
  68. "sort" : [
  69. { "result.execution_time" : "desc" }
  70. ],
  71. "query": {
  72. "match": {
  73. "watch_id": "school_watcher"
  74. }
  75. }
  76. }
  77. ## 进行数据测试:
  78. POST /school/student
  79. {
  80. "name": "hello",
  81. "age": 18,
  82. "course": "elasticsearch",
  83. "study_date": "2018-08-20T20:30:50",
  84. "mark": "take care day day"
  85. }
  1. 可视化操作watcher,可以启用、禁用、添加修改、删除watcher
    1. ## watch使用文章:https://www.cnblogs.com/reboot51/p/8328720.html

    watcher使用:

    ```shell

    创建一个watcher,比如定义一个trigger 每个10s钟看一下input里的数据

    PUT _xpack/watcher/watch/applog_error_watcher { “trigger”: { “schedule”: { “interval”: “10s” } }, “input”: { “search”: { “request”: { “indices”: [“javalog-app-*”], “body”: {
    1. "size": 0,
    2. "query": {
    3. "match": {
    4. "level": "ERROR"
    5. }
    6. }
    } } } }, “condition”: { “compare”: { “ctx.payload.hits.total”: { “gt”: 0 } } }, “transform”: { “search”: { “request”: { “indices”: [“javalog-app-*”], “body”: {
    1. "size": 10,
    2. "query": {
    3. "match": {
    4. "name": "hello"
    5. }
    6. }
    } } } }, “actions”: { “test_error”: { “throttle_period”: “1m”, “webhook” : { “method” : “POST”, “url” : “http://192.168.11.32:8001/watch“, “body” : “{ \”title\”: \”异常错误告警\”, \”traceId\”: \”{{#ctx.payload.hits.hits}} {{_source.traceId}} {{/ctx.payload.hits.hits}}\”, \”spanId\” : \”{{#ctx.payload.hits.hits}} {{_source.spanId}} {{/ctx.payload.hits.hits}}\”, \”level\”:\”告警级别P1\”, \”body\”: \”{{#ctx.payload.hits.hits}} {{_source.messageInfo}} {{/ctx.payload.hits.hits}}\” } } } } }

{{#ctx.payload.hits.hits}} {{_source.traceId}} {{/ctx.payload.hits.hits}}

{{#ctx.payload.hits.hits}} {{_source.spanId}} {{/ctx.payload.hits.hits}}

{{#ctx.payload.hits.hits}} {{_source.messageInfo}} {{/ctx.payload.hits.hits}}

查询error

GET javalog-app-2019.01.24/_search { “query”: { “match”: { “level.keyword”: “ERROR” } } }

查看一个watcher

GET _xpack/watcher/watch/applog_error_watcher

删除一个watcher

DELETE _xpack/watcher/watch/applog_error_watcher

执行watcher

POST _xpack/watcher/watch/applog_error_watcher/_execute

查看执行结果

GET /.watcher-history*/_search?pretty { “sort” : [ { “result.execution_time” : “desc” } ], “query”: { “match”: { “watch_id”: “applog_error_watcher” } } }

```