SNMPとMRTG設定メモ

2016.04.23
2009.07.29


 図でクライアントがNMS( ネットワーク管理ステーション)、管理エージェントがサーバになります。そしてその二つを結ぶプロトコルがSNMPです。
 figure(nms and server)


1  net-snmpのインストール

 まず管理エージェントとしてnet-snmpをインストールします。
  lennyでは snmpd というパッケージです。
 # apt-get install snmpd

 またsnmp パッケージ(dが付かない)もインストールしておきます。
 こちらは下の管理エージェントの設定ツールsnmpconfやアクセスツール
 snmpwalkなどが入っています。
  # apt-get install snmp


2  管理エージェントの設定

 管理エージェント snmpdの設定ファイルは/etc/snmp/snmpd.confです。
 
 これを次のようにユーザ権限でホームディレクトリに て設 定します。

$ snmpconf -g basic_setup

The following installed configuration files were found:

   1:  /etc/snmp/snmpd.conf
   2:  /etc/snmp/snmptrapd.conf

Would you like me to read them in?  Their content will be merged with the
output files created by this session.

Valid answer examples: "all", "none","3","1,2,5"

Read in which (default = all):
(ENTER)

************************************************
*** Beginning basic system information setup ***
************************************************
Do you want to configure the information returned in the system MIB group (contact info, etc)? (default = y):
(ENTER)

Configuring: syslocation
Description:
  The [typically physical] location of the system.
    Note that setting this value here means that when trying to
    perform an snmp SET operation to the sysLocation.0 variable will make
    the agent return the "notWritable" error code.  IE, including
    this token in the snmpd.conf file will disable write access to
    the variable.
    arguments:  location_string

The location of the system:  Computer Room 01
(システムの所在地)

Finished Output: syslocation  "Computer room 01"

Configuring: syscontact
Description:
  The contact information for the administrator
    Note that setting this value here means that when trying to
    perform an snmp SET operation to the sysContact.0 variable will make
    the agent return the "notWritable" error code.  IE, including
    this token in the snmpd.conf file will disable write access to
    the variable.
    arguments:  contact_string

The contact information:
hoge@hogehoge.com(システム管理者アドレス)

