LoginSignup
10
11

More than 5 years have passed since last update.

ec2 munin で EC2 インスタンス MariaDBを楽々監視する!

Last updated at Posted at 2015-04-09

AWS EC2でmunin 楽々リソース監視 しよう!

munin.png

aws で監視といえば、もちろんcloud watch !! なんですが、例えば、データベースでいうと、rds で立てた場合は、データベースのリソース監視は一通り揃っていて便利ですよね?でわでわ、今回のようにEC2で Mariadb 10.0 を独自で作った・なんて言った場合は、いろいろ不便だったりします。また、他にも、redisを独自で立てたり、なんとかミドルウェアを別インスタンスで立てたりとかさまざまな要件がが発生した場合、ここは、cloud watch で、ここは、別の監視ツールでなんてやっていたら、リソース監視そのものの一貫性がなくて混乱とかしちゃいそう・・

ってなわけで、比較的に簡単に構築できる、muninを使って、すべてのec2 インスタンスを統合監視できるかを試してみました!

早速インストールを説明しますが、その前にやった環境についてざっくり説明します。

検証した環境

こんな環境で検証してみました

・ VPC構成で、public ネットワークセグメント(10.0.0.0/24)と private ネットワークセグメント(10.0.1.0/24)を持っている
・ munin サーバ はパブリックセグメントに配置しており、EIPでpublic ipを割り当てている(Private:10.0.0.111 Public: 52.5.72.111)
・ munin クライアント側はpublic ipを持っておらず Mariadb 10.0 がインストールされている(Private: 10.0.1.101)

epel のインストール

この作業は、amazon linux では、必要なさそうですが、ec2 じゃなくて、他社VPSを利用する場合だけに記載しております

cd /tmp ; wget http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
# ※amazon linux の場合は、epel 入っているっぽなので、不要です

apacheのインストール

とりあえず、なにも考えないでこれは入れておきましょう

yum insrall httpd

munin のインストール

yum --enablerepo=epel install munin munin-node

apacheの起動

/etc/init.d/httpd start

munin のパスワードを設定する

ブラウザからアクセスした際のログインアカウントを設定します

cd /etc/munin/
htpasswd -c munin-htpasswd 任意のユーザー名
New password: 任意のパスワード
Re-type new password: 任意のパスワード
Adding password for user 任意のユーザー名

ここまできて、ブラウザからアクセスできるか確認します。ちなみにアクセスはできるけど
データの収集はまだ開始していないので、ディレクトリしかパースされてこないはずです。

munin.conf の編集

;を挟んで左が、グループ分けの名前、右が監視対象ホスト(クライント)の識別子を記入します
addressは、muninクライント(監視される側)のipaddressですね (^_^)

vi /etc/munin/munin.conf
---------------
[dbms;db-mst01]                                                                                                                        address 10.0.1.101                                                                                                                      
use_node_name yes

munin クライアントのインストール

munin-node のインストール

クライアント側では、単純には、上記munin.serverはどこかを設定します

yum --enablerepo=epel install munin-node

munin-node.conf の編集

cd /etc/munin

vi munin-node.conf
-----
host_name stat.myserver.com
cidr_allow 127.0.0.1/32
cidr_allow 10.0.0.0/24

munin_clientの起動

ひとまず起動して、プロセスがあるかを確認してます

/etc/init.d/munin-node start
ps -efw | grep munin | grep -v grep
root     26959     1  0 07:44 ?        00:00:00 /usr/bin/perl -wT /usr/sbin/munin-node

確認したら一度終了しておきます。

/etc/init.d/munin-node stop

munin MySQLモジュールのインストール

モジュールのインストール

munin MySQL モジュールとは書きましたが、単純には、perlのMySQL ドライバ周りをインストールするだけです
私の場合はこんなのを入れました。

yum install --enablerepo=epel perl-IPC-ShareLite perl-Cache-Cache perl-Cache perl-DBD-MySQL

MySQL(mariaDBの接続設定)

今回は、rootをuseridとして利用しましたが、あまりdbでrootアカウントを利用するのはよくないと思いますので
監視専用アカウントを作成してそれを設定したほうがいと思い・・・ます。

vi /etc/munin/plugin-conf.d/munin-node
------------------------------------------
[mysql*]
env.mysqlopts -u root -pパスワード
env.mysqladmin /usr/bin/mysqladmin
env.mysqlconnection DBI:mysql:mysql;host=127.0.0.1;port=3306
env.mysqluser root
env.mysqlpassword パスワード
------------------------------------------

mysqlのpluginを設定

muninで監視項目を追加する方法は単純に/usr/share/munin/plugins/から、/etc/munin/pluginsにシンボリックリンクを貼るだけです今回はmysql(mariadb)

