概述
node_exporter除了可以收集系统指标外,还可以采集我们自定义的监控指标。采集自定义监控指标是通过textfile模块来完成的,textfile模块默认会随着node_exporter启动而启动,如果想要采集自定义指标,还需要在启动node_exporter的时候,添加–collector.textfile.directory=""参数,这个参数是自定义的采集路径,所有自定义监控指标文件都放在这个目录下,且文件名都以.prom结尾。
自定义指标
启动node_exporter
--创建目录 # mkdir -p /opt/node_exporter/prom --以指定采集路径的方式启动 # nohup /opt/node_exporter/node_exporter --collector.textfile.directory="/opt/node_exporter/prom" > /opt/node_exporter/node_exporter.log 2>&1 &
复制
创建监控指标文件
# cd /opt/node_exporter/prom # vi db_heartbeat.prom --HELP 和 TYPE 如果没有制定,node_exporter会自动添加 # HELP db_select Metric read from /opt/node_exporter/prom/db_heartbeat.prom # TYPE db_select untyped db_select{database="172.16.3.90:5432"} 1 db_select{database="172.16.3.90:7432"} 0
复制
在浏览器中可以看到,我们自定义的指标已经采集到
定时任务
自定义监控指标大多数需要与crontab结合,按着需求设置采集指标的时间。
flock命令
为了防止某个任务的执行时间超过了 crontab 中为此任务设定的执行周期,使用flock命令将crontab串行化:
flock -xn /tmp/flock.lock -c ‘xxx.sh’ --如果/tmp/flock.lock不存在,flock会自动创建
Usage: flock [options] <file|directory> <command> [command args] flock [options] <file|directory> -c <command> flock [options] <file descriptor number> Options: -s --shared get a shared lock -x --exclusive get an exclusive lock (default) -u --unlock remove a lock -n --nonblock fail rather than wait -w --timeout <secs> wait for a limited amount of time -E --conflict-exit-code <number> exit code after conflict or timeout -o --close close file descriptor before running command -c --command <command> run a single command string through the shell -h, --help display this help and exit -V, --version output version information and exit For more details see flock(1).
复制
MogDB 探活脚本
通过sql(select 1;)进行探活
vi /opt/scripts/db_heartbeat.sh #!/bin/bash source /home/omm/.bashrc nums=( 172.16.3.90:5432:opengauss_exporter:opengauss_exporter123 172.16.3.90:7432:opengauss_exporter:opengauss_exporter123 ) for i in $(seq 0 $[${#nums[*]}-1]) do ip=`echo ${nums[$i]}|awk -F ':' '{print $1}'` port=`echo ${nums[$i]}|awk -F ':' '{print $2}'` username=`echo ${nums[$i]}|awk -F ':' '{print $3}'` password=`echo ${nums[$i]}|awk -F ':' '{print $4}'` result=`gsql "host=$ip port=$port user=$username password=$password dbname=postgres" -t -c "select 1"` if [ $? -eq 0 ]; then echo "db_select{database=\"$ip:$port\"} 1" >> /opt/node_exporter/prom/db_heartbeat.prom else echo "db_select{database=\"$ip:$port\"} 0" >> /opt/node_exporter/prom/db_heartbeat.prom fi done
复制
MogHA状态检测脚本
vi /opt/scripts/mogha_status.sh --需要部署在每一台mogha所在的服务器上 #!/bin/bash ip=`ip addr |grep -i global|head -1|awk '{print$2}'|cut -d / -f 1` mogha_status=`systemctl status mogha |grep -i 'Active:' |awk -F ' ' '{print $2}'` if [ "$mogha_status" == 'active' ]; then echo "mogha_status {database=\"$ip\"} 1" >> /opt/node_exporter/prom/mogha_status.prom else echo "mogha_status {database=\"$ip\"} 0" >> /opt/node_exporter/prom/mogha_status.prom fi --部署在prometheus服务器上即可,通过探测端口判断mogha状态 #!/bin/bash nums=( 172.16.3.90:8081 172.16.3.91:8081 ) for i in $(seq 0 $[${#nums[*]}-1]) do ip=`echo ${nums[$i]}|awk -F ':' '{print $1}'` port=`echo ${nums[$i]}|awk -F ':' '{print $2}'` result=`(sleep 1;) | telnet $ip $port|grep "]"|wc -l` if [ $result -eq 0 ]; then echo "mogha_status {database=\"$ip\"} 0" >> /opt/node_exporter/prom/mogha_status.prom else echo "mogha_status {database=\"$ip\"} 1" >> /opt/node_exporter/prom/mogha_status.prom fi done
复制
crontab
--执行脚本之前,先清理.prom文件,防止监控指标重复 * * * * * /usr/bin/flock -xn /tmp/flock.lock -c ">/opt/node_exporter/prom/db_heartbeat.prom && /usr/bin/bash /opt/scripts/db_heartbeat.sh >> /opt/scripts/db_heartbeat.log" * * * * * /usr/bin/flock -xn /tmp/flock1.lock -c ">/opt/node_exporter/prom/mogha_status.prom && /usr/bin/bash /opt/scripts/mogha_status.sh >> /opt/scripts/mogha_status.log"
复制
最后修改时间:2022-02-11 21:27:37
「喜欢这篇文章,您的关注和赞赏是给作者最好的鼓励」
关注作者
【版权声明】本文为墨天轮用户原创内容,转载时必须标注文章的来源(墨天轮),文章链接,文章作者等基本信息,否则作者和墨天轮有权追究责任。如果您发现墨天轮中有涉嫌抄袭或者侵权的内容,欢迎发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。
文章被以下合辑收录
评论
您好,您的文章已入选合格奖,10墨值奖励已经到账请查收!
❤️我们还会实时派发您的流量收益。
3年前

评论
相关阅读
2025年3月国产数据库大事记
墨天轮编辑部
892次阅读
2025-04-03 15:21:16
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
MogDB
289次阅读
2025-04-17 10:41:41
openGauss 7.0.0-RC1 版本正式发布!
Gauss松鼠会
207次阅读
2025-04-01 12:27:03
MogDB 发布更新,解决 openGauss 数据库在长事务情况下Ustore表膨胀问题
云和恩墨
189次阅读
2025-04-16 09:52:02
openGauss 7.0.0-RC1 版本体验:一主一备快速安装指南
孙莹
180次阅读
2025-04-01 10:30:07
鲲鹏RAG一体机解决方案正式发布 openGauss DataVec向量数据库助力DeepSeek行业应用
Gauss松鼠会
124次阅读
2025-03-31 10:00:29
荣誉时刻!openGauss认证证书快递已发,快来看看谁榜上有名!
墨天轮小教习
113次阅读
2025-04-23 17:39:13
openGauss6.0.0适配操作系统自带的软件,不依赖三方库
来杯拿铁
76次阅读
2025-04-18 10:49:53
opengauss使用gs_probackup进行增量备份恢复
进击的CJR
70次阅读
2025-04-09 16:11:58
GitCode 成 openGauss 新归宿,国产开源数据库里程碑事件
严少安
64次阅读
2025-04-27 11:37:53