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/