监控系统中,除了监控数据可视化外,个人认为最重要的就是告警了。好的告警,能很大程度上避免故障的发生。但是不好的告警,可能会费时费力,而且会出现“狼来了”效应,导致严重的后果。

PrometheusAlert

  前面已经介绍了,PrometheusAlert 是一个国产开源的告警插件,类似于 PrometheusDingtalk。只不过功能更加强大而已。这里直接介绍他的安装。

下载

  PrometheusAlert 源码托管在 github 上,点击进入 github 仓库。对应有 Release 版本的安装包。如果 github 无法访问或者访问很慢,可以通过 gitee 仓库 中下载,当前(指写文章当天)Release 版本要低于 github。这里使用 4.6.1 版本。

PrometheusAlert Github Page

  直接使用 wget 会因为下载地址重定向导致下载失败,只能先需要下载好在上传到 Linux 上。在上图红色框中位置点击绿色框文字跳转到如下图的下载页。选择 linux 版本下载(红色框标红)。

PrometheusAlert Download Page

解压

  下载 linux.zip 安装包之后,通过 Xftp 这类的上传工具上传包。注意,如果没有 unzip 命令,请提前安装。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
[root@prometheusalert ~]# unzip linux
[root@prometheusalert ~]# ll
total 15412
-rw-------. 1 root root 1368 Dec 29 10:35 anaconda-ks.cfg
drwxr-xr-x. 7 root root 120 Nov 11 11:46 linux
-rw-r--r--. 1 root root 15774088 Dec 30 16:58 linux.zip
[root@prometheusalert ~]# tree -L 1 linux
linux
├── conf
├── db
├── logs
├── PrometheusAlert
├── static
├── user.csv
├── views
└── zabbix

5 directories, 3 files
[root@prometheusalert ~]# mv linux /opt/prometheusalert-4.6.1
[root@prometheusalert ~]# ln -s /opt/prometheusalert-4.6.1/ /usr/local/prometheusalert

  到这里基本的配置已经做好了。下面开始配置 PrometheusAlert

修改配置

  配置文件的完整路径为 /usr/local/prometheusalert/conf/app.conf ,这里配置文件中都有中文解释说明,就不做阐述。主要说明就是数据库的选择。如果使用 mysql 请使用修改按照下面配置进行按需修改。

1
2
3
4
5
6
db_driver=mysql
db_host=127.0.0.1
db_port=3306
db_user=root
db_password=xFTO8AE8RikpTIwb
db_name=prometheusalert

配置 systemd 管理并启动

  可执行文件是 /usr/local/prometheusalert/PrometheusAlert ,这里要先赋予可执行权限。

1
[root@prometheusalert ~]# chmod 755 /usr/local/prometheusalert/PrometheusAlert

  这里测试过,使用 systemd 管理,会在访问的 http://192.168.0.251:8080 的时候如下错误。

1
PrometheusAlert:can't find templatefile in the path:views/index.html

  因此我们只能手动运行或者编写脚本在使用 systemd 管理。这里推荐使用 systemd 管理。

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
64
65
66
67
68
69
70
71
72
73
[root@prometheusalert ~]# cat /etc/init.d/PrometheusAlert
#!/bin/bash
#################################################################################
# 作者:行茗
# 时间:2021-12-31
# 作用:PrometheusAlert 启动脚本
#################################################################################
export PATH="$PATH"
cd /usr/local/prometheusalert
prometheusalert_dir="/usr/local/prometheusalert"
prometheusalert_status=$(netstat -tunlp | grep PrometheusA | grep 8080 | wc -l)
start() {
if [ "${prometheusalert_status}" -eq 0 ]; then
nohup ./PrometheusAlert > /dev/null 2>&1 &
return_value=$?
if [ "${return_value}" -eq 0 ]; then
echo "PrometheusAlert start successful"
else
echo "PrometheusAlert start fault, please check config file"
fi
else
echo "PrometheusAlert in running"
fi
}
stop() {
if [ ${prometheusalert_status} -gt 0 ]; then
prometheus_pid=$(netstat -tunlp | grep PrometheusA | grep 8080 | awk "NR==1" | awk '{print $7}' | awk -F "/" '{print $1}')
kill -9 ${prometheus_pid}
return_value=$?
if [ "${return_value}" -eq 0 ]; then
echo "PrometheusAlert stop successful"
else
echo "PrometheusAlert stop fault, please check redis status"
fi
else
echo "PrometheusAlert is not running, please execure $0 start"
fi
}
case "$1" in
start)
start && exit 0
;;
stop)
stop || exit 2
;;
restart)
stop
sleep 10s
start && exit 0
;;
*)
echo $"Usage: $0 {start|stop|restart}"
exit 2
;;
esac
exit $?
[root@prometheusalert ~]# chmod 755 /etc/init.d/PrometheusAlert
[root@prometheusalert ~]# cat /usr/lib/systemd/system/PrometheusAlert.service
[Unit]
Description=Prometheus Alert
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/PrometheusAlert start
ExecRestart=/etc/init.d/PrometheusAlert restart
ExecStop=/etc/init.d/PrometheusAlert stop
Restart=on-failure

[Install]
WantedBy=multi-user.target
[root@prometheusalert ~]# systemctl daemon-reload
[root@prometheusalert ~]# systemctl enable --now PrometheusAlert

  这里已经安装好 PrometheusAlert 了。相关信息可以通过浏览器访问 http://192.168.0.251:8080 查看到。

