munin 2.0を試す(グラフ描画など情報集約側だけ)

俺の Munin がこんなに軽いわけがない #監視ツール勉強会によればmunin 2.0 がいけてるらしいので試してみる。
munin-nodeは1.4系でかまわないみたいなので助かる*1
まあ、そんな簡単にAPIが変わるわけがないというか変わったらいけないというか。

OSはUbuntu 12.04です。

ダウンロード

まずはソースコードを取得する。

# wgetで取得
$ wget https://downloads.sourceforge.net/project/munin/stable/2.0.2/munin-2.0.2.tar.gz
# 展開
$ tar xzf munin-2.0.2.tar.gz
# 移動
$ cd munin-2.0.2
# インストール説明を読む
$ less INSTALL
INSTALLを読む。

今回はmunin-nodeをインストールしないので"Server"の方だけ読めばいい。"Node" (munin-node)は既存の1.6, 1.7のを使うので今回は対象外。
注意:下記の方法だと同じサーバーでmunin-nodeを動かしている場合munin-nodeも2.0系にバージョンアップされる可能性があります。

Munin installation requirements
===============================

2.0 now requires IPv6 *libraries*, but IPv6 doesn't need to be configured.
If you plan to really *use* IPv6, please read the note at the end.

On all hosts:

  • A reasonable Perl 5 (ie: at least 5.8)
  • GNU Make - do not attempt to use any other make (build time only)
  • Module::Build - this is part of perl 5.10, for earlier Perls it must be installed (build time only)
  • The perl module Time::HiRes
  • For TLS(SSL) to work you'll need Net::SSLeay everywhere

Server:

  • A web server. Any web server supporting simple file service and CGI or Fast CGI should work. Apache HTTPD should be suitable. Also reported to be working is nginx and lighttpd, please contribute your

configs.

  • RRD with Perl support - this means that RRDs.pm must be available and "perl -MRRDs -e ':;'" must run without errors.
  • Perl modules for server:

...(以下必要なCPANモジュール)

Node:
...(以下munin-nodeの要件)

Installing
==========
NOTE!!! (NFSを使っている場合はmake installで失敗するから、/var/tmpや/tmpなどローカルディスクにインストールするようにとのこと)
...

To install both master and node:
- Makefile.configをよく読んで必要に応じて書き換えること
-"munin"というユーザーとグループを作っておくこと。ログインできなくてよいがcronが実行できること。
-いきなりmake installせずに、まずmakeすること
-make install
-CGIが必須。普通はFastCGI.
-/opt/munin/www/docs/.htaccessの説明。Basic認証のファイル(/etc/opt/munin/munin-htpasswd)が必要。exporesモジュールが必要。
-/etc/opt/munin/munin.confを編集してノードを指定すること。
-cronの設定。build/resourcesまたはbuild/dists以下にサンプルがある。

To install a node:
...

Notes about node plugins
... (基本的なことだけどわかりやすくまとまっている)

Using CPAN shell
... (CPANモジュールのインストール方法など)

IPv6
... (IPv6対応のこと)

インストール

"RRD with Perl support - this means that RRDs.pm must be available"とのことだが、RRDs.pmのインストール方法がわからなくて困った。

$ sudo cpan
cpan> install RRDs #としてもだめ。

どうもCPANモジュールではなくてrrdtoolの方に入っているっぽい。
UbuntuなどDebian系なら

$ sudo apt-get install rrdtool librrds-perl

とすればよい。


"Server"に必要なCPANモジュールは以下の通り

  • Time::HiRes
  • Storable
  • Digest::MD5,
  • HTML::Template
  • Text::Balanced
  • Params::Validate
  • TimeDate
  • Net::SSLeay (SSL/TLSを使う場合)
  • Getopt::Long
  • File::Copy::Recursive
  • CGI::Fast
  • IO::Socket::INET6
  • Log::Log4perl 1.18 or later (以下のモジュールに依存)
    • IPC::Shareable
    • Log::Dispatch
    • Log::Dispatch::FileRotate
    • MIME::Lite
    • Mail::Sender
    • Mail::Sendmail
    • MailTools

TimeDateは正確にはTemplate::Plugin::TimeDate。
MailToolsはモジュール名ではないらしく
ここでも

cpan> install MailTools

では入らなかった*2。が、warningやcriticalのメールが飛ばなくていいならとりあえず不要だと思う。

CPANモジュールは次のコマンドで全部入ります(Net::SSLeayとMailToolsは除外)。

