node_exporter自定义监控项

在日常运维工作中,我们时常跟监控打交道,近些年监控平台从Zabbix往Prometheus转,这里跟容器业务应用得越来越多有比较大的关系。

node_exporter提供了很多基础的监控指标,如:CPU、内存、磁盘、网卡等;可实际工作中业务是多种多样的,总免不了定制一些自定义的监控指标,那如何在node_export中添加自定义监控项呢?

操作步骤:

1、自定义任务生成Prometheus可以识别的*.prom文件,格式为 key:value

root@Worker-10-12-213-205:~# cat /opt/apps/prometheus/scripts/mminer_process.sh
#!/bin/bash
curdate=$(date +%Y%m%d)
num=$(ps -ef |grep massminer |grep -v grep|wc -l)
num_w=$(ps -ef |grep masswallet |grep -v grep|wc -l)
poc_block=$(grep "block submitted via PoC miner accepted" /home/mass/massminer/logs/mass-${curdate}*.log | wc -l)
miner_count=$(grep -E '"find qualities for next block, waiting for proper slot" binding_count=([1-9]+)' /home/mass/massminer/logs/mass-${curdate}*.log | wc -l)
massminer_localbaseheight=$(/home/mass/massminer/massminercli --log_level error getclientstatus | jq ".localBestHeight" | bc -l)
massminer_knownbaseheight=$(/home/mass/massminer/massminercli --log_level error getclientstatus | jq ".knownBestHeight" | bc -l)
echo "masswallet_process ${num_w}" >> ${prom}
echo "#HELP poc_block read from /opt/apps/proetheus/node-exporter/mminer.prom" >> ${prom}
echo "#TYPE poc_block untyped" >> ${prom}
echo "poc_block ${poc_block}" >> ${prom}
echo "#HELP miner_count read from /opt/apps/proetheus/node-exporter/mminer.prom" >> ${prom}
echo "#TYPE miner_count untyped" >> ${prom}
echo "miner_count ${miner_count}" >> ${prom}
echo "#HELP massminer_localbaseheight read from /opt/apps/proetheus/node-exporter/mminer.prom" >> ${prom}
echo "#TYPE massminer_localbaseheight untyped" >> ${prom}
echo "massminer_localbaseheight ${massminer_localbaseheight}" >> ${prom}
echo "#HELP massminer_knownbaseheight read from /opt/apps/proetheus/node-exporter/mminer.prom" >> ${prom}
echo "#TYPE massminer_knownbaseheight untyped" >> ${prom}
echo "massminer_knownbaseheight ${massminer_knownbaseheight}" >> ${prom}
exit 0

2、定期执行自定义任务,如:crontab或程序自定义

root@Worker-10-12-213-205:~# crontab -l
*/1 * * * * /opt/apps/prometheus/scripts/mminer_process.sh

3、启动node_exporter时,通过–collector.textfile.directory指定*.prom所在目录

/opt/apps/prometheus/bin/node_exporter --web.listen-address=10.12.214.65:9100 --collector.textfile.directory=/opt/apps/prometheus/node-exporter/

正常完成这个步骤后,可以通过node_exporter的9100端口,就能查看已收集的指标。

发表评论

邮箱地址不会被公开。 必填项已用*标注