Application Insights が発表されてからかなりの時間が経過したが、実は Application Insights に collectd と連携してログ情報を取り扱う機能が追加されているのをご存じだろうか?
Application Insights のライブラリはアプリケーション( war, ear の中身等)に仕込んで設定するのが原則のソリューションだ。
そんな Application Insights だが、Windows Server の場合は Application Insights Status Monitor をインストールすれば特に追加の対応が不要な一方、Linux 側には同等のコンポーネントが存在しなかった。
今回紹介する collectd と Application Insights を連携することで、特にアプリケーション自体を変更をすることなく OS に対して追加の設定のみで CPU 使用率、メモリ使用量、I/O 使用量を取得することが可能になる。
今回構築する環境
Application Insights と collectd を連携するためには以下が必要となる。
collectd 自体は collectdをインストールしてサーバの監視をしよう! を見て頂くとして、Application Insights と collectd を連携する手順を次から記載する。
環境構築に必要な手順
手順の概要は collectd: Application Insights での Unix パフォーマンス メトリック に記載があるが、こちらでもフォローアップする。
まずは CentOS に SSH でログインし、以下のコマンドを叩き込む。collectd をインストールする場合、EPEL パッケージを入れる必要があるので、注意が必要だ。
yum install epel-release
yum update
yum install java-1.8.0-openjdk.x86_64 java-1.8.0-openjdk-devel.x86_64
yum install httpd httpd-devel
yum install collectd collectd-rrdtool collectd-web collectd-java
次に以下の設定のために /etc/collectd/collectd.conf を編集する。
- collectd の java, cpu, load, disk プラグインを有効化する
- collectd の Application Insights Java Plugin を設定する
以下の様な記載になる認識だ。
...
# collectd plugins
LoadPlugin cpu
LoadPlugin disk
LoadPlugin load
...
# Enable Java Plugin
LoadPlugin "java"
# Configure Java Plugin
<Plugin "java">
JVMArg "-verbose:jni"
JVMArg "-Djava.class.path=/usr/share/collectd/java/applicationinsights-collectd-1.0.3.jar:/usr/share/collectd/java/collectd-api.jar"
# Enabling Application Insights plugin
LoadPlugin "com.microsoft.applicationinsights.collectd.ApplicationInsightsWriter"
# Configuring Application Insights plugin
<Plugin ApplicationInsightsWriter>
InstrumentationKey "<自身の instrument key>"
</Plugin>
</Plugin>
上記で記載した Application Insights collectd ライター プラグイン を取得する必要があるので、リンクより取得する。また、applicationinsights-collectd-x.x.x.jar はバージョンアップがこまめにされているので、リンクが切れていたら「バージョンが上がったのかな?」と推察してリンクからバージョンを修正して jar を取得する心意気が必要になる。
また、上記で記載した通り ApplicatinInsights.xml という構成ファイルが必要になる点にも注意が必要だ。
mv /home/azureuser/applicationinsights-collectd-1.0.3.jar /usr/share/collectd/java/
mv /home/azureuser/ApplicationInsights.xml /usr/share/collectd/java/ApplicationInsights.xml
ApplicatinInsights.xml を配置しない場合、以下の様なエラーが発生して ApplicationInsights の情報が Azure ポータル側に反映されない。
[root@AppInsightsVM ~]# tail -n 30 /var/log/messages
......
Mar 5 04:36:27 AppInsightsVM collectd: [Dynamic-linking native method java.io.FileOutputStream.writeBytes ... JNI]
<b><span style="color: #ff0000">Mar 5 04:36:27 AppInsightsVM collectd: AI: INFO 05-03-2016 04:36, 1: Configuration file 'ApplicationInsights.xml' was NOT found by default class loader
Mar 5 04:36:27 AppInsightsVM collectd: AI: INFO 05-03-2016 04:36, 1: Did not find configuration file 'ApplicationInsights.xml' in '/usr/share/collectd/java'
Mar 5 04:36:27 AppInsightsVM collectd: AI: INFO 05-03-2016 04:36, 1: Did not find configuration file 'ApplicationInsights.xml' in '/usr/share/collectd/java'
Mar 5 04:36:27 AppInsightsVM collectd: AI: WARN 05-03-2016 04:36, 1: Configuration file 'ApplicationInsights.xml' could not be found</span></b>
以上で設定は完了だが、SELinux の設定が有効化されている場合、collectd を実行した場合に以下のエラーが発生する。
[root@AppInsightsVM ~]# tail -n 30 /var/log/messages
Mar 5 04:13:38 AppInsightsVM systemd: collectd.service: main process exited, code=exited, status=1/FAILURE
Mar 5 04:13:38 AppInsightsVM systemd: Unit collectd.service entered failed state.
Mar 5 04:13:38 AppInsightsVM systemd: collectd.service failed.
Mar 5 04:13:38 AppInsightsVM systemd: collectd.service holdoff time over, scheduling restart.
Mar 5 04:13:38 AppInsightsVM systemd: Started Collectd statistics daemon.
Mar 5 04:13:38 AppInsightsVM systemd: Starting Collectd statistics daemon...
<span style="color: #ff0000"><b>Mar 5 04:13:38 AppInsightsVM collectd: OpenJDK 64-Bit Server VM warning: INFO: os::commit_memory(0x00007fc7b780a000, 2555904, 1) failed; error='Permission denied' (errno=13)</b></span>
Mar 5 04:13:38 AppInsightsVM collectd: #
Mar 5 04:13:38 AppInsightsVM collectd: # There is insufficient memory for the Java Runtime Environment to continue.
Mar 5 04:13:38 AppInsightsVM collectd: # Native memory allocation (mmap) failed to map 2555904 bytes for committing reserved memory.
Mar 5 04:13:38 AppInsightsVM collectd: # An error report file with more information is saved as:
今回は以下のコマンドを利用して一時的に SELinux を無効化するが、自身の環境とポリシーに合わせて設定を行ってほしい。
[root@AppInsightsVM ~]# setenforce 0
上記の完了後、collectd と Apache を起動する。
[root@AppInsightsVM ~]# systemctl start collectd
[root@AppInsightsVM ~]# systemctl start httpd
設定がうまくいっている場合、管理ポータルから情報の取得が可能となる。以下の様にポータルの Application Insights 画面からメトリックスエクスプローラの「グラフの追加」から各種グラフを表示することができる。