cd /etc/munin/plugins
ln -s /usr/share/munin/plugins/mysql_ mysql_commands
ln -s /usr/share/munin/plugins/mysql_ mysql_innodb_bpool
ln -s /usr/share/munin/plugins/mysql_ mysql_innodb_io
ln -s /usr/share/munin/plugins/mysql_ mysql_innodb_log
ln -s /usr/share/munin/plugins/mysql_ mysql_innodb_tnx
ln -s /usr/share/munin/plugins/mysql_ mysql_select_types
ln -s /usr/share/munin/plugins/mysql_ mysql_table_locks
ln -s /usr/share/munin/plugins/mysql_ mysql_connections
ln -s /usr/share/munin/plugins/mysql_ mysql_slow
ln -s /usr/share/munin/plugins/mysql_ mysql_myisam_indexes
ln -s /usr/share/munin/plugins/mysql_ mysql_network_traffic
ln -s /usr/share/munin/plugins/mysql_ mysql_qcache
ln -s /usr/share/munin/plugins/mysql_ mysql_qcache_mem
/etc/init.d/munin-node start
/sbin/chkconfig/munin-node on

mariadbに正常に接続できるか確認する

こんなエラーになったのではないでしょうか?あれれれ・・

/usr/sbin/munin-run mysql_commands
Unknown section: INDIVIDUAL BUFFER POOL INFO at /etc/munin/plugins/mysql_commands line 1098.

どうも、mariaで認識されないコマンドがある模様で /usr/share/munin/plugins/mysql_ のエラー対象行付近にある、下記項目に

'INDIVIDUAL BUFFER POOL INFO'   => \&skip,

という行を下記あたりに追記してくれれば解決します

vi /usr/share/munin/plugins/mysql_
---------------------------------
    my %section_map = (

        'BUFFER POOL AND MEMORY'   => \&parse_buffer_pool_and_memory,
        'FILE I/O'                 => \&parse_file_io,
        'INSERT BUFFER AND ADAPTIVE HASH INDEX'
            => \&parse_insert_buffer_and_adaptive_hash_index,
        'LATEST DETECTED DEADLOCK' => \&skip,
        'LATEST FOREIGN KEY ERROR' => \&skip,
        'LOG'                      => \&parse_log,
        'ROW OPERATIONS'           => \&skip,
        'SEMAPHORES'               => \&parse_semaphores,
        'TRANSACTIONS'             => \&parse_transactions,
        'BACKGROUND THREAD'        => \&skip,
        'INDIVIDUAL BUFFER POOL INFO'   => \&skip,    # → これを追記                                                                    
    );
---------------------------------

再度チャレンジ

どうでしょう?きっと今回はうまくいったかと思います

/usr/sbin/munin-run mysql_commands
Com_delete.value 1371
Com_insert.value 3896
Com_insert_select.value 0
Com_load.value 0
Com_replace.value 0
Com_replace_select.value 0
Com_select.value 1419070
Com_update.value 6555
Com_update_multi.value 0

監視項目で不要なものを削除したい場合

ぶっちゃけ見ないものをがんがんグラフ表示するのものしんどいので、いらないグラフは消したいと思います。

