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/

相关文章
相关标签/搜索