Prometheus+Pushgateway监控内网服务器
因为公司业务及客户安全需要,公司项目大多都是部署在内网中,这就导致了对于服务器及服务的实时状态我们是不知道的,应领导要求监控内网服务器,最终选择了下图方式。
环境:
prometheus/pushgateway:CentOS Linux release 7.9.2009 (Core)
node_exporter:Debian 12.6
要是连图都看不明白,那就麻溜转行吧。
服务端
安装prometheus
[root@golive-monitor ~]# cd /opt/
[root@golive-monitor opt]# ls
prometheus-2.54.0-rc.0.linux-amd64.tar.gz pushgateway-1.9.0.linux-amd64.tar.gz
[root@golive-monitor opt]# tar zxf prometheus-2.54.0-rc.0.linux-amd64.tar.gz -C /usr/local/
[root@golive-monitor opt]# cd /usr/local/
[root@golive-monitor local]# mv prometheus-2.54.0-rc.0.linux-amd64/ prometheus
[root@golive-monitor local]# cd prometheus/
[root@golive-monitor prometheus]# ls
console_libraries consoles LICENSE NOTICE prometheus prometheus.yml promtool
[root@golive-monitor prometheus]# cp prometheus /usr/bin/
设置密码
[root@golive-monitor prometheus]# htpasswd -nBC 12 '' | tr -d ':\n'
New password:
Re-type new password:
$2y$12$tI7axgArleCnWR/a.iiaRezVa70le9c593/v0.eIbZchKQt4TDeii
[root@golive-monitor prometheus]# cat config.yml
basic_auth_users:
admin: $2y$12$tI7axgArleCnWR/a.iiaRezVa70le9c593/v0.eIbZchKQt4TDeii
systemd接管prometheus
[root@golive-monitor prometheus]# cat /etc/systemd/system/prometheus.service
[Unit]
Description=Prometheus Monitoring
Wants=network-online.target
After=network-online.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/bin/prometheus \ # prometheus 可执行文件的实际位置
--config.file=/usr/local/prometheus/prometheus.yml \ #指定 Prometheus 配置文件的位置
--storage.tsdb.path=/usr/local/prometheus/data/ \ #指定存储 TSDB 数据的路径
--storage.tsdb.retention.time=60d \ #数据保留时间设置为 60 天
--web.listen-address=0.0.0.0:9090 \ #监听的地址和端口
--web.config.file=/usr/local/prometheus/config.yml #指定用于 Web 服务器的配置文件
Restart=always
[Install]
WantedBy=multi-user.target
[root@golive-monitor prometheus]# systemctl daemon-reload
[root@golive-monitor prometheus]# ystemctl enable prometheus.service --now
Created symlink /etc/systemd/system/multi-user.target.wants/prometheus.service → /etc/systemd/system/prometheus.service.
安装pushgateway
[root@golive-monitor prometheus]# cd /opt/
[root@golive-monitor opt]# tar zxf pushgateway-1.9.0.linux-amd64.tar.gz -C /usr/local/
[root@golive-monitor local]# mv pushgateway-1.9.0.linux-amd64/ pushgateway
[root@golive-monitor local]# cd pushgateway/
[root@golive-monitor pushgateway]# ls
LICENSE NOTICE pushgateway
[root@golive-monitor pushgateway]# cp pushgateway /usr/bin/
设置密码
[root@golive-monitor pushgateway]# htpasswd -nBC 12 '' | tr -d ':\n'
New password:
Re-type new password:
$2y$12$.SgL8lWI0/e8tW2dgl6EV.4ySHtdIgKBmrmx9JR0qGstZZsci2fp2
[root@golive-monitor pushgateway]# cat config.yml
basic_auth_users:
admin: $2y$12$.SgL8lWI0/e8tW2dgl6EV.4ySHtdIgKBmrmx9JR0qGstZZsci2fp2
systemd接管pushgateway
[root@golive-monitor pushgateway]# cat /etc/systemd/system/pushgateway.service
[Unit]
Description=Prometheus Pushgateway
After=network.target
[Service]
User=root
Group=root
Type=simple
ExecStart=/usr/bin/pushgateway \ # pushgateway 可执行文件的实际位置
--web.listen-address=0.0.0.0:9091 \ #监听的地址和端口
--web.config.file=/usr/local/pushgateway/config.yml #指定用于 Web 服务器的配置文件
Restart=always
RestartSec=10s
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
[root@golive-monitor pushgateway]# systemctl daemon-reload
[root@golive-monitor pushgateway]# systemctl enable pushgateway.service --now
Created symlink from /etc/systemd/system/multi-user.target.wants/pushgateway.service to /etc/systemd/system/pushgateway.service.
配置prometheus抓取pushgateway数据
[root@golive-monitor pushgateway]# cd ../prometheus/
[root@golive-monitor prometheus]# cat prometheus.yml
………………
#在最后添加,注意格式
- job_name: "pushgateway"
static_configs:
- targets: ["192.168.32.26:9091"]
basic_auth:
username: admin
password: 密码
honor_labels: true #告诉 Prometheus 在处理来自目标的指标数据时,保留这些指标数据中的标签。例如,从不同服务或系统中收集监控数据时,可以确保原始标签不会被覆盖。
[root@golive-monitor prometheus]# systemctl restart prometheus.service
被监控端
配置代理服务器
#纯内网肯定监控不到,但是业务所处的内网区域和DMZ区域有一个互通的http端口用于做代理,所以我们先登录代理服务器配置一下转发。
root@proxy:~# cd /usr/local/nginx/conf/vhost/
root@proxy:/usr/local/nginx/conf/vhost# ls
proxy.conf
root@proxy:/usr/local/nginx/conf/vhost# cat proxy.conf
server {
listen 808;
………………
location /pushgateway/ { proxy_pass http://Pushgateway地址; }
}
root@proxy:/usr/local/nginx/conf/vhost# nginx -t
nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok
nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful
root@proxy:/usr/local/nginx/conf/vhost# nginx -s reload
业务端安装node_expoter
root@test:~# cd /opt/
root@test:/opt# tar zxf node_exporter-1.8.2.linux-amd64.tar.gz -C /usr/local/
root@test:/opt# cd /usr/local/
root@test:/usr/local# mv node_exporter-1.8.2.linux-amd64/ node_exporter
root@test:/usr/local# cd node_exporter/
root@test:/usr/local/node_exporter# ls
LICENSE NOTICE node_exporter
root@test:/usr/local/node_exporter# cp node_exporter /usr/bin/
systemd接管node_exporter
root@test:/usr/local/node_exporter# cat /etc/systemd/system/node_exporter.service
[Unit]
Description=Node Exporter
After=network.target
[Service]
User=node_exporter
Group=node_exporter
Type=simple
ExecStart=/usr/bin/node_exporter \
--web.listen-address=127.0.0.1:9100
Restart=always
RestartSec=10s
LimitNOFILE=65536
[Install]
WantedBy=multi-user.target
root@test:/usr/local/node_exporter# systemctl daemon-reload
root@test:/usr/local/node_exporter# systemctl enable node_exporter.service --now
Created symlink /etc/systemd/system/multi-user.target.wants/node_exporter.service -> /etc/systemd/system/node_exporter.service.
推送脚本
root@test:/usr/local/node_exporter# cat node_exporter.sh
#!/bin/bash
# Pushgateway代理的URL
PUSHGATEWAY_URL="XXX"
# Node Exporter的URL
NODE_EXPORTER_URL="http://localhost:9100/metrics"
# 实例名称,可以是机器的IP或主机名
INSTANCE="test" # 替换成你的实例名称
while true; do
# 从 Node Exporter 获取所有 metrics
metrics=$(curl -s "$NODE_EXPORTER_URL")
# 推送数据到 Pushgateway
echo "$metrics" | curl -u "admin:密码" --data-binary @- "$PUSHGATEWAY_URL/metrics/job/pushgateway/instance/$INSTANCE"
# 每次执行后等待15秒
sleep 15
done
使用supervisor守护脚本
root@test:/usr/local/node_exporter# cat /etc/supervisord/node_exporter_sh.ini
[program:node_exporter_sh]
command=/usr/bin/bash /usr/local/node_exporter/node_exporter.sh
autostart=true
autorestart=true
user=root
numprocs=1
redirect_stderr=true
stdout_logfile=/home/wwwlogs/node_exporter_sh.log
root@test:/usr/local/node_exporter# supervisorctl update
Grafana配置
这里就不展示怎么添加数据源了,并且我们直接使用现成模板1860。
最后效果大概就是现在这样。
另外提一下,如果图表不显示,大概率是因为仪表盘表达式问题,可自行解决。