mrtgでネットワーク通信量,CPU負荷やPC各部の温度などをグラフ化

Home > Software > ソフトウエア開発・サーバ管理のメモ帳 > このページ

Last Update 2023/08/22

20070408-cpu.png

 

 

CPU負荷・温度・ネットワーク転送量などをマシンから読み出し、MRTGでグラフ化してhtmlファイルに出力する方法のメモ

snmpd, lm_sensorsのセットアップと基本的な使い方も記している

なお、Linuxマシンでは標準機能として簡単にセットアップできるが、Windowsマシンでは公式・非公式の各種ソフトウエアを組み合わせる必要があり少し敷居が高く一般的ではない
そのため、このページのWindows向け情報は2008年ごろで停止し更新を行っていない

 

 

もくじ

 

 

Linuxマシンでの設定

検証環境 : Ubuntu 22.04 (2023年8月現在の情報)

SNMP

snmpdとsnmpツールのインストール

SNMPのサーバ機能だけをインストールする場合。mrtgのみの利用であれば、snmpdだけをインストールすれば事足りる

$ sudo apt install snmpd

SNMPの情報をコマンドラインから読み出すクライアント機能だけ場合

※ MIBを使わず、OIDだけでよいのならsnmp-mibs-downloaderのインストールは不要

$ sudo apt install snmp snmp-mibs-downloader

 

snmpdの設定

インストール直後のsnmpd.conf

$ sudo cat /etc/snmp/snmpd.conf | grep -v ^# | grep -v ^$
sysLocation    Sitting on the Dock of the Bay
sysContact     Me <me@example.org>
sysServices    72
master  agentx
agentaddress  127.0.0.1,[::1]
view   systemonly  included   .1.3.6.1.2.1.1
view   systemonly  included   .1.3.6.1.2.1.25.1
rocommunity  public default -V systemonly
rocommunity6 public default -V systemonly
rouser authPrivUser authpriv -V systemonly
includeDir /etc/snmp/snmpd.conf.d

下線を引いた部分で、loopback(自分自身)以外からのアクセス制限をしているが、他のPCの値を読み出したい場合はここを変更すればよい

ネットワークの情報にアクセスするため、設定を加える必要がある。具体的には赤で着色した部分を加えるとネットワーク情報が得られる

どのOIDサブツリーを有効化するかの参考資料としては、「SNMPによるネットワークモニタリング」(ITmedia エンタープライズ)が分かりやすい。OIDのリファレンスは「Reference record for OID 1.3.6.1」などを参照

#  system + hrSystem groups only
view   systemonly  included   .1.3.6.1.2.1.1
view   systemonly  included   .1.3.6.1.2.1.25.1

##### add (from here)
# interfaces
view   systemonly  included   .1.3.6.1.2.1.2
# at
view   systemonly  included   .1.3.6.1.2.1.3
# ip
view   systemonly  included   .1.3.6.1.2.1.4
# icmp
view   systemonly  included   .1.3.6.1.2.1.5
# tcp
view   systemonly  included   .1.3.6.1.2.1.6
# udp
view   systemonly  included   .1.3.6.1.2.1.7
# ucdavis
view   systemonly  included   .1.3.6.1.4.1.2021
##### add (to here)

snmpd.conf の編集が終了したら、snmpdを再起動する

$ sudo systemctl restart snmpd

 

snmpクライアントツールの初期設定

MIBを表示できるように、/etc/snmp/snmp.conf を修正する

/etc/snmp/snmp.conf
# 修正前
#mibs :

# 修正後
mibs ALL

snmpwalk実行時に次のようなエラー表示されることがある

$ snmpwalk -v1 localhost -c public network
Bad operator (INTEGER): At line 73 in /usr/share/snmp/mibs/ietf/SNMPv2-PDU
End of MIB

これは、MIBの定義ファイル SNMPv2-PDU にエラーがあるため。Google検索すると、修正用ファイルが配布されているので、それを参考にして修正を行えばよい(野良ファイルなので、内容をよく吟味してから置き換えること)

ここでは、ダウンロードした修正ファイルと現状の差分を表示している。これで良ければ、手動で修正すれば良い

$ wget -O SNMPv2-PDU http://pastebin.com/raw.php?i=p3QyuXzZ

$ diff -b ./SNMPv2-PDU /usr/share/snmp/mibs/ietf/SNMPv2-PDU
73c73
< -- max-bindings INTEGER ::= 2147483647
---
> max-bindings INTEGER ::= 2147483647
100c100
<             INTEGER (0..2147483647),
---
>             INTEGER (0..max-bindings),
108,109c108,109
<         non-repeaters   INTEGER (0..2147483647),
<         max-repetitions INTEGER (0..2147483647),
---
>         non-repeaters   INTEGER (0..max-bindings),
>         max-repetitions INTEGER (0..max-bindings),
111,112c111
<             -- VarBindList
< 	    SEQUENCE (SIZE (0..2147483647)) OF VarBind
---
>             VarBindList
132c131
< -- VarBindList ::= SEQUENCE (SIZE (0..2147483647)) OF VarBind
---
> VarBindList ::= SEQUENCE (SIZE (0..max-bindings)) OF VarBind

$ sudo cp ./SNMPv2-PDU /usr/share/snmp/mibs/ietf/SNMPv2-PDU

 

snmpwalkコマンドの使い方

かんたんな使い方を次に示す

全体表示、OIDの前方一致検索、OID/MIB表示の切り替え(-On スイッチ)、MIB完全一致検索は次のように行える

