Nginx + Munin でシステム監視を行うメモ

Linux
この記事は約8分で読めます。
この記事は最終更新日より 1 年以上経過しています。
画像やリンクが無効になっている可能性もあるのでご了承下さい。

 元々は Apache で動かして居た Munin だが、後から yum install munin-nginx をすると Nginx の設定だけは入る。しかし細かい所は手動で直していかないと上手く動かなかったので、グラフ拡大やら出来ず結構嵌まった。
 新規に Munin をインストールする場合と Apache からの移行では若干はまるポイントがあるから、そこを少し書いてみる。

Munin のインストール

 Nginx は既に設定済で Web Server としての動作をしているものすると、Munin のインストールのみ。

yum install munin.noarch munin-nginx munin-node.noarch

 これで必要分のファイルは揃う。

 そして、ここでやっておかないと嵌まる処理がオーナー変更。標準だと apache.munin になっているからマズい。

chown -R munin. /var/log/munin

 特に、apache からの移行だと、/var/log/munin/ 以下にも apache がオーナーのログファイルが幾つかあったりするのだが、munin-cgi-graph.log と munin-cgi-html.log の二つは munin ユーザーから書き込みが出来ないと spawn-fcgi が動かないもんでもの凄い嵌まった。

必要なサービスを有効化と起動

systemctl enable munin-node munin-fcgi-graph munin-fcgi-html
systemctl start munin-node munin-fcgi-graph munin-fcgi-html
munin-fcgi-{graph, html} でエラーが出る場合

 上で書いたパーミッションの件がマズいと munin-fcgi-{graph,html} の二つが起動に転ける。エラーログにも出ないから頭を抱えたが、起動スクリプトの中身の spawn-fcgi コマンドに -n と引数を与えるとフォアグラウンドで動く。よって表示されたエラーでやっと原因が分かった感じで解決出来たという訳。

spawn-fcgi -n -s /var/run/munin/fcgi-graph.sock -U nginx -u munin -g munin  /var/www/cgi-bin/munin-cgi-graph
Status: 500
Content-type: text/html

<h1>Software error:</h1>
<pre>Can't open /var/log/munin/munin-cgi-graph.log (許可がありません) at /usr/share/perl5/vendor_perl/Log/Log4perl/Appender/File.pm li
ne 103.
</pre>
<p>
For help, please send mail to this site's webmaster, giving this error message
and the time and date of the error.

</p>
[Mon Dec 21 21:34:00 2015] munin-cgi-graph: Can't open /var/log/munin/munin-cgi-graph.log (許可がありません) at /usr/share/perl5/vendor_perl/Log/Log4perl/Appender/File.pm line 103.

HTML とグラフの生成

 次のコマンドを叩けば /var/www/html/munin/index.html の他、グラフデータが生成される。5 分おきに cron で実行される物を直接叩いている感じ。

su - munin --shell=/usr/bin/munin-cron

Nginx の設定

 Munin 用にバーチャルホストを作成して運用するタイプなので、次のような設定ファイルになっている。

server {
    # Example configuration! Change this to suit your needs.
    # Access munin at http://localhost/munin/
    # NOTE - Do not remove this file, otherwise munin package upgrade
    # recreates this.

    listen       80;
    server_name  munin.example.com;
    root /var/www/html/munin;
    index index.html;

#    location / {
#        root   /usr/share/nginx/html;
#        index  index.html index.htm;
#    }

    # redirect server error pages to the static page /40x.html
    #
    error_page  404              /404.html;
    location = /40x.html {
        root   /usr/share/nginx/html;
    }

    # redirect server error pages to the static page /50x.html
    #
    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root   /usr/share/nginx/html;
    }

    location ^~ /munin-cgi/munin-cgi-graph/ {
        access_log off;
        fastcgi_split_path_info ^(/munin-cgi/munin-cgi-graph)(.*);
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/var/run/munin/fcgi-graph.sock;
        include fastcgi_params;
    }

    location /munin/static/ {
        alias /etc/munin/static/;
    }

    location /munin/ {
        fastcgi_split_path_info ^(/munin)(.*);
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass unix:/var/run/munin/fcgi-html.sock;
        include fastcgi_params;
    }

}

 「location /」のブロックは不要なのでコメントアウト。root を設定したら終わり。nginx を再起動する。

Munin の Plugins 設定

 これは基本的に自分で監視したい Plugin を /etc/munin/plugins 以下にどんどんシンボリックリンクを作成して行くのみ。

cd /etc/munin/plugins
ln -s /usr/share/munin/plugins/PLUGIN_NAME ./

 Plugin の設定は /etc/munin/plugin-conf.d/munin-node に追記していけば良い。Plugin 毎の設定項目は Plugin ファイルのヘッダに記述されていたりする。
 例えば nginx_status の設定なら「=CONFIGURATION」のセクション以下に説明があったりするので、何か困ったらソースを見ると良い。

=head1 CONFIGURATION

This shows the default configuration of this plugin.  You can override
the status URL.

  [nginx*]
      env.url http://localhost/nginx_status

Nginx must also be configured.  Firstly the stub-status module must be
compiled, and secondly it must be configured like this:

  server {
        listen 127.0.0.1;
        server_name localhost;
        location /nginx_status {
                stub_status on;
                access_log   off;
                allow 127.0.0.1;
                deny all;
        }
  }

 他、存在しない Plugin はどこかから拾ってきても良い。例えば PHP-FPM の Plugin は「torut/php5-fpm-multi-munin-plugin · GitHub」に便利な物がある。丁寧に使い方も書いてある。

おわりに

 今、こうして記事をかいていると、なんでこんな事で嵌まっていたのだろうと思うような内容だが、同様にしてはまる人の参考になればこれ幸いと言う事で。

追記 (2018/09/14)

 Munin 2.0.40 へのアップデートを行うと munin-cgi-{graph,html} までのパスが変わったり systemd の unit ファイルが入れ替わったりとあれこれ苦労したので、次の記事も参考として頂けるとこれ幸い。

CentOS 7.5 にて epel から入れた munin-2.0.40 で色々面倒な事に
 サーバーの動作状況などは Munin を用いた監視としているが、Web サーバーとして Nginx を使用している場合はグラフ拡大などを行う部分の設定が色々と面倒な物で。昨晩あたりに munin-2...
著者プロフィール
ぶっち

本格的に PC へ触れ始めてたのは 1990 年位から。
興味は PC 全般。OS は Windows と Linux などを嗜む。
プログラマやネットワークエンジニアを経てフリーに活動している 2 児の父な 40 代半ばのおじさんです。

ぶっちをフォローする

コメント

  1. 美味しく参考にさせて頂きました。どう考えてもハマりそうだったので、ハマる前にnginxの設定をコピペさせていただきました。ちなみにトライ&エラーなしの1発OKでした。

    nginxって、CGIやPHPの設定面倒ですよね。

    •  フィードバックを頂きましてありがとうございます。1 発動作 OK との事で何よりです。
       Nginx は CGI 周りが特に面倒ですね。最初の一歩さえ乗り越えればあとはどうとでもなるのですが Munin は特にハマった部分でしたorz

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.