一、拉取所需要的镜像
docker pull prom/node-exporter
docker pull grafana/grafana
docker pull prom/prometheus
docker pull prom/alertmanager
其中
prom/node-exporter:用于收集主机系统信息和指标的
grafana/grafana:是一个用于可视化和分析监控指标的开源平台。
prom/prometheus:是一个开源的监控系统,用于收集和存储时间序列数据,并提供基于数据的查询、报警和可视化功能。
prom/alertmanager:是 Prometheus 的告警管理器,用于处理和路由来自 Prometheus 服务器的告警通知。
二、容器启动
(1)启动node-exporter
docker run --name node-exporter -d -p 9100:9100 -v /proc:/host/proc:ro -v /sys:/host/sys:ro -v /:/rootfs:ro prom/node-exporter
(2)启动alertmanager
在启动alertmanager之前,可以自己编写一个alertmanager.yml文件,然后使用卷挂载到该容器内部。
# 创建挂载目录
mkdir /opt/alertmanager
cd /opt/alertmanager/
# 编写该文件
vim alertmanager.yml
alertmanager.yml
global:
resolve_timeout: 5m
route: # 告警路由配置,定义如何处理和发送告警
receiver: webhook
group_wait: 30s
group_interval: 1m
repeat_interval: 5m #告警间隔时间5分钟
group_by: [alertname]
routes:
- receiver: webhook
group_wait: 10s
receivers: # 告警接收者配置,定义如何处理和发送告警
- name: webhook
webhook_configs:
- url: http://192.168.110.45:8060/dingtalk/webhook1/send # 告警 Webhook URL,钉钉服务url
send_resolved: true # 是否发送已解决的告警。如果设置为 true,则在告警解决时发送通知
启动
docker run --name alertmanager -d -p 9093:9093 -v /opt/alertmanager/:/etc/alertmanager/ --name alertmanager prom/alertmanager
(3)启动grafana
docker run --name grafana -d -p 3000:3000 --name=grafana grafana/grafana
(4)启动prometheus
在启动prometheus之前,可以自己编写一个prometheus.yml文件,然后使用卷挂载到该容器内部。
# 创建挂载目录
mkdir /opt/prometheus
cd /opt/prometheus/
# 编写该文件
vim prometheus.yml
prometheus.yml文件
global:
scrape_interval: 60s
evaluation_interval: 60s
# Alertmanager配置
alerting:
alertmanagers:
- static_configs:
- targets: ["192.168.110.45:9093"] # 告警服务地址
# rule配置
rule_files:
- "/etc/prometheus/rules.yml" #挂载在容器中的路径
scrape_configs:
- job_name: prometheus
static_configs:
- targets: ['192.168.110.45:9090'] # 采取prometheus指标数据
labels:
instance: prometheus
- job_name: linux
static_configs:
- targets: ['192.168.110.45:9100'] # 采取本地指标数据,该服务和端口node_exporter容器已经暴露
labels:
instance: localhost
将prometheus和alertmanager进行关联,在prometheus目录下创建一个rules.yml文件的告警规则
root@ai:/opt/prometheus# pwd
/opt/prometheus
root@ai:/opt/prometheus# cat rules.yml
groups:
- name: host_monitoring
rules:
- alert: 内存报警
expr: netdata_system_ram_MiB_average{chart="system.ram",dimension="free",family="ram"} < 800
for: 2m
labels:
team: node
annotations:
Alert_type: 内存报警
Server: '{{$labels.instance}}'
explain: "内存使用量超过90%,目前剩余量为:{{ $value }}M"
- alert: CPU报警
expr: netdata_system_cpu_percentage_average{chart="system.cpu",dimension="idle",family="cpu"} < 20
for: 2m
labels:
team: node
annotations:
Alert_type: CPU报警
Server: '{{$labels.instance}}'
explain: "CPU使用量超过80%,目前剩余量为:{{ $value }}"
- alert: 磁盘报警
expr: netdata_disk_space_GiB_average{chart="disk_space._",dimension="avail",family="/"} < 4
for: 2m
labels:
team: node
annotations:
Alert_type: 磁盘报警
Server: '{{$labels.instance}}'
explain: "磁盘使用量超过90%,目前剩余量为:{{ $value }}G"
- alert: 服务告警
expr: up == 0
for: 2m
labels:
team: node
annotations:
Alert_type: 服务报警
Server: '{{$labels.instance}}'
explain: "netdata服务已关闭"
注意:因为我们是使用挂载的方式将配置文件投射到容器中,相应的文件路径也要发生变化。
比如在prometheus.yaml文件中:rule_files: [“/etc/prometheus/rules.yml”] 的路径就要填写为容器内该文件存放的路径。
启动
docker run --name prometheus -d -p 9090:9090 -v /opt/prometheus:/etc/prometheus prom/prometheus
(5)启动webhook-prometheus-dingtalk
该服务不使用docker的方式,而是直接运行已经编译好的二进制文件,然后长期运行在服务器上的方式
1、首先获取钉钉告警机器人的webhook token和加签
2、安装webhook-dingtalk插件
wget https://github.com/timonwong/prometheus-webhook-dingtalk/releases/download/v2.1.0/prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
tar zxf prometheus-webhook-dingtalk-2.1.0.linux-amd64.tar.gz
mkdir -p /usr/local/prometheus/webhook-dingtalk
mv prometheus-webhook-dingtalk-2.1.0.linux-amd64/* /usr/local/prometheus/webhook-dingtalk
3、配置webhook-dingtalk使用systemd管理
cp /usr/local/prometheus/webhook-dingtalk/config.example.yml /usr/local/prometheus/webhook-dingtalk/config.yml
vim /usr/lib/systemd/system/webhook.service
#webhook.service文件内容
[Unit]
Description=Prometheus-Server
After=network.target
[Service]
ExecStart=/usr/local/prometheus/webhook-dingtalk/prometheus-webhook-dingtalk --config.file=/usr/local/prometheus/webhook-dingtalk/config.yml
User=root
[Install]
WantedBy=multi-user.target
4、Webhook-dingtalk配置钉钉webhook地址,即修改config.yml文件(该配置文件目前为默认的,)
Webhook-dingtalk配置相对比较简单,只改以下三处即可,如下图:
加签秘钥、webhook地址是咱们在钉钉创建机器人时获取的!
vim /usr/local/prometheus/webhook-dingtalk/config.yml
5、添加钉钉报警模板
vim /usr/local/prometheus/webhook-dingtalk/template.tmpl
{{ define "__subject" }}
[{{ .Status | toUpper }}{{ if eq .Status "firing" }}:{{ .Alerts.Firing | len }}{{ end }}]
{{ end }}
{{ define "__alert_list" }}{{ range . }}
---
{{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }}
**告警主题**: {{ .Annotations.summary }}
**告警类型**: {{ .Labels.alertname }}
**告警级别**: {{ .Labels.severity }}
**告警主机**: {{ .Labels.instance }}
**告警信息**: {{ index .Annotations "description" }}
**告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}
{{ define "__resolved_list" }}{{ range . }}
---
{{ if .Labels.owner }}@{{ .Labels.owner }}{{ end }}
**告警主题**: {{ .Annotations.summary }}
**告警类型**: {{ .Labels.alertname }}
**告警级别**: {{ .Labels.severity }}
**告警主机**: {{ .Labels.instance }}
**告警信息**: {{ index .Annotations "description" }}
**告警时间**: {{ dateInZone "2006.01.02 15:04:05" (.StartsAt) "Asia/Shanghai" }}
**恢复时间**: {{ dateInZone "2006.01.02 15:04:05" (.EndsAt) "Asia/Shanghai" }}
{{ end }}{{ end }}
{{ define "default.title" }}
{{ template "__subject" . }}
{{ end }}
{{ define "default.content" }}
{{ if gt (len .Alerts.Firing) 0 }}
**====侦测到{{ .Alerts.Firing | len }}个故障====**
{{ template "__alert_list" .Alerts.Firing }}
---
{{ end }}
{{ if gt (len .Alerts.Resolved) 0 }}
**====恢复{{ .Alerts.Resolved | len }}个故障====**
{{ template "__resolved_list" .Alerts.Resolved }}
{{ end }}
{{ end }}
{{ define "ding.link.title" }}{{ template "default.title" . }}{{ end }}
{{ define "ding.link.content" }}{{ template "default.content" . }}{{ end }}
{{ template "default.title" . }}
{{ template "default.content" . }}
6、启动 && 开机自启
systemctl enable webhook.service --now
systemctl status webhook.service
7、验证,查看端口是否启动
netstat -anput |grep 8060
三、测试
将node-exporter所在的容器停掉,查看效果
docker stop node-exporter所在的容器id
钉钉查看告警
参考:https://blog.csdn.net/weixin_45310323/article/details/130655840
https://blog.csdn.net/qq_52589631/article/details/131152150