一、prometheus监控架构
二、Prometheus vs Zabbix
指标 | Prometheus | Zabbix |
---|---|---|
语言 | Golang(Go) | PHP,C,GO |
部署 | 二进制,解压即用. | yum,编译,数据库,php依赖 |
是否容易掌握 | 门槛较高 | 容易使用 |
监控方式 | 通过各种exporter,监控一般都是基于http | 各种模板,客户端,自定义监控,各种协议. |
应用场景 | 监控服务,容器,k8s | 监控系统底层,硬件,系统,网络 |
三、Prometheus使用流程
官网:https://prometheus.io/
下载页面:https://prometheus.io/download/
官方文档:https://prometheus.io/docs/instrumenting/exporters/
1.创建用户
没特殊需要就:
useradd prometheus
会创建用户prometheus,加目录在/home/prometheus
passwd prometheus
需要指定参数如下:
groupadd -g 2000 prometheus
useradd -d /home/prometheus -u 2000 -g prometheus prometheus
/bin/echo 'passwordprometheus' |/usr/bin/passwd --stdin prometheus
/bin/cp /etc/skel/.bash* /home/prometheus -r
cd /home
chown prometheus:prometheus /home/prometheus
chmod 755 /home/prometheus
2.普通用户下上传并解压
su - prometheus
rz(上传的一种)
tar -zxf prometheus-2.37.6.linux-amd64.tar.gz
rm -f prometheus-2.37.6.linux-amd64.tar.gz
mv prometheus-2.37.6.linux-amd64 prometheus
3.更改配置
cd prometheus
./prometheus --version
prometheus, version 2.37.6 (branch: HEAD, revision: 8ade**)
build user: root@5f96027a7c3e
build date: 20230220-09:36:40
go version: go1.19.6
platform: linux/amd64
vi prometheus.yml
#全局定义部分
global:
#这个间隔表示,prometheus采集数据的间隔.
scrape_interval: 15s
#执行对应的rules(规则)间隔,一般报警规则.
evaluation_interval: 15s
#用于配置警告信息,alertmanager配置。
alerting:
alertmanagers:
- static_configs:
- targets:
#用于配置告警规则,用列表的方式
rule_files:
#数据采集的配置,配置客户端地址
scrape_configs:
#第一种配置方式为静态配置,直接指定
#任务名字.体现采集哪些机器,哪些指标.
- job_name: "prometheus"
static_configs:
- targets: ["localhost:9090"]
#第二种方法是基于文件服务发现的配置方式
- job_name: 'all_reserver_hosts'
#指定文件位置,每5s读取一次,添加或修改删除都不用修改主配置文件,不需要重启
file_sd_configs:
- files: ['/home/prometheus/prometheus/moniter_config/all_reserver_hosts/*.yml']
refresh_interval: 5s
配置文件中有三个配置块:global,rule_files和scrape_configs。
global:控制Prometheus服务器的全局配置。
第一个是scrape_interval,它控制Prometheus抓取目标的频率,也可以为单个目标重写此值。
evaluation_interval选项控制Prometheus评估规则的频率。 Prometheus使用规则创建新的时间序列并生成警报。
rule_files:Prometheus服务器加载的任何规则的位置。 现在我们没有规则。
scrape_configs:Prometheus监视的资源。 由于Prometheus还将自己的数据公开为HTTP端点,因此它可以抓取并监控自身的健康状况。
在默认配置中,有一个名为prometheus的作业,它会抓取Prometheus服务器公开的时间序列数据。
包含一个静态配置的目标,即端口9090上的localhost。Prometheus希望指标在/metrics路径上的目标上可用。
所以这个默认的工作是通过URL抓取:http//localhost:9090/metrics。
返回的时间序列数据将详细说明Prometheus服务器的状态和性能。
注意:只要修改了主配置文件就需要重启生效,之后添加修改删除配置文件目录时都需要重启
4.配置systemd
我这里指定的端口号9000,默认的端口为9090
日志默认在/var/log/message中
mkdir /home/prometheus/prometheus/data
su - root
vim /usr/lib/systemd/system/prometheus.service
[Unit]
Description=Prometheus Server
Documentation=https://prometheus.io
After=network.target
[Service]
Type=simple
ExecStart=/home/prometheus/prometheus/prometheus \
--config.file=/home/prometheus/prometheus/prometheus.yml \
--web.listen-address=:9000 \
--web.max-connections=512 \
--log.level=info \
--web.enable-lifecycle \
--web.enable-admin-api \
--storage.tsdb.retention=30d \
--storage.tsdb.path=/home/prometheus/prometheus/data
User=prometheus
Group=prometheus
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
[Install]
WantedBy=multi-user.target
下面是启动命令可能用到的参数,详细的可以查看./prometheus --help
#配置文件
--config.file="prometheus.yml"
#监听端口
--web.listen-address="0.0.0.0:9090"
#空闲连接的超时时间
--web.read-timeout=5m
#最大连接数
--web.max-connections=512
#可从外部访问Prometheus的URL(例如,如果Prometheus是通过反向代理提供的)。
#用于生成返回到Prometheus本身的相对和绝对链接。
#如果URL包含路径部分,它将被用作Prometheus服务的所有HTTP端点的前缀。
#如果省略,则会自动派生相关的URL组件。
--web.external-url=<URL>
#指标(数据)存储的基本路径
--storage.tsdb.path="data/"
#将数据保留多长时间。 此标志已被弃用,请改用“ storage.tsdb.retention.time”。
--storage.tsdb.retention=STORAGE.TSDB.RETENTION
#将数据保留多长时间。默认15天
--storage.tsdb.retention.time=STORAGE.TSDB.RETENTION.TIME
#可以为块存储的最大字节数。 支持的单位:KB,MB,GB,TB,PB。
--storage.tsdb.retention.size=STORAGE.TSDB.RETENTION.SIZE
#不在数据目录中创建锁文件
--storage.tsdb.no-lockfile
#允许重叠的块,从而启用垂直压缩和垂直查询合并。
--storage.tsdb.allow-overlapping-blocks
#压缩tsdb WAL
--storage.tsdb.wal-compression
#关闭或配置重新加载时等待刷写数据的时间
--storage.remote.flush-deadline=<duration>
#在单个查询中通过远程读取接口返回的最大样本总数。 0表示没有限制。 对于流式响应类型,将忽略此限制。
--storage.remote.read-sample-limit=5e7
#并发远程读取调用的最大数目。 0表示没有限制。
--storage.remote.read-concurrent-limit=10
#用于流式传输远程读取响应类型的单个帧中的最大字节数。 请注意,客户端也可能会限制帧大小。 1MB为默认情况下由protobuf推荐
--storage.remote.read-max-bytes-in-frame=1048576
#容忍中断以恢复警报“ for”状态的最长时间。
--rules.alert.for-outage-tolerance=1h
#警报和恢复的“ for”状态之间的最短持续时间。 仅对于配置的“ for”时间大于宽限期的警报,才保持此状态。
--rules.alert.for-grace-period=10m
#将警报重新发送到Alertmanager之前等待的最短时间。
--rules.alert.resend-delay=1m
#等待的Alertmanager通知的队列容量。
--alertmanager.notification-queue-capacity=10000
#向Alertmanager发送警报的超时。
--alertmanager.timeout=10s
#在表达式求值期间检索指标的最大回溯持续时间。
--query.lookback-delta=5m
#最大查询时间。
--query.timeout=2m
#最大查询并发数
--query.max-concurrency=20
#单个查询可以加载到内存中的最大样本数。
#请注意,如果查询尝试将更多的样本加载到内存中,则查询将失败,因此这也限制了查询可以返回的样本数。
--query.max-samples=50000000
#日志级别
--log.level=info
#日志格式
--log.format=logfmt Output format of log messages. One of: [logfmt, json]
5.启动
su - root
systemctl daemon-reload
systemctl enable --now prometheus
systemctl status promethes
6.验证
###查看端口是否启动
netstat -ntpl
###查看进程
ps -ef |grep prometheus
###查看服务状态
systemctl status prometheus.service
● prometheus.service - Prometheus Server
Loaded: loaded (/usr/lib/systemd/system/prometheus.service; enabled; vendor preset: disabled)
Active: active (running) since Fri 2023-05-12 11:40:35 CST; 2 days ago
Docs: https://prometheus.io
Main PID: 22408 (prometheus)
CGroup: /system.slice/prometheus.service
└─22408 /home/prometheus/prometheus/prometheus --config.file...
登录web UI 查看:(以server地址为192.168.1.1为例)
浏览器登录http://192.168.1.1:9000
可以显示如下界面则prometheus部署安装成功
查看targets页面能看到配置的prometheus本机是up的状态即可
四、配置主机监控-客户端
登录被监控主机
su - root
cd /home/
#上传node_exporter包
cp /nas/node_exporter-1.5.0.linux-amd64.tar.gz .
tar -zxf node_exporter-1.5.0.linux-amd64.tar.gz
rm -f node_exporter-1.5.0.linux-amd64.tar.gz
mv node_exporter-1.5.0.linux-amd64/ node_exporter
#指定的端口号为9100
vi /usr/lib/systemd/system/node-exporter.service
[Unit]
Description=node exporter
After=network.target
[Service]
Type=simple
ExecStart=/home/node_exporter/node_exporter --web.listen-address=:9100
Restart=on-failure
KillMode=process
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl enable --now node-exporter.service
Created symlink from /etc/systemd/system/multi-user.target.wants/node-exporter.service to /usr/lib/systemd/system/node-exporter.service.
netstat -ntpl |grep 9100
tcp6 0 0 :::9100 :::* LISTEN 5505/node_exporter
curl http://192.168.1.2:9100/metrics 能够输出一系列指标则部署成功.
五、配置主机监控-服务端
登录prometheus主机
1.配置监控主机
su - prometheus
mkdir -p prometheus/moniter_config/all_reserver_hosts/
cd prometheus/moniter_config/all_reserver_hosts/
编写配置生成脚本
vi yml_host_create.sh
config()
{
cat > ./$1.yml <<EOF
- targets: [ "$1:9100" ]
labels:
group: "host"
host: "$2"
EOF
}
sed -i '/^$/d' ./hostlist
n=`cat ./hostlist | wc -l`
for ((i=1;i<=${n};i++))
do
j=`sed -n "${i}p" ./hostlist`
config $j
done
hostlist 添加需要监控的主机信息
依次按node_exporter部署地址,主机名(可以写简单易懂的名称)参考如下:
vim hostlist
192.168.2.1 nginx01
192.168.2.2 nginx02
192.168.2.3 nginx03
192.168.2.4 nginx04
后续添加节点只需修改hostlist即可,hostlist中的配置可重复执行
执行sh yml_host_create.sh 完成后会在当前目录下生成ip.yml文件
2.验证
浏览器登录prometheus,如下图在Targets项中可以看到新增的主机,State状态为up
点击Endpoint 项中的http:// IP:9100/metrics 能够跳转到对应的监控数据页则prometheus对接完成,如下图:
六、配置rules告警规则
1.配置规则
su – prometheus
mkdir -p prometheus/rules/host
cd prometheus/rules/host
vi host.yml 插入如下内容:
groups:
- name: Host
rules:
- alert: Host_CPU
expr: 100 * (1 - avg(irate(node_cpu_seconds_total{mode="idle"}[2m])) by(addr)) > 90
for: 5m
labels:
severity: critical
annotations:
summary: "主机 {{$labels.host}} 的CPU使用率达到 {{$value}} ,标准为90%,请尽快查看!"
- alert: Host_Memory
expr: (node_memory_MemTotal_bytes - node_memory_Buffers_bytes - node_memory_Cached_bytes - node_memory_MemAvailable_bytes) / node_memory_MemTotal_bytes * 100 > 90
for: 5m
labels:
severity: critical
annotations:
summary: "主机 {{$labels.host}} 的内存使用率达到 {{$value}} ,标准为90%,请尽快查看!"
- alert: Host_Disk
expr: 100 * (node_filesystem_size_bytes{device!~"rootfs"} - node_filesystem_avail_bytes{device!~"rootfs"}) / node_filesystem_size_bytes{device!~"rootfs"} > 90
for: 5m
labels:
severity: critical
annotations:
summary: "主机 {{$labels.host}} 的磁盘{{$labels.mountpoint}}使用率达到 {{ $value }} ,标准为90%,请尽快查看!"
- alert: Host_load5
expr: node_load5 > 16
for: 5m
labels:
severity: critical
annotations:
summary: "主机 {{$labels.host}} 的5分钟平均负载为 {{ $value }} ,超过30,尽快查看!"
- alert: Host_TCP_tw
expr: node_sockstat_TCP_tw > 5000
for: 5m
labels:
severity: critical
annotations:
summary: "主机 {{$labels.host}} 中等待关闭的TCP连接数达到 {{ $value }} ,尽快查看!"
- alert: Host_TCP_CurrEstab
expr: node_netstat_Tcp_CurrEstab > 3000
for: 5m
labels:
severity: critical
annotations:
summary: "主机 {{$labels.host}} 中当前状态为 ESTABLISHED 或 CLOSE-WAIT 的 TCP 连接数达到 {{ $value }} ,尽快查看!"
- alert: Host_Filesystem_readonly
expr: node_filesystem_readonly > 0
for: 1m
labels:
severity: critical
annotations:
summary: "主机 {{$labels.host}} 磁盘 {{$labels.device}} 挂载目录文件系统只读,无法写入数据,可能是因为磁盘故障或文件系统损坏,尽快查看!"
- alert: Host_is_up
expr: up{group="host"} == 0
for: 15s
labels:
severity: critical
annotations:
summary: "主机 {{ $labels.host }} 停止运行超过15秒!!!"
2.配置规则匹配文件列表
cd ~/prometheus/
vim prometheus.yaml
rule_files:
- "/home/prometheus/prometheus/rules/host/*.yml"
3.重启服务
su - root
systemctl restart prometheus.service
systemctl status prometheus.service
4.验证
登录web界面
查看Alerts界面
能看到读取的rules文件和读取到的规则
绿色为无告警,黄色的为有告警