导语:需要获取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