Personal tools
You are here: Home コラム 技術コラム snmp+mrtgの設定のメモ
Document Actions

snmp+mrtgの設定のメモ

サーバ上のネットワーク負荷、CPU負荷、空きメモリ、httpdのプロセス数などの推移を チェックするために、MRTGをセットアップしました。 そのときのメモです。

snmpとmrtgでの設定メモです。
あまり詳しく調べないまま書いているので、間違えたことや変なことを書いている可能性もあります。

概要


MRTGはネットワーク負荷など監視し、その推移を画像にしたHTMLページを生成してくれるツールです。
詳しくは
MRTG日本語ページ
http://www.mrtg.jp/doc/
などを参照してください

同じ作者が作成した
RRDTool
http://people.ee.ethz.ch/~oetiker/webtools/rrdtool/
もありますが、今回はなじみがあるMRTGを使いました。

サーバとMRTGの間の通信にはSNMPを使用しました。
サーバにはsnmpエージェントとしてnet-snmpdをセットアップします。
また、apacheが入っているPCにsnmpマネージャとしてMRTGをセットアップします


net-snmpdのインストール、設定


snmpdのインストール、設定については

【連載 】SNMPによるネットワークシステムの監視 (STACK*)


Linux管理者への道 第7回 障害の兆候を見逃さないためのサーバ監視 Part3 (@IT)

あたりに詳しく書いてあります。

大体の流れとしては、
1. net-snmpdをインストール
2. snmpd.confのセキュリティ設定のcom2sec、group、view、accessを設定します。
コミュニティ名をデフォルトのまま使わないように注意します。
3. snmpd.confのsyscontactとsyslocationの設定をします。
3. 試しにsnmpdを起動してみる
com2secで指定したアクセス可能なネットワーク内から
snmpwalk -v1 -c コミュニティ名 サーバ名
などのようにしてレスポンスが返ってくるか確認する。

とりあえずここまででsnmpdの設定は一通り終わりです。

MRTGのインストール、設定


net-snmpdの準備ができたら、MRTGをセットアップします。

MRTGのセットアップは、

MRTGによるサーバ監視システムの構築 (@IT)


【連載 】SNMPによるネットワークシステムの監視 第2回:MRTGの導入 (STACK*)


あたりに詳しく書いてあります。


1. MRTGを動かすサーバを決める


MRTGはグラフ化する際に、htmlとpngを生成するので、webサーバ越しに見ると便利です。

2. MRTGをインストール



3. MRTGの設定(cfgmaker)


webサーバのDocumentRootを/var/www/htmlと仮定します。また、設定ファイルの置き場所を/etc/mrtg、
MRTGの出力ファイルの場所を/var/www/html/mrtgとします。
ICONは/var/www/html/mrtg-doc におきました

% cfgmaker --output=/etc/mrtg/mrtg.cfg コミュニティ名@(監視対象の)ホスト名
とすると、以下のような設定ファイルの雛形がmrtg.cfgに書き込まれます。
(コミュニティ名communame、ホスト名server1の場合)

----

# Created by
# /usr/bin/cfgmaker communame@server1


### Global Config Options

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

# or for NT
# WorkDir: c:\mrtgdata

### Global Defaults

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

EnableIPv6: no

######################################################################
# System: server1
# Description: Linux server1 2.4.xx #1 Thu Dec 26 20:58:54 JST 2002 i686
# Contact: Root <xxx@ariel-networks.com> (configure /etc/snmp/snmp.local.conf)
# Location: Unknown (configure /etc/snmp/snmp.local.conf)
######################################################################

### Interface 2 >> Descr: 'eth0' | Name: '' | Ip: 'xxx.xxx.xxx.xxx' | Eth: 'xx-xx-xx-xx-xx-xx-xx' ###