$ snmpwalk -v 2 -On localhost -c public
.1.3.6.1.2.1.1.1.0 = STRING: Linux Ubuntu2204M720q 6.2.0-26-generic #26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jul 13 16:27:29 UTC 2 x86_64
.1.3.6.1.2.1.1.2.0 = OID: .1.3.6.1.4.1.8072.3.2.10
.1.3.6.1.2.1.1.3.0 = Timeticks: (364331) 1:00:43.31
.1.3.6.1.2.1.1.4.0 = STRING: Me 
.1.3.6.1.2.1.1.5.0 = STRING: Ubuntu2204M720q
.1.3.6.1.2.1.1.6.0 = STRING: Sitting on the Dock of the Bay

〜 途中省略 〜

.1.3.6.1.2.1.25.1.5.0 = Gauge32: 1
.1.3.6.1.2.1.25.1.6.0 = Gauge32: 297
.1.3.6.1.2.1.25.1.7.0 = INTEGER: 0
.1.3.6.1.2.1.25.1.7.0 = No more variables left in this MIB View (It is past the end of the MIB tree)

$ snmpwalk -v 1 -On localhost -c public 1.3.6.1.2.1.25
.1.3.6.1.2.1.25.1.1.0 = Timeticks: (373933) 1:02:19.33
.1.3.6.1.2.1.25.1.2.0 = STRING: 2023-8-25,23:14:17.0,+9:0
.1.3.6.1.2.1.25.1.3.0 = INTEGER: 393216
.1.3.6.1.2.1.25.1.4.0 = STRING: "BOOT_IMAGE=/boot/vmlinuz-6.2.0-26-generic root=UUID=82a50900-6d82-4639-80c2-4c3e2caf2156 ro quiet splash vt.handoff=7
"
.1.3.6.1.2.1.25.1.5.0 = Gauge32: 1
.1.3.6.1.2.1.25.1.6.0 = Gauge32: 296
.1.3.6.1.2.1.25.1.7.0 = INTEGER: 0
End of MIB

$ snmpwalk -v 1 localhost -c public 1.3.6.1.2.1.25
HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (380216) 1:03:22.16
HOST-RESOURCES-MIB::hrSystemDate.0 = STRING: 2023-8-25,23:15:20.0,+9:0
HOST-RESOURCES-MIB::hrSystemInitialLoadDevice.0 = INTEGER: 393216
HOST-RESOURCES-MIB::hrSystemInitialLoadParameters.0 = STRING: "BOOT_IMAGE=/boot/vmlinuz-6.2.0-26-generic root=UUID=82a50900-6d82-4639-80c2-4c3e2caf2156 ro quiet splash vt.handoff=7
"
HOST-RESOURCES-MIB::hrSystemNumUsers.0 = Gauge32: 1
HOST-RESOURCES-MIB::hrSystemProcesses.0 = Gauge32: 296
HOST-RESOURCES-MIB::hrSystemMaxProcesses.0 = INTEGER: 0
End of MIB

$ snmpwalk -v 1 localhost -c public hrSystemUptime
HOST-RESOURCES-MIB::hrSystemUptime.0 = Timeticks: (395668) 1:05:56.68

 

OID, MIB 正引き, 逆引き

$ snmptranslate -On UCD-DISKIO-MIB::diskIONWritten.9
.1.3.6.1.4.1.2021.13.15.1.1.4.9

$ snmptranslate .1.3.6.1.4.1.2021.13.15.1.1.6.9
UCD-DISKIO-MIB::diskIOWrites.9

 

よく使うOID, MIB

# CPUロードアベレージ(1分,5分,15分), uptimeコマンドで表示されるもの
UCD-SNMP-MIB::laNames.1 (.1.3.6.1.4.1.2021.10.1.2.1) = STRING: Load-1
UCD-SNMP-MIB::laNames.2 (.1.3.6.1.4.1.2021.10.1.2.2) = STRING: Load-5
UCD-SNMP-MIB::laNames.3 (.1.3.6.1.4.1.2021.10.1.2.3) = STRING: Load-15
UCD-SNMP-MIB::laLoad.1 (.1.3.6.1.4.1.2021.10.1.3.1) = STRING: 0.77
UCD-SNMP-MIB::laLoad.2 (.1.3.6.1.4.1.2021.10.1.3.2) = STRING: 0.61
UCD-SNMP-MIB::laLoad.3 (.1.3.6.1.4.1.2021.10.1.3.3) = STRING: 0.50
UCD-SNMP-MIB::laLoadInt.1 (.1.3.6.1.4.1.2021.10.1.5.1) = INTEGER: 77
UCD-SNMP-MIB::laLoadInt.2 (.1.3.6.1.4.1.2021.10.1.5.2) = INTEGER: 61
UCD-SNMP-MIB::laLoadInt.3 (.1.3.6.1.4.1.2021.10.1.5.3) = INTEGER: 50


# CPU使用100分率(ユーザ, システム, アイドル状態)
UCD-SNMP-MIB::ssCpuUser.0 (.1.3.6.1.4.1.2021.11.9.0) = INTEGER: 15
UCD-SNMP-MIB::ssCpuSystem.0 (.1.3.6.1.4.1.2021.11.10.0) = INTEGER: 3
UCD-SNMP-MIB::ssCpuIdle.0 (.1.3.6.1.4.1.2021.11.11.0) = INTEGER: 80


# スワップメモリー 総容量, 空き容量
UCD-SNMP-MIB::memTotalSwap.0 (.1.3.6.1.4.1.2021.4.3.0) = INTEGER: 24389624 kB
UCD-SNMP-MIB::memAvailSwap.0 (.1.3.6.1.4.1.2021.4.4.0) = INTEGER: 24389624 kB
# 物理メモリー 総容量, 空き容量
UCD-SNMP-MIB::memTotalReal.0 (.1.3.6.1.4.1.2021.4.5.0) = INTEGER: 16108740 kB
UCD-SNMP-MIB::memAvailReal.0 (.1.3.6.1.4.1.2021.4.6.0) = INTEGER: 10598624 kB


