Prometheus自带的exporter模板覆盖了很多监控功能了,但是免不了业务需要做一些特殊监控,这需要使用到pushgateway实现自定义监控功能。
结合上一篇文章的机器,在172.24.8.95安装prometheus的机器上安装pushgateway。
一、安装pushgateway
wget https://github.com/prometheus/pushgateway/releases/download/v1.4.3/pushgateway-1.4.3.linux-amd64.tar.gz
tar -zxvf pushgateway-1.4.3.linux-amd64.tar.gz
cd pushgateway-1.4.3.linux-amd64 && cp pushgateway /usr/bin
cat > /usr/lib/systemd/system/pushgateway.service < [Unit] Description=pushgateway Documentation=https://github.com/prometheus/pushgateway After=network.target [Service] ExecStart=/usr/bin/pushgateway Restart=on-failure [Install] WantedBy=multi-user.target EOF systemctl daemon-reload && systemctl enable pushgateway && systemctl start pushgateway && systemctl status pushgateway 二、编辑prometheus.yml vim /usr/local/prometheus/prometheus.yml #自定义监控 - job_name: "pushgateway" honor_labels: true file_sd_configs: - files: - targets/pushgateway/*.json refresh_interval: 5m #说明 #true #当为true时,prometheus在拉取时会使用pushgateway上的job名称和instance的标签 #false #当为false时,prometheus在拉取时会添加exported_的前缀,并在服务器上为这些标签添加新值 systemctl restart prometheus 三. 数据推送 #格式 http:// #我们推送一个指标做测试 echo 'metrics_name 1' | curl --data-binary @- http://172.24.8.95:9091/metrics/job/test1/ cd /usr/local/prometheus vim memory.sh #编辑脚本推送数据 #!/bin/bash total_memory=$(free |awk '/Mem/{print $2}') used_memory=$(free |awk '/Mem/{print $3}') job_name="custom_memory" instance_name="172.24.8.95" cat < custom_memory_total $total_memory custom_memory_used $used_memory EOF sh memory.sh #把数据推送到pushgateway中,如下图: corntab -e #设置定时任务10秒种持续推送 * * * * * sleep 10 /usr/bin/bash /usr/local/prometheus/memory.sh 检查已有数据推送过来 把自定义监控用grafana展现: 1、点击Explore 2、添加在脚本中设置的:custom_memory_total 和 custom_memory_used 的监控 点击Run query,把页面拉到底下,会出现监控信息 点击Add to dashboard ,即第5点,即可保存设置的监控 还需要点击Dashboard settings,对设置好的监控模板进行保存
留言与评论(共有 0 条评论)
“”