ほっしーの技術ネタ備忘録

技術ネタの備忘録です。基本的に私が忘れないためのものです。他の人の役にも立つといいなぁ。

CPU 温度を取得する

mrtg とかで CPU の温度をプロットしようかと思って。
最近の Intel の CPU なので、使用するカーネルモジュールは coretemp でいいらしい。

/boot/loader.conf に以下の行を追加。

coretemp_load="YES"

再起動せずに有効にするならさらに

$ sudo kldload coretemp

で、結果は sysctl で出てきます。

$ sysctl dev.cpu.0.temperature
dev.cpu.0.temperature: 35.0C

あとはこれをいい感じにロギングすれば OK。
まずはロギングするスクリプトを cron で 5 分おきに実行。

#!/bin/sh

for i in `jot 290`; do
  /sbin/sysctl -n dev.cpu.0.temperature dev.cpu.1.temperature | tr -d 'C' | tr '\n' '\t' >> /tmp/log/$$.log
  echo >> /tmp/log/$$.log
  sleep 1
done

/bin/mv /tmp/log/$$.log /tmp/log/cputemp_last.log

で、mrtg から実行するたびに awk で集計。

#!/bin/sh
/usr/bin/awk '{t=t+$2}END{print t*100/NR}' /tmp/log/cputemp_last.log
/usr/bin/awk '{if($2>m)m=$2}END{print m*100}' /tmp/log/cputemp_last.log

コアの最大温度と5分間(-10秒)の平均温度を記録します。
小数点以下もざっくり見ようかと100倍してあります。