Prometheus是一款开源监控告警系统,自2012年开源以来,许多公司和组织都使用Prometheus,并且他拥有众多开发者和活跃的社区。

Prometheus拥有许多特性

  • 由时序数据kv标识组成的多维度数据模型

  • PromQL 灵活的查询语句

  • 不依赖分布式存储;单个服务器节点是自治的

  • HTTP拉取方式收集时序数据

  • 同时支持时序数据推送 pushGateway

  • 通过服务发现或静态配置发现监控目标

  • 支持多种图形和仪表板模式

Prometheus架构图

核心组件

  • Prometheus server 收集和存储时序数据核心组件

  • client libaries 应用集成的核心库

  • push gateway 支撑短存活的监控项

  • exporters 特殊类型的监控

  • alertmanager 处理告警信息

prometheus安装

prometheus版本迭代比较快,建议采用2.21以上版本,prometheus有着良好的向下兼容性;新版本的功能页比较丰富,同时支撑快速升级

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 下载prometheus,此处以Linux为例,也可使用Windows,建议个人开发测试使用Windows,线上请安装Linux并且做好权限隔离
wget https://github.com/prometheus/prometheus/releases/download/v2.38.0/prometheus-2.38.0.linux-amd64.tar.gz

# 解压文件
tar -xf prometheus-2.38.0.linux-amd64.tar.gz
cd prometheus


nohup ./prometheus
--config.file="prometheus.yml" \ # 指定prometheus配置文件,使用默认即可,如果配置systemd,建议使用全路径
--web.listen-address="0.0.0.0:9090" \ # 绑定服务IP和端口号
--web.enable-lifecycle \ # 开启http生命周期管理命令,开启后支持热加载配置文件
--storage.tsdb.path="data/" \ # 收集后的数据存储位置
>> /dev/null 2>&1 &

# 完整命令
nohup ./prometheus --config.file="prometheus.yml" --web.listen-address="0.0.0.0:9090" --web.enable-lifecycle --storage.tsdb.path="data/" >> /dev/null 2>&1 &

alertmanger安装

alertmanager 是prometheus体系中的告警消息发送服务;alertmanager支持多种消息推送,自行测试请直接使用email,官方建议使用webhook或者微信钉钉等hook方式接收消息,本次以webhook为例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 下载 alertmanager ,建议使用与prometheus相同发布周期的版本,或者直接下载最新版本的alertmanager
wget https://github.com/prometheus/alertmanager/releases/download/v0.24.0/alertmanager-0.24.0.linux-amd64.tar.gz

#解压
tar -xf alertmanager-0.24.0.linux-amd64.tar.gz
cd alertmanager
# 启动alertmanager
nohup ./alertmanager
--config.file="alertmanager.yml" \ # 指定alertmanager的配置文件
--storage.path="data/" \ #alertmanager数据存储目录
--web.listen-address=":9093" \ # 绑定IP和端口号
>> /dev/null 2>&1 &
# alertmanager支持集群模式,如果使用webhook等方式不建议使用机器,单个服务足够支撑
# 完整命令
nohup ./alertmanager --config.file="alertmanager.yml" --storage.path="data/" --web.listen-address=":9093" >> /dev/null 2>&1 &

alertmanager 配置文件

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
smtp_from: <tmpl_string> # 邮件发送方
smtp_smarthost: <string> # 邮件服务器地址
smtp_hello: <string> | default = "localhost" ] # 邮件招呼语
smtp_auth_username: <string> ] # 电子邮件用户,一定要全邮件地址 xxx@email.com
smtp_auth_password: <secret> ] # 密码
smtp_require_tls: <bool> | default = true ] # 如果邮件系统支持不使用tls,可以直接关闭


route: # 消息路由配置
group_by: ['alertname'] # 路由分组名
group_wait: 30s # 组内消息等待时间
group_interval: 1m
repeat_interval: 1m # 消息重发循环时间,如果告警一直显示 firing,每个 ${repeat_interval} ,发送一次,建议根据个人要求自行修改
receiver: 'web.hook' # 全局默认receiver,如果配置的receiver拦截消息,消息不会发送到这一层
routes: # 配置子路由,他是个数组,根据需要配置
- receiver: 'email' # 指定消息接收者
continue: false # 默认false,即此项路由匹配到,就不再向下传递
match: # 全词变量匹配,仅支持告警模板中的labels属性配置
[label:labelvalue]
match_re: # 正则表达式匹配
[ <labelname>: <regex>, ... ]
# 从这个路由配置上看,还是一个路由简单,开发者根据自己需要开发自有的webhook处理消息发送
receivers: # 配置receiver,他是个数组,需要自行配置
- name: 'web.hook'
webhook_configs:
- url: 'http://127.0.0.1:8000/api/alert/webhook'
inhibit_rules:
- source_match:
severity: 'critical'
target_match:
severity: 'warning'
equal: ['alertname', 'dev', 'instance']