$ sudo cpan #rootでインストールしないとだめ
cpan> install Log::Log4perl::Appender::RRDs \
 Digest::MD5\
 HTML::Template\
 Text::Balanced\
 Params::Validate\
 Template::Plugin::TimeDate\
 Getopt::Long\
 File::Copy::Recursive\
 CGI::Fast\
 IO::Socket::INET6\
 Log::Log4perl\
 IPC::Shareable\
 Log::Dispatch\
 Log::Dispatch::FileRotate\
 MIME::Lite\
 Mail::Sender\
 Mail::Sendmail

あとURIも必要だった。

cpan> install URI

Perlモジュールがインストールできたらmakeの準備

#この際なのでjavaも入れておく
$ sudo apt-get install openjdk-7-jdk

#ユーザー追加
$ sudo groupadd -g 4949 munin
$ sudo useradd -u 4949 -g munin -s /bin/nologin -m munin

いざmake

#ビルド
$ make

#エラーがなければインストール
$ sudo make install
ディレクトリ・ファイル権限

CGIでHTMLやグラフ画像を作成するのでファイルの所有者、権限を変更しておく。もし既存のmuninがあって上書きインストールした場合は -R が必要となるかも。

$ sudo chgrp www-data /opt/munin/log/munin
$ sudo chmod g+rwx /opt/munin/log/munin
$ sudo chgrp www-data /var/opt/munin/cgi-tmp
$ sudo chmod g+rwx /var/opt/munin/cgi-tmp
munin.confの編集

CGIでグラフ描画などを行なうようにmunin.confを編集します。

$ sudo vim /etc/opt/munin/munin.conf

/etc/opt/munin/munin.confの以下の二つのコメントアウトを外す。これでcronでグラフやHTMLが生成されなくなる。

graph_strategy cgi
html_strategy cgi
cronの設定

設定ファイルが用意されているのでそれをcron.dにコピーする。

$ sudo cp build/resources/linux-cron.d_munin /etc/cron.d/munin
Apacheの設定

FastCGIはapt-getで入れられるのがmod_fcgidだったのでmod_fastcgiではなくmod_fcgidの方を使った。

#FastCGIモジュールを追加
$ sudo apt-get install libapache2-mod-fcgid

#VirtualHost設定
$ sudo vim /etc/apache2/site-available/munin-2.0 #設定内容は後述
$ sudo a2ensite munin-2.0

#rewriteモジュールとexpiresモジュールも必要
$ sudo a2enmod expires
$ sudo a2enmod rewrite

#a2ensiteやa2enmodはUbuntuのapacheで使えるサイト有効化、モジュール有効化コマンドです。
#やっていることはシンボリックリンクを張っているだけ。

#再起動
$ sudo service apache2 restart
ApacheのVirtualHost設定

/etc/apache2/site-available/munin-2.0の内容は以下の通り。CgiHowto2 – Muninを参考にして、パスをインストールパスに合わせて変更するなどした。
Aliasを使う例が多いけど、今回は専用のドメインを当てることにした。

<VirtualHost *:80>
    ServerName munin.local
    ServerAlias munin

    DocumentRoot /opt/munin/www/docs

    ErrorLog     /var/log/apache2/munin-error.log
    CustomLog    /var/log/apache2/munin-access.log combined

    ServerSignature Off

    # /opt/munin/www/docs/staticがあるので不要
    #Alias /static /etc/opt/munin/static

    # Rewrites
    RewriteEngine On

    # HTML
    RewriteCond %{REQUEST_URI} !^/static
    RewriteCond %{REQUEST_URI} .html$ [or]
    RewriteCond %{REQUEST_URI} =/
    RewriteRule ^/(.*)         /opt/munin/www/cgi/munin-cgi-html/$1 [L]

    # Images

    # - remove path to munin-cgi-graph, if present
    RewriteRule ^/cgi-bin/munin-cgi-graph/(.*) /$1

    RewriteCond %{REQUEST_URI} !^/static
    RewriteCond %{REQUEST_URI} .png$
    RewriteRule ^/(.*)         /opt/munin/www/cgi/munin-cgi-graph/$1 [L]

    # Ensure we can run (fast)cgi scripts
    <Directory "/opt/munin/www/cgi">
        Options +ExecCGI
        <IfModule mod_fcgid.c>
            SetHandler fcgid-script
        </IfModule>
        <IfModule !mod_fcgid.c>
            SetHandler cgi-script
        </IfModule>
    </Directory>

    # /opt/munin/www/docs/.htaccessを無効化
    <Directory "/opt/munin/www/docs">
        AllowOverride None
    </Directory>

    # /opt/munin/www/docs/.htaccessから移動
    ExpiresActive On
    ExpiresDefault M310

</VirtualHost>

拡大縮小もできて楽しいです。でも拡大はあまりいらないかなあ。

*1:Munin / Thread: [munin-users-jp] munin 2.0 RC6版

*2:MailToolsのインストールはinstall Mail::Internetとすればいいようだ。