# ドライブ, パーティションごとの読み書きサイズ、回数
UCD-DISKIO-MIB::diskIODevice.9 (.1.3.6.1.4.1.2021.13.15.1.1.2.9) = STRING: nvme0n1
UCD-DISKIO-MIB::diskIODevice.10 (.1.3.6.1.4.1.2021.13.15.1.1.2.10) = STRING: nvme0n1p1
… 省略
UCD-DISKIO-MIB::diskIONRead.9 (.1.3.6.1.4.1.2021.13.15.1.1.3.9) = Counter32: 1875687936
UCD-DISKIO-MIB::diskIONRead.10 (.1.3.6.1.4.1.2021.13.15.1.1.3.10) = Counter32: 604160
… 省略
UCD-DISKIO-MIB::diskIONWritten.9 (.1.3.6.1.4.1.2021.13.15.1.1.4.9) = Counter32: 559379456
UCD-DISKIO-MIB::diskIONWritten.10 (.1.3.6.1.4.1.2021.13.15.1.1.4.10) = Counter32: 0
… 省略
UCD-DISKIO-MIB::diskIOReads.9 (.1.3.6.1.4.1.2021.13.15.1.1.5.9) = Counter32: 38456
UCD-DISKIO-MIB::diskIOReads.10 (.1.3.6.1.4.1.2021.13.15.1.1.5.10) = Counter32: 48
… 省略
UCD-DISKIO-MIB::diskIOWrites.9 (.1.3.6.1.4.1.2021.13.15.1.1.6.9) = Counter32: 16673
UCD-DISKIO-MIB::diskIOWrites.10 (.1.3.6.1.4.1.2021.13.15.1.1.6.10) = Counter32: 0


# lm_sensorsで得られる各デバイスの温度
LM-SENSORS-MIB::lmTempSensorsDevice.1 (.1.3.6.1.4.1.2021.13.16.2.1.2.1) = STRING: wlp2s0_intel_ac3165_temp
… 省略
LM-SENSORS-MIB::lmTempSensorsDevice.9 (.1.3.6.1.4.1.2021.13.16.2.1.2.9) = STRING: nvme_wd_sn550_temp
LM-SENSORS-MIB::lmTempSensorsValue.1 (.1.3.6.1.4.1.2021.13.16.2.1.3.1) = Gauge32: 35000
… 省略
LM-SENSORS-MIB::lmTempSensorsValue.9 (.1.3.6.1.4.1.2021.13.16.2.1.3.9) = Gauge32: 37850

 

 

lm_sensors

lm_sensorsパッケージをインストール

lm_sensorsパッケージは、M/Bのセンサーチップから値を読み出したり、PCIバス等を介してデバイスのセンサー値読みだすことが出来るソフトウエア

まずインストールを行う

$ sudo apt install lm_sensors

 

lm_sensorsの初期設定

パッケージをインストール後、セットアップ プログラムsensors-detectを実行する
どのセンサーから読み込み設定するかYES/NOの対話形式でセットアップが進められるが、基本的にはデフォルトオプションを全て選択(Enterキーを連打)して構わない

YES/NOをすべてデフォルトオプションを選択する場合は、--auto スイッチを付けて実行しても良い

なお、sensors-detectのマニュアルページには「デフォルトオプションがNOである“推奨以外の”センサー読み出しを試すと、ハードウエアに致命的なダメージを与えることがある」と書かれている

$ sensors-detect [--auto]

sensors-detectの実行が終わると、次のようなメッセージが表示される。
この例では/etc/modulesに追加すべきドライバーが示されているので、これを追加すれば読み出せるセンサー項目が増える(はず)…

sensors-detect実行時のメッセージ(最後の部分を抜粋)
Now follows a summary of the probes I have just done.
Just press ENTER to continue: 

Driver `coretemp':
    * Chip `Intel digital thermal sensor' (confidence: 9)

To load everything that is needed, add this to /etc/modules:
#----cut here----
# Chip drivers
coretemp
#----cut here----
If you have some drivers built into your kernel, the list above will
contain too many modules. Skip the appropriate ones!

Do you want to add these lines automatically to /etc/modules? (yes/NO)
            

 

lm_sensorsの表示設定/カスタマイズ

ここまでで基本的設定が終わっているので、マシンを一旦再起動し、sensorsコマンドでセンサー値一覧を表示してみる

実行例

$ sensors
coretemp-isa-0000
Adapter: ISA adapter
Package id 0:  +43.0°C  (high = +74.0°C, crit = +82.0°C)
Core 0:        +43.0°C  (high = +74.0°C, crit = +82.0°C)
Core 1:        +41.0°C  (high = +74.0°C, crit = +82.0°C)
Core 2:        +43.0°C  (high = +74.0°C, crit = +82.0°C)
Core 3:        +43.0°C  (high = +74.0°C, crit = +82.0°C)

acpitz-acpi-0
Adapter: ACPI interface
temp1:        +27.8°C  (crit = +119.0°C)

iwlwifi_1-virtual-0
Adapter: Virtual device
temp1:        +38.0°C  

nvme-pci-0100
Adapter: PCI adapter
Composite:  +38.9°C  (low  =  -5.2°C, high = +79.8°C)
                                (crit = +84.8°C)
            

より詳細に表示する場合は

$ sensors -u
coretemp-isa-0000
Adapter: ISA adapter
Package id 0:
    temp1_input: 43.000
    temp1_max: 74.000
    temp1_crit: 82.000
    temp1_crit_alarm: 0.000
