es上的的Watcher示例


Watcher插件配置(創建預警任務)

watcher目前是沒有界面配置的,需要通過Resfulapi調用創建、管理、更新預警任務

創建一個Watcher任務的流程是怎樣的?

我們先來看下創建一個預警demo的api

復制代碼
curl -XPUT 'http://localhost:9200/_watcher/watch/log_error_watch' -d '
{ "trigger": { "schedule": { "interval": "1m" } }, "input": { "search": { "request": { "indices": [ "monitor_ticket_bargainfindermaxrq_201610" ], "body": { "query": { "bool": { "must": [ { "match": { "collectionName": "BargainFinderMaxRQ" } }, { "range": { "createTime": { "gt" : "now-1m" } } } ] } } } } } }, "condition": { "compare": { "ctx.payload.hits.total": { "gt": 20 } } }, "actions": { "send_email": { "email": { "to": "xxxxxx@xxx.com", "subject": "TICKET_BFM 過去一分鐘內錯誤發生次數超過20,請關注", "body": "TICKET_BFM 過去一分鐘內錯誤發生次數超過20,請關注" } } } }'
復制代碼

當然actions里面還支持模板,比如

復制代碼
"actions": {
        "send_email": {
            "email": {
                "to": "xxx@xxx.com",
                "subject": "TICKET_BFM 過去一分鐘內錯誤發生{{ctx.payload.hits.total}}次數超過20,請關注",
                "body": "TICKET_BFM:{{ctx.watch_id}}  過去一分鐘內錯誤發生{{ctx.payload.hits.total}}次數超過20,請關注 Happened at {{ctx.execution_time}}"
            }
        }
    }
復制代碼

從api的content我們可以看出,內容主要主體分成trigger(觸發設置),input(來源設置),condition(條件),actions(觸發動作設置)

這里我們設置觸發間隔時間為1m,來源是monitor_ticket_bargainfindermaxrq_201610索引塊,查詢條件是collectionName字段值是BargainFinderMaxRQ,以及時間范圍是過去1分鍾到現在,然后觸發條件是當數量大於20,動作設置的是發郵件

Action類型其實有4種,EMail(郵件),Webhook(第三方對接),Index(索引),Logging(日志記錄),相關配置可以參考下官方文檔

如果配置了發送郵件,那么需要在es的yml文件配置下郵件發送服務器

我這里設置的是公司的服務器

watcher.actions.email.service.account:
  work:
    profile: standard
    email_defaults:
      from: '<yourname>@xx.com'
    smtp:
      auth: true
      starttls.enable: true
      host: smtp.xx.com
      port: 25
      user: yourname@xx.com
      password: password

注:設置之后重啟下es服務器


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM