[prometheus]-node-exporter通过textfile上报数据

导语:需要获取cpu频率和温度并上传到prometheus

启动参数

查看node-exporter的github描述,node-exporter启动的时候指定–collector.textfile.directory=/opt/exporter/node_exporter/key 即可, 这个参数会解析指定目录下的所有以prom结尾的文件.

即:

我们只需要把自定义脚本的输出Metrics数据写入到指定目录下的文件,并以Prom结尾即可, Prometheus在定时采集node exporter获取数据的时候,也会把这份数据一同上传, 非常方便。这样就可以用shell了

/etc/systemd/system/node_exporter.service 修改如下

[Unit]
Description=Prometheus node_exporter
[Service]
User=root
ExecStart=/usr/bin/node_exporter --web.listen-address=:19100 --log.level=error --collector.textfile.directory=/data1/exporter/node_exporter/key
ExecStop=/usr/bin/killall node_exporter
MemoryLimit=300M
CPUQuota=100%

[Install]
WantedBy=default.target
#!/bin/bash
  if [ ! -d "/data1/exporter/node_exporter/key/" ];then
     echo dir not exist
     mkdir -p /data1/exporter/node_exporter/key/
  else
     echo  dir exist
  fi
prom_file=/data1/exporter/node_exporter/key/key.prom
cnt_physical_cpu=$(cat /proc/cpuinfo | grep 'physical id' | sort | uniq | wc -l)
cnt_processor=$(cat /proc/cpuinfo | grep 'processor' | wc -l)
#local_hostname=$(hostname -f)
#local_ip=$(hostname -I | cut -d" " -f 1)

#echo $local_hostname
#echo $local_ip
echo "" > $prom_file
index=1
while [ $cnt_physical_cpu -ge $index ]; do
  let cpu_id=index-1
  #echo index $index
  basic_frequency=$(cat /proc/cpuinfo | grep "cpu MHz" | sed -n "${index}p" | awk -F ':' '{print $2}')
  let index+=1
# {cpu="9",mode="idle"}
  echo "cpu_Mhz{cpu=\"$cpu_id\"}$basic_frequency" >> $prom_file
#  echo "pu_Mhz$cpu_id $basic_frequency" | curl   --data-binary @- http://192.168.10.84:9091/metrics/job/cpu_info/instance/$local_ip/hostname/$local_hostname
  # wendu
#  echo cat /sys/class/thermal/thermal_zone$cpu_id/temp
  if [ ! -e "/sys/class/thermal/thermal_zone$cpu_id/temp" ];then
     echo temp file not exist
  else
     temp=$(cat /sys/class/thermal/thermal_zone$cpu_id/temp)
     echo  "cpu_temp{cpu=\"$cpu_id\"} $temp" >> $prom_file
  fi
#  echo  "cpu_temp$cpu_id $temp" | curl   --data-binary @- http://192.168.10.84:9091/metrics/job/cpu_info/instance/$local_ip/hostname/$local_hostname
  # dangqianmoshi
#  echo cat /sys/devices/system/cpu/cpu$cpu_id/cpufreq/scaling_governor
#  cpu_model=$(cat /sys/devices/system/cpu/cpu$cpu_id/cpufreq/scaling_governor)
#  echo "cpu_model$cpu_id $cpu_model"
#  echo "cpu_model$cpu_id $cpu_model" | curl   --data-binary @- http://192.168.10.84:9091/metrics/job/cpu_info/instance/$local_ip/hostname/$local_hostname



done

echo "* * * * * bash /data1/t.1.sh" >> /var/spool/cron/crontabs/root

参考

https://www.cnblogs.com/ggzhangxiaochao/p/13948483.html

https://izsk.me/2019/08/28/Node-Exporter%E9%87%87%E9%9B%86%E8%87%AA%E5%AE%9A%E4%B9%89Metrics/

https://www.cnblogs.com/xiaobaozi-95/p/10684524.html

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爷来辣

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值