Core 0:

〜 途中省略 〜

temp1:
  temp1_input: 36.000

nvme-pci-0100
Adapter: PCI adapter
Composite:
  temp1_input: 38.850
  temp1_max: 79.850
  temp1_min: -5.150
  temp1_crit: 84.850
  temp1_alarm: 0.000
            

mrtgで温度を抜き出す場合、センサー名が一意になっている必要がある

上に示した例では、temp1と表示されているものがマザーボード(acpitz-acpi-0)とwifiカード(iwlwifi_1-virtual-0)で重複しており、mrtgからは見分けがつかない

そこで、センサー名をカスタマイズする

/etc/sensors3.confを編集しても構わないのだが、パッケージがアップデートされるときに初期値に戻されてしまう
したがって、/etc/sensors.d/ディレクトリ内に新たなconfファイルを作成する

/etc/sensors.d/acpitz.conf
chip "acpitz-acpi-0"
    label temp1 "MB_acpi_temp"
/etc/sensors.d/cpu.conf
chip "coretemp-isa-0000"
    label temp1 "cpu_package_temp"
    label temp2 "cpu_core0_temp"
    label temp3 "cpu_core1_temp"
    label temp4 "cpu_core2_temp"
    label temp5 "cpu_core3_temp"
/etc/sensors.d/iwlwifi.conf
chip "iwlwifi_1-virtual-0"
    label temp1 "wlp2s0_intel_ac3165_temp"
/etc/sensors.d/nvme-pci-0100.conf
chip "nvme-pci-0100"
    label temp1 "nvme_wd_sn550_temp"

labelは即時反映、温度の限界値を変更するsetの反映には次のコマンドでset値を再読込する

$ sensors -s

最終的には次のような表示になった。これでmrtgから読みとることが出来るはずだ

$ sensors
coretemp-isa-0000
Adapter: ISA adapter
cpu_package_temp:  +42.0°C  (high = +74.0°C, crit = +82.0°C)
cpu_core0_temp:    +42.0°C  (high = +74.0°C, crit = +82.0°C)
cpu_core1_temp:    +40.0°C  (high = +74.0°C, crit = +82.0°C)
cpu_core2_temp:    +43.0°C  (high = +74.0°C, crit = +82.0°C)
cpu_core3_temp:    +41.0°C  (high = +74.0°C, crit = +82.0°C)

acpitz-acpi-0
Adapter: ACPI interface
MB_acpi_temp:  +27.8°C  (crit = +119.0°C)

iwlwifi_1-virtual-0
Adapter: Virtual device
wlp2s0_intel_ac3165_temp:  +35.0°C  

nvme-pci-0100
Adapter: PCI adapter
nvme_wd_sn550_temp:  +37.9°C  (low  =  -5.2°C, high = +79.8°C)
                              (crit = +84.8°C)
            

次の記事も参考になる

「CPUの温度やファンの回転数などをモニタするには」(@IT)
http://www.atmarkit.co.jp/flinux/rensai/linuxtips/743hwmonitor.html

 

/sys 以下のファイルから値を直接読み取る

/sys/class/hwmon以下に、デバイス名と温度の値を格納したディレクトリへのリンクが格納されている

$ ll /sys/class/hwmon/
drwxr-xr-x  2 root root 0 2023-08-25 14:40:08 ./
drwxr-xr-x 85 root root 0 2023-08-25 14:40:08 ../
lrwxrwxrwx  1 root root 0 2023-08-25 14:40:08 hwmon0 -> ../../devices/virtual/thermal/thermal_zone0/hwmon0/
lrwxrwxrwx  1 root root 0 2023-08-25 05:40:10 hwmon1 -> ../../devices/pci0000:00/0000:00:1b.0/0000:01:00.0/nvme/nvme0/hwmon1/
lrwxrwxrwx  1 root root 0 2023-08-25 05:40:10 hwmon2 -> ../../devices/platform/coretemp.0/hwmon/hwmon2/
lrwxrwxrwx  1 root root 0 2023-08-25 05:40:11 hwmon3 -> ../../devices/virtual/thermal/thermal_zone1/hwmon3/
lrwxrwxrwx  1 root root 0 2023-08-25 05:40:26 hwmon4 -> ../../devices/pci0000:00/0000:00:14.0/usb1/1-7/1-7:1.1/0003:046D:C534.0002/0003:046D:4023.0003/power_supply/hidpp_battery_0/hwmon4/
            

温度が格納されたファイルを、(上述のリンク先ではなく)実際のディレクトリで検索してみる

$ find /sys/ -name 'temp*_input' -print
find: ‘/sys/kernel/tracing’: 許可がありません
find: ‘/sys/kernel/debug’: 許可がありません
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp3_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp4_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp5_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp2_input
/sys/devices/pci0000:00/0000:00:1b.0/0000:01:00.0/nvme/nvme0/hwmon1/temp1_input
/sys/devices/virtual/thermal/thermal_zone0/hwmon0/temp1_input
/sys/devices/virtual/thermal/thermal_zone1/hwmon3/temp1_input
find: ‘/sys/fs/pstore’: 許可がありません
find: ‘/sys/fs/bpf’: 許可がありません
            

temp*_input ファイルに温度が、name ファイルにデバイスの名前等が格納されている

 

 

drivetemp

hddtempは廃止され、drivetempカーネルモジュールに移行

以前より使われていたhddtempはフェードアウトし、今後はdrivetempが使われるようになるとのこと

hddtemp (0.3-beta15-54) unstable; urgency=medium

hddtemp has been dead upstream for many years and is therefore in a minimal maintenance mode. It will be shipped in the Debian Bullseye release, but will not be present in the Debian Bookworm release.