模板自定义

  前面也提到过,使用PrometheusAlert的原因是其是一个支持多平台,可自定义模板的一个插件。这里简单介绍一下钉钉的模板自定义。

  浏览器访问 http://your_ip:8080 进入对应的页面进行定制。这里我访问 http://192.168.0.251:8080 。首页如下图。

PrometheusAlert HomePage

  在首页上点击 AlertTemplate 进入模板自定义页面。这里会要求登录账号密码,这个信息在 /usr/local/prometheusalert/conf/app.conf 中能找到。

PrometheusAlert AlertTemplate

  图片中你看到的路径,就是自定义模板的接口URL。AlertManager就是调用这个地址来进行告警。

PrometheusAlert AlertTemplate Alter

  这里的Json内容,是给测试模板的,可以在日志文件中拿到对应的信息(日志拿到的可读性不高,可以先通过VS Code 等工具格式化)。测试之前需要需要提前保存模板。

  这里给出我使用的模板,告警显示红色,恢复显示绿色。按照自己的需求进行修改。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
{{ $var := .externalURL}}{{ range $k,$v:=.alerts }}
{{if eq $v.status "resolved"}}
# <font color="#00FF00">Prometheus恢复信息</font>
### <font color="#228B22">{{$v.labels.alertname}}</font>
###### <font color="#000000">告警级别:{{$v.labels.severity}}</font>
###### <font color="#000000">开始时间:{{GetCSTtime $v.startsAt}}</font>
###### <font color="#000000">结束时间:{{GetCSTtime $v.endsAt}}</font>
###### <font color="#000000">故障主机:{{$v.labels.alert_host}}</font>
### <font color="#000000">故障信息:{{$v.labels.alert_host}}{{$v.annotations.description}}</font>
![Prometheus](https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png)
{{else}}
# <font color="#FF0000">Prometheus告警信息</font>
### <font color="#FF0000">{{$v.labels.alertname}}</font>
###### <font color="#000000">告警级别:{{$v.labels.severity}}</font>
###### <font color="#000000">开始时间:{{GetCSTtime $v.startsAt}}</font>
###### <font color="#000000">结束时间:{{GetCSTtime $v.endsAt}}</font>
###### <font color="#000000">故障主机:{{$v.labels.alert_host}}</font>
### <font color="#FF0000">故障信息:{{$v.labels.alert_host}}{{$v.annotations.description}}</font>
![Prometheus](https://raw.githubusercontent.com/feiyu563/PrometheusAlert/master/doc/alert-center.png)
{{end}}
{{ end }}

AlertManager

  在 《监控系统之Prometheus-01 基于 Prometheus 的监控系统的基础规划》 中就有说过,这是一个告警插件。用来将接收到的告警通过整理按路由发送给正确的接受器。

  介绍一下他的安装和配置。

下载

  AlertManager 是 Prometheus 的组件,相关下载可以在 Prometheus 下载站 中找到。这里选择版本 0.23

1
[root@alertmanager ~]# wget https://github.com/prometheus/alertmanager/releases/download/v0.23.0/alertmanager-0.23.0.linux-amd64.tar.gz

解压

1
2
[root@alertmanager ~]# tar -zxvf alertmanager-0.23.0.linux-amd64.tar.gz -C /opt
[root@alertmanager ~]# ln -s /opt/alertmanager-0.23.0.linux-amd64 /usr/local/alertmanager

配置 Systemd 管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[root@alertmanager ~]# cat /usr/lib/systemd/system/alertmanager.service 
[Unit]
Description=Alertmanager
Wants=network-online.target
After=network-online.target

[Service]
Type=simple
ExecReload=/bin/kill -HUP $MAINPID
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml \
--storage.path=/data/alertmanager/ \
--data.retention=120h

SyslogIdentifier=Alertmanager
Restart=on-failure

[Install]
WantedBy=multi-user.target

修改配置文件

  这里主要是对 PrometheusAlert 的地址的使用。这里给出一个简单配置。

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
[root@alertmanager ~]# cat /usr/local/alertmanager/alertmanager.yaml
global:
resolve_timeout: 5m
route:
group_by: ['instance']
group_wait: 10m
group_interval: 10s
repeat_interval: 10m
receiver: 'web.hook.prometheusalert'
routes:
- receiver: 'prometheusalert-dingding'
group_wait: 10s
receivers:
- name: 'web.hook.prometheusalert'
webhook_configs:
- url: 'http://192.168.0.251:8080/prometheus/alert'
- name: 'prometheusalert-dingding'
webhook_configs:
- url: 'http://192.168.0.251:8080/prometheusalert?type=dd&tpl=prometheus-dd&ddurl=https://oapi.dingtalk.com/robot/send?access_token=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']

启动

  将 PrometheusAlert 的 URL 地址写入到配置之后,就可以启动 AlertManager 了。

1
2
[root@alertmanager ~]# systemctl daemon-reload
[root@alertmanager ~]# systemctl enable --now alertmanager

Prometheus 对接 AlertManager

  AlertManager 安装好之后,就可以从 Prometheus 中对接 AlertManager。

  Prometheus 配置文件中,添加如下(详细配置文件在《监控系统之Prometheus-03 Prometheus 的安装》里查看)。

1
2
3
4
5
alerting:
alertmanagers:
- static_configs:
- targets:
- 192.168.0.252:9093

  同时需要配置 rules 才能使得告警生效。这个后面会专门有文章进行讲解总结。

  修改完之后需要重载配置文件。

1
[root@prometheus ~]# curl -X POST http://192.168.0.254:9090/-/reload