その他
    ホーム 技術発信 DoRuby FreeBDにmuninを入れて負荷監視

    FreeBDにmuninを入れて負荷監視

    この記事はアピリッツの技術ブログ「DoRuby」から移行した記事です。情報が古い可能性がありますのでご注意ください。

    よしだです。

    今回はサーバ監視ツール munin を FreeBSD に入れる手順を紹介します。

     やりたいこと

    ここでは nginx や MySQL が実行されているウェブサーバを監視することを目的とします。

    そのために、これとは別に監視用のマシンを用意して、そこに munin を入れていきます。

     想定環境

    • ◇ munin を入れるサーバマシン
    • ◇ 監視対象となるnginx+MySQLサーバ

    (どちらも FreeBSD)

     サーバ側セットアップ

    サーバ側には port から sysutils/munin-master をインストールします。

    cd /usr/ports/sysutils/munin-master
    make install clean
    

    インストールが正常に終了した後、監視対象となるサーバのIPアドレスを設定します。

    # vim /usr/local/etc/munin/munin.conf
    (※下記のような差分が出るように編集)
    # diff /usr/local/etc/munin/munin.conf /usr/local/etc/munin/munin.conf.sample
    < [domain.name]
    <     address x.x.x.x
    <     use_node_name yes
    

     クライアント側セットアップ

    サーバ側には port から sysutils/munin-node をインストールします。

    (※sysutils/munin-common というスケルトンもありますが、どうやらこれは munin-master もしくは munin-node のいずれかを入れるときに自動的にインストールされる共通ライブラリらしいです)

    cd /usr/ports/sysutils/munin-node
    make install clean
    

    監視対象マシンにクライアントマシン自身をエントリします。

    # cp /usr/local/etc/munin/munin-node.conf /usr/local/etc/munin/munin-node.conf.org
    (※念のため初期設定をバックアップ)
    # vim /usr/local/etc/munin/munin-node.conf
    (※下記のような差分が出るように編集)
    # diff /usr/local//etc/munin/munin-node.conf /usr/local//etc/munin/munin-node.conf.org
    51,52c51,52
    < #host *
    < host 127.0.0.1
    ---
    > host *
    > # host 127.0.0.1
    

    次に munin-cron を実行してクライアントの情報を定期的にサーバに送信するようにします。

    # sudo -u munin /usr/local/bin/munin-cron
    

    最後に munin のログローテート用に、syslog の設定に追加の記述を行います。

    ここで munin-node.log の所有者が munin になるようにしておかないと、深夜 0 時とともに munin-node が落ちることになります。

    # vim  /etc/newsyslog.conf
    (※下記のような差分が出るように編集)
    # diff /etc/newsyslog.conf
    - /var/log/munin/munin-node.log                644  7     *    @T00  Z /var/run/munin/munin-node.pid
    + /var/log/munin/munin-node.log   munin:munin             644  7     *    @T00  Z /var/run/munin/munin-node.pid
    

     プラグインの追加

    nginx の監視

    手始めに nginx を監視するためのプラグインをクライアント側に導入します。

    ln -s /usr/local/share/munin/plugins/nginx_status /usr/local/etc/munin/plugins/
    ln -s /usr/local/share/munin/plugins/nginx_request /usr/local/etc/munin/plugins/
    

    作業はこれだけで終わりです。

    あとは munin-node を再起動すればプラグインが有効になります。

    /usr/local/etc/rc.d/munin-node restart
    

    MySQL の監視

    次に MySQL の監視設定を追加します。

    perl /usr/local/share/munin/plugins/mysql_ suggest

    で、出現したコマンドの中から監視したいものを選択します。

    perl /usr/local/share/munin/plugins/mysql_ suggest
    bin_relay_log
    commands
    connections
    files_tables
    innodb_bpool
    innodb_bpool_act
    innodb_insert_buf
    innodb_io
    innodb_io_pend
    innodb_log
    innodb_rows
    innodb_semaphores
    innodb_tnx
    myisam_indexes
    network_traffic
    qcache
    qcache_mem
    replication
    select_types
    slow
    sorts
    table_locks
    tmp_tables
    

    たとえば network_traffic を有効にしたい場合は下記のようにします。

    ln -s /usr/local/share/munin/plugins/mysql_ /usr/local/etc/munin/plugins/mysql_network_traffic
    

    最後に munin-node を再起動してプラグインを有効にします。

    /usr/local/etc/rc.d/munin-node restart
    

     おまけ

    おまけ1: MySQL監視がうまくいかない場合

    Can't locate DBI.pm in @INC 
    

    というメッセージ我出る場合、

    cd /usr/ports/databases/p5-DBI
    make install clean
    

    で DBIライブラリ(DB接続用)をインストールします。

    その後、下記のようにファイルを編集します。

    # vim /usr/local/etc/munin/plugin-conf.d/plugins.conf
    (※下記のような差分が出るように編集)
    # diff /usr/local/etc/munin/plugin-conf.d/plugins.conf /usr/local/etc/munin/plugin-conf.d/plugins.conf.org
    

    [mysql*]

    env.mysqladmin /usr/local/bin/mysqladmin env.mysqlshow /usr/local/bin/mysqlshow +env.mysqlconnection DBI:mysql:mysql +env.mysqluser root

    設定変更後は munin-node を再起動します。

    /usr/local/etc/rc.d/munin-node restart
    

    おまけ2: BASIC 認証を行う

    htpasswd でパスワードファイルを作成後、ウェブサーバに設定を追加します。

    (下記は nginx の場合を例にしています)

    # htpasswd -c /usr/local/etc/munin/munin-htpasswd ユーザ名
    # vim /usr/local/etc/nginx/nginx.conf
    (※下記のような記述を追加)
            location /munin {
                root /usr/local/www;
                access_log /var/log/nginx-munin-access.log;
                error_log /var/log/nginx-munin-error.log;
                auth_basic "realm";
                auth_basic_user_file /var/www/munin/.htpasswd;
            }
    # /usr/local/etc/rc.d/nginx reload
    記事を共有