ELK的sentinl告警配置詳解

背景

sentinl的監控&告警是經過watch實現的。html

1、Watch Execution

執行開始的時候, watcher爲watch建立watch執行上下文. 執行上下文提供腳本和模板, 能夠訪問watch元數據、payload、wathcID、執行時間和觸發器.web

執行過程,watcher具體執行:npm

  1. 將輸入數據做爲監視上下文中的payload加載. 這使得數據能夠用於執行過程當中的全部後續步驟. 此步驟由watch輸入控制.
  2. 評估watch情況以肯定是否繼續處理watch. 若是條件知足,則處理進入下一步;若是不知足,則中止執行watch.
  3. 將轉變應用與見識payload(若是須要)
  4. 執行已經知足條件且watch未受限制的監控.

2、Watch Acknowledge and Throttling

watcher支持基於時間和基於確認的限制, 能夠防止對同一個事件重複執行操做.默認狀況下, Watcher使用基於時間的限制. 還能夠在每一個操做或者在監控級別配置限制週期。api

3、Scripts and Templates

定義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

如下代碼顯示了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" : { ... }
}
  • metadata : watch定義的靜態metadata
  • payload: 當前watch payload
  • id
  • Execution_time: watch執行開始時間
  • trigger: 有關事件觸發器的信息
    • triggered_time:實際觸發時間
    • scheduled_time:計劃觸發時間
  • vars: 可在執行期間由不一樣構造設置和訪問的動態變量.這些變量的範圍限定爲單個執行(即它們不是持久的,不能在同一個watch的不一樣執行之間使用)

Using Scripts

可使用腳本定義 conditions和transforms. 默認腳本語言是painless.post

Elasticsearch5.0開始內置新的腳本語言painless.網站

腳本能夠引用監視執行上下文中的任何值或經過腳本參數顯式傳遞的值.atom

Using Templates

可使用模板爲watch定義動態內容. 執行的時候, 模板從監視執行上下文中提取數據. 例如, 可使用模板爲電子郵件填充主題字段, 其中數據存儲在payload中. 模板還能夠訪問經過模板參數顯示傳遞的值.

可使用 Mustache腳本語言指定模板.

Inline Templates and Scripts

要定義內聯模板或者腳本, 只須要直接在字段值中指定便可.

對於腳本,只須要將內聯腳本定義爲腳本字段的值便可.

Stored Templates and Scripts

若是存儲模板和腳本, 則能夠經過id引用它們.

要使用已經存儲的模板和腳本, 定義的時候須要經過id字段顯示指定id. 例如,下文直接引用id=email_notification_subject 的模板.

{
  ...
  "actions" : {
    "email_notification" : {
      "email" : {
        "subject" : {
          "id" : "email_notification_subject",
          "params" : {
            "color" : "red"
          }
        }
      }
    }
  }
}

4、訪問搜索結果

conditions、transforms、actions能夠經過ctx訪問搜索結果,例如:

  • 訪問全部匹配結果: ctx.payload.hits
  • 引用命中總數:ctx.payload.hits.total
  • 要訪問特定匹配, 請使用其從零開始的數組索引.
  • 要從特定命中獲取字段值, 請使用 ctx.payload.hits.hits. .fields.

5、轉換Transform

轉換處理並更改ctx中的有效內容,以便爲監控操做作好準備. 若是在transform處理後 payload爲空,則不執行任何操做.

搜索轉換(Search transform)

在集羣上執行搜索,並使用返回的搜索響應替換watch執行上下文中的當前有效內容.

腳本轉換(Script transform)

對當前有效payload執行腳本(Javascript)並將其替換爲新生成的有效payload.
支持:

  • 轉換格式類型
  • 生成全新的有效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 }); }});"
  }
}

鏈轉換(Chain transform)

在鏈中執行已經配置轉換的有序列表, 其中一個轉換的輸出用做鏈中下一個轉換的輸入.

"transform": {
  "chain": [
    {
      "search": {
        "request": {
          "index": [
            "credit_card"
          ],
          "body": {
            "size": 300,
            "query": {
              "bool": {
                "must": [
                  {
                    "match": {
                      "Class": 1
                    }
                  }
                ]
              }
            }
          }
        }
      }
    },
    {
      script: {
        script: "payload.hits.total > 100"
      }
    }
  ]
}

6、觸發事件(Actions)

actions用於將Watcher獲取的任何結果傳遞給集羣中的用戶,API或新文檔。 能夠爲每一個操做定義多個操做和組。

actions使用{{mustache}} logic-less模板語法,執行的時候會使用響應payload中提供的具體值迭代數組、擴展模板中的標記。

當Watcher返回超過其條件的數據時,執行「Actions」。

支持的「Actions」類型以下:

Email

經過電子郵件/ SMTP發送查詢結果和消息(須要kibana中配置郵件發送)

"email" : {
       "to" : "root@localhost",
       "from" : "sentinl@localhost",
       "subject" : "Alarm Title",
       "priority" : "high",
       "body" : "Series Alarm {{ payload._id}}: {{payload.hits.total}}",
       "stateless" : false
       }

Email HTML

使用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
       }

webHook

向遠程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}}"
     }
    }

webHook via Proxy

經過代理向遠程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

"slack" : {
       "channel": "#channel",
       "message" : "Series Alarm {{ payload._id}}: {{payload.hits.total}}",
       "stateless" : false
      }

Report (BETA)

使用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

輸出查詢結果和消息到控制檯,方便調試

"console" : {
   "priority" : "DEBUG",
   "message" : "Average {{payload.aggregations.avg.value}}"
   }

Storing Payload

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/

相關文章
相關標籤/搜索