MRTGによるサーバ監視



●NET-SNMPのインストール

 NET-SNMPをインストールします。
# yum -y install net-snmp

●NET-SNMPの設定
# vi /etc/snmp/snmpd.conf ← NET-SNMP設定ファイル編集
#       sec.name  source          community
#com2sec notConfigUser  default       public ← コメントアウト
com2sec local localhost private ← 追加
com2sec mynetwork 192.168.1.0/24 public ← 追加(ネットワークアドレスは各自の環境に合わせること)
#       groupName      securityModel securityName
#group   notConfigGroup v1c           notConfigUser ← コメントアウト
#group   notConfigGroup v2c           notConfigUser ← コメントアウト
group MyROGroup v1 local ← 追加
group MyROGroup v2c local ← 追加
group MyROGroup v1 mynetwork ← 追加
group MyROGroup v2c mynetwork ← 追加
# Make at least  snmpwalk -v 1 localhost -c public system fast again.
#       name           incl/excl     subtree         mask(optional)
#view    systemview    included   .1.3.6.1.2.1.1 ← コメントアウト
#view    systemview    included   .1.3.6.1.2.1.25.1.1 ← コメントアウト
view all included .1 80 ← 追加
# Finally, grant the group read-only access to the systemview view.
#       group          context sec.model sec.level prefix read   write  notif
#access  notConfigGroup ""      any       noauth    exact  systemview none none ← コメントアウト
access MyROGroup "" any noauth exact all none none ← 追加
access MyRWGroup "" any noauth exact all all none ← 追加
# Check the / partition and make sure it contains at least 10 megs.
#disk / 10000
disk / 10000 ← 追加(ディスク使用率表示用設定)

●NET-SNMPの起動
# /etc/rc.d/init.d/snmpd start ← NET-SNMP起動
snmpd を起動中:                                            [  OK  ]
# chkconfig snmpd on ← NET-SNMP自動起動設定
# chkconfig --list snmpd ← NET-SNMP自動起動設定確認
snmpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off ← ランレベル2〜5のon確認

●MRTGのインストール

 MRTGをインストールします。
# yum -y install mrtg
※ここではサーバープライベートIPアドレスを192.168.1.2と仮定します。

MRTG設定ファイル作成
# cfgmaker --ifref=descr --ifdesc=descr public@192.168.1.2 > /etc/mrtg/mrtg.cfg
 ↑ MRTG設定ファイル作成
 さまざまなMRTGを表示させる場合は、上記ファイル名は柔軟に変えた方がいいです。
 この時、下記のようなエラーが表示された場合は、
SNMP Error:
no response received
SNMPv1_Session (remote host: "192.168.1.2" [192.168.1.2].161)
                  community: "public"
                 request ID: -739989760
                PDU bufsize: 8000 bytes
                    timeout: 2s
                    retries: 5
                    backoff: 1)
 at /usr/bin/../lib/mrtg2/SNMP_util.pm line 629
SNMPWALK Problem for 1.3.6.1.2.1.1 on public@192.168.1.2::::::v4only
 at /usr/bin/cfgmaker line 950
WARNING: Skipping public@192.168.1.2: as no info could be retrieved
 以下を確認してください。
・/etc/rc.d/init.d/snmpd restart してみる。
・snmpd.confの内容確認。
・# vi /etc/hosts.allow
 snmpd : ALL ← 追加してみる。

MRTG設定ファイル編集
# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
#  for UNIX
# WorkDir: /home/http/mrtg
WorkDir: /var/www/mrtg ← 追加(MRTGによる編集結果HTML格納先ディレクトリを指定する)
# Options[_]: growright, bits ← コメント解除(グラフの過去〜未来への推移方向を左→右にする)
↓
Options[_]: growright, noinfo
EnableIPv6: no
Language: utf8 ← 追加(日本語化)、OSの文字コードに合わせる
Refresh: ← リフレッシュ間隔は5分より短くできない
### Interface 2 >> Descr: 'eth0' | Name: '' | Ip: '192.168.1.2' | Eth: '00-a0-c9-de-81-af' ###
Target[192.168.1.2_eth0]: \eth0:public@192.168.1.2:
 ↓ 
Target[eth0]: \eth0:public@192.168.1.2: ← 変更
SetEnv[192.168.1.2_eth0]: MRTG_INT_IP="192.168.1.2" MRTG_INT_DESCR="eth0"
 ↓ 
SetEnv[eth0]: MRTG_INT_IP="192.168.1.2" MRTG_INT_DESCR="eth0" ← 変更
MaxBytes[192.168.1.2_eth0]: 12500000
 ↓ 
