Cactiをインストールする (CentOS 7環境)

CentOS 7の環境にCactiをインストールします。

httpdのインストール

下記コマンドを実行し、httpdをインストールします。
yum -y install httpd

phpのインストール

下記コマンドを実行し、PHPをインストールします。
yum -y install php

net-snmpのインストール

下記コマンドを実行して net-snmpをインストールします。
yum -y install net-snmp

SELinuxの無効化

SELinuxを無効にします。設定手順はこちらの記事を参照してください。

MariaDBのインストール

MariaDBをインストールします。インストール方法はこちらの記事を参照してください。

MariaDBの起動

SELinuxが導入されている場合、MariaDBが起動できないことがあります。下記のコマンドを実行した後でMariaDBを起動します。
cd /usr/bin/
chcon system_u:object_r:bin_t:s0 mysqld_safe
上記コマンドを実行後
systemctl start mariadb
コマンドを実行します。
データベースの起動後、mysql_secure_installation コマンドを利用してMariaDBの初期設定をします。手順の詳細はこちらの記事を参照してください。

RRDTool のインストール

下記コマンドを実行してRRDToolをインストールします。
yum -y install rrdtool

EPEL Repository (EPELリポジトリ)を有効にする

下記のコマンドを実行します。
wget http://dl.fedoraproject.org/pub/epel/7/x86_64/e/epel-release-7-9.noarch.rpm
rpm -ivh epel-release-7-9.noarch.rpm
バージョン番号はバージョンアップとともに変化するので、http://dl.fedoraproject.org/pub/epel/7/x86_64/eから最新のバージョン番号を確認します。

Cacti のインストール

Cactiをインストールします。EPELリポジトリを有効にすることで、yumコマンドでcactiがインストールできるようになります。
yum -y install cacti

httpd.conf (/etc/httpd/conf.d/cacti.conf) の編集

インストールができていれば、/etc/httpd/conf.d/cacti.conf ファイルが設置されています。このファイルを編集します。
su
gedit /etc/httpd/conf.d/cacti.conf
コマンドを実行しエディタを起動します。

変更前

#
# Cacti: An rrd based graphing tool
#

# For security reasons, the Cacti web interface is accessible only to
# localhost in the default configuration. If you want to allow other clients
# to access your Cacti installation, change the httpd ACLs below.
# For example:
# On httpd 2.4, change "Require host localhost" to "Require all granted".
# On httpd 2.2, change "Allow from localhost" to "Allow from all".

Alias /cacti    /usr/share/cacti

<Directory /usr/share/cacti/>
	<IfModule mod_authz_core.c>
		# httpd 2.4
		Require host localhost
	</IfModule>
	<IfModule !mod_authz_core.c>
		# httpd 2.2
		Order deny,allow
		Deny from all
		Allow from localhost
	</IfModule>
</Directory>

<Directory /usr/share/cacti/install>
	# mod_security overrides.
	# Uncomment these if you use mod_security.
	# allow POST of application/x-www-form-urlencoded during install
	#SecRuleRemoveById 960010
	# permit the specification of the rrdtool paths during install
	#SecRuleRemoveById 900011
</Directory>


# These sections marked "Require all denied" (or "Deny from all")
# should not be modified.
# These are in place in order to harden Cacti.
<Directory /usr/share/cacti/log>
	<IfModule mod_authz_core.c>
		Require all denied
	</IfModule>
	<IfModule !mod_authz_core.c>
		Order deny,allow
		Deny from all
	</IfModule>
</Directory>
<Directory /usr/share/cacti/rra>
	<IfModule mod_authz_core.c>
		Require all denied
	</IfModule>
	<IfModule !mod_authz_core.c>
		Order deny,allow
		Deny from all
	</IfModule>
</Directory>

変更後

今回Apache (Httpd)のバージョンは2.4のため、"# httpd 2.4"のセクションを変更します。httpd 2.2までの書式と異なるので注意してください。
#
# Cacti: An rrd based graphing tool
#

# For security reasons, the Cacti web interface is accessible only to
# localhost in the default configuration. If you want to allow other clients
# to access your Cacti installation, change the httpd ACLs below.
# For example:
# On httpd 2.4, change "Require host localhost" to "Require all granted".
# On httpd 2.2, change "Allow from localhost" to "Allow from all".

Alias /cacti    /usr/share/cacti