Nowadays the 'drivetemp' kernel module is a better alternative. It uses the Linux Hardware Monitoring kernel API (hwmon), so the temperature is returned the same way and using the same tools as other sensors.

Loading this module is as easy as directory:

echo drivetemp > /etc/modules-load.d/drivetemp.conf

-- Aurelien Jarno <aurel32@debian.org> Tue, 02 Feb 2021 20:27:44 +0100

https://sources.debian.org/src/hddtemp/0.3-beta15-54/debian/NEWS/

linux-modules-extra-VERSION-generic パッケージに含まれるdrivetemp.koモジュールをロードすることで、S.M.A.R.Tから読み込むSATAドライブの温度も確認できるようになる

 

drivetempのテストとインストール

drivetempを一時的に有効化する

$ sudo modprobe -v drivetemp

/sys ディレクトリに新たに追加される監視項目を確認する。前出の例「/sys 以下のファイルから読み取る」との差分を着色した

ata1/host0/target0 が、/dev/sdaに接続したSATA SSDのデータ

$ find /sys/ -name 'temp*_input' -print
find: ‘/sys/kernel/tracing’: 許可がありません
find: ‘/sys/kernel/debug’: 許可がありません
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp3_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp4_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp1_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp5_input
/sys/devices/platform/coretemp.0/hwmon/hwmon2/temp2_input
/sys/devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/hwmon/hwmon5/temp1_input
/sys/devices/pci0000:00/0000:00:1b.0/0000:01:00.0/nvme/nvme0/hwmon1/temp1_input
/sys/devices/virtual/thermal/thermal_zone0/hwmon0/temp1_input
/sys/devices/virtual/thermal/thermal_zone1/hwmon3/temp1_input
find: ‘/sys/fs/pstore’: 許可がありません
find: ‘/sys/fs/bpf’: 許可がありません

$ cat /sys/devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/hwmon/hwmon5/name
drivetemp

$ cat /sys/devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/hwmon/hwmon5/temp1_input
33000

$ cat /sys/devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/hwmon/hwmon5/device/model
CSSD-S6B960CG3VX

後ろ3桁のゼロを取れば、mrtgの入力にできる。もしくは、(drivetempをLinux起動時にロードすれば)sensorsコマンドでも出力できる

$ cat /sys/devices/pci0000:00/0000:00:17.0/ata1/host0/target0:0:0/0:0:0:0/hwmon/hwmon5/temp1_input \
    | awk '{print substr($0, 1, length($0)-3)}'
33

この値をmrtgで利用する場合は、Debianのドキュメントに書かれている通り、Linux起動時にカーネルモジュールを読み込むよう次のように設定する

$ sudo sh -c "echo drivetemp > /etc/modules-load.d/drivetemp.conf"

 

 

MRTG

MRTGのインストール

mrtgはPerlを使っているため、Perlがインストールされていることが前提となる

$ sudo apt install mrtg

 

mrtgの設定

インストール直後の設定ファイルは次のような内容となっている

/etc/mrtg/mrtg.cfg
# Global configuration
WorkDir: /var/www/html/mrtg

# Put your rules below or run cfgmaker(1)

設定ファイルを自動で作成するスクリプトを実行する

$ cfgmaker public@localhost
--base: Get Device Info on public@localhost:
--base: Vendor Id: Unknown Vendor - 1.3.6.1.4.1.8072.3.2.10
--base: Populating confcache
--base: Get Interface Info
--base: Walking ifIndex
--snpd:   public@localhost: -> 1 -> ifIndex = 1
--snpd:   public@localhost: -> 2 -> ifIndex = 2
--snpd:   public@localhost: -> 3 -> ifIndex = 3
--base: Walking ifType
--snpd:   public@localhost: -> 1 -> ifType = 24
--snpd:   public@localhost: -> 2 -> ifType = 6
--snpd:   public@localhost: -> 3 -> ifType = 6
--base: Walking ifAdminStatus
--snpd:   public@localhost: -> 1 -> ifAdminStatus = 1
--snpd:   public@localhost: -> 2 -> ifAdminStatus = 1
--snpd:   public@localhost: -> 3 -> ifAdminStatus = 1
--base: Walking ifOperStatus
--snpd:   public@localhost: -> 1 -> ifOperStatus = 1
--snpd:   public@localhost: -> 2 -> ifOperStatus = 2
--snpd:   public@localhost: -> 3 -> ifOperStatus = 1
--base: Walking ifMtu
--snpd:   public@localhost: -> 1 -> ifMtu = 65536
--snpd:   public@localhost: -> 2 -> ifMtu = 1500
--snpd:   public@localhost: -> 3 -> ifMtu = 1500
--base: Walking ifSpeed
--snpd:   public@localhost: -> 1 -> ifSpeed = 10000000
--snpd:   public@localhost: -> 2 -> ifSpeed = 0
--snpd:   public@localhost: -> 3 -> ifSpeed = 0
# Created by 
# /usr/bin/cfgmaker public@localhost


### Global Config Options

#  for UNIX
# WorkDir: /home/http/mrtg

#  for NT
# WorkDir: c:\mrtgdata

#  for several Linux, like Debian, RHEL and it's derivatives
WorkDir: /var/www/html/mrtg

#  for others Linux
# WorkDir: /srv/http/mrtg

### Global Defaults

#  to get bits instead of bytes and graphs growing to the right
# Options[_]: growright, bits

EnableIPv6: no

######################################################################
# System: Ubuntu2204M720q
# Description: Linux Ubuntu2204M720q 6.2.0-26-generic #26~22.04.1-Ubuntu SMP PREEMPT_DYNAMIC Thu Jul 13 16:27:29 UTC 2 x86_64
# Contact: Me 
# Location: Sitting on the Dock of the Bay
######################################################################


