使用nginx实现prometheus自定义监控

文章讲述了如何使用shell脚本实时抓取Linux系统的内存可用指标,将数据写入Nginx的静态页面,然后通过Nginx提供HTTP服务,最终在Prometheus中创建Job来定期拉取和展示这些监控指标的过程。
摘要由CSDN通过智能技术生成

主要思路:使用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上显示图形

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值