<Directory /usr/share/cacti/>
	<IfModule mod_authz_core.c>
		# httpd 2.4
		#Require host localhost
		Require all granted
	</IfModule>
	<IfModule !mod_authz_core.c>
		# httpd 2.2
		Order deny,allow
		Deny from all
		Allow from localhost
	</IfModule>
</Directory>

<Directory /usr/share/cacti/install>
	# mod_security overrides.
	# Uncomment these if you use mod_security.
	# allow POST of application/x-www-form-urlencoded during install
	#SecRuleRemoveById 960010
	# permit the specification of the rrdtool paths during install
	#SecRuleRemoveById 900011
</Directory>


# These sections marked "Require all denied" (or "Deny from all")
# should not be modified.
# These are in place in order to harden Cacti.
<Directory /usr/share/cacti/log>
	<IfModule mod_authz_core.c>
		Require all denied
	</IfModule>
	<IfModule !mod_authz_core.c>
		Order deny,allow
		Deny from all
	</IfModule>
</Directory>
<Directory /usr/share/cacti/rra>
	<IfModule mod_authz_core.c>
		Require all denied
	</IfModule>
	<IfModule !mod_authz_core.c>
		Order deny,allow
		Deny from all
	</IfModule>
</Directory>

MariaDB(MySQL) タイムゾーンの設定

MariaDBのタイムゾーンの設定をします。手順の詳細はこちらの記事を確認してください。

下記のコマンドを実行します。
mysql_tzinfo_to_sql /usr/share/zoneinfo | mysql -u root -p mysql

/etc/my.cnf ファイル、または、 /etc/my.cnf.d/ ディレクトリ内のcnfファイルを編集します。
cnfファイル内の mysqld セクションに "default-time-zone" を記述します。MariaDBのバージョン10.2の場合は、/etc/my.cnf.d/server.cnf ファイルの[mysqld]セクションに記述するのが良いかもしれません。
/etc/my.cnf.d/server.cnf
[mysqld]
default-time-zone = 'Asia/Tokyo'

続いてSQLのプロンプトから
  show variables like '%time_zone%';
コマンドを実行します。下記の結果が返ります。
+------------------+---------------+
| Variable_name    | Value         |
+------------------+---------------+
| system_time_zone | ???? (?W????) |
| time_zone        | Asia/Tokyo    |
+------------------+---------------+
2 rows in set (0.01 sec)
time_zone の値が "Asia/Tokyo" に設定されていればOKです。

cactiuser のアカウントが TimeZoneテーブルにアクセスできるよう下記のコマンドを実行します。
  grant select on mysql.time_zone_name to 'cactiuser'@'localhost' identified by '(cactiuser のパスワード)';

MariaDBの設定

/etc/my.cnf ファイルか /etc/my.cnf.d/ ディレクトリ内の設定ファイルの [mysqld]セクションに下記のオプションパラメータをついkします。今回は、/etc/my.cnf.d/server.cnf ファイルの[mysqld]セクションに記述します。
/etc/my.cnf.d/server.cnf
[mysqld]
default-time-zone = 'Asia/Tokyo'
collation-server = utf8mb4_unicode_ci
character-set-client = utf8mb4
character-set-server = utf8mb4
max_heap_table_size = 300M
tmp_table_size = 128M
join_buffer_size = 128M
innodb_buffer_pool_size = 960M
innodb_doublewrite = OFF
innodb_flush_log_at_timeout = 5
innodb_read_io_threads = 48
innodb_write_io_threads = 32
default-time-zone は先のタイムゾーン設定の手順で追記した部分になります。

データベースの作成

下記コマンドを実行してSQLコマンドプロンプトを表示します。
mysql -u root -p

下記コマンドを実行してmariadbにデータベースを作成します。
create database cacti;

ユーザーの作成

下記SQLコマンドを実行してcactiuserユーザーを作成します。
GRANT ALL ON cacti.* TO cactiuser@localhost IDENTIFIED BY '(cactiユーザーのパスワード)';
FLUSH privileges;
補足
設定によってはcactiユーザーを作成する場合もありますが、スタンダードな手順では"cactiuser"を作成するのが一般的です。
以下のコマンドを実行します。identified by の後の '(cactiユーザーのパスワード)' はパスワードになりますので、変更のうえコマンドを実行します。
GRANT ALL ON cacti.* TO cacti@localhost IDENTIFIED BY '(cactiユーザーのパスワード)';
FLUSH privileges;


