sentinl的監控&告警是經過watch實現的。html
執行開始的時候, watcher爲watch建立watch執行上下文. 執行上下文提供腳本和模板, 能夠訪問watch元數據、payload、wathcID、執行時間和觸發器.web
執行過程,watcher具體執行:npm
watcher支持基於時間和基於確認的限制, 能夠防止對同一個事件重複執行操做.默認狀況下, Watcher使用基於時間的限制. 還能夠在每一個操做或者在監控級別配置限制週期。api
定義watch的時候可使用scripts和templates. scripts和template能夠引用watch執行過程的context元素,包括 watch payload. 執行上下文定義的變量, script和template(parameter placeholders參數佔位符)能夠直接引用.數組
watcher使用Elastic search的腳本基礎體系,同時支持 inline腳本(inline Templates and Scripts)和stored(stored Templates and Scripts)app
如下代碼顯示了Watch Execution Context的基本結構:less
{ "ctx" : { "metadata" : { ... }, "payload" : { ... }, "watch_id" : "<id>", "execution_time" : "20150220T00:00:10Z", "trigger" : { "triggered_time" : "20150220T00:00:10Z", "scheduled_time" : "20150220T00:00:00Z" }, "vars" : { ... } }
可使用腳本定義 conditions和transforms. 默認腳本語言是painless.post
Elasticsearch5.0開始內置新的腳本語言painless.網站
腳本能夠引用監視執行上下文中的任何值或經過腳本參數顯式傳遞的值.atom
可使用模板爲watch定義動態內容. 執行的時候, 模板從監視執行上下文中提取數據. 例如, 可使用模板爲電子郵件填充主題字段, 其中數據存儲在payload中. 模板還能夠訪問經過模板參數顯示傳遞的值.
可使用 Mustache腳本語言指定模板.
要定義內聯模板或者腳本, 只須要直接在字段值中指定便可.
對於腳本,只須要將內聯腳本定義爲腳本字段的值便可.
若是存儲模板和腳本, 則能夠經過id引用它們.
要使用已經存儲的模板和腳本, 定義的時候須要經過id字段顯示指定id. 例如,下文直接引用id=email_notification_subject 的模板.
{ ... "actions" : { "email_notification" : { "email" : { "subject" : { "id" : "email_notification_subject", "params" : { "color" : "red" } } } } } }
conditions、transforms、actions能夠經過ctx訪問搜索結果,例如:
轉換處理並更改ctx中的有效內容,以便爲監控操做作好準備. 若是在transform處理後 payload爲空,則不執行任何操做.
在集羣上執行搜索,並使用返回的搜索響應替換watch執行上下文中的當前有效內容.
對當前有效payload執行腳本(Javascript)並將其替換爲新生成的有效payload.
支持:
建立新的有效payload屬性:
"transform": { "script": { "script": "payload.outliers = payload.aggregations.response_time_outlier.values['95.0']" } }
過濾聚合桶:
"transform": { "script": { "script": "payload.newlist=[]; payload.payload.aggregations['2'].buckets.filter(function( obj ) { return obj.key; }).forEach(function(bucket){ console.log(bucket.key); if (doc_count.length > 1){ payload.newlist.push({name: bucket.key }); }});" } }
在鏈中執行已經配置轉換的有序列表, 其中一個轉換的輸出用做鏈中下一個轉換的輸入.
"transform": { "chain": [ { "search": { "request": { "index": [ "credit_card" ], "body": { "size": 300, "query": { "bool": { "must": [ { "match": { "Class": 1 } } ] } } } } } }, { script: { script: "payload.hits.total > 100" } } ] }
actions用於將Watcher獲取的任何結果傳遞給集羣中的用戶,API或新文檔。 能夠爲每一個操做定義多個操做和組。
actions使用{{mustache}} logic-less模板語法,執行的時候會使用響應payload中提供的具體值迭代數組、擴展模板中的標記。
當Watcher返回超過其條件的數據時,執行「Actions」。
支持的「Actions」類型以下:
經過電子郵件/ SMTP發送查詢結果和消息(須要kibana中配置郵件發送)
"email" : { "to" : "root@localhost", "from" : "sentinl@localhost", "subject" : "Alarm Title", "priority" : "high", "body" : "Series Alarm {{ payload._id}}: {{payload.hits.total}}", "stateless" : false }
使用HTML正文經過電子郵件/ SMTP發送查詢結果和消息(須要kibana中配置郵件發送)
"email_html" : { "to" : "root@localhost", "from" : "sentinl@localhost", "subject" : "Alarm Title", "priority" : "high", "body" : "Series Alarm {{ payload._id}}: {{payload.hits.total}}", "html" : "<p>Series Alarm {{ payload._id}}: {{payload.hits.total}}</p>", "stateless" : false }
向遠程Web API發送post消息
"webhook" : { "method" : "POST", "host" : "remote.server", "port" : 9200, "path": ":/{{payload.watcher_id}}", "body" : "{{payload.watcher_id}}:{{payload.hits.total}}", "create_alert" : true }
向遠程web API發送get請求:
"webhook" : { "method" : "GET", "host" : "remote.server", "port" : 9200, "path" : "/trigger", "params" : { "watcher": "{{watcher.title}}", "query_count": "{{payload.hits.total}}" } }
經過代理向遠程API發送消息 - 電報示例:
"webhook": { "method": "POST", "host": "remote.proxy", "port": "3128", "path": "https://api.telegram.org/bot{botId}/sendMessage", "body": "chat_id={chatId}&text=Count+total+hits:%20{{payload.hits.total}}", "headers": { "Content-Type": "application/x-www-form-urlencoded" }, "create_alert" : true }
發送消息到 slack
"slack" : { "channel": "#channel", "message" : "Series Alarm {{ payload._id}}: {{payload.hits.total}}", "stateless" : false }
使用PhantomJS獲取網站快照並經過電子郵件/ SMTP發送。
須要kibana配置中的操做設置須要Pageres / PhantomJS:
npm install -g pageres
發送報告:
"report" : { "to" : "root@localhost", "from" : "kaae@localhost", "subject" : "Report Title", "priority" : "high", "body" : "Series Report {{ payload._id}}: {{payload.hits.total}}", "snapshot" : { "res" : "1280,900", "url" : "http://127.0.0.1/app/kibana#/dashboard/Alerts", "path" : "/tmp/", "params" : { "username" : "username", "password" : "password", "delay" : 5000, "crop" : false } }, "stateless" : false }
輸出查詢結果和消息到控制檯,方便調試
"console" : { "priority" : "DEBUG", "message" : "Average {{payload.aggregations.avg.value}}" }
ELK默認狀況不會存儲原始payload,防止重複。可是能夠經過以下配置保存和修改原始payload
"save_payload":true
具體例子以下:
"email" : { "to" : "root@localhost", "from" : "sentinl@localhost", "subject" : "Alarm Title", "priority" : "high", "body" : "Series Alarm {{ payload._id}}: {{payload.hits.total}}", "stateless" : false, "save_payload" : true }
參考: https://sentinl.readthedocs.io/en/latest/Watcher-Anatomy/ https://sentinl.readthedocs.io/en/latest/Watcher-Actions/