Zabbix Server 4.0の構築(CentOS 7&CloudGarage)

f:id:kanna1088:20181018115743p:plain


Zabbix 4.0 LTSがリリースされたので、構築してみようと思います。

検証環境

CloudGarage DAPプラン(NHN JAPAN)

CPU Memory SSD OS
1core 1GB 50GB CentOS 7

構築手順

注意事項として、CloudGarageのCentOS 7はインスタンス構築時点では、
firewalldが入っていないためfirewall-cmdを使ったファイアウォール設定ができません。
そのため、iptablesを使うか、firewalldをインストール
または、CloudGarageコントロールパネル上から設定する必要があります。
この記事では、新機能としてCloudGarageのコントロールパネルでファイアウォールがポート指定で設定できるようになったので、コントロールパネルから設定します。

インスタンスを構築

※ 今回DAPを利用していることを想定していますが、DAPとは別にCloudGarageプランの契約を行った場合、インスタンス構築の手順が異なります。
これは、DAPリリース時と現在で取り扱ってるプランが違うためです。
現在(2018/10/16)、DAPで利用できるプランは旧プラン(BOXプラン)となります。

CloudGarageのコントロールパネルへログインして、「インスタンス追加」を選択します。
インスタンスを下記のように設定して、「インスタンス作成」を選択して、構築をしてください。

Zabbix Server用
プラン選択:BOXプラン
BOXプラン:[数字] DevAssistProgram用プラン
インスタンス選択:1GBタイプ
OS/イメージ選択:CentOS(7.3_64bit)
接続許可ポート:詳細設定

プロトコル ポート 送信元IPアドレス
TCP 22 無記入(ANY)
TCP 80 無記入(ANY)

送信元IPアドレスを自分の環境のグローバルIPアドレスにすれば、
自分しかアクセスできない環境にすることもできるので、必要に応じて使ってください。

SSH Key設定:任意
インスタンス情報 インスタンス名:Zabbix (任意)
インスタンス情報 コメント:任意
インスタンス情報 rootパスワード:任意

初期設定(ユーザー追加、アップデートなど)

インスタンス構築時点ではrootユーザしかおらず、インストールされているパッケージも古いため、
セキュリティ向上のために最低限必要な設定を行います。
まずは、新しくユーザーを追加して、管理者権限を付与させます。
ここではユーザー名は「user」とします。
SSHでrootユーザーとしてログインしてください。
以下のコマンドを実行します。

[root@zabbix ~]# useradd -m -G wheel user (ユーザ追加と管理者権限を付与しました。)
[root@zabbix ~]# passwd user       (追加したユーザにパスワードを設定します。) 

OpenSSHサーバーの設定を変更します。
/etc/ssh/sshd_config内の
PermitRootLogin yes を PermitRootLogin no に書き換えて再起動します。
これにより、SSHでrootアカウントへ接続することができなくなります。

[root@zabbix ~]# sed -i "s/PermitRootLogin yes/PermitRootLogin no" /etc/ssh/sshd_config
[root@zabbix ~]# systemctl restart sshd

rootアカウントで接続ができなくなっていると思います。

次に、インスタンスのアップデートを行います。
SSHで先ほど設定したアカウントで接続後、以下のコマンドを入力してください。

[user@zabbix ~]$ sudo yum update -y

sudo は一般ユーザが一時的に管理者の権限を利用する場合に使うコマンドです。
コマンドを実行すると以下のように表示されますので、userのパスワードを入力して実行してください。

We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for user:

Complete!と表示されたら一度再起動させましょう。

[user@zabbix ~]$ sudo reboot

データベースのインストール

Zabbixのデータ保管先として、MariaDBをインストールします。

[user@zabbix ~]$ sudo yum install mariadb-server -y
[user@zabbix ~]$ sudo systemctl start mariadb
[user@zabbix ~]$ sudo systemctl enable mariadb

MariaDBの初期設定を実行します。

[user@zabbix ~]$ mysql_secure_installation

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):(インストールした時点ではパスワードが設定されていないのでそのままEnterキーを押す)
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y
New password:(任意でMariaDBのrootパスワードを決める)
Re-enter new password:
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] Y
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] Y
 ... Success!

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] Y
 - Dropping test database...
 ... Success!
 - Removing privileges on test database...
 ... Success!

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] Y
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

次に、ZabbixがアクセスするためのMariaDBアカウントとデータベースを設定します。
ここでは、ユーザー名はzabbix、パスワードは任意とします。

[user@zabbix ~]$ mysql -uroot -p
MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin; (データベースの作成)
MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost identified by '(任意のパスワード)';
MariaDB [(none)]> exit

Zabbixのインストール

[user@zabbix ~]$ sudo rpm -ivh https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
[user@zabbix ~]$ sudo yum install zabbix-server-mysql zabbix-web-mysql zabbix-agent -y

Zabbixのインストールが終わったら、先ほど作成したデータベースにZabbix用のデータをインポートします。

[user@zabbix ~]$ zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix

ZabbixサーバーのconfigファイルにDBのパスワード記述します。

[user@zabbix ~]$ sudo sed -i "s/# DBPassword=/DBPassword=(設定したパスワード)/" /etc/zabbix/zabbix_server.conf

初期状態では、DBNameとDBUserはzabbixで指定されているので、違う名前で作成した場合、別途指定が必要です。
次はWebサーバーの設定です。タイムゾーンの設定を行います。

[user@zabbix ~]$ sudo sed -i "s/# php_value date.timezone Europe\/Riga/php_value date.timezone Asia\/Tokyo/" /etc/httpd/conf.d/zabbix.conf

Zabbixを実行しましょう。

[user@zabbix ~]$ sudo systemctl start zabbix-server
[user@zabbix ~]$ sudo systemctl start zabbix-agent
[user@zabbix ~]$ sudo systemctl start httpd
[user@zabbix ~]$ sudo systemctl enable zabbix-server
[user@zabbix ~]$ sudo systemctl enable zabbix-agent
[user@zabbix ~]$ sudo systemctl enable httpd

Zabbixフロントエンドの設定

http://[インスタンスのグローバルIPアドレス]/zabbix/にアクセスします。

f:id:kanna1088:20181018112908p:plainf:id:kanna1088:20181018112911p:plainf:id:kanna1088:20181018112914p:plainf:id:kanna1088:20181018112923p:plainf:id:kanna1088:20181018112926p:plainf:id:kanna1088:20181018112928p:plain
画像のように進めていって下さい。
Finishを押すとログイン画面へ移動します。
この時点では初期アカウントが設定されていて、ユーザー名:Admin パスワード:zabbixでログインできます。
初期のままではまずいので、変更しましょう。
一番上のメニューからAdministration→Usersをクリックします。
リストの中に、Adminというのがあるので選択します。
項目のAliasがログインに必要なユーザー名で、Passwordがパスワードの部分になります。
guestアカウントを使用しない場合は選択して、Groupsの中にDisabledを追加してください。
これで設定は終わりです。

いままでの設定ですでに、Zabbix Server自身のモニタリングが行われていると思います。
Configuration→Hostsへ移動して確認してみてください。
f:id:kanna1088:20181018144856p:plain 画像のように、StatusがenableでAvailabilityのZBXが緑色になっていれば、zabbix agentを通じてZabbix Serverのモニタリングを行っていることが確認できます。