### Interface 1 >> Descr: 'lo' | Name: 'No Name' | Ip: '127.0.0.1' | Eth: 'No Ethernet Id' ###
### The following interface is commented out because:
### * it is a Software Loopback interface
# 
# Target[localhost_1]: 1:public@localhost:
# SetEnv[localhost_1]: MRTG_INT_IP="127.0.0.1" MRTG_INT_DESCR="lo"
# MaxBytes[localhost_1]: 1250000
# Title[localhost_1]: Traffic Analysis for 1 -- Ubuntu2204M720q
# PageTop[localhost_1]: <h1>Traffic Analysis for 1 -- Ubuntu2204M720q</h1>
# 		<div id="sysdetails">

〜 途中省略 〜

### Interface 3 >> Descr: 'wlp2s0' | Name: 'No Name' | Ip: '192.168.1.102' | Eth: '24-ee-9a-b4-61-12' ###
### The following interface is commented out because:
### * has no ifSpeed property
# 
# Target[localhost_3]: 3:public@localhost:
# SetEnv[localhost_3]: MRTG_INT_IP="192.168.1.102" MRTG_INT_DESCR="wlp2s0"
# MaxBytes[localhost_3]: 0
# Title[localhost_3]: Traffic Analysis for 3 -- Ubuntu2204M720q
# PageTop[localhost_3]: <h1>Traffic Analysis for 3 -- Ubuntu2204M720q</h1>
# 		<div id="sysdetails">
# 			<table>
# 				<tr>
# 					<td>System:</td>
# 					<td>Ubuntu2204M720q in Sitting on the Dock of the Bay</td>
# 				</tr>
# 				<tr>
# 					<td>Maintainer:</td>
# 					<td>Me <me@example.org></td>
# 				</tr>
# 				<tr>
# 					<td>Description:</td>
# 					<td>wlp2s0  </td>
# 				</tr>
# 				<tr>
# 					<td>ifType:</td>
# 					<td>ethernetCsmacd (6)</td>
# 				</tr>
# 				<tr>
# 					<td>ifName:</td>
# 					<td>No Name</td>
# 				</tr>
# 				<tr>
# 					<td>Max Speed:</td>
# 					<td>0.0 Bytes/s</td>
# 				</tr>
# 				<tr>
# 					<td>Ip:</td>
# 					<td>192.168.1.102 (Ubuntu2204M720q)</td>
# 				</tr>
# 			</table>
# 		</div>

自動で作成(画面表示)された設定ファイルの内容から、今回必要なWifiインターフェース部分をコピペし、さらにCPUとNVMe SSDの温度をsensorsから読み取るものを追加して、サンプル設定ファイルを作成したのが次の例

/etc/mrtg/mrtg.cfg
WorkDir: /var/www/html/mrtg
Options[_]: growright, bits
EnableIPv6: no

### Interface 3 >> Descr: 'wlp2s0' | Name: 'No Name' | Ip: '192.168.1.102' | Eth: '24-ee-9a-b4-61-12' ###
### The following interface is commented out because:
### * has no ifSpeed property
# 
Target[localhost_3]: 3:public@localhost:
SetEnv[localhost_3]: MRTG_INT_IP="192.168.1.102" MRTG_INT_DESCR="wlp2s0"
MaxBytes[localhost_3]: 100000000
Title[localhost_3]: Traffic Analysis for wlp2s0 -- Ubuntu2204M720q
PageTop[localhost_3]: Traffic Analysis for wlp2s0 -- Ubuntu2204M720q

### CPU and NVMe SSD temperature
Target[temperature]: `/usr/bin/sensors | grep -e 'cpu_package_temp' -e 'nvme_wd_sn550_temp' | awk '{print $2}' | sed -e 's/+//' -e 's/[^0-9+\.].*//'`
MaxBytes[temperature]: 100
Title[temperature]: CPU and NVMe disk Temperature
PageTop[temperature]: CPU and NVMe disk Temperature
Options[temperature]: growright,absolute,gauge,nopercent,noinfo
WithPeak[temperature]: ymw
#unscaled[temperature]: dwmy
YLegend[temperature]: Temperature
ShortLegend[temperature]: C
LegendI[temperature]: CPU
LegendO[temperature]: NVMe disk
Legend1[temperature]: CPU
Legend2[temperature]: NVMe disk
            

MRTGのマニュアルによれば、Target キーワードに外部コマンドを用いる場合は、次のようなフォーマットにする必要がある

If you want to monitor something which does not provide data via snmp you can use some external program to do the data
The external command must return 4 lines of output:

  • Line 1 : current state of the first variable, normally 'incoming bytes count'
  • Line 2 : current state of the second variable, normally 'outgoing bytes count'
  • Line 3 : string (in any human readable format), telling the uptime of the target.
  • Line 4 : string, telling the name of the target.

Depending on the type of data your script returns you might want to use the 'gauge' or 'absolute' arguments for the Options keyword.

Example:

Target[myrouter]: `/usr/local/bin/df2mrtg /dev/dsk/c0t2d0s0`

