主要思路:使用shell脚本抓取监控指标的值,写入文件中,使用nginx提供http服务,在prometheus中创建job拉取nginx暴露的metrics。一个不太成熟的想法,后期会慢慢完善。
shell脚本抓取指标
[root@134 html]# cat ~/monitor.sh
#!/bin/bash
while true
do
mem_avail=`free|awk '/Mem/{print $NF}'`
sed -i "/^node_memory_MemAvailable_bytes/c\node_memory_MemAvailable_bytes ${mem_avail}" /usr/share/nginx/html/metrics
sleep 10
done
nginx静态页面
[root@134 html]# cat /usr/share/nginx/html/metrics
# HELP node_memory_MemAvailable_bytes Memory information field MemAvailable_bytes.
# TYPE node_memory_MemAvailable_bytes gauge
node_memory_MemAvailable_bytes 776720
nginx配置
[root@134 html]# cat /etc/nginx/conf.d/monitor.conf
server {
listen 9109;
location / {
root /usr/share/nginx/html;
add_header Content-Type text/plain;
}
}
启动nginx
[root@134 nginx]# nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful
[root@134 nginx]# systemctl start nginx
在prometheus服务器上测试获取监控指标
[root@prometheus prometheus]# curl http://192.168.44.134:9109/metrics
# HELP node_memory_MemAvailable_bytes Memory information field MemAvailable_bytes.
# TYPE node_memory_MemAvailable_bytes gauge
node_memory_MemAvailable_bytes 776588
执行shell脚本
[root@134 html]# nohup sh ~/monitor.sh &>/dev/null &
[root@134 html]# jobs
[1]+ Running nohup sh ~/monitor.sh &>/dev/null &
在prometheus中添加job
访问prometheus的web页面,使用promQL语句查询
在prometheus上显示图形