kibana Sentinl插件

发布 : 2019-05-05 分类 : 运维 浏览 :

需求

Docker环境将日志都收集到elk中,日志的监控及告警方式与之前传统解决方案有所区别,而且kibana默认是不支持监控及告警,所以需要找一款开源软件支持。kibana的Sentinl 是一款基于JavaScript语言的开源插件,主要是有监控,告警及报表功能,正好符合需求,以下为部署及使用记录。

软件环境

服务名 版本 功能
elasticsearch 6.2.4 日志存储
logstash 6.6.2 日志格式化
kibana 6.2.4 结果展示,告警等
filebeat 6.5.4 日志收集

本文只介绍Sentinl的部署,其它服务部署暂时不做介绍

Sentinl安装

  • 安装很简单,使用kibana-plugin命令指定包即可,如果是其它版本,修改github链接中的版本号即可。

    1
    # kibana-plugin install https://github.com/sirensolutions/sentinl/releases/download/tag-6.2.4/sentinl-v6.2.4.zip
  • 报警邮件设置

还需要修改kibana的配置,增加email的smtp配置,如果使用腾讯企业邮箱,ssl要改为false,不然会有问题,增加完配置后要重启kibana

1
2
3
4
5
6
7
8
9
10
11
12
# cat config/kibana.yml
.....其它内容略过....
sentinl:
settings:
email:
active: true
user: xxx@xxx.cn
password: ******
host: smtp.exmail.qq.com
ssl: false
report:
active: true
  • sentinl界面

重启完kibana后左侧菜单栏中可以看到sentinl选项,上方有三个选项栏分别是监控(watchers),告警(Alarms)和报告(Reports)

image

监控项及告警配置

  • 先看一下我的日志格式。

image

创建监控项(watchers)

到Sentinl界面点击右上角的NEW,点击后会有引导模式(Wizard)和高级模式(Advanced),本文使用高级模式,引导模式没搞明白 T.T。。

我们想要监控nginx-info-*索引,5个分钟以内,status字段为200的数据,如果总条数大于10条则触发告警(status字段是nginx的状态码,此告警正常环境肯定不会这么配置,只是以此为例好触发告警才设置的此条件)

高级模式中json配置如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
{
"actions": {
"email_html_alarm_e1e775d4-9c7c-4794-8d61-eed666fcb146": {
"name": "状态码告警",
"throttle_period": "5m",
"email_html": {
"to": "wangguangyuan@okay.cn",
"from": "wangguangyuan@okay.cn",
"stateless": false,
"subject": "邮件标题",
"priority": "high",
"html": "<p>Hi {{watcher.username}},</p>\n<p>状态码为200的共 {{payload.hits.total}}条,请检查 .</p>\n<div style=\"color: grey\">\n <hr>\n <p>触发条件:</p>\n <ul><li>{{watcher.condition.script.script}}</li></ul>\n</div>"
}
}
},
"input": {
"search": {
"request": {
"index": [
"nginx-info-*"
],
"body": {
"query": {
"bool": {
"must": [
{
"term": {
"status": {
"value": "200"
}
}
}
],
"filter": {
"range": {
"@timestamp": {
"from": "now-5m"
}
}
}
}
}
}
}
}
},
"condition": {
"script": {
"script": "payload.hits.total >= 10"
}
},
"trigger": {
"schedule": {
"later": "every 2 minutes"
}
},
"disable": true,
"report": false,
"title": "watcher_title",
"save_payload": false,
"spy": false,
"impersonate": false
}

推荐配合kibana的Dev Tools工具,还有json验证工具搭配调试

拆分解析

  • 告警方式设置

我使用的html email的方式,常用的还有email方式(和html email区别就是内容展示方式有一些区别)和webhook方式(可以与钉钉或微信对接)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
{
"actions": {
"email_html_alarm_e1e775d4-9c7c-4794-8d61-eed666fcb146": {
"name": "状态码告警",
"throttle_period": "5m",
"email_html": {
"to": "xxx@xxx.cn",
"from": "xxx@xxx.cn",
"stateless": false,
"subject": "邮件标题",
"priority": "high",
"html": "<p>Hi {{watcher.username}},</p>\n<p>状态码为200的共 {{payload.hits.total}}条,请检查 .</p>\n<div style=\"color: grey\">\n <hr>\n <p>触发条件:</p>\n <ul><li>{{watcher.condition.script.script}}</li></ul>\n</div>"
}
}
},
  • name : 告警的名字
  • throttle_period : 节流限制,即在设定的时间内不会重复告警
  • to : 收信人
  • from : 发信人,和kibana配置文件中一致即可
  • subject : 标题
  • priority : 优先级
  • html : 邮件正文
  • input 定制规则

以下内容的意思是,查询nginx-info-*索引中5分钟以内status等于200的数据

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
"input": {
"search": {
"request": {
"index": [
"nginx-info-*"
],
"body": {
"query": {
"bool": {
"must": [
{
"term": {
"status": {
"value": "200"
}
}
}
],
"filter": {
"range": {
"@timestamp": {
"from": "now-5m"
}
}
}
}
}
}
}
}
},
  • 条件设置

查询数据大于10条则告警

1
2
3
4
5
"condition": {
"script": {
"script": "payload.hits.total >= 10"
}
},
  • 执行计划

2 分钟执行一次此监控任务

1
2
3
4
5
"trigger": {
"schedule": {
"later": "every 2 minutes"
}
},

配置完之后点击右上角的SAVE保存

我们手动触发一下此条件

image

Alarms 中可以看到告警
image

查看告警邮件

image

其它条件

  • 查询10分钟内,upstream_respone_time超过2秒小于5秒的请求
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
"input": {
"search": {
"request": {
"index": [
"logstash-wf-nginx-info-*"
],
"body": {
"query": {
"bool": {
"must": [
{
"range": {
"upstream_response_time": {
"gte": 2,
"lte": 5
}
}
}
],
"filter": {
"range": {
"@timestamp": {
"from": "now-10m"
}
}
}
}
}
}
}
}
},
  • 查询5分钟内带error日志的条数

    这里最好使用match 查询不要用wildcard

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
"input": {
"search": {
"request": {
"index": [
"*wf*"
],
"body": {
"query": {
"bool": {
"must": [
{
"match": {
"message": "error"
}
}
],
"filter": {
"range": {
"@timestamp": {
"from": "now-5m"
}
}
}
}
}
}
}
}
},

总结

总体来说sentinl还不错,不过需要对json和es的查询语法熟悉,像我这种小白写个这种简单条件整了半天。

参考文档

Kibana使用sentinl报警实践,某大佬博客,写的比我详细

告警处的一些环境变量

ElasticSearch各种查询关键字的区别(重要)

本文作者 : WGY
原文链接 : http://geeklive.cn/2019/05/05/kibana-sentinl/undefined/kibana-sentinl/
版权声明 : 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明出处!
留下足迹