Note the use of the backticks (`), not apostrophes (') around the command.

mrtg.cfgの設定により複数のhtmlページが作成される場合、それらにリンクを張ったindex.htmlを自動的に作成するスクリプトを実行する

$ sudo -u mrtg sh -c "indexmaker /etc/mrtg/mrtg.cfg > /var/www/html/mrtg/index.html"

 

mrtgのテスト実行

設定が終わればコマンドラインでテスト実行を行い、エラーメッセージが出ないか確認する
次の例では、html等を出力するWorkDir (/var/www/html/mrtg)のオーナーがユーザ名mrtgなので、sudoで実行ユーザを強制(-u mrtg)して実行している

$ sudo -u mrtg sh -c "LANG=C mrtg /etc/mrtg/mrtg.cfg"

/etc/mrtg/ 内にロック ファイルが、/var/www/html/mrtg/ 内に各種データファイルが作成されるが、実行時の毎回ユーザが違うと、上書き不能エラーとなる可能性がある。ここでは ユーザを mrtg で固定して行っている(sudo -u mrtg …)

 

cronでmrtgの自動実行を設定する

MRTGを5分ごとに起動させるための設定は、公式マニュアルでは次のように記載すればよいと書かれている

if you live in Linux Land the line may look like this if you are using crontab -e

*/5 * * * *  <mrtg-bin>/mrtg <path to mrtg-cfg>/mrtg.cfg \
    --logging /var/log/mrtg.log

or like this if you use /etc/crontab

*/5 * * * *  mrtg-user  <mrtg-bin>/mrtg <path to mrtg-cfg>/mrtg.cfg \
    --logging /var/log/mrtg.log

実際に、ここでの設定参考例は次のようなものになる
(crontabコマンドで編集したファイルは /var/spool/cron/crontabs/root に格納される)

タスクの最後に >/dev/null 2>&1 を付けているのは、mrtgがテキストデータを標準出力・エラー出力に返したときに、cronがメールを送信するのを阻止するため

$ sudo crontab -u root -e
*/5 * * * * mrtg LANG=C LC_ALL=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg > /dev/null 2>&1

グラフを見るには、Apacheが走っている場合は

http://localhost/mrtg/index.html

そうでない場合は、直接ファイルを見に行く

file:///var/www/mrtg/index.html

 

MRTGの公式マニュアル

Tobi Oetiker's MRTG - The Multi Router Traffic Grapher

 

 

Windowsマシンでの設定

検証環境 : Windows Vista, 7 (このセクションより下は2008年当時の情報)

Windows SNMP サービスのインストール

20070408-instsnmp01.png
Windows XP 系 の場合 はWindowsコンポーネントの追加と削除からSNMPを有効化する

20070408-instsnmp03.png
Windows 7/8/10 系 の場合 は Windowsの機能からSNMPを有効化する

サービスを自動起動させるよう設定する

20070408-instsnmp02.png

Windows 7/8/10系の場合は、サービス名を右クリックしてプロパティを表示し、コミュニティ名を設定する必要性がある。(通常、コミュニティ名はpublicとする)

20070408-instsnmp04.png

Windows標準のSNMPエージェントで得られるのは MIB-2, RFC1514など。ハードウエアの状態などは得られない。

 

システム関連 のSNMPエージェント拡張

CPUの負荷状態やメモリーの利用状況、ハードディスクの容量などを得るためのSNMPエージェント拡張機能をインストールする。

Windows NT系に対応したフリーのもので、よく使われているものは次の2つくらいか

どちらか一つを入れればよい。

CPU温度などをSNMP経由で得たい場合は

MBM (Motherboard Monitor) 用の拡張機能の SNMP Informant-MBM (http://www.wtcs.org/informant/products.htm)をインストールする。

なお、MBMは2004年に開発と配布が終了しているらしい。

SpeedFan 用の拡張も配布されている。

SpeedFan SNMP Extension (http://code.bastart.eu.org/projects/sfsnmp/)

MIBが配布されていないので、OIDを直接叩く以外に無い。

CPU温度などの温度センサーは次のOIDに割り付けられている。
.1.3.6.1.4.1.16.0.1.x  (x=0,1,2...)

Windows 7/8/10系のUAC制御でSpeedFanとMRTGの実行ユーザが違う場合、SNMP経由でうまく取り込めません。SpeedFanのログファイルを用いてデータを取り込むプログラムを公開しています (sf2mrtg)

 

Windows版Perlのインストール

MRTGはPerlで書かれているため、Perlの実行環境を整える。

Active Perl (http://aspn.activestate.com/ASPN/Downloads/ActivePerl/)

 

MRTGのインストール

MRTGの入手と日本語ドキュメント (http://www.mrtg.jp/doc/)

Windows用zipファイルをダウンロードして、C:\Program Filesに解凍する。

mrtg.cfgを自分で作成しない場合は、自マシンのNIC情報のみを付加したmrtg.cfgを自動生成させることも出来る。
※ MRTGのログデータを格納するフォルダ名には、空白が入ってはいけないようだ。

cfgmaker による mrtg.cfg 自動作成方法
c:\Program Files\mrtg-2.15.0\bin>perl 
cfgmaker public@localhost --global "WorkDir: c:\mrtg" --output 
mrtg.cfg
--base: Get Device Info on public@localhost:
--base: Vendor Id:
--base: Populating confcache
--coca: populate confcache public@localhost:
--coca: store in confcache public@localhost_ Descr MS TCP Loopback 
interface --> 1
--coca: store in confcache public@localhost_ Descr Marvell Yukon 88E8053 
PCI-E Gigabit Ethernet Controller #2 --> 65539
--coca: store in confcache public@localhost_ Type 24 --> 1
--coca: store in confcache public@localhost_ Type 6 --> 65539
--coca: store in confcache public@localhost_ Ip 127.0.0.1 --> 1
--coca: store in confcache public@localhost_ Ip 192.168.1.10 --> 
65539
--coca: store in confcache public@localhost_ Eth --> 1
--coca: store in confcache public@localhost_ Eth 00-01-80-61-00-00 
--> 65539
--base: Get Interface Info
--base: Walking ifIndex
--base: Walking ifType
--base: Walking ifAdminStatus
--base: Walking ifOperStatus
--base: Walking ifMtu
--base: Walking ifSpeed
--base: Writing mrtg.cfg
            

自動作成で出来るファイルは次のようなものなので、これと同じ書式にしたがって自分でmrtg.cfgを作ってもよい

c:\Program Files\mrtg-2.15.0\bin\mrtg.cfg
# Created by
# cfgmaker public@localhost --global "WorkDir: h:\mrtg" --output mrtg.cfg
    
### Global Config Options
    
# for UNIX
# WorkDir: /home/http/mrtg
    
# or for NT
WorkDir: c:\mrtg
    
### Global Defaults
# to get bits instead of bytes and graphs growing to the right
# Options[_]: growright, bits
    
EnableIPv6: no
    
Target[localhost_65539]: 65539:public@localhost:
SetEnv[localhost_65539]: MRTG_INT_IP="192.168.1.10" 
MRTG_INT_DESCR="Marvell-Yukon-88E8053-PCI-E-Gigabit-Ethernet-Controller-#2"
MaxBytes[localhost_65539]: 12500000
Title[localhost_65539]: Network Interface
PageTop[localhost_65539]: Network Interface
            

 

MRTGの実行

MRTG本体のあるフォルダで実行する。

c:\Program Files\mrtg-2.15.0\bin>perl mrtg mrtg.cfg
            

 

Windows 版 cron サービスをインストール

Windows 版 cron サービスをインストールする。
Cron NT service (http://www.kalab.com/freeware/cron/cron.htm)

インストーラがインストール先のフォルダ名を聞いてくるので、(たとえば)c:\Windows\system32 にインストールする。
アンインストール用のショートカットなどは「全く」作成されない。インストールしたフォルダでcmd.exeを開き crons.exe /? と実行すれば取り得るオプションが表示される。

Linuxと同じく、1分おきにcrontabを確認しに来るので、crons.exeがあるフォルダに
crontab ファイルを作成する。

c:\Windows\system32\crontab
0,5,10,15,20,25,30,35,40,45,50,55 * * * * cron-mrtg-run.cmd
            

※ */5 のような記述はシンタックスエラーになる。完全にLinux互換とはいかない様だ...

なお、実行するコマンドは次のようなスクリプトファイルにまとめている。

c:\Windows\system32\crontab
cd c:\Program Files\mrtg-2.15.0\bin
perl mrtg mrtg.cfg
            

 

MIBブラウザ

Windowsには snmpwalk コマンド等の SNMP を扱うプログラムが標準では提供されていない。
GUIでMIBのOIDツリーを眺められる次のようなツールを使う。

GetIf SNMP MIB Browser (http://www.wtcs.org/informant/free_snmp_tools.htm)
インストールしたら、GetIfのフォルダ内に「mibs」というサブフォルダが作成される。ここに、SNMP Informant や SNMP4W2K のインストールフォルダから MIBツリーの定義ファイルをコピーする。
※ コピーしない場合は、何のキーワードも表示されないだけで、データ自体は参照できる。

20070408-mib01.png

Host Name に、ターゲットのアドレスを入力 (自マシンの場合は localhost) して Enter キーを押す

20070408-mib02.png

MBrowser タブに移り、OIDツリーをたどって必要な項目を表示(①)させて enter キーを押す。
SNMP経由でレスポンスが返ってきたら、必要な項目をクリック(②)して、enter キーを押す。
そうすると、OIDの識別値が ③ に表示される。

OIDを直接指定してSNMPエージェントから値を得るだけなら、マイクロソフト純正のツールがある。
SnmpUtilG というソフトが、Windows 2000 Resource Kit や Windows XP Support Tool として配布されている。

 

mrtg.cfg の設定例と出力グラフのサンプル

たとえば、CPU負荷のグラフを描かせたい場合は、次のような設定を追加する。(SNMP4W2Kの場合)

c:\Program Files\mrtg-2.15.0\bin\mrtg.cfg
# SNMP4W2Kを用いる場合
Target[cpuload]: 
1.3.6.1.4.1.311.1.1.3.1.1.2.1.3.1.48&1.3.6.1.4.1.311.1.1.3.1.1.2.1.4.1.48:public@localhost:
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
            

20070408-cpu.png

SpeedFanで温度変化のグラフを描かせたい場合は、次のような設定を追加する。

c:\Program Files\mrtg-2.15.0\bin\mrtg.cfg
# sf2mrtg を用いる場合
Target[temp]: `C:\Program Files\SpeedFan\sf2mrtg\sf2mrtg.exe`
# SpeedFan SNMP Extension を用いる場合
# Target[temp]: 1.3.6.1.4.1.16.0.1.0&1.3.6.1.4.1.16.0.1.1:public@localhost:
MaxBytes[temp]: 6000
Title[temp]: System Temperature Log (MRTG)
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 x 100
LegendI[temp]: System Temp
LegendO[temp]: CPU Temp
Legend1[temp]: System Temp
Legend2[temp]: CPU Temp
            

20070408-temp.png

MRTG の cfgmaker で作成されたネットワークの負荷を監視するグラフは次のようになる。

c:\Program Files\mrtg-2.15.0\bin\mrtg.cfg
Target[network]: 65539:public@localhost:
SetEnv[network]: MRTG_INT_IP="192.168.1.1" MRTG_INT_DESCR="Marvell-Yukon-88E8053-PCI-E-Gigabit-Ethernet-Controller"
MaxBytes[network]: 62500
Title[network]: Network Interface Log (MRTG)
PageTop[network]: Network Interface
Options[network]: growright,nopercent,unknaszero,bits
            

20070408-net.png