BLOG

ブログ

【初心者】ZABBIXサーバーを導入する(構築編)

こんにちは。1月に入社させてもらいましたkashimaです。

社内ブログ初投稿ということで頑張ってみたいと思います!

ZABBIXとは

サーバーやネットワークを一元的に監視してくれる無料のオープンソースソフトウェアです。

サーバーの負荷が上昇したり、システムがダウンするなどしたら通知してくれたりもします。

サーバーの死活監視、CPU使用率、メモリ使用率、ディスク使用率など多くの項目を監視することができ、低コストでカスタマイズ性の高いところが魅力ですね!

どうやって導入するの?

最近はAWS環境上のEC2にZabbixサーバーを置いて、監視したいインスタンスからZabbixサーバーの情報を設定して監視するパターンが多いです。てなわけでAWS環境上にZabbixサーバーを作ってみたいと思います。

前提条件としてはAWSアカウントを作成していればそれだけでOKです

1. インスタンスを起動

AWSコンソールにログインしたら、EC2ダッシュボードに行きましょう。

インスタンスを起動ボタンを押して、新しいインスタンスを作成します。

今回は導入手順だけなので、設定値は割と適当です。

セキュリティグループのポートを開いておくのを忘れずに

  • 名前 : zabbix-server
  • OS : Amazon-Linux2
  • インスタンスタイプ : t2.micro(無料利用枠)
  • キーペア : なければ作成
  • セキュリティグループ : ポート80(HTTP)およびポート22(SSH)を開いておく

とりあえずこんな感じでいいと思います。

2. EC2インスタンスに接続

コマンドを打ってEC2インスタンスに接続しましょう

ssh -i "your-key-pair.pem" ec2-user@your-ec2-public-ip

キーペアへのパスと、先ほど起動したEC2のパブリックIPを入れて接続します。

fingerprintはyesでOK

3. OS設定と下準備

まずはEC2インスタンス上でOSをアップデートします。

$ sudo yum update
No packages marked for update

タイムゾーンと言語も変更しておきましょう

$ timedatectl set-timezone Asia/Tokyo
$ localectl set-locale LANG=ja_JP.UTF-8

rebootしておきます。

$ reboot
Connection to xx.xxx.xx.xx closed by remote host.
Connection to xx.xxx.xx.xx closed.

4. MariaDBのセットアップ

今回はMariaDBを使ってセットアップしていきます。

まずはインスタンス上でMariaDBをインストールしましょう

$ sudo yum -y install mariadb-server

MariaDBを起動します

$ sudo systemctl start mariadb
$ sudo systemctl enable mariadb

ついでにステータスも確認しておきましょう

省いていますが、Activeになってる的なメッセージが出ればOKです🐤

$ systemctl status mariadb
● mariadb.service - MariaDB database server
   Loaded: loaded (/usr/lib/systemd/system/mariadb.service; enabled; vendor preset: disabled)
   Active: active (running) since 木 2024-01-25 17:16:42 JST; 10s ago

MariaDBのパスワード初期設定を行います。

$ mysql_secure_installation

対話形式で色々と質問されるので、Change the root password?の時に任意のパスワードを入力してEnterを押してください。

その他は「y」で進めてOKです。

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): 
OK, successfully used password, moving on...

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

You already have a root password set, so you can safely answer 'n'.

Change the root password? [Y/n] 
New password: 
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!

きちんとMariaDBにログインできるか確認しておきましょう

パスワードは先ほど設定したパスワードを入力し、ログインが確認できたらexitしておきましょう

$ mysql -u root -p
// 先ほど設定したpasswordでログイン
Enter password: 

Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 25
Server version: 5.5.68-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> exit

これでDBのセットアップは完了です

5. Zabbixエージェントのインストール

DBのセットアップが終わったので次はZabbixのリポジトリをインストールしてきます。

キャッシュも削除しておきます。

$ sudo sudo rpm -Uvh https://repo.zabbix.com/zabbix/5.0/rhel/7/x86_64/zabbix-release-5.0-1.el7.noarch.rpm
$ yum clean all

次にZabbixエージェントをインストールします

$ sudo yum -y install zabbix-server-mysql zabbix-agent

CentOSのSoftware Collections (SCL)をインストールします。

sudo yum -y install http://mirror.centos.org/altarch/7/extras/aarch64/Packages/centos-release-scl-rh-2-3.el7.centos.noarch.rpm

Zabbixのリポジトリ設定ファイルを編集します。書き込み権限が必要なのでsudoをつけます。

zabbix-frontendのenabledを0から1へ変更

$ sudo vim /etc/yum.repos.d/zabbix.repo

[zabbix]
name=Zabbix Official Repository - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-frontend]
name=Zabbix Official Repository frontend - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/frontend
enabled=1 // ここを0から1に変える
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591