MaxBytes[eth0]: 12500000 ← 変更
Title[192.168.1.2_eth0]: eth0 -- fedora.fedorasrv.com
 ↓ 
Title[eth0]: eth0トラフィック ← 変更
PageTop[192.168.1.2_eth0]: <H1>eth0 -- fedora.fedorasrv.com</H1>
 ↓ 
PageTop[eth0]: <H1>eth0トラフィック</H1> ← 変更
以降の行を削除(ここから)
		<div id="sysdetails">
			<table>
				<tr>
					<td>System:</td>
					<td>fedora.fedorasrv.com in Unknown (edit /etc/snmp/snmpd.conf)</td>
				</tr>
				<tr>
					<td>Maintainer:</td>
					<td>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</td>
				</tr>
				<tr>
					<td>Description:</td>
					<td>eth0  </td>
				</tr>
				<tr>
					<td>ifType:</td>
					<td>ethernetCsmacd (6)</td>
				</tr>
				<tr>
					<td>ifName:</td>
					<td>eth0</td>
				</tr>
				<tr>
					<td>Max Speed:</td>
					<td>12.5 MBytes/s</td>
				</tr>
				<tr>
					<td>Ip:</td>
					<td>192.168.1.2 (fedorasrv.com)</td>
				</tr>
			</table>
		</div>
以降の行を削除(ここまで)
以降はNICを複数挿している場合のみ
### Interface 3 >> Descr: 'eth1' | Name: '' | Ip: '' | Eth: '00-90-99-80-27-2d' ###
Target[192.168.1.2_eth1]: \eth1:public@192.168.1.2:
 ↓ 
Target[eth1]: \eth1:public@192.168.1.2: ← 変更
SetEnv[192.168.1.2_eth1]: MRTG_INT_IP="" MRTG_INT_DESCR="eth1"
 ↓ 
SetEnv[eth1]: MRTG_INT_IP="" MRTG_INT_DESCR="eth1" ← 変更
MaxBytes[192.168.1.2_eth1]: 12500000
 ↓ 
MaxBytes[eth1]: 12500000 ← 変更
Title[192.168.1.2_eth1]: eth1 -- linux
 ↓ 
Title[eth1]: eth1トラフィック ← 変更
PageTop[192.168.1.2_eth1]: <H1>eth1 -- linux</H1>
 ↓ 
PageTop[eth1]: <H1>eth1トラフィック</H1> ← 変更
以降の行を削除(ここから)
		<div id="sysdetails">
			<table>
				<tr>
					<td>System:</td>
					<td>fedora.fedorasrv.com in Unknown (edit /etc/snmp/snmpd.conf)</td>
				</tr>
				<tr>
					<td>Maintainer:</td>
					<td>Root <root@localhost> (configure /etc/snmp/snmp.local.conf)</td>
				</tr>
				<tr>
					<td>Description:</td>
					<td>eth0  </td>
				</tr>
				<tr>
					<td>ifType:</td>
					<td>ethernetCsmacd (6)</td>
				</tr>
				<tr>
					<td>ifName:</td>
					<td>eth0</td>
				</tr>
				<tr>
					<td>Max Speed:</td>
					<td>12.5 MBytes/s</td>
				</tr>
				<tr>
					<td>Ip:</td>
					<td>192.168.1.2 (fedorasrv.com)</td>
				</tr>
			</table>
		</div>
以降の行を削除(ここまで)

CPU使用率表示設定追加
# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
以下の全行を追加
### CPU Usage ###
Target[cpu]: .1.3.6.1.4.1.2021.11.50.0&.1.3.6.1.4.1.2021.11.52.0:private@localhost
MaxBytes[cpu]: 100
#Unscaled[cpu]: dwmy # 縦軸を100%にしないで実績値に合わせる(100%にするとグラフがみえないため)
Options[cpu]: growright, noinfo, nopercent
YLegend[cpu]: CPU usage(%)
ShortLegend[cpu]: (%)
LegendI[cpu]: ユーザ  
LegendO[cpu]: システム
Legend1[cpu]: CPU使用率(ユーザ)(%)
Legend2[cpu]: CPU使用率(システム)(%)
Title[cpu]: CPU使用率
PageTop[cpu]: <H1>CPU使用率</H1>

空きメモリ量表示設定追加
# free ← メモリMAX値確認
             total       used       free     shared    buffers     cached
Mem:         254024      49460      10744          0       4088      18428
        ↑ 物理メモリMAX値
