prometheus自定义metrics的key值取不到
今天碰到一个redis info得出来的文件prom里的key格式明明是对的,Prometheus却始终取不到值.
后来想想要么后缀名不对,目录不对,或者是不是格式不对,cat -A redis_key.prom,果然返回来的格式不对,里面多了"^M"windows换行符.特此记录分享下.
解决办法
keytmp="/home/redis.tmp"
keyfile="/data/script/key-node-exporter/rredis.prom"
keypass=`cat /etc/redis/6379.conf |grep -v "^#"|grep requirepass|awk '{print $2}'`
#获取redis info的状态值
redis-cli -h 127.0.0.1 -p 6379 -a $keypass info 2>/dev/null >$keytmp.tmp
#删除windows换行符
tr -d "\015" <$keytmp.tmp >$keytmp
#cat redis.tmp |grep connected_clients|awk -F: '{print $2}'
echo "redis_connected_clients `cat $keytmp|grep connected_clients|awk -F: '{print $2}'`">$keyfile
附docker-compose启动配置文件,我的自定义key都放在/data/script/key-node-exporter/*.prom
docker-compose.yml
version: '3'
services:
node-exporter:
image: prom/node-exporter
container_name: node-exporter
hostname: node-exporter
restart: unless-stopped
ports:
- "9100:9100"
command:
- '--path.procfs=/host/proc'
- '--path.sysfs=/host/sys'
- '--collector.filesystem.ignored-mount-points=^/(sys|proc|dev|host|etc)($$|/)'
- '--collector.textfile.directory=/node_exporter-key'
- '--path.rootfs=/rootfs'
volumes:
- /proc:/host/proc:ro
- /sys:/host/sys:ro
- /:/rootfs:ro
- /data/script/key-node-exporter:/node_exporter-key
curl http://IP:9100/metrics