zoukankan      html  css  js  c++  java
  • ELK的sentinl告警配置详解

    背景

    sentinl的监控&告警是通过watch实现的。

    一、Watch Execution

    执行开始的时候, watcher为watch创建watch执行上下文. 执行上下文提供脚本和模板, 可以访问watch元数据、payload、wathcID、执行时间和触发器.

    执行过程,watcher具体执行:

    1. 将输入数据作为监视上下文中的payload加载. 这使得数据可以用于执行过程中的所有后续步骤. 此步骤由watch输入控制.
    2. 评估watch状况以确定是否继续处理watch. 如果条件满足,则处理进入下一步;如果不满足,则停止执行watch.
    3. 将转变应用与见识payload(如果需要)
    4. 执行已经满足条件且watch未受限制的监控.

    二、Watch Acknowledge and Throttling

    watcher支持基于时间和基于确认的限制, 可以防止对同一个事件重复执行操作.默认情况下, Watcher使用基于时间的限制. 还可以在每个操作或者在监控级别配置限制周期。

    三、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)

    Watch Execution Context

    以下代码显示了Watch Execution Context的基本结构:

    {
      "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.

    Elasticsearch5.0开始内置新的脚本语言painless.

    脚本可以引用监视执行上下文中的任何值或通过脚本参数显式传递的值.

    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"
              }
            }
          }
        }
      }
    }
    

    四、访问搜索结果

    conditions、transforms、actions可以通过ctx访问搜索结果,例如:

    • 访问所有匹配结果: ctx.payload.hits
    • 引用命中总数:ctx.payload.hits.total
    • 要访问特定匹配, 请使用其从零开始的数组索引.
    • 要从特定命中获取字段值, 请使用 ctx.payload.hits.hits..fields.

    五、转换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"
          }
        }
      ]
    }
    

    六、触发事件(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/

  • 相关阅读:
    安装node-gyp
    node版本切换
    electron-vue运行只出现项目目录不出现效果
    高级运维工程师的必备技术
    linux 下的shutdown指令
    数据库实体联系模型与关系模型
    数据库表设计1
    实体-关系模型
    Excel中怎么快速选中区域
    EXCEL中给包含某个字段的单元格所在行标注颜色
  • 原文地址:https://www.cnblogs.com/amyzhu/p/10193557.html
Copyright © 2011-2022 走看看