-/+ buffers/cache:      26944      33260
Swap:       522072      74100     118672
        ↑ スワップメモリMAX値
# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
以下の全行を追加
### Memory Free ####
Target[mem]: .1.3.6.1.4.1.2021.4.6.0&.1.3.6.1.4.1.2021.4.4.0:private@localhost
MaxBytes1[mem]: 254024 ← 物理メモリMAX値を指定する
MaxBytes2[mem]: 522072 ← スワップメモリMAX値を指定する
Unscaled[mem]: dwmy
Options[mem]: gauge, absolute, growright, noinfo
YLegend[mem]: Mem Free(Bytes)
ShortLegend[mem]: Bytes
kilo[mem]: 1024
kMG[mem]: k,M,G,T,P
LegendI[mem]: Real
LegendO[mem]: Swap
Legend1[mem]: 空き物理メモリ[MBytes]
Legend2[mem]: 空きスワップメモリ[MBytes]
Title[mem]: 空きメモリ量
PageTop[mem]: <H1>空きメモリ量</H1>

ディスク使用率表示設定追加
# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
以下の全行を追加
### Disk Used ####
Target[disk]: .1.3.6.1.4.1.2021.9.1.9.1&.1.3.6.1.4.1.2021.9.1.9.1:private@localhost
MaxBytes[disk]: 100
Unscaled[disk]: dwmy
Options[disk]: gauge, absolute, growright, nopercent, noinfo
YLegend[disk]: Disk Used(%)
ShortLegend[disk]: (%)
LegendI[disk]: / Disk used
LegendO[disk]: / Disk Used
Legend1[disk]: / Disk used
Legend2[disk]: / Disk used
Title[disk]: ディスク使用率
PageTop[disk]: <H1>ディスク使用率</H1>

ディスク温度表示設定追加
# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
以下の全行を追加
### Disk Temperature ####
#Target[hddtemp]: `/usr/sbin/hddtemp /dev/sda1 | awk '{print $3}' | cut -b1-2`
Target[hddtemp]: `/etc/mrtg/hddtemp.sh`
MaxBytes[hddtemp]: 100
Options[hddtemp]: growright,absolute,gauge,nopercent,noinfo
WithPeak[hddtemp]: ymw
YLegend[hddtemp]: Temperature
ShortLegend[hddtemp]: ℃
LegendI[hddtemp]: Temp:
LegendO[hddtemp]:
Legend1[hddtemp]: HDD Temperature in Degrees Celcius
Legend2[hddtemp]:
Title[hddtemp]: HDD温度
PageTop[hddtemp]: <H1>HDD温度</H1>

 hddtempをインストールします。
# yum -y install hddtemp
 下記のMRTG起動時に以下のエラーが表示されます。
2009-09-09 12:44:35: WARNING: Problem with External get '/etc/mrtg/hddtemp.sh':
   Expected a Number for 'out' but nothing'
2009-09-09 12:44:35: ERROR: Target[hddtemp][_OUT_] 
   ' $target->[4]{$mode} ' did not eval into defined data
 これに対処するため、2つの引数を渡すように/etc/mrtg/hddtemp.shの作成します。
# vi /etc/mrtg/hddtemp.sh ← 作成
#!/bin/sh
# /dev/sda1
echo `/usr/sbin/hddtemp -n /dev/sda1` ← 環境に合わせる
# /dev/sda2
echo `/usr/sbin/hddtemp -n /dev/sda2` ← 環境に合わせる
# chmod +x /etc/mrtg/hddtemp.sh ← 実行権付与

M/B & CPU温度表示設定追加
# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
以下の全行を追加
### M/B & CPU temp ####
Target[cputemp]: `/usr/bin/sensors | grep temp | awk '{print $2}' | sed -e 's/+//' -e 's/[^0-9+\.].*//'`
MaxBytes[cputemp]: 100
Options[cputemp]: growright,absolute,gauge,nopercent,noinfo
WithPeak[cputemp]: ymw
#unscaled[cputemp]: dwmy
YLegend[cputemp]: Temperature
ShortLegend[cputemp]: ℃
LegendI[cputemp]: M/B
LegendO[cputemp]: CPU
Legend1[cputemp]: M/B
Legend2[cputemp]: CPU
Title[cputemp]: M/B & CPU温度
PageTop[cputemp]: M/B & CPU温度

 lm_sensorsをインストールします。