コマンドを実行した後、
quit;
でSQLプロンプトを終了します。

補足

mysqladmin -u root -p create cactiuser
mysqladmin -u root -p create cacti
コマンドでもユーザーの作成はできます。

テーブル等の作成

下記のコマンドを実行し、テーブルなどを作成します。
mysql -u cactiuser -p cacti < /usr/share/doc/cacti-0.8.8g/cacti.sql

データベースユーザーが "cacti" の場合

データベース名とデータベースのユーザーが同じ名前のためわかりにくいです。
mysql -u cacti -p cacti < /usr/share/doc/cacti-0.8.8g/cacti.sql


コマンドの書式は下記となります。
mysql -u (データベースのユーザー名) -p (データベース名) < /usr/share/doc/cacti-0.8.8g/cacti.sql

/etc/cacti/db.php の編集

データベースのユーザー名やパスワードをデフォルトのものから変更した場合は、/etc/cacti/db.php を編集します。

補足

/usr/share/cacti/include/config.php のシンボリックリンクがあるため、/usr/share/cacti/include/config.php を編集しても同様の処理となります。
/etc/cacti/db.php
<?php
/*
 +-------------------------------------------------------------------------+
 | Copyright (C) 2004-2016 The Cacti Group                                 |
 |                                                                         |
 | This program is free software; you can redistribute it and/or           |
 | modify it under the terms of the GNU General Public License             |
 | as published by the Free Software Foundation; either version 2          |
 | of the License, or (at your option) any later version.                  |
 |                                                                         |
 | This program is distributed in the hope that it will be useful,         |
 | but WITHOUT ANY WARRANTY; without even the implied warranty of          |
 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the           |
 | GNU General Public License for more details.                            |
 +-------------------------------------------------------------------------+
 | Cacti: The Complete RRDTool-based Graphing Solution                     |
 +-------------------------------------------------------------------------+
 | This code is designed, written, and maintained by the Cacti Group. See  |
 | about.php and/or the AUTHORS file for specific developer information.   |
 +-------------------------------------------------------------------------+
 | http://www.cacti.net/                                                   |
 +-------------------------------------------------------------------------+
*/

/* make sure these values refect your actual database/host/user/password */
$database_type = "mysql";
$database_default = "cacti";
$database_hostname = "localhost";
$database_username = "cactiuser"; /* 必要に応じて変更 */
$database_password = "(cactiユーザーのパスワード)"; /* 必要に応じて変更 */
$database_port = "3306";
$database_ssl = false;

/*
   Edit this to point to the default URL of your Cacti install
   ex: if your cacti install as at http://serverip/cacti/ this
   would be set to /cacti/
*/
//$url_path = "/cacti/";

/* Default session name - Session name must contain alpha characters */
//$cacti_session_name = "Cacti";

?>

アクセス権の変更

plugins, log, rra ディレクトリに書き込み権限を付与します。下記コマンドを実行します。
chmod -R 777 /usr/share/cacti/plugins
chmod -R 777 /usr/share/cacti/log
chmod -R 777 /usr/share/cacti/rra
またh
chmod -R 777 /var/www/htdocs/cacti/plugins/*
chmod -R 777 /var/www/htdocs/cacti/log/*
chmod -R 777 /var/www/htdocs/cacti/rra/*
または
chmod -R 777 /var/www/htdocs/cacti/plugins
chmod -R 777 /var/www/htdocs/cacti/log
chmod -R 777 /var/www/htdocs/cacti/rra

cron.d の編集

/etc/cron.d/cacti ファイルを編集し、crondで5分に1回pollerが実行されるようにします。

変更前

#*/5 * * * *	cacti	/usr/bin/php /usr/share/cacti/poller.php > /dev/null 2>&1

変更後

コメント記号を外します。
*/5 * * * *	cacti	/usr/bin/php /usr/share/cacti/poller.php > /dev/null 2>&1

ファイアウォールの編集

ファイアウォールの設定を変更して、httpで外部からアクセス可能な状態に変更します。
著者
iPentec.com の代表。ハードウェア、サーバー投資、管理などを担当。
Office 365やデータベースの記事なども担当。
掲載日: 2017-01-19
iPentec all rights reserverd.