Target[server1_2]: 2:communame@server1:
SetEnv[server1_2]: MRTG_INT_IP="xxx.xxx.xxx.xxx" MRTG_INT_DESCR="eth0"
MaxBytes[server1_2]: 1250000
Title[server1_2]: Traffic Analysis for 2 -- server1
PageTop[server1_2]: <H1>Traffic Analysis for 2 -- server1</H1>
<TABLE>
<TR><TD>System:</TD> <TD>server1 in Unknown (configure /etc/snmp/snmp.local.conf)</TD><:/TR>
<TR><TD>Maintainer:</TD> <TD>Root <xxxx@ariel-networks.com> (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>1250.0 kBytes/s</TD><:/TR>
<TR><TD>Ip:</TD> <TD>xxx.xxx.xxx.xxx ()</TD><:/TR>
</TABLE>

----

エラーのみしか出力されない場合は、以下のことを確認します
- 監視したいサーバ/ネットワーク機器でsnmpエージェントが動いている事
- MRTGを動かすサーバから監視対象のudpの161番ポートに通信可能なこと

設定ファイルの雛形では、ネットワーク負荷を表示する設定が書き込まれています。
出力ディレクトリのWorkDir、文字コードのLanguageの設定を下記のように変更しました。

----

WorkDir: /var/www/html/mrtg
Language: euc-jp
IconDir: ../mrtg_docs/

----

実際にmrtgを動かします。
mrtg /etc/mrtg/mrtg.cfg
3回連続して動かすと、pngファイルが出力されます。

以下にアクセスすることで、作成されたHTMLファイルを確認できます
/var/www/html/mrtg/server1_2.html
http://サーバ名/mrtg/server1_2.html
(server1_2の部分には、設定ファイルの監視項目の名前(targetの[]内に入っている文字)が入ります。)

確か、FedoraCore2のapacheでは文字化けしたような気がします。
httpd.confで、AddDefaultCharsetをoffにしました。

複数のサーバ、ネットワーク機器を監視する場合、
% cfgmaker --output=/etc/mrtg/mrtg.cfg コミュニティ名@サーバ1 コミュニティ名@サーバ2 ....
として全て1つの設定ファイルで行うことができるようです。

私は知らなかったので、監視機器毎に設定ファイルを作成しました。
WorkDirも、/var/www/html/mrtg/server1、/var/www/html/mrtg/server2というように分けています。
% cfgmaker --output=/etc/mrtg/サーバ1.cfg コミュニティ名@サーバ1
% cfgmaker --output=/etc/mrtg/サーバ2.cfg コミュニティ名@サーバ2


4. indexmakerでindex.html作成


設定ファイルに監視項目が複数ある場合、indexページを作成した方が便利です。
indexmaker 設定ファイル > 出力htmlファイル
というようにすると、監視項目へのリンクが並んだindexページが作成できます
%indexmaker /etc/mrtg/mrtg.conf > /var/www/mrtg/index.html



5. crontabでの定期的な実行


MRTGは定期的に実行する必要があります。
デーモンとして起動することもできるようですが、crontabで動かすことにしました。

5分おきにmrtgを動かしています。

----

% crontab -e
% /5 * * * * /usr/bin/mrtg /etc/mrtg/mrtg.cfg

----

6. 監視項目の追加


CPU負荷、空きメモリ量、ディスク容量、プロセス数や、任意のスクリプトの返す値などもグラフ化できるようです。
1つの画像に2項目までグラフ化できます。

【連載 】SNMPによるネットワークシステムの監視 第3回:MRTGの設定 (STACK *)

などに詳しく書いてあるようです。

6.1. CPU負荷



監視対象サーバのsnmpd.confのCPU負荷に関する設定部分は以下の部分です。
----

# load average checks
#

# load [1MAX=12.0] [5MAX=12.0] [15MAX=12.0]
#
# 1MAX: If the 1 minute load average is above this limit at query
# time, the errorFlag will be set.
# 5MAX: Similar, but for 5 min average.
# 15MAX: Similar, but for 15 min average.

# Check for loads:
load 12 14 14

(以下、情報が格納されるMIBの説明)

----

mrtg側の設定ファイルは以下のようになります。
ここでは、1分間のCPUの平均負荷と、5分間の平均負荷をグラフ化します
----

#
# CPU Load Avarage
#

Target[cpu]: .1.3.6.1.4.1.2021.10.1.5.1&.1.3.6.1.4.1.2021.10.1.5.2:communame@server1:
ThreshMaxI[cpu]: 50
MaxBytes[cpu]: 100
Title[cpu]: CPU Load Average
PageTop[cpu]: %lt;H1> Load Average on Server1 </H1>
Options[cpu]: gauge
YLegend[cpu]: CPU Load Average
ShortLegend[cpu]: %
Legend1[cpu]: 1分間のCPU平均負荷
Legend2[cpu]: 5分間のCPU平均負荷
LegendI[cpu]: 1分
LegendO[cpu]: 5分

----

6.2. 空きメモリのチェック



mrtg側で以下のような設定をを行うと、空きメモリ容量をチェックすることができます。
実メモリとスワップの2つをグラフ化しています。
MaxBytesには監視対象サーバの総メモリ量を書き込んでいます。
----

#
# Memory Usage
#

Target[mem]: 1.3.6.1.4.1.2021.4.4.0&1.3.6.1.4.1.2021.4.6.0:communame@server1:
#ThreshMaxI[mem]: 80
MaxBytes1[mem]: 979956
MaxBytes2[mem]: 902148
Title[mem]: Memory Usage "real" and "swap"
PageTop[mem]: %lt;H1>Memory Usage "real" and "swap" on Server1 </H1>
Options[mem]: gauge
YLegend[mem]: Memory Usage
ShortLegend[mem]: kbyte
Legend1[mem]: 空きメモリ量(swap)
Legend2[mem]: 空きメモリ量(real)
LegendI[mem]: 空きメモリ量(swap)
LegendO[mem]: 空きメモリ量(real)

----

6.3. 使用ディスク容量のチェック


監視対象サーバのsnmpd.confの使用ディスク容量チェックに関する設定部分は以下の部分です。

----

###############################################################################
# disk checks
#

# The agent can check the amount of available disk space, and make
# sure it is above a set limit.

# disk PATH [MIN=100000]
#
# PATH: mount path to the disk in question.
# MIN: Disks with space below this value will have the Mib's errorFlag set.
# Default value = 100000.

# Check the / partition and make sure it contains at least 10 megs.

disk / 5%
disk /home 5%

(以下、情報が格納されるMIBの説明)

----

mrtg側の設定ファイルは以下のようになります。
ここでは、/と、/homeの使用ディスク容量をチェックします。
----

#
# Disk Usage
#

Target[df]: .1.3.6.1.4.1.2021.9.1.9.1&.1.3.6.1.4.1.2021.9.1.9.2:communame@server1:
ThreshMaxI[df]: 80
MaxBytes[df]: 100
Title[df]: Disk Usage "/"
PageTop[df]: %lt;H1>Disk Usage "/" on server1 </H1>
Options[df]: gauge
YLegend[df]: Disk Usage
ShortLegend[df]: %
Legend1[df]:/
Legend2[df]:/home
LegendI[df]:/
LegendO[df]:/home

----

/だけしかチェックしない場合は、targetとして同じMIBを2回書きます
Target[df]: .1.3.6.1.4.1.2021.9.1.9.1&.1.3.6.1.4.1.2021.9.1.9.1:communame@server1:


6.4. 特定のプロセス数のチェック


監視対象サーバのsnmpd.confのプロセス数チェックに関する設定部分は以下の部分です。
以下では、httpdと、postmasterのプロセス数をチェックするようにしています

----

###############################################################################
# Process checks.
#
# The following are examples of how to use the agent to check for
# processes running on the host. The syntax looks something like:
#
# proc NAME [MAX=0] [MIN=0]
#
# NAME: the name of the process to check for. It must match
# exactly (ie, http will not find httpd processes).
# MAX: the maximum number allowed to be running. Defaults to 0.
# MIN: the minimum number to be running. Defaults to 0.

#
# Examples:
#

# Make sure mountd is running
proc httpd 30 1
proc postmaster 10 1

(以下、情報が格納されるMIBの説明)

----

httpdのプロセス数を監視するmrtg側の設定ファイルは以下のようになります。
----

#
# Number of HTTP process
#
Target[http_proc]: .1.3.6.1.4.1.2021.2.1.5.1&.1.3.6.1.4.1.2021.2.1.5.1:communame@server1:
ThreshMaxI[http_proc]: 150
MaxBytes[http_proc]: 150
Title[http_proc]: Number of httpd (80 port)
PageTop[http_proc]: %lt;H1> Number of httpd on server1 </H1>
Options[http_proc]: gauge,integer
YLegend[http_proc]: httpd
ShortLegend[http_proc]: process
Legend1[http_proc]:起動中のhttpd
Legend2[http_proc]:
LegendI[http_proc]:起動中のhttpd
LegendO[http_proc]:

----


6.5. 特定プログラムの実行結果



特定プログラムの実行結果をチェックするには、監視サーバのsnmpd.confで
exec 名前 プログラム名 と書きます。

.1.3.6.1.4.1.2021.8.1.100.n にプログラムの戻り値、
.1.3.6.1.4.1.2021.8.1.101.n にプログラムの標準出力への出力の1行目が入ります。

execを複数書いた場合は、最初のものがn=1、次のものはn=2、...となります。

以下の例では、tomcatのスレッド数と、httpdのopen pipe数をチェックしています
----

###############################################################################
# Executables/scripts
#

#
# You can also have programs run by the agent that return a single
# line of output and an exit code. Here are two examples.
#
# exec NAME PROGRAM [ARGS ...]
#
# NAME: A generic name.
# PROGRAM: The program to run. Include the path!
# ARGS: optional arguments to be passed to the program

exec tomcat_threads /bin/sh /etc/snmp/bin/tomcat_threads.sh
exec lsof_pipe_httpd /bin/sh /etc/snmp/bin/lsof_pipe_httpd.sh

----

上記の場合、
.1.3.6.1.4.1.2021.8.1.100.1 ... /etc/snmp/bin/tomcat_threads.shの戻り値
.1.3.6.1.4.1.2021.8.1.100.2 ... /etc/snmp/bin/lsof_pipe_httpd.shの戻り値

.1.3.6.1.4.1.2021.8.1.101.1 ... /etc/snmp/bin/tomcat_threads.shの標準出力への出力の1行目
.1.3.6.1.4.1.2021.8.1.101.2 ... /etc/snmp/bin/tomcat_threads.shの標準出力への出力の1行目
となります。

tomcat_threads.shでは、tomcatのスレッド数を戻り値で渡しています。
----

#!/bin/sh
tomcat_threads=`ps -U tomcat-sun -mf | grep tomcat | wc -l`
exit $tomcat_threads

----

lsof_pipe_httpd.shでは、httpdのopen pipe数を標準出力で渡しています
----

#!/bin/sh
lsof_num=$(/usr/sbin/lsof -c httpd | grep pipe | wc -l )
echo $lsof_num
exit 0

----

mrtgの設定は以下のようになります。
tomcatのスレッド数を表示する画像と、httpdのopenpipeの画像が出力されます。

----

#
# Number of Tomcat Threads
#
Target[tomcat_proc]: .1.3.6.1.4.1.2021.8.1.100.1&.1.3.6.1.4.1.2021.8.1.100.1:communame@Server1:
ThreshMaxI[tomcat_proc]: 50
MaxBytes[tomcat_proc]: 50
Title[tomcat_proc]: Number of Tomcat Threads
PageTop[tomcat_proc]: %lt;H1> Number of Tomcat Threads on Server1 </H1>
Options[tomcat_proc]: gauge,integer
YLegend[tomcat_proc]: Tomcat
ShortLegend[tomcat_proc]: threads
Legend1[tomcat_proc]: 起動中のTOMCAT
Legend2[tomcat_proc]:
LegendI[tomcat_proc]: 起動中のTOMCAT
LegendO[tomcat_proc]:

#
# Number of open pipe (lsof)
#
Target[server1_lsof]: .1.3.6.1.4.1.2021.8.1.101.2&.1.3.6.1.4.1.2021.8.1.101.2:communame@Server1:
#ThreshMaxI[server1_lsof]: 10
MaxBytes[server1_lsof]: 104698
Title[server1_lsof]: Number of open pipe
PageTop[server1_lsof]: %lt;H1> Number of Open pipe on Server1 </H1>
Options[server1_lsof]: gauge,integer
YLegend[server1_lsof]: open pipe
ShortLegend[server1_lsof]: pipes
Legend1[server1_lsof]:httpd
Legend2[server1_lsof]:
LegendI[server1_lsof]:httpd
LegendO[server1_lsof]:

----

その他


今のところただグラフ化しているだけですが、閾値を越えたら何らかのアクションをとるようにしてみたいです。
また、監視サーバ、機器が別のネットワークにある場合、udpの161番ポートへは通信できないことが多いようです。
何らかのトンネリングが必要なのかもしれません。

Copyright(C) 2001 - 2006 Ariel Networks, Inc. All rights reserved.