# yum -y install lm_sensors
 sensors-detectコマンドでセンサーを検出します。途中でいくつか質問が表示されますが、基本的には[Enter]キーを押すだけでかまいません。
 詳細は、CPUの温度やファンの回転数などをモニタするにはを参照してください。
# sensors-detect
Stopping lm_sensors:                                       [  OK  ]
# sensors-detect revision 5666 (2009-02-26 17:15:04 +0100)
# System: Hewlett-Packard HP Compaq dc7600 Ultra-slim Desktop
# Board: Hewlett-Packard 09FCh
This program will help you determine which kernel modules you need
to load to use lm_sensors most effectively. It is generally safe
and recommended to accept the default answers to all questions,
unless you know what you're doing.
Some south bridges, CPUs or memory controllers contain embedded sensors.
Do you want to scan for them? This is totally safe. (YES/no): 
Silicon Integrated Systems SIS5595...                       No
VIA VT82C686 Integrated Sensors...                          No
VIA VT8231 Integrated Sensors...                            No
AMD K8 thermal sensors...                                   No
AMD K10 thermal sensors...                                  No
Intel Core family thermal sensor...                         No
Intel AMB FB-DIMM thermal sensor...                         No
VIA C7 thermal and voltage sensors...                       No
Some Super I/O chips contain embedded sensors. We have to write to
standard I/O ports to probe them. This is usually safe.
Do you want to scan for Super I/O sensors? (YES/no): 
Probing for Super-I/O at 0x2e/0x2f
Trying family `National Semiconductor'...                   No
Trying family `SMSC'...                                     Yes
Found `SMSC SCH5307-NS Super IO'                            Success!
    (address 0x480, driver `smsc47b397')
Probing for Super-I/O at 0x4e/0x4f
Trying family `National Semiconductor'...                   No
Trying family `SMSC'...                                     No
Trying family `VIA/Winbond/Fintek'...                       No
Trying family `ITE'...                                      No
Some systems (mainly servers) implement IPMI, a set of common interfaces
through which system health data may be retrieved, amongst other things.
We have to read from arbitrary I/O ports to probe for such interfaces.
This is normally safe. Do you want to scan for IPMI interfaces?
(YES/no): 
Probing for `IPMI BMC KCS' at 0xca0...                      No
Probing for `IPMI BMC SMIC' at 0xca8...                     No
Some hardware monitoring chips are accessible through the ISA I/O ports.
We have to write to arbitrary I/O ports to probe them. This is usually
safe though. Yes, you do have ISA I/O ports even if you do not have any
ISA slots! Do you want to scan the ISA I/O ports? (yes/NO): 
Lastly, we can probe the I2C/SMBus adapters for connected hardware
monitoring devices. This is the most risky part, and while it works
reasonably well on most systems, it has been reported to cause trouble
on some systems.
Do you want to probe the I2C/SMBus adapters now? (YES/no): 
Sorry, no supported PCI bus adapters found.
Module i2c-dev loaded successfully.
Next adapter: intel drm CRTDDC_A (i2c-0)
Do you want to scan it? (YES/no/selectively): 
Now follows a summary of the probes I have just done.
Just press ENTER to continue: 
Driver `smsc47b397':
  * ISA bus, address 0x480
    Chip `SMSC SCH5307-NS Super IO' (confidence: 9)
Do you want to overwrite /etc/sysconfig/lm_sensors? (YES/no): 
Starting lm_sensors: loading module smsc47b397             [  OK  ]
Unloading i2c-dev... OK

●MRTG起動(システムの文字コードがUTF-8の場合)
# mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(1回目)
ERROR: Mrtg will most likely not work properly when the environment
       variable LANG is set to UTF-8. Please run mrtg in an environment
       where this is not the case. Try the following command to start:
       env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg 
