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服務器