システムの温度変化を、手っ取り早くグラフ化して監視する。
lm-sensors : 温度、電圧、ファン回転数等を得るツール
マザーボード上に、lm-sensorsが対応する計測ICチップ(LM75等)が実装されている場合にしか使えない。
■ Fedora, CentOSの場合
インストール
# yum install lm_sensors
初期設定 (センサーの初期値などの入力を促されるが、すべてデフォルト値=ENTERキーを押しておけば問題ない)。
初期設定完了後、/etc/modprobe.conf, /etc/rc.local, /etc/conf.d/lm_sensors, /etc/modules-load.d/lm_sensors.confなどのファイルにカーネルドライバの読込設定行を付け加えるかどうか聞いてくるので、デフォルト値=ENTERを無意識に連打するのではなく、画面を注視すること。
# sensors-detect
■ Ubuntuの場合
インストール
$ sudo apt-get install lm-sensors
初期設定 (注意点はFedora/CentOSの場合と同じで、カーネルドライバの設定ファイルが/etc/modulesである点が違うだけ)
$ sudo sensors-detect 〜 中略(監視するセンサーの選択など、質問にはデフォルト値=ENTERキーで構わない) 〜 To make the sensors modules behave correctly, add these lines to /etc/modules: #----cut here---- # I2C adapter drivers i2c-i801 # Chip drivers # no driver for Fintek custom power control IC yet eeprom w83627hf #----cut here----
■ 各センサーの名称変更、正常値範囲の設定など
設定ファイルは /etc/sensors.conf (または/etc/sensors3.conf)なので、必要であれば値を適宜修正する
… (前略) …
chip "w83627thf-*" "w83637hf-*"
label in0 "VCore"
label in1 "+12V"
label in2 "+3.3V"
label in3 "+5V"
label in4 "-12V"
label in7 "V5SB"
label in8 "VBat"
compute in1 ((28/10)+1)*@, @/((28/10)+1)
compute in3 ((34/51)+1)*@, @/((34/51)+1)
compute in4 (5.14*@)-14.91, (@+14.91)/5.14
compute in7 ((6.8/10)+1)*@ , @/((6.8/10)+1)
set in0_min 0.988 * 0.98 # Pentium M 750 Vccmax
set in0_max 1.356 * 1.00 # Pentium M 750 Vcclfm
set in1_min 12 * 0.95
set in1_max 12 * 1.05
set in2_min 3.3 * 0.95
set in2_max 3.3 * 1.05
set in3_min 5.0 * 0.95
set in3_max 5.0 * 1.05
set in4_min -12 * 1.10
set in4_max 0
set in7_min 5 * 0.95
set in7_max 5 * 1.05
set in8_min 3.0 * 0.80
set in8_max 3.0 * 1.20
# set sensor1 1
# set sensor2 2
# set sensor3 3435
label temp1 "M/B Temp"
label temp2 "CPU Temp"
ignore temp3
set temp1_over 45
set temp1_hyst 35
set temp2_over 60
set temp2_hyst 50
label fan1 "Case Fan"
label fan2 "CPU Fan"
ignore fan3
set fan1_div 8
set fan1_min 1400 # fanx_min must be written after fanx_div
set fan2_div 8
set fan2_min 2000
set beep_enable 0
■ lm-sensorsデーモンの起動と、測定値の表示
Linuxを再起動するか、次の方法で強制的にカーネルドライバを読み込んでデーモンを常駐させる。
Fedora/CentOS の場合
# /etc/rc.d/init.d/lm_sensors restart
Ubuntuの場合
$ sudo modprobe i2c-i801 $ sudo modprobe eeprom $ sudo modprobe w83627hf $ sudo service lm-sensors restart
端末上で現在のセンサーの計測値を表示する
# sensors
w83627thf-isa-0290 ← マシンのセンサー名(sensors.confではこのセンサー項目を編集すれば良い)
Adapter: ISA adapter
VCore: +1.00 V (min = +0.97 V, max = +1.35 V)
+12V: +12.10 V (min = +11.37 V, max = +12.59 V)
+3.3V: +3.38 V (min = +3.14 V, max = +3.47 V)
+5V: +4.96 V (min = +4.75 V, max = +5.25 V)
-12V: -5.62 V (min = -13.18 V, max = -0.02 V)
V5SB: +5.00 V (min = +4.76 V, max = +5.24 V)
VBat: +3.30 V (min = +2.40 V, max = +3.60 V)
Case Fan: 1577 RPM (min = 1394 RPM, div = 8) (beep)
CPU Fan: 2191 RPM (min = 2008 RPM, div = 8) (beep)
M/B Temp: +26°C (high = +45°C, hyst = +35°C) sensor = thermistor
CPU Temp: +41.0°C (high = +60°C, hyst = +50°C) sensor = diode (beep)
alarms:
beep_enable:
Sound alarm disabled
ここまでの情報は @ITの次のトピックに書かれている
「CPUの温度やファンの回転数などをモニタするには」
http://www.atmarkit.co.jp/flinux/rensai/linuxtips/743hwmonitor.html
hddtemp : ハードディスク温度を得るツール
インストール
sudo apt-get install hddtemp
非管理者(一般ユーザ)が値を表示できるようにする
sudo chmod u+s /usr/bin/hddtemp
値の表示
hddtemp /dev/sda /dev/sda: ST1000DM003-9YN162: 29°C
ネットワークコマンドで値を表示できるよう、デーモンを有効にする
# hddtemp network daemon switch. If set to true, hddtemp will listen
# for incoming connections.
RUN_DAEMON="true"
値の表示
nc 127.0.0.1 7634 |/dev/sda|ST1000DM003-9YN162|26|C|
MRTG : 観測値の時系列グラフ、HTML化ツール (cronで定期的に実行する)
MRTGのインストール
# sudo apt-get install mrtg # cfgmaker public@localhost --global "WorkDir: /var/www/mrtg" --output /etc/mrtg/mrtg.cfg
設定ファイルをいじる
### Global Config Options
# for Linux
WorkDir: /var/www/mrtg
# or for NT
# WorkDir: c:\mrtgdata
### Global Defaults
Options[_]: growright,unknaszero
EnableIPv6: no
# HTML Language
Language:iso2022jp
# eth0転送量のログ ~ cfgmaker が自動生成したものを改変 ~
Target[network]: #eth0:public@localhost:
SetEnv[network]: MRTG_INT_IP="192.168.1.200" MRTG_INT_DESCR="eth0"
MaxBytes[network]: 62500
Title[network]: Network Interface Log (MRTG)
PageTop[network]: Network Interface
Options[network]: growright,nopercent,unknaszero,bits
# CPU, システム温度のログ(lm_sensorsを用いる)
Target[cputemp]: `/usr/bin/sensors | grep Temp | awk '{print $3}' | sed -e 's/+//' -e 's/[^0-9+\.].*//' && echo && uname -n`
MaxBytes[temp]: 60
Title[temp]: System Temperature Log
PageTop[temp]: System Temperature Log
Options[temp]: growright,absolute,gauge,nopercent,unknaszero,integer
WithPeak[temp]: ymw
Unscaled[temp]: dwmy
YLegend[temp]: Temperature
ShortLegend[temp]: deg-C
LegendI[temp]: System Temp
LegendO[temp]: CPU Temp
Legend1[temp]: System Temp
Legend2[temp]: CPU Temp
MRTGのマニュアルによれば、Target キーワードに外部コマンドを用いる場合は、計測された測定値を4行で出力させる必要性があるそうだ。
1行目:LegendI(数値)、2行目:LegendO(数値)、3行目:稼働時間(文字列)、4行目:対象名(文字列)。
MRTGを5分おきに起動させるために、cronの設定ファイルが次の様になっていることを確認する。(インストーラが自動設定しているはず)
*/5 * * * * root LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok
HTMLファイルは/etc/mrtg/mrtg.cfgのWorkDirで設定されたディレクトリに生成される。
ここまでの情報は @ITの次のトピックに書かれている
「MRTGでCPUの温度変化をグラフ化するには」
http://www.atmarkit.co.jp/flinux/rensai/linuxtips/797mrtg2cpu.html
MRTG設定ファイルの記述方法は
http://www.mrtg.jp/
■ CPU利用率をグラフにしたい場合
Target[cpuload]: `snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.11.10|awk '{print $4}' && snmpwalk -v 1 localhost -c public .1.3.6.1.4.1.2021.11.9|awk '{print $4}' && echo && uname -n`
MaxBytes[cpuload]: 100
Title[cpuload]: CPU Load Log (MRTG)
PageTop[cpuload]: CPU Load Log
Options[cpuload]: growright,absolute,gauge,nopercent,unknaszero,integer
WithPeak[cpuload]: ymw
Unscaled[cpuload]: dwmy
YLegend[cpuload]: CPU Load
ShortLegend[cpuload]: %
LegendI[cpuload]: System
LegendO[cpuload]: User
Legend1[cpuload]: System
Legend2[cpuload]: User
■ HDDの温度変化もグラフにしたい場合
MRTG設定ファイル
Target[hddtemp]: `/usr/bin/sensors | grep "M/B Temp" | awk '{print $3}' | sed -e 's/+//' -e 's/[^0-9+\.].*//' && /usr/sbin/hddtemp /dev/sda | awk '{print $3}'|sed -e 's/[^0-9+\.].*//' && uptime && df | grep sda`
MaxBytes[hddtemp]: 100
Title[hddtemp]: HDD and Motherboard Temperature by lm_sensors and hddtemp
PageTop[hddtemp]: <h1>HDD and Motherboard Temperature</h1>
Options[hddtemp]: growright,absolute,gauge,nopercent
WithPeak[hddtemp]: ymw
#unscaled[hddtemp]: dwmy
YLegend[hddtemp]: Temperature
ShortLegend[hddtemp]: C
LegendI[hddtemp]: M/B
LegendO[hddtemp]: HDD
Legend1[hddtemp]: M/B
Legend2[hddtemp]: HDD
Legend3[hddtemp]: system uptime
Legend4[hddtemp]: partition df
■ そのほかのデータをSNMP経由で知りたいとき…
snmpwalkで必要なMIBを探し出し、表示されたラベルをMRTGで利用できる形に変換する
# snmpwalk -v 1 -c public localhost .1.3.6.1.4.1.2021 | less # snmptranslate -On UCD-DISKIO-MIB::diskIONRead.17
■ uptimeをhtmlファイルに出力したい場合
uptimeを出力する次のようなプログラムを作成する。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define BUFFER_SIZE 256
int main(void)
{
FILE *fi;
char strUptime[BUFFER_SIZE], strBuffer[BUFFER_SIZE];
long intUptimeSec;
if((fi = fopen("/proc/uptime", "rt")) == NULL)
{ /* ファイルが開けない場合、エラー終了 */
printf("error\n");
exit(EXIT_FAILURE);
}
fgets(strBuffer, BUFFER_SIZE-1, fi);
fclose(fi);
strcpy(strUptime, strtok(strBuffer, " "));
intUptimeSec = strtol(strUptime, NULL, 10);
printf("%ld days %02ld:%02ld:%02ld\n", intUptimeSec/(24*60*60),
intUptimeSec%(24*60*60)/(60*60), intUptimeSec%(60*60)/60, intUptimeSec%60);
exit(EXIT_SUCCESS);
}
そして、mrtgのデータを入力する行を次のようにする。(ここでの例は、システム温度の設定行)
Target[temp]: `/usr/bin/sensors | grep Temp | awk '{print $3}' | sed -e 's/+//' -e 's/[^0-9+\.].*//' && /usr/local/bin/get_uptime && uname -n`
■ 関連記事
・(Linux) Ubuntu 9.10 kernel 2.6.31でlm-sensorsから情報取得できない