上記のエラーが表示された場合は、下記を実行
# env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg
Rateup WARNING: /usr/bin/rateup could not read the primary log file for eth0
Rateup WARNING: /usr/bin/rateup The backup log file for eth0 was invalid as well
Rateup WARNING: /usr/bin/rateup Can't remove eth0.old updating log file
Rateup WARNING: /usr/bin/rateup Can't rename eth0.log to eth0.old updating log file
Rateup WARNING: /usr/bin/rateup could not read the primary log file for cpu
Rateup WARNING: /usr/bin/rateup The backup log file for cpu was invalid as well
Rateup WARNING: /usr/bin/rateup Can't remove cpu.old updating log file
Rateup WARNING: /usr/bin/rateup Can't rename cpu.log to cpu.old updating log file
Rateup WARNING: /usr/bin/rateup could not read the primary log file for mem
Rateup WARNING: /usr/bin/rateup The backup log file for mem was invalid as well
Rateup WARNING: /usr/bin/rateup Can't remove mem.old updating log file
Rateup WARNING: /usr/bin/rateup Can't rename mem.log to mem.old updating log file
Rateup WARNING: /usr/bin/rateup could not read the primary log file for disk
Rateup WARNING: /usr/bin/rateup The backup log file for disk was invalid as well
Rateup WARNING: /usr/bin/rateup Can't remove disk.old updating log file
Rateup WARNING: /usr/bin/rateup Can't rename disk.log to disk.old updating log file
# mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(2回目)
または
# env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(2回目)
Rateup WARNING: /usr/bin/rateup Can't remove eth0.old updating log file
Rateup WARNING: /usr/bin/rateup Can't remove cpu.old updating log file
Rateup WARNING: /usr/bin/rateup Can't remove mem.old updating log file
Rateup WARNING: /usr/bin/rateup Can't remove disk.old updating log file
# mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(3回目)
または
# env LANG=C /usr/bin/mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(3回目)
# ← エラーが出なくなります
※MRTGは過去2回起動時のログを使用するため、mrtg.cfg修正後2回目までのMRTG起動時には必ずWARNINGが出るので、mrtg.cfg修正後はMRTG起動を3回行います。

 関連ファイルが/var/www/mrtgの下に作成されます。

●MRTG確認

内部からのみアクセスできるようにする場合
# vi /etc/httpd/conf.d/mrtg.conf ← mrtg監視結果アクセス設定ファイル編集
#
# This configuration file maps the mrtg output (generated daily)
# into the URL space.  By default these results are only accessible
# from the local host.
#
Alias /mrtg /var/www/mrtg

    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
    Allow from 192.168 ← 環境に合わせて追加
    Allow from ::1
    # Allow from .example.com

# /etc/rc.d/init.d/httpd reload ← Webサーバへ設定を反映
httpd を再読み込み中:                                      [  OK  ]

外部からもアクセスできるようにする場合
# vi /etc/httpd/conf.d/mrtg.conf ← mrtg監視結果アクセス設定ファイル編集
#
# This configuration file maps the mrtg output (generated daily)
# into the URL space.  By default these results are only accessible
# from the local host.
#
Alias /mrtg /var/www/mrtg
以下の全行を削除
<Location /mrtg>
    Order deny,allow
    Deny from all
    Allow from 127.0.0.1
    Allow from ::1
    # Allow from .example.com
</Location>
# /etc/rc.d/init.d/httpd reload ← Webサーバへ設定を反映
httpd を再読み込み中:                                      [  OK  ]

ネットワークトラフィック状況監視画面確認

http://(サーバのIPアドレス)/mrtg/eth0.htmlにアクセスして画面が表示されればOK

CPU使用率監視画面確認

http://(サーバのIPアドレス)/mrtg/cpu.htmlにアクセスして画面が表示されればOK

空きメモリ量監視画面確認

http://(サーバのIPアドレス)/mrtg/mem.htmlにアクセスして画面が表示されればOK

ディスク使用率監視画面確認

http://(サーバのIPアドレス)/mrtg/disk.htmlにアクセスして画面が表示されればOK

ディスク温度監視画面確認

http://(サーバのIPアドレス)/mrtg/hddtemp.htmlにアクセスして画面が表示されればOK

M/B & CPU温度監視画面確認

http://(サーバのIPアドレス)/mrtg/cputemp.htmlにアクセスして画面が表示されればOK

MRTG自動起動設定

 下記のファイルが作成されています。
# cat /etc/cron.d/mrtg
*/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ファイルが文字化けしてしまうため、「charset=iso-8859-1」を「charset=UTF-8」に書き換えます。
 これで問題がなければ、crondを再起動します。
# /etc/rc.d/init.d/crond restart ← MRTG定期自動起動設定ファイル変更反映
crond を停止中:                                            [  OK  ]
crond を起動中:                                            [  OK  ]

●MRTGインデックスページ作成

 各監視画面を統合したこのようなインデックスページをMRTG付属のindexmakerというツールで作成します。
# indexmaker --columns=1 --addhead="<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=utf8\">" /etc/mrtg/mrtg.cfg > /var/www/mrtg/index.html ← MRTGインデックスページ作成
# vi /var/www/mrtg/index.html ← MRTGインデックスページ編集
<!-- <meta http-equiv="content-type" content="text/html; charset=iso-8859-15" > -->
 ↑ コメントアウト(文字化け対策)