[zabbix-debuginfo]
name=Zabbix Official Repository debuginfo - $basearch
baseurl=http://repo.zabbix.com/zabbix/5.0/rhel/7/$basearch/debuginfo/
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX-A14FE591
gpgcheck=1

[zabbix-non-supported]
name=Zabbix Official Repository non-supported - $basearch
baseurl=http://repo.zabbix.com/non-supported/rhel/7/$basearch/
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-ZABBIX
gpgcheck=1

これ必要なのか???日本語パッケージをインストールしちゃいます。

$ sudo yum -y install zabbix-web-mysql-scl zabbix-apache-conf-scl zabbix-web-japanese

6. DBの作成

Zabbix用のDBを作成していきます。4で設定したDBのパスワードを入力してログインしてください。

$ mysql -u root -p
Enter password: // 4で設定したDBのパスワード

Zabbixというデータベースを作成します。

MariaDB [(none)]> create database zabbix character set utf8 collate utf8_bin;
Query OK, 1 row affected (0.00 sec)

データベースにアクセスするためのユーザとPWを作成します。

MariaDB [(none)]> create user zabbix@localhost identified by 'password'; // 任意のPW
Query OK, 0 rows affected (0.00 sec)

ユーザーに対してデータベースへのアクセス権を付与します。

ここまでできたらDBからはexitで抜けてOKです。

MariaDB [(none)]> grant all privileges on zabbix.* to zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> exit
Bye

7. Zabbixサーバーの設定を行う

DBの作成も完了したので、Zabbixの初期データをimportします。

$ zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix
Enter password: // DBのパスワード

Zabbixサーバーの設定ファイルにDBのzabbixユーザのPWを直接書いちゃいます。

$ sudo vim /etc/zabbix/zabbix_server.conf

// 書き込む場所はここ
### Option: DBPassword
#       Database password.
#       Comment this line if no password is used.
#
# Mandatory: no
# Default:
DBPassword=#を外してDBのパスワードを直接入力する。

phpのタイムゾーンを日本時間に設定します。

一番下のphp_value[date.timezone]をAsia/Tokyoに変更します。

$ sudo vim /etc/opt/rh/rh-php72/php-fpm.d/zabbix.conf
[zabbix]
user = apache
group = apache

listen = /var/opt/rh/rh-php72/run/php-fpm/zabbix.sock
listen.acl_users = apache
listen.allowed_clients = 127.0.0.1

pm = dynamic
pm.max_children = 50
pm.start_servers = 5
pm.min_spare_servers = 5
pm.max_spare_servers = 35
pm.max_requests = 200

php_value[session.save_handler] = files
php_value[session.save_path]    = /var/opt/rh/rh-php72/lib/php/session/

php_value[max_execution_time] = 300
php_value[memory_limit] = 128M
php_value[post_max_size] = 16M
php_value[upload_max_filesize] = 2M
php_value[max_input_time] = 300
php_value[max_input_vars] = 10000
php_value[date.timezone] = Asia/Tokyo

Zabbixサーバー、Zabbixエージェントを立ち上げます。

$ sudo systemctl restart zabbix-server zabbix-agent
$ sudo systemctl enable zabbix-server zabbix-agent

Apacheを起動します

$ sudo systemctl restart httpd
$ sudo systemctl enable httpd
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

PHPを起動します

$ sudo systemctl restart rh-php72-php-fpm
$ sudo systemctl enable rh-php72-php-fpm
Created symlink from /etc/systemd/system/multi-user.target.wants/rh-php72-php-fpm.service to /usr/lib/systemd/system/rh-php72-php-fpm.service.

これでサーバー側の設定は完了です!お疲れ様でした😎

8. ブラウザで確認する

ここまで来たらブラウザでZabbixの画面を確認しましょう。

http://${ec2-public-ip}/zabbix

上記にアクセスするとこんな画面が出てくると思います。

Next Stepでどんどん進んでいきこの画面で

ユーザー名:Admin

パスワード:zabbix

でログインします。(初期設定時)

そしたらこんな画面に入れます。

最初は英語なので左下のUser SettingでLanguageをJapaneseにしておきましょう。

以上でZabbixサーバーの構築は終わりです。

9. 終わりに

今回はZabbixサーバーを立ち上げただけなので実際に監視はしていませんが、Zabbixを使うことでサーバーの状態をキャッチアップし、管理者に対して迅速にエスカレーションを行うことができます。

また、Zabbixはローコストでカスタマイズ性も高いですがOSやversionによる制約でサーバー構築がなかなか上手くいかないという点もあります(完全に感想です)

次の記事では実際にインスタンスを立ち上げた上で監視対象になるように設定する記事を書いてみたいと思います!

参考記事

https://atsushinotes.com/procedure_zabbix_install_to_login_centos-mysql-apache

RELATED ARTICLE

  • この記事を書いた人
  • 最新の記事

tsukasa-kashima