Muninでサーバ監視システムを構築する
2016/08/02
管理しているサーバが増えてきて、気づいたらサービスが落ちていたという事が発生するようになったので、最近流行っているMuninを利用してサーバ監視システムを構築したいと思います。
前提条件
- WEBサーバが構築済みである事
- EPELのリポジトリを追加してある事
- ファイアウォール設定済みの時は監視サーバ・監視対象のサーバ共にtcp 4949 ポートが開放されている事(クラウドサーバなどでファイアウォールを設定している時は、iptables以外にそちらでも開放する)
Muninパッケージのインストール
EPELリポジトリを利用して、Muninパッケージをインストールします。
[root@test]# yum -y install munin --enablerepo=epel←Muninインストール
インストールは完了です。
muninとmunin-nodeがインストールされますが、muninは監視統計データを集約して表示する役割、munin-nodeはリソース情報を集めて送信する役割になります。
監視サーバ自身を監視しない場合はmuninのみのインストールで大丈夫で、監視されるサーバはmunin-nodeだけで大丈夫です。
管理ページへのベーシック認証設定
Apacheの場合
Muninをインストールすると、/etc/httpd/conf.c/munin.confファイルが作成されていて、
管理ページへアクセスする際は、ベーシック認証が必要となるように設定されています。
※Nginxでも作成されるが、有効ではない
Nginxの場合
htpasswdファイルを利用してベーシック認証が可能になるように、Nginxの設定ファイルを変更します。
今回は、バーチャルサーバtest.confの設定ファイルを編集します。
[root@test]# vi /etc/nginx/conf.d/test.conf←test.confファイルの編集 以下を追記 location / { auth_basic "Restricted"; auth_basic_user_file /etc/nginx/.htpasswd;←.htpasswdのパスを指定 }
Apache・Nginx共通
htpasswdコマンドで、ユーザとパスワードを作成します。
[root@test]# htpasswd -c -b /etc/munin/munin-htpasswd user password←今回はユーザ:user パスワード:passwordで作成しました。
オプション -c : 新しいパスワードファイルを作成する。既存ファイルがあった場合は上書きされる。
オプション -b : パスワードを対話式ではなくコマンドライン引数として入力できるようにする。
※すでにhtpasswdファイルがある場合で、追記したい場合はオプション -c を外してください。
Apacheの構文チェックを行なって再起動します
[root@test]# httpd -t←構文チェック Syntax OK [root@test]# service httpd restart←Apache再起動
munin-nodeの起動と自動起動設定
Muninをインストールすると、munin-nodeという監視エージェントもインストールされますので、エージェントの起動と自動起動設定を行います。
[root@test]# service munin-node start←munin-node起動 Starting Munin Node: [ ←OK ] [root@test]# chkconfig munin-node on←自動起動設定
動作確認
Muninを起動すると、メモリやディスクなどの監視データの収集が開始され、収集されたデータから管理ページが5分置きに作成されます。
初回動作確認の際は5分以上待ってから管理ページへアクセスします。
5分待っても作成されない場合はcronが起動していない可能性があるので確認してください。
[root@test]# /etc/rc.d/init.d/crond status←crondの起動確認 crond は停止しています←起動していない場合 [root@test]# service crond start←crond起動 crondを起動中: [ ←OK ] [root@test]# chkconfig crond on←crond自動起動設定 [root@test]#chkconfig --list crond crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
待ちきれない場合は、cronを手動で実行します。
[root@test]# sudo -u munin munin-cron←muninユーザのmunin-cron実行
/var/www/html/munin配下を確認してみて、htmlファイルが作成されていたらアクセス可能になります。
URLはhttp://ドメイン名/munin/になるので、アクセスすると、下記のような画面が表示されます。
監視項目を追加する(Apache)
デフォルトの監視項目(Sensor)は[disk][network][postfix][processes][system]の5項目になっています。
デフォルト以外のSensorにはプラグインの設定が必要となります。
Apacheプラグインの設定
SensorにApacheのプラグインを設定します。
予め用意されているプラグインのシンボリックリンクを作成します。
[root@test]# ln -s /usr/share/munin/plugins/apache_accesses /etc/munin/plugins/apache_accesses [root@test]# ln -s /usr/share/munin/plugins/apache_processes /etc/munin/plugins/apache_processes [root@test]# ln -s /usr/share/munin/plugins/apache_volume /etc/munin/plugins/apache_volume
Apache設定ファイルの修正
下記のようにhttpd.confを設定して、Apacheの再起動を行います。
[root@test]# vi /etc/httpd/conf/httpd.conf←httpd.conf設定変更 LoadModule status_module modules/mod_status.so←モジュールがロードされている事を確認 ・ ・ ・ # # ExtendedStatus controls whether Apache will generate "full" status # information (ExtendedStatus On) or just basic information (ExtendedStatus # Off) when the "server-status" handler is called. The default is Off. # ExtendedStatus On←コメントアウトを外す [root@test]# service httpd restart←Apache再起動
Apacheプラグインの動作確認
Apache監視プラグインの動作を確認します。
[root@test]# munin-run apache_accesses←動作確認 accesses80.value U←失敗してる(本来なら数値が返る) [root@test]# munin-run apache_processes←動作確認 busy80.value U idle80.value U free80.value U [root@test]# munin-run apache_volume←動作確認 volume80.value U
この状態だとapacheのグラフが真っ白のままになってしまいます。
apacheのグラフを表示できるようにする。
/etc/munin/plugin-conf.d/munin-nodeとapacheの設定ファイルを修正します。
[root@test]# vi /etc/munin/plugin-conf.d/munin-node←設定ファイル編集 [apache_*]←apacheの項目を追加 env.url http://127.0.0.1:%d/server-status?auto env.ports 80 [root@test]# vi /etc/httpd/conf.d/hogehoge.com.conf←ヴァーチャルホストで設定しているので、ヴァーチャルホストの設定ファイル編集 ←VirtualHostの設定を追加 <VirtualHost *:80> ServerName 127.0.0.1 <Location /server-status> SetHandler server-status Order deny,allow Deny from all allow from 127.0.0.1 </Location> </VirtualHost>
監視項目を追加する(Nginx+PHP-FPM)
PHP-FPMのプラグインインストール
PHP-FPMのプラグインはデフォルトでは無いのでインストールします。
[root@test]# cd /usr/share/munin/plugins←ディレクトリ移動 [root@test plugins]# git clone git://github.com/tjstein/php5-fpm-munin-plugins.git←gitでプラグイン取得 [root@test plugins]# chmod +x php5-fpm-munin-plugins/phpfpm_*←ディレクトリのパーミッション設定変更 [root@test plugins]# cd /etc/munin/plugins/←ディレクトリ移動 [root@test plugins]# ln -s /usr/share/munin/plugins/php5-fpm-munin-plugins/phpfpm_average phpfpm_average [root@test plugins]# ln -s /usr/share/munin/plugins/php5-fpm-munin-plugins/phpfpm_connections phpfpm_connections [root@test plugins]# ln -s /usr/share/munin/plugins/php5-fpm-munin-plugins/phpfpm_memory phpfpm_memory [root@test plugins]# ln -s /usr/share/munin/plugins/php5-fpm-munin-plugins/phpfpm_processes phpfpm_processes [root@test plugins]# ln -s /usr/share/munin/plugins/php5-fpm-munin-plugins/phpfpm_status phpfpm_status
PHP-FPMのステータス変更
[root@test]# vi /etc/php-fpm.d/www.conf←php-fpmの設定変更 [root@test]# systemctl restart php-fpm←php-fpmの再起動
Nginxプラグインの設定
SensorにNginxのプラグインを設定します。
予め用意されているプラグインのシンボリックリンクを作成します。
[root@test]# ln -s /usr/share/munin/plugins/nginx_status /etc/munin/plugins/nginx_status [root@test]# ln -s /usr/share/munin/plugins/nginx_request /etc/munin/plugins/nginx_request
nginxの設定
nginxの設定ファイルにmunin-nodeがnginxとPHP-FPMのリソース情報を取得できるようにバーチャルホストを設定します。
ローカルホストからしかアクセスしないので、allow 127.0.0.1とします。
[root@test]# vi /etc/nginx/conf.d/default.conf←設定ファイル編集 下記を追記 location /nginx_status { stub_status on; access_log off; allow 127.0.0.1; deny all; } location /phpfpm_status { include fastcgi_params; fastcgi_pass localhost:9000; fastcgi_param SCRIPT_FILENAME $fastcgi_script_name; allow 127.0.0.1; deny all; }
munin-nodeのconf設定
munin-nodeの監視対象にnginxとPHP-FPMを追加します。
[root@test]# vi /etc/munin/plugin-conf.d/munin-node←設定ファイル編集 [nginx*]←末尾に追記 env.url http://localhost/nginx_status [phpfpm*] env.url http://localhost/phpfpm_status env.phpbin php-fpm
正常にプラグインが稼働するかどうかはmunin-runで確認できます。
問題がある場合は、UnknownServiceなどと表示されます。
[root@test]# /usr/sbin/munin-run phpfpm_average←munin-runで確認
php_average.value 24277674
munin-nodeとApache(Nginx)の再起動
munin-nodeとApache(Nginx)を再起動します。
[root@test]# service munin-node restart←munin-nodeの再起動 [root@test]# service httpd restart←Apacheの再起動
動作確認
Muninのトップページにapacheの項目が追加されているのを確認します。
Muninクライアントの構築
ここまでで、監視サーバの構築を行いましたが監視を行いたいサーバに環境を構築します。
munin-nodeのインストール
監視対象サーバに、監視エージェントをインストールします。
[root@test]# yum -y install munin-node --enablerepo=epel←munin-nodeインストール
munin-node設定ファイルの編集
[root@test]# vi /etc/munin/munin-node.conf←munin-node設定ファイル編集 # may repeat the allow line as many times as you'd like allow ^127\.0\.0\.1$ allow ^::1$ allow ^111\.222\.333\.444$←監視サーバのIPアドレスが111.222.333.444の場合、このように追記
munin-nodeの起動と自動起動設定
エージェントの起動と自動起動設定を行います。
[root@test]# service munin-node start←munin-nodeを起動します。 [root@test]# chkconfig munin-node on←自動起動設定を行います。
この他に、[apacheのグラフを表示できるようにする。]の設定も行って下さい。
これで、監視対象のクライアント側の設定は完了です。
監視サーバへの監視対象クライアントの追加
クライアントの設定が完了したので、MuninサーバにMuninクライアントを追加します。
これは監視サーバの設定になります。
munin-node設定ファイルの編集
[root@test]# vi /etc/munin/munin.conf←munin設定ファイルの編集 [localhost] address 127.0.0.1 use_node_name yes [client1]←管理画面に表示させる名称 address 111.222.333.444←クライアントのIPアドレス use_node_name yes
以上のように、監視したいサーバの分だけ記載します。
動作確認
正常に動作していると、監視サーバのトップページにクライアントの名称が表示されます。
追加が成功していると、/var/www/html/muninディレクトリの配下にmunin.confで指定した名称(今回はclient1)のディレクトリが作成され、HTMLファイルが作成されます。
以上でMuninサーバの構築は完了です。