CentOS

2007/4/11 03:50 PM 更新

MRTG

SNMPで取得したMIB情報を可視化(グラフ化)するためにMRTGを導入する。
MRTGは以下のソフトウェアが必要になるので事前にインストールしておく。
 ・zlib
 ・libpng
 ・gd

MRTGの公式サイトより最新のソースファイルを入手しインストールする。
$ wget http://oss.oetiker.ch/mrtg/pub/mrtg-2.15.1.tar.gz
$ tar xvzf mrtg-2.15.1.tar.gz
$ cd mrtg-2.15.1
$ ./configure
$ make
# make install
MRTGがファイルを作成するディレクトリを作成する。
また、ソースファイルのimagesディレクトリ配下のファイルをコピーしておく。
# mkdir -p /usr/local/mrtg-2/htdocs/
# cp $SRC/images/* /usr/local/mrtg-2/htdocs/


一番基本的なトラフィックをグラフ化してみる。
トラフィック量を取得するMIBは以下のとおりなのでこの値を使う。
 ・.1.3.6.1.2.1.2.2.1.10.2
 ・.1.3.6.1.2.1.2.2.1.16.2

  /usr/local/mrtg-2/conf/mrtg.cfg
######################################################################
# Multi Router Traffic Grapher -- Example Configuration File
######################################################################
# This file is for use with mrtg-2.0
#
# Minimal mrtg.cfg
#--------------------

WorkDir: /usr/local/mrtg-2/htdocs
Language: eucjp

#+++++++++++++++
#Traffic --> bruna --> eth0
#+++++++++++++++
Target[bruna_eth0_traffic]:      .1.3.6.1.2.1.2.2.1.10.2&.1.3.6.1.2.1.2.2.1.16.2:public@192.168.xx.yy:
Options[bruna_eth0_traffic]:     growright,bits
AbsMax[bruna_eth0_traffic]:      1250000
MaxBytes[bruna_eth0_traffic]:    16000
WithPeak[bruna_eth0_traffic]:    wmy
YLegend[bruna_eth0_traffic]:     Bits per Second
ShortLegend[bruna_eth0_traffic]: b/s
LegendI[bruna_eth0_traffic]:      In
LegendO[bruna_eth0_traffic]:      Out
Legend1[bruna_eth0_traffic]:     INトラフィック
Legend2[bruna_eth0_traffic]:     OUTトラフィック
Legend3[bruna_eth0_traffic]:     IN 5分平均ピーク
Legend4[bruna_eth0_traffic]:     OUT 5分平均ピーク
Colours[bruna_eth0_traffic]:     LIGHTBLUE#61A0DF,DKBLUE#0000DD,DARKGREEN#006600,VIOLET#FF00FF
Title[bruna_eth0_traffic]:       bruna --> eth0 --> traffic
PageTop[bruna_eth0_traffic]:     <h1>bruna --&gt; eth0 --&gt; traffic</h1>
MRTGは実行したときの取得データを元にグラフを描画するため複数のデータが必要になる。
なお、最初2回のコマンド実行時は過去データが無いためにエラーが出る。
# LANG=C
# /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/conf/mrtg.cfg
Rateup WARNING: /usr/local/mrtg-2/bin/rateup could not read the primary log file for bruna_eth0_traffic
Rateup WARNING: /usr/local/mrtg-2/bin/rateup The backup log file for bruna_eth0_traffic was invalid as well
Rateup WARNING: /usr/local/mrtg-2/bin/rateup Can't remove bruna_eth0_traffic.old updating log file
Rateup WARNING: /usr/local/mrtg-2/bin/rateup Can't rename bruna_eth0_traffic.log to bruna_eth0_traffic.old updating log file

# /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/conf/mrtg.cfg
Rateup WARNING: /usr/local/mrtg-2/bin/rateup Can't remove bbruna_eth0_traffic.old updating log file

# /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/conf/mrtg.cfg
これで先ほど作成したディレクトリにファイルができているはずである。
# ls -al /usr/local/mrtg-2/htdocs
-rw-r--r--  1 root   root    4023  4月 10 15:40 bruna_eth0_traffic-day.png
-rw-r--r--  1 root   root    1508  4月 10 14:05 bruna_eth0_traffic-month.png
-rw-r--r--  1 root   root    1668  4月 10 15:15 bruna_eth0_traffic-week.png
-rw-r--r--  1 root   root    1883  4月  9 17:40 bruna_eth0_traffic-year.png
-rw-r--r--  1 root   root    6668  4月 10 15:40 bruna_eth0_traffic.html
-rw-r--r--  1 root   root   63636  4月 10 15:40 bruna_eth0_traffic.log
-rw-r--r--  1 root   root   63634  4月 10 15:35 bruna_eth0_traffic.old
-rw-r--r--  1 root   root     343  1月 30 10:15 mrtg-l.gif
-rw-r--r--  1 root   root     538  1月 30 10:15 mrtg-l.png
-rw-r--r--  1 root   root    1144  1月 30 10:15 mrtg-m.gif
-rw-r--r--  1 root   root     414  1月 30 10:15 mrtg-m.png
-rw-r--r--  1 root   root    1779  1月 30 10:15 mrtg-r.gif
-rw-r--r--  1 root   root    1759  1月 30 10:15 mrtg-r.png
-rw-r--r--  1 root   root    4696  1月 30 10:15 mrtg-ti.gif
-rw-r--r--  1 root   root    4293  1月 30 10:15 mrtg-ti.png
-rw-r--r--  1 root   root    3602  1月 30 23:50 mrtg.css
定期的にMRTGを動かしてデータを収集する必要があるので5分に1回コマンドが実行されるようにcronを設定する。
  /etc/crontab
# For MRTG
0-59/5 * * * * root /usr/local/mrtg-2/bin/mrtg /usr/local/mrtg-2/conf/mrtg.cfg > /dev/null 2>&1
ブラウザからアクセスできるようApacheの設定変更をおこなう。
  /usr/local/apache2/conf/httpd.conf
    Alias       /mrtg/         "/usr/local/mrtg-2/htdocs/"

ブラウザでhttp://site/mrtg/xxx.htmlにアクセスしグラフが見えればOK。
bruna_eth0_traffic-day.png