Finished Output: syscontact  hoge@hogehoge.com
Do you want to properly set the value of the sysServices.0 OID (if you don't know, just say no)? (default = y): 
(ENTER)
(システムの 提供するサー ビスを設定したいですか?)

Configuring: sysservices
Description:
  The proper value for the sysServices object.
    arguments:  sysservices_number


does this host offer physical services (eg, like a repeater) [answer 0 or 1]: 0
(物理層での サービスをし てますか? Yes 1  No 0)

does this host offer datalink/subnetwork services (eg, like a bridge): 0

(ブリッ ジのようなデータリンク層のサービスをしてますか? Yes 1  No 0)

does this host offer internet services (eg, supports IP): 1
(IPのサポートをしてますか? Yes 1  No 0)

does this host offer end-to-end services (eg, supports TCP): 1
(TCPのサポートをしてますか? Yes 1  No 0)

does this host offer application services (eg, supports SMTP): 1
(SMTP のサポートをしてますか? Yes 1  No 0)

Finished Output: sysservices 76
**************************************
*** BEGINNING ACCESS CONTROL SETUP ***
**************************************
Do you want to configure the agent's access control? (default = y): 
(ENTER)
(アクセ スコントロールの設定をしますか)
Do you want to allow SNMPv3 read-write user based access (default = y): (ENTER)

Configuring: rwuser
Description:
  a SNMPv3 read-write user
    arguments:  user [noauth|auth|priv] [restriction_oid]

The SNMPv3 user that should have read-write access:
hoge (読み書き可能なユーザ名)
The minimum security level required for that user [noauth|auth|priv, default = auth]:
(ENTER)
(最低限のセキュリティレベルの設定)

The OID that this community should be restricted to [if appropriate]: (ENTER)
(コミュ ニティのOIDを限定する。)

Finished Output: rwuser  hoge
Do another rwuser line? (default = y):
n (他のrwユーザを設定するか?)
Do you want to allow SNMPv3 read-only user based access (default = y): (ENTER)
(Read -onlyのユーザを設定するか。)

Configuring: rouser
Description:
  a SNMPv3 read-only user
    arguments:  user [noauth|auth|priv] [restriction_oid]

Enter the SNMPv3 user that should have read-only access to the system:
guest
(Read- onlyの ユーザの名前)

The minimum security level required for that user [noauth|auth|priv, default = auth]:
(ENTER)
The OID that this community should be restricted to [if appropriate]: (ENTER)

Finished Output: rouser  guest
Do another rouser line? (default = y):
n(他のroユーザを設定するか?)
Do you want to allow SNMPv1/v2c read-write community access (default = y):  (ENTER)
 (SNMPv1/v2 読み書き可能なコミュニティを設定するか?)

Configuring: rwcommunity
Description:
  a SNMPv1/SNMPv2c read-write access community name
    arguments:  community [default|hostname|network/bits] [oid]

Enter the community name to add read-write access for:
hoge.com
(読み書き可 能なコミュニ ティ名)

Enter the community name to add read-write access for: hoge.com
The hostname or network address to accept this community name from [RETURN for all]:
192.168.1.0/24
(コミュ ニティにアクセスできるホスト名又はネットワークアドレス)
The OID that this community should be restricted to [RETURN for no-restriction]:
(Return ならOID を限定しない。)

Finished Output: rwcommunity  hoge.com 192.168.1.0/24
Do another rwcommunity line? (default = y):
n
Do you want to allow SNMPv1/v2c read-only community access (default = y):(ENTER)
(Read -onlyのコミュニティを設定するか)

Configuring: rocommunity
Description:
  a SNMPv1/SNMPv2c read-only access community name
    arguments:  community [default|hostname|network/bits] [oid]

The community name to add read-only access for:
public
(Read -onlyのコミュニティ名)
The hostname or network address to accept this community name from [RETURN for all]: 127.0.0.1
(Read -onlyのホスト名またはネットワークアドレス)

The OID that this community should be restricted to [RETURN for no-restriction]:
(ENTER)
(Return ならOID を限定しない。)

Finished Output: rocommunity  public 127.0.0.1
Do another rocommunity line? (default = y): n
(他の Read-only のコミュニティを設定するか。)
****************************************
*** Beginning trap destination setup ***
****************************************
Do you want to configure where and if the agent will send traps? (default = y): n
(トラップの設定をするか。)

****************************************
*** Beginning monitoring setup ***
****************************************
Do you want to configure the agent's ability to monitor various aspects of your system? (default = y): 
(ENTER)
(システ ムにおけるモニタの設定をするか。)

Do you want to configure the agents ability to monitor processes? (default = y): (ENTER)
(プロセ スおけるモニタの設定をするか。)

Configuring: proc
Description:
  Check for processes that should be running.
      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.

    The results are reported in the prTable section of the UCD-SNMP-MIB tree
    Special Case:  When the min and max numbers are both 0, it assumes
    you want a max of infinity and a min of 1.

Name of the process you want to check on:
httpd
(チェックし たいプロセス 名)

Maximum number of processes named 'httpd' that should be running [default = 0]:(ENTER)
(最大プロセス数)
Minimum number of processes named 'httpd' that should be running [default = 0]:(ENTER)
(最小プ ロセス数)
Finished Output: proc  httpd
Do another proc line? (default = y):
n
(他のチェッ クしたいプロ セスの設定)

Do you want to configure the agents ability to monitor disk space? (default = y):
(ENTER)
(ディス クスペースのモニタについての設定)

Configuring: disk
Description:
  Check for disk space usage of a partition.
    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.
            Can be a raw byte value or a percentage followed by the %
            symbol.  Default value = 100000.

    The results are reported in the dskTable section of the UCD-SNMP-MIB tree

Enter the mount point for the disk partion to be checked on:
/var
(チェックす るマウントポ イント)
Enter the minimum amount of space that should be available on /var: 5%
(最小の空き容量)
Finished Output: disk  /var 5%
Do another disk line? (default = y): 
n(他のディスクも設定するか)
Do you want to configure the agents ability to monitor load average? (default = y): (ENTER)
(ロード アベレージのモニタリングの設定)

Configuring: load
Description:
  Check for unreasonable load average values.
    Watch the load average levels on the machine.

     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.

    The results are reported in the laTable section of the UCD-SNMP-MIB tree

Enter the maximum allowable value for the 1 minute load average: 
80
(1分間のロードアベレージの許される最大値)
Enter the maximum allowable value for the 5 minute load average: 60
(5分間の ロードアベレー ジの許される最大値)
Enter the maximum allowable value for the 15 minute load average: 50
(15分 間のロードアベレージの許される最大値)

Finished Output: load  80 60 50
Do another load line? (default = y): n
(他のロード アベレージの 設定)
Do you want to configure the agents ability to monitor file sizes? (default = y):  (ENTER)
(ファイ ルサイズをモニタリングするか)
Configuring: file
Description:
  Check on the size of a file.
    Display a files size statistics.
    If it grows to be too large, report an error about it.

     file /path/to/file [maxsize_in_bytes]

       if maxsize is not specified, assume only size reporting is needed.

    The results are reported in the fileTable section of the UCD-SNMP-MIB tree

Enter the path to the file you wish to monitor: 
/var/log/apache2/access.log
(モニタリン グするファイ ル名)
Enter the maximum size (in bytes) allowable for /var/log/apache/access.log: (ENTER)

Finished Output: file  /var/log/apache2/access.log
Do another file line? (default = y): 
n
(他のファイ ルサイズをモ ニタリングするか)
The following files were created:

  snmpd.conf

These files should be moved to /usr/share/snmp if you
want them used by everyone on the system.  In the future, if you add
the -i option to the command line I'll copy them there automatically for you.

Or, if you want them for your personal use only, copy them to
/home/hoge/.snmp .  In the future, if you add the -p option to the
command line I'll copy them there automatically for you.



これで自分のホームディレクトリにsnmp.confが作成されます。
もしさらにsendmailプロセスもチェックしたい場合は以下をsnmp.confのそれらしきところに
追加します。
proc sendmail
file /var/log/mail.log
そして
このファイルsnmpd.confを/etc/snmp/の下にコピーします。
snmpdは
# /etc/init.d/snmpd restart
で再起動できます。


3 管理エージェントへのアクセス


$ snmpwalk  -v <バージョン>  -c <コミュニティ名>  <ホスト名> [<OID>]
     バージョンは通常2c
例 OID が省略された例
$ snmpwalk -v 2c -c public localhost

NICに関する情報はifxxxという名前がついている。下はOIDにifSpeedを指定した例。

$ snmpwalk -v 2c -c public localhost ifSpeed
IF-MIB::ifSpeed.1 = Gauge32: 10000000
IF-MIB::ifSpeed.2 = Gauge32: 100000000
IF-MIB::ifSpeed.3 = Gauge32: 100000000
IF-MIB::ifSpeed.4 = Gauge32: 0
プロセスに関する情報は
$ snmpwalk -v 2c -c public localhost prTable 
ファイルに関する情報は
$ snmpwalk -v 2c -c public localhost fileTable

OID が決っているならsnmpgetコマンドで

$ snmpget -v 2c -c public localhost ifSpeed.1
IF-MIB::ifSpeed.1 = Gauge32: 1000000

$snmpget -v 2c  -c public localhost prCount.1
UCD-SNMP-MIB::prCount.1 = INTEGER: 0

$snmpget -v 2c -c public localhost fileSize.1
UCD-SNMP-MIB::fileSize.1 = INTEGER: 776 kB

4 mrtgの設定(ネットワークトラフィックのグラフ化)

 mrtgはsnmpdの動作しているネットワーク機器にsnmpリクエストをしてデータを採取し、Webページで閲覧できるようにグ ラフ化し ます。
インストールは多分 mrtg とmrtgutils mrtg-contrib の3つをapt-get install コマンドで入れとけばいいみたいです。
(ほんとはzlibやlibpngやGDというライブラリが必要ですが、大抵、すでにインストールされています。)
設定は cfgmakerコマンドを使って次のようにします。\はバックスラッシュです。
$ cfgmaker \
> --global 'WorkDir: /var/www/mrtg'\
> --global 'Options[_]: bits,growright' \
> --output /home/hoge/mrtg.cfg \
> public@localhost
これで カレントディレクトリにmrtg.cfgが作成されます。
なお --globalの--の前にスペースが無かったりOptionsがOptionになっていたりするとmrtgが動かなかったりmrtg.cfg が作成さ れなかったりします。落ち着いて作成すべし。
 できたらこれを /etc/に置きます。デフォルトのをバックアップしておくといいでしょう。

この/etc/mrtg.cfgを開いて
WorkDir: /var/www/mrtg--global
の行を探して行頭に#をつけてコメントにします。
そしてmrtgを表示するためのdirを指定します。ここではhogeさんのところのpublic_html/mrtgの
下にグラフが作成される例です。
#EnableIPv6: no
#WorkDir: /var/www/mrtg--global
WorkDir: /home/hoge/public_html/mrtg

 始動は
 
# cd /etc
# mrtg mrtg.cfg

です。
あとはhttp://ホストアドレス/mrtg/xxxxx.htmlファイルをブラウザで閲覧します。
Debianの場合はcron命令をしなくてもどんどん更新されています。
動作例

mrtg.cfgの例
 下のはcfgmakerで作成したが緑のフォントの部分は最初#でコメントアウトされていた。#を抜いたとき空白でなく削除で前へ積めないと動 作しな い。
# Created by
# /usr/bin/cfgmaker --global "WorkDir: /var/www/mrtg" --global "Options[_]: bits,growright" --output /home/hoge/mrtg.cfg public@localhost


### Global Config Options

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

#  for Debian
#WorkDir: /var/www/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
#WorkDir: /var/www/mrtg
WorkDir: /home/hoge/public_html/mrtg
Options[_]: bits,growright

######################################################################
# System: dti-vps-srv27
# Description: Linux dti-vps-srv27 2.6.18-194.3.1.el5.028stab069.6 #1 SMP Wed May 26 18:31:05 MSD 2010 i686
# Contact: hoge@hogehoge.com
# Location: "Computer Room 01"
######################################################################


### Interface 1 >> Descr: 'lo' | Name: 'lo' | Ip: '127.0.0.1' | Eth: '' ###
### 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="venet0"
# MaxBytes[localhost_1]: 1250000
# Title[localhost_1]: Traffic Analysis for 1 -- dti-vps-srv27
# PageTop[localhost_1]: <h1>Traffic Analysis for 1 -- dti-vps-srv27</h1>
#         <div id="sysdetails">
#             <table>
#                 <tr>
#                     <td>System:</td>
#                     <td>dti-vps-srv27 in "Computer Room 01"</td>
#                 </tr>
#                 <tr>
#                    <td>Maintainer:</td>
#                     <td>hoge@hoge.com</td>
#                 </tr>
#                 <tr>
#                     <td>Description:</td>
#                     <td>lo  </td>
#                 </tr>
#                 <tr>
#                     <td>ifType:</td>
#                     <td>softwareLoopback (24)</td>
#                 </tr>
#                 <tr>
#                     <td>ifName:</td>
#                     <td>lo</td>
#                 </tr>
#                 <tr>
#                     <td>Max Speed:</td>
#                     <td>10.0 Mbits/s</td>
#                 </tr>
#                 <tr>
#                     <td>Ip:</td>
#                     <td>127.0.0.1 (localhost.localdomain)</td>
#                 </tr>
#             </table>
#         </div>


### Interface 3 >> Descr: 'venet0' | Name: 'venet0' | Ip: '183.181.2.77' | Eth: '' ###
### The following interface is commented out because:
### * has a speed of 0 which makes no sense
#
Target[localhost_3]: 3:public@localhost:
SetEnv[localhost_3]: MRTG_INT_IP="183.181.2.77" MRTG_INT_DESCR="venet0"
MaxBytes[localhost_3]: 1250000
Title[localhost_3]: Traffic Analysis for 3 -- dti-vps-srv27
PageTop[localhost_3]: <h1>Traffic Analysis for 3 -- dti-vps-srv27</h1>
        <div id="sysdetails">
            <table>
                <tr>
                    <td>System:</td>
                    <td>dti-vps-srv27 in "Computer Room 01"</td>
                </tr>
                <tr>
                    <td>Maintainer:</td>
                    <td>hoge@hogehoge.com</td>
                </tr>
                <tr>
                    <td>Description:</td>
                    <td>venet0  </td>
                </tr>
                <tr>
                    <td>ifType:</td>
                    <td>Other (1)</td>
                </tr>
                <tr>
                    <td>ifName:</td>
                    <td>venet0</td>
                </tr>
                <tr>
                    <td>Max Speed:</td>
                    <td>10.0 Mbits/s</td>
                </tr>
                <tr>
                    <td>Ip:</td>
                    <td>183.181.2.77 (v-183-181-2-77.ub-freebit.net)</td>
                </tr>
            </table>
        </div>





5 mrtgの設定(CPUのロードアベレージのグラフ化)

( ここからlenny版では未確認)

まずCPUのロードアベレージが採取できるかを次の命令でためします。
# snmpwalk -v 2c -c public localhost laLoadInt     <----- public はコミュニティ名
UCD-SNMP-MIB::laLoadInt.1 = INTEGER: 68            <----- 1分間の平均ロードアベレージ
UCD-SNMP-MIB::laLoadInt.2 = INTEGER: 53            <----- 5分間の平均ロードアベレージ
UCD-SNMP-MIB::laLoadInt.3 = INTEGER: 20            < -----15分間の平均ロードアベレージ

まずCPUのロードアベレージが採取できるかを次の命令でためします。
# snmpwalk -v 2c -c public localhost laLoadInt     <----- public はコミュニティ名
UCD-SNMP-MIB::laLoadInt.1 = INTEGER: 68            <----- 1分間の平均ロードアベレージ
UCD-SNMP-MIB::laLoadInt.2 = INTEGER: 53            <----- 5分間の平均ロードアベレージ
UCD-SNMP-MIB::laLoadInt.3 = INTEGER: 20            < -----15分間の平均ロードアベレージ

上のようにデータが取得できたら次に/etc/mrtg/mrtg.cfgに以下を書き加えます。

Target[cpu]: laLoadInt.1&laLoadInt.2:public@localhost
MaxBytes[cpu]: 1000                              <------最大取得データ量
Options[cpu]: gauge,nopercent  <---- gaugeを指定すると前回との差分をとらない。nopercentでパーセント表示にしない。
Title[cpu]: Load average       <---- Webページタイトル
PageTop[cpu]: <H1> Load average</H1>
YLegend[cpu]: by 100           <---- グラフのY軸のラベル
ShortLegend[cpu]: &nbsp;   <---- 単位として表示する文字列
LegendI[cpu]:1min      <---- 第1パラメータ(laLoadInt.1)につけるラベル。
LegendO[cpu]:5min      <---- 第2パラメータ (laLoadInt.1)につけるラベル。


  始動は前述と同様、次のようにします。
 
# cd /etc
# mrtg mrtg.cfg

6. /var/log/syslogのエラー(追記 Debian jessie 2016.04.23)

RaspberryPiで /var/log/syslogのエラーにこんなのが頻繁でてくるようになった。

snmpd[753]: error on subcontainer 'ia_addr' insert (-1)

放っておくとsyslogがこのエラーでどんどん埋まってしまう。
対処方法 赤太斜体文字のところが修正箇所
/etc/default/snmpd
# This file controls the activity of snmpd

# Don't load any MIBs by default.
# You might comment this lines once you have the MIBs downloaded.
export MIBS=

# snmpd control (yes means start daemon).
SNMPDRUN=yes

# snmpd options (use syslog, close stdin/out/err).
SNMPDOPTS='-LS6d -Lf /dev/null -u snmp -g snmp -I -smux,mteTrigger,mteTriggerConf -p /run/snmpd.pid'


これでエラーが出なくなった。

目次