Opensourcetechブログ

OpensourcetechによるNGINX/Kubernetes/Zabbix/Neo4j/Linuxなどオープンソース技術に関するブログです。

Collectd & Apache2.4で作るシステムリソース 収集&モニタリング on CentOS7

こんにちは、鯨井貴博@opensourcetechです。

 

 今回は、Virtualbox上の仮想マシン(CentOS7)にて、

LPICレベル2の範囲に含まれるcollectdを使ってみます。

 

なおCentOS7についてはインストールされている前提となりますので、

CentOS7のインストールから行う場合は、

その1その2その3Netinstall編をご覧ください。

 

また、CentOS6環境でcollectdを使いたい方は、

こちらをご覧ください。

 

 

 

 まずは、collectdパッケージを含んでいるepelレポジトリを利用する準備から始めます。

http://ftp.riken.jp/Linux/fedora/epel/7/x86_64/repoview/epel-release.html

にあるepel-release-7-1.noarchを取得します。

wget http://ftp.riken.jp/Linux/fedora/epel/7/x86_64/e/epel-release-7-1.noarch.rpm

f:id:opensourcetech:20140908191638p:plain

f:id:opensourcetech:20140908191647p:plain

 

続いて取得したrpmをインストールします。

rpm -ivh epel-release-7-1.noarch.rpm

すると、/etc/yum.repos.depel-testing.repoelep.repoというレポジトリファイルが作成されます。

f:id:opensourcetech:20140908191656p:plain

 

collectdのインストールを行います。

yum install collectd

f:id:opensourcetech:20140908191701p:plain

f:id:opensourcetech:20140908191704p:plain

 

collectdがインストール出来たら、

設定ファイルである/etc/collectd.confを編集します。

f:id:opensourcetech:20140908191707p:plain

編集箇所は、Hostnameというパラメータにて、「"Collectd on CentOS7(任意名)"」を設定します。

f:id:opensourcetech:20140908191714p:plain

 

また、システムリソースを取得しrrdファイルに格納してくれるcollectd-rrdtoolもインストールします。やたらと依存関係が多いです(笑)

yum install collectd-rrdtool

f:id:opensourcetech:20140908191719p:plain

f:id:opensourcetech:20140908191729p:plain

 

 

続いてcollectdの起動。

CentOS7ではsystemdが使われているので、

systemctl start collectd.serviceとします。

 systemdについて知りたい方は、こちら

f:id:opensourcetech:20140908191732p:plain

 

collectdを起動すると、/var/lib/collectd/rrd/ホスト名ディレクトリ配下の

各リソースディレクトリ内にrrdファイルが作成されます。

f:id:opensourcetech:20140908191735p:plain

 

続いて、

取得したrrdファイルの情報をグラフ化し、ブラウザから閲覧する為に、

httpdおよびcollectd-webをインストールします。

yum install httpd

yum install collectd-web

f:id:opensourcetech:20140908191738p:plain

f:id:opensourcetech:20140908191741p:plain

f:id:opensourcetech:20140908191744p:plain

f:id:opensourcetech:20140908191750p:plain

 

Collectd用にApache(httpd)を設定するのですが、

collectd-webのインストールとともに、

/etc/httpd/conf.dディレクトリにcollectd.confというファイルが出来ますので、

これを編集します。

f:id:opensourcetech:20140908191754p:plain

 

編集内容としては、

グラフ化を行うスクリプトであるindex.cgiが実行出来るように、

Options ExecCGI(CGI使用の許可)と、

Allow from 192.168.11.0/24(アクセス制御)を設定しました。

f:id:opensourcetech:20140908191757p:plain

 

そして、httpdの起動。

systemctl start httpd.service

f:id:opensourcetech:20140908191800p:plain

lsof -i:80でTCP80番ポートの使用状況を確認します。

f:id:opensourcetech:20140908191803p:plain

 

 

続いて、iptablesを設定し、apache(httpd)への通信を許可します。

iptables -I IN_public_allow 2 -p tcp --dport 80 -m state --state NEW -j ACCEPT

 

なお、CentOS7におけるiptablesについては、以下をご覧ください。

CentOS6と7の差分対応 (iptables) - Opensourcetechブログ(ゼウス・ラーニングパワー裏BLOG)

f:id:opensourcetech:20140908191805p:plain

f:id:opensourcetech:20140908191808p:plain

 

そしていよいよ、クライアントのブラウザより、

http://collectdのIPアドレス/collectd/bin/index.cgiへアクセスします。

 

が、しかし、

何故か「403 Forbidden」となりアクセスが拒否されます。

You don't have permission to access /collectd/bin/index.cgi on this server.とあることからどうもアクセス制御に引っかかっているようです。

f:id:opensourcetech:20140908191811p:plain

 

しかし、Allow fromとDeny fromはきちんと設定したような気がしたので、

念の為、Apacheのドキュメントを確認しました。

Upgrading to 2.4 from 2.2 - Apache HTTP Server Version 2.4

 

すると、apache2.4ではアクセス制御の設定方法が変更されているとの記載が、、(汗)

Order deny,allow

Deny from allは、Require all denied

 

Order allow,deny

Allow from allは、Require all granted

 

Order Deny,Allow

Deny from all

Allow from example.orgは、Require host example.orgにせよとあるではありませんか。

f:id:opensourcetech:20140908191815p:plain

f:id:opensourcetech:20140908191823p:plain

rpm -q httpdとすると、

確かにバージョン2.4です。

f:id:opensourcetech:20140908191832p:plain

 

早速、/etc/httpd/conf.d/collectd.confを再編集します。

編集箇所は以下です。

f:id:opensourcetech:20140908191845p:plain

 

そして、httpdの再起動。

systemctl stop httpd.service

systemctl start httpd.service

f:id:opensourcetech:20140908191849p:plain

 

改めてブラウザより、http://collectdのIPアドレス/collectd/bin/index.cgiにアクセスすると、今度は無事に表示されました。

f:id:opensourcetech:20140908191852p:plain

 

リソース表示もOK!

Apache2.4のUpdateに少しあせりながらも、

無事完了です。

f:id:opensourcetech:20140908191856p:plain

 

 

 

 

collectdが試験範囲に含まれているLPICレベル2を勉強されている方は、

こちらもどうぞ!

LPICレベル2は取得できる!!

Opensourcetech by Takahiro Kujirai