cd /etc/munin/plugins/
ll
合計 0
lrwxrwxrwx 1 root root 28  4月  8 07:02 cpu -> /usr/share/munin/plugins/cpu
lrwxrwxrwx 1 root root 27  4月  8 07:02 df -> /usr/share/munin/plugins/df
lrwxrwxrwx 1 root root 33  4月  8 07:02 df_inode -> /usr/share/munin/plugins/df_inode
lrwxrwxrwx 1 root root 34  4月  8 07:02 diskstats -> /usr/share/munin/plugins/diskstats
lrwxrwxrwx 1 root root 32  4月  8 07:02 entropy -> /usr/share/munin/plugins/entropy
lrwxrwxrwx 1 root root 30  4月  8 07:02 forks -> /usr/share/munin/plugins/forks
lrwxrwxrwx 1 root root 35  4月  8 07:02 fw_packets -> /usr/share/munin/plugins/fw_packets
lrwxrwxrwx 1 root root 32  4月  8 07:02 if_err_eth0 -> /usr/share/munin/plugins/if_err_
lrwxrwxrwx 1 root root 28  4月  8 07:02 if_eth0 -> /usr/share/munin/plugins/if_
lrwxrwxrwx 1 root root 35  4月  8 07:02 interrupts -> /usr/share/munin/plugins/interrupts
lrwxrwxrwx 1 root root 33  4月  8 07:02 irqstats -> /usr/share/munin/plugins/irqstats
lrwxrwxrwx 1 root root 29  4月  8 07:02 load -> /usr/share/munin/plugins/load
lrwxrwxrwx 1 root root 31  4月  8 07:02 memory -> /usr/share/munin/plugins/memory
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_commands -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_connections -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_innodb_bpool -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_innodb_io -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_innodb_log -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_innodb_tnx -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_myisam_indexes -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_network_traffic -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_qcache -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_qcache_mem -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_select_types -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_slow -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 31  4月  8 09:02 mysql_table_locks -> /usr/share/munin/plugins/mysql_
lrwxrwxrwx 1 root root 32  4月  8 07:02 netstat -> /usr/share/munin/plugins/netstat
lrwxrwxrwx 1 root root 36  4月  8 07:02 nfs4_client -> /usr/share/munin/plugins/nfs4_client
lrwxrwxrwx 1 root root 35  4月  8 07:02 nfs_client -> /usr/share/munin/plugins/nfs_client
lrwxrwxrwx 1 root root 39  4月  8 07:02 ntp_kernel_err -> /usr/share/munin/plugins/ntp_kernel_err
lrwxrwxrwx 1 root root 44  4月  8 07:02 ntp_kernel_pll_freq -> /usr/share/munin/plugins/ntp_kernel_pll_freq
lrwxrwxrwx 1 root root 43  4月  8 07:02 ntp_kernel_pll_off -> /usr/share/munin/plugins/ntp_kernel_pll_off
lrwxrwxrwx 1 root root 35  4月  8 07:02 ntp_offset -> /usr/share/munin/plugins/ntp_offset
lrwxrwxrwx 1 root root 35  4月  8 07:02 ntp_states -> /usr/share/munin/plugins/ntp_states
lrwxrwxrwx 1 root root 35  4月  8 07:02 open_files -> /usr/share/munin/plugins/open_files
lrwxrwxrwx 1 root root 36  4月  8 07:02 open_inodes -> /usr/share/munin/plugins/open_inodes
lrwxrwxrwx 1 root root 33  4月  8 07:02 proc_pri -> /usr/share/munin/plugins/proc_pri
lrwxrwxrwx 1 root root 34  4月  8 07:02 processes -> /usr/share/munin/plugins/processes
lrwxrwxrwx 1 root root 43  4月  8 07:02 sendmail_mailqueue -> /usr/share/munin/plugins/sendmail_mailqueue
lrwxrwxrwx 1 root root 43  4月  8 07:02 sendmail_mailstats -> /usr/share/munin/plugins/sendmail_mailstats
lrwxrwxrwx 1 root root 45  4月  8 07:02 sendmail_mailtraffic -> /usr/share/munin/plugins/sendmail_mailtraffic
lrwxrwxrwx 1 root root 29  4月  8 07:02 swap -> /usr/share/munin/plugins/swap
lrwxrwxrwx 1 root root 32  4月  8 07:02 threads -> /usr/share/munin/plugins/threads
lrwxrwxrwx 1 root root 31  4月  8 07:02 uptime -> /usr/share/munin/plugins/uptime
lrwxrwxrwx 1 root root 30  4月  8 07:02 users -> /usr/share/munin/plugins/users
lrwxrwxrwx 1 root root 31  4月  8 07:02 vmstat -> /usr/share/munin/plugins/vmstat
  • 私の場合、下記を消しました。消す・・といってもシンボリックリンクを外しただけっす
unlink sendmail_mailqueue 
unlink sendmail_mailstats
unlink sendmail_mailtraffic
unlink users
unlink threads
unlink proc_pri
unlink entropy
unlink irqstats
unlink forks
unlink interrupts
unlink ntp_offset
unlink ntp_kernel_pll_off
unlink ntp_kernel_pll_freq
unlink ntp_kernel_err
unlink if_err_eth0
unlink if_err_eth1
unlink ntp_states
unlink fw_conntrack
unlink fw_forwarded_local
unlink fw_packets
unlink postfix_mailqueue
unlink postfix_mailvolume
unlink proc_pri

起動してみましょう

一通りの設定が済んだところで 起動してみます。

/etc/init.d/munin-node start
/sbin/chkconfig munin-node on

WEBを確認してみよう

muninサーバのIPアドレスを叩いて、ブラウザで対象クライアントのリソース状況が表示されているか確認してみましょう!

こんな感じになっていれば、オッケーです!!

2015-04-09_103950.jpg

上記のように項目が出てこない場合

muninは、munin server側から監視クライアントの値をcronを利用して読み取るしくみになっております。なので、設定したはずなのに表示されてこない!なんて場合は、munin server側にあるlogを確認することで、ある程度の原因をさぐることができます。

グラフや設定項目の表示されない理由は、

・設定不備
・ネットワークの設定(IPTABLES等)

の2パターンが多いです。

ことネットワークがらみの場合は、munin serverから、クライアントにport 4949で疎通がとれるのか?下記コマンドで確認してみましょう。当方は、VPC構成で、public セグメント(munin server側)、private セグメント(監視クライント側)の2つのネットワークアドレス(CIDRブロック)を持っていたため、結構ハマりました。。

  • muninクライアントからポート4949にlocalホストで疎通がとれるか確認する。
nmap localhost -p 4949
  • 次にmuninサーバーから確認する
nmap [クライアントのIP] -p 4949

ちなみに、amazon ec2 (CPC構成)上記でエラーになる場合は、クライアントのiptableで4949のポートが通さないような設定になっている可能性が高い為、これを設定する必要があります。EC2の場合は、SG(セキュリティグループ)の設定を確認してください。私の場合はこんな感じにしました。

sg.jpg

それでは、良いmunin ライフを!

10
11
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
10
11