■概要

ネットワークトラフィック監視ツール(NET-SNMP+MRTG)を導入する。
ここでは、ネットワーク負荷の他に、CPU使用率、空きメモリ量、ディスク使用率の統計をブラウザから監視できるようにする。
サンプル(当サイトの現在のネットワーク負荷、CPU使用率、空きメモリ量、ディスク使用率の統計)

Webサーバー構築済であること


■NET-SNMPインストール

[root@centos ~]# yum -y install net-snmp ← net-snmpインストール

■NET-SNMP設定

[root@centos ~]# 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 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起動

[root@centos ~]# /etc/rc.d/init.d/snmpd start ← NET-SNMP起動
snmpd を起動中:                                            [  OK  ]

[root@centos ~]# chkconfig snmpd on ← NET-SNMP自動起動設定

[root@centos ~]# chkconfig --list snmpd ← NET-SNMP自動起動設定確認
snmpd           0:off   1:off   2:on    3:on    4:on    5:on    6:off ← ランレベル2~5のon確認

■MRTGインストール

[root@centos ~]# yum -y install mrtg ← MRTGインストール

■MRTG設定

※ここではサーバープライベートIPアドレスを192.168.1.3とする

(1)MRTG設定ファイル作成
[root@centos ~]# cfgmaker --ifref=descr --ifdesc=descr public@192.168.1.3 > /etc/mrtg/mrtg.cfg ← MRTG設定ファイル作成
--base: Get Device Info on public@192.168.1.3:
--base: Vendor Id: 
--base: Populating confcache
--snpo: confcache public@192.168.1.3: Descr lo --> 1
--snpo: confcache public@192.168.1.3: Descr eth0 --> 2
--snpo: confcache public@192.168.1.3: Descr sit0 --> 3
--snpo: confcache public@192.168.1.3: Type 24 --> 1
--snpo: confcache public@192.168.1.3: Type 6 --> 2
--snpo: confcache public@192.168.1.3: Type 131 --> 3
--snpo: confcache public@192.168.1.3: Ip 127.0.0.1 --> 1
--snpo: confcache public@192.168.1.3: Ip 192.168.1.3 --> 2
--snpo: confcache public@192.168.1.3: Eth  --> 1
--snpo: confcache public@192.168.1.3: Eth 00-16-3e-35-32-64 --> 2
--snpo: confcache public@192.168.1.3: Eth  --> 3 (duplicate)
--base: Get Interface Info
--base: Walking ifIndex
--base: Walking ifType
--base: Walking ifAdminStatus
--base: Walking ifOperStatus
--base: Walking ifSpeed

(2)MRTG設定ファイル編集
[root@centos ~]# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
#  for UNIX
# WorkDir: /home/http/mrtg
WorkDir: /var/www/mrtg ← 追加(MRTGによる編集結果HTML格納先ディレクトリを指定する)

#  to get bits instead of bytes and graphs growing to the right
# Options[_]: growright, bits
Options[_]: growright, noinfo ← 追加(グラフの過去~未来への推移方向を左→右にする)

EnableIPv6: no
Language:eucjp ← 追加(日本語化)

### Interface 2 >> Descr: 'eth0' | Name: '' | Ip: '192.168.1.3' | Eth: '00-a0-c9-de-81-af' ###

Target[192.168.1.3_eth0]: \eth0:public@192.168.1.3:
↓
Target[eth0]: \eth0:public@192.168.1.3: ← 変更
SetEnv[192.168.1.3_eth0]: MRTG_INT_IP="192.168.1.3" MRTG_INT_DESCR="eth0"
↓
SetEnv[eth0]: MRTG_INT_IP="192.168.1.3" MRTG_INT_DESCR="eth0" ← 変更
MaxBytes[192.168.1.3_eth0]: 12500000
↓
MaxBytes[eth0]: 12500000 ← 変更
Title[192.168.1.3_eth0]: eth0 -- linux
↓
Title[eth0]: eth0トラフィック ← 変更
PageTop[192.168.1.3_eth0]: <H1>eth0 -- linux</H1>
↓
PageTop[eth0]: <H1>eth0トラフィック</H1> ← 変更
以降の行を削除(ここから)
 <TABLE>
   <TR><TD>System:</TD>     <TD>linux 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></TD></TR>
   <TR><TD>Max Speed:</TD>  <TD>12.5 MBytes/s</TD></TR>
   <TR><TD>Ip:</TD>         <TD>192.168.1.3 ()</TD></TR>
 </TABLE>
以降の行を削除(ここまで)

以降はNICを複数挿している場合のみ
### Interface 3 >> Descr: 'eth1' | Name: '' | Ip: '' | Eth: '00-90-99-80-27-2d' ###

Target[192.168.1.3_eth1]: \eth1:public@192.168.1.3:
↓
Target[eth1]: \eth1:public@192.168.1.3: ← 変更
SetEnv[192.168.1.3_eth1]: MRTG_INT_IP="" MRTG_INT_DESCR="eth1"
↓
SetEnv[eth1]: MRTG_INT_IP="" MRTG_INT_DESCR="eth1" ← 変更
MaxBytes[192.168.1.3_eth1]: 12500000
↓
MaxBytes[eth1]: 12500000 ← 変更
Title[192.168.1.3_eth1]: eth1 -- linux
↓
Title[eth1]: eth1トラフィック ← 変更
PageTop[192.168.1.3_eth1]: <H1>eth1 -- linux</H1>
↓
PageTop[eth1]: <H1>eth1トラフィック</H1> ← 変更
以降の行を削除(ここから)
 <TABLE>
   <TR><TD>System:</TD>     <TD>linux 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>eth1  </TD></TR>
   <TR><TD>ifType:</TD>     <TD>ethernetCsmacd (6)</TD></TR>
   <TR><TD>ifName:</TD>     <TD></TD></TR>
   <TR><TD>Max Speed:</TD>  <TD>12.5 MBytes/s</TD></TR>
 </TABLE>
以降の行を削除(ここまで)

(3)CPU使用率表示設定追加
[root@centos ~]# vi /etc/mrtg/mrtg.cfg ← MRTG設定ファイル編集
以下の全行を追加
### CPU Load Average ###
Target[cpu]: .1.3.6.1.4.1.2021.10.1.5.1&.1.3.6.1.4.1.2021.10.1.5.2:public@192.168.1.3
MaxBytes[cpu]: 100
Unscaled[cpu]: dwmy
Options[cpu]: gauge, absolute, growright, noinfo, nopercent
YLegend[cpu]: CPU Load(%)
ShortLegend[cpu]: (%)
LegendI[cpu]: 1分間平均
LegendO[cpu]: 5分間平均
Legend1[cpu]: 1分間平均(%)
Legend2[cpu]: 5分間平均(%)
Title[cpu]: CPU使用率
PageTop[cpu]: <H1>CPU使用率</H1>

(4)空きメモリ量表示設定追加
[root@centos ~]# 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値

[root@centos ~]# 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:public@192.168.1.3
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>

(5)ディスク使用率表示設定追加
[root@centos ~]# 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:public@192.168.1.3
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>

■MRTG起動(システムの文字コードがUTF-8の場合)

(1)設定ファイルの文字コードをEUCに変換
[root@centos ~]# cp /etc/mrtg/mrtg.cfg /etc/mrtg/mrtg.cfg.org ← 設定ファイルをリネームコピー

[root@centos ~]# nkf -e /etc/mrtg/mrtg.cfg.org > /etc/mrtg/mrtg.cfg ← 設定ファイルの文字コードをEUCに変換
※以後、MRTG設定ファイルを編集する場合は/etc/mrtg/mrtg.cfg.orgを編集するようにし、編集後、nkfコマンドでEUCに変換するようにすること

(2)MRTG起動スクリプト作成
[root@centos ~]# vi mrtg.sh ← MRTG起動スクリプト作成
#!/bin/bash

LOCK=/var/lock/mrtg/mrtg_l
CONFCACHE=/var/lib/mrtg/mrtg.ok

export LANG=ja_JP.eucJP
mrtg /etc/mrtg/mrtg.cfg --lock-file $LOCK --confcache-file $CONFCACHE

[root@centos ~]# chmod 700 mrtg.sh ← MRTG起動スクリプトへ実行権限付加

(3)MRTG起動
[root@centos ~]# ./mrtg.sh ← MRTG起動(1回目)
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

[root@centos ~]# ./mrtg.sh ← 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

[root@centos ~]# ./mrtg.sh ← MRTG起動(3回目)
※MRTGは過去2回起動時のログを使用するため、mrtg.cfg修正後2回目までのMRTG起動時には必ずWARNINGが出るので、mrtg.cfg修正後はMRTG起動を3回行なう。

■MRTG起動(システムの文字コードがEUCの場合)

[root@centos ~]# mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(1回目)
Rateup WARNING: /usr/bin//rateup Can't remove eth0.old updating log file
Rateup WARNING: /usr/bin//rateup Can't remove eth1.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

[root@centos ~]# mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(2回目)
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

[root@centos ~]# mrtg /etc/mrtg/mrtg.cfg ← MRTG起動(3回目)
※MRTGは過去2回起動時のログを使用するため、mrtg.cfg修正後2回目までのMRTG起動時には必ずWARNINGが出るので、mrtg.cfg修正後はMRTG起動を3回行なう。

■MRTG確認

(1)内部からのみアクセスできるようにする場合
[root@centos ~]# 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 192.168.1 ← 追加(内部ネットワークからのアクセスを許可)
    Allow from ::1
    # Allow from .example.com
</Location>

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

(2)外部からもアクセスできるようにする場合
[root@centos ~]# 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 All ← 追加(全てのホストからのアクセスを許可)
    #Allow from 127.0.0.1 ← 行頭に#を追加(コメントアウト)
    Allow from ::1
    # Allow from .example.com
</Location>

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

(3)ネットワークトラフィック状況監視画面確認
http://サーバー名/mrtg/eth0.htmlにアクセスしてこのような画面が表示されればOK

(4)CPU使用率監視画面確認
http://サーバー名/mrtg/cpu.htmlにアクセスしてこのような画面が表示されればOK

(5)空きメモリ量監視画面確認
http://サーバー名/mrtg/mem.htmlにアクセスしてこのような画面が表示されればOK

(6)ディスク使用率監視画面確認
http://サーバー名/mrtg/disk.htmlにアクセスしてこのような画面が表示されればOK

■MRTG自動起動設定(システムの文字コードがUTF-8の場合のみ)

[root@centos ~]# vi /etc/cron.d/mrtg ← MRTG定期自動起動設定ファイル編集
*/5 * * * * root /usr/bin/mrtg /etc/mrtg/mrtg.cfg --lock-file /var/lock/mrtg/mrtg_l --confcache-file /var/lib/mrtg/mrtg.ok
↓
*/5 * * * * root /root/mrtg.sh ← 作成したMRTG起動スクリプトに変更
※システムの文字コードがEUCの場合はデフォルトのままで問題なし

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

各監視画面を統合したこのようなインデックスページをMRTG付属のindexmakerというツールで作成する
[root@centos ~]# indexmaker --columns=1 \
--addhead="<META HTTP-EQUIV=\"Content-Type\" CONTENT=\"text/html; charset=euc-jp\">" \
/etc/mrtg/mrtg.cfg > /var/www/mrtg/index.html ← MRTGインデックスページ作成