2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

WSL環境のCentOS7 にZabbixでの監視環境を構築する

Posted at

初めに

前回WSLでCentOS7をインストールするで作成した環境にZabbixの監視環境を作成していきます。
前回と違う環境となりますが、やったことは前回と一緒です。

[root@lifebook ~]# cat /etc/redhat-release
CentOS Linux release 7.6.1810 (Core) 
[root@lifebook ~]# uname -r
4.4.0-18362-Microsoft
[root@lifebook ~]# 

Zabbixの動作に必要なパッケージをインストール

Zabbixの動作に必要なパッケージをインストールしていきます。
今回はApache、PHP、MariaDBの構成で構築します。

[root@lifebook ~]# yum -y groupinstall web-server
[root@lifebook ~]# yum -y groupinstall php
[root@lifebook ~]# yum -y groupinstall mariadb

Zabbix ServerとAgentのインストール

Zabbix4.4の公式リポジトリをインストールして行きます。
インストールする際は新しいバージョンが公開されていないか確認してからインストールしてください
Zabbix Official Repository

現在新しいリポジトリは4.4が新しいぽいので4.4をインストールします。

[root@lifebook ~]# yum -y install https://repo.zabbix.com/zabbix/4.4/rhel/7/x86_64/zabbix-release-4.4-1.el7.noarch.rpm

リポジトリ追加が完了したらZabbix Serverの関連パッケージをインストール

[root@lifebook ~]# yum -y install zabbix-web-mysql zabbix-web-japanese zabbix-server-mysql

Zabbix Serverのインストールが完了したら、次はZabbix Agentをインストール

[root@lifebook ~]# yum -y install zabbix-agent

設定ファイルの変更

各設定ファイルを変更していきます。
Apache
"/etc/httpd/conf.d/zabbix.conf"で"php_value date.timezone"がコメントアウトされているので、
外してタイムゾーンを変更します。

[root@lifebook ~]# vi /etc/httpd/conf.d/zabbix.conf

#
# Zabbix monitoring system php web frontend
#

Alias /zabbix /usr/share/zabbix

<Directory "/usr/share/zabbix">
    Options FollowSymLinks
    AllowOverride None
    Require all granted

    <IfModule mod_php5.c>
        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 always_populate_raw_post_data -1
        php_value date.timezone Asia/Tokyo
    </IfModule>
</Directory>

Zabbix Server
最低限の設定としてDB関連の設定を行います

[root@lifebook ~]# vi /etc/zabbix/zabbix_server.conf

### Option: DBHost
#       Database host name.
#       If set to localhost, socket is used for MySQL.
#       If set to empty string, socket is used for PostgreSQL.
#
# Mandatory: no
# Default: 
# 監視サーバー内にDBをインストールするのでコメントアウトしておく
# DBHost=localhost

### Option: DBName
#       Database name.
#
# Mandatory: yes
# Default:
# DBName=

# DB名
# デフォルト以外の名前を使用する場合は変更する
DBName=zabbix

### Option: DBSchema
#       Schema name. Used for IBM DB2 and PostgreSQL.
#
# Mandatory: no
# Default:
# DBSchema=

### Option: DBUser
#       Database user.
#
# Mandatory: no
# Default:

# DBユーザー名
# デフォルト以外の名前を使用する場合は変更する
DBUser=zabbix

### Option: DBPassword
#       Database password.
#       Comment this line if no password is used.
#
# Mandatory: no
# Default:

# DBパスワード
# コメントアウトされているので外して任意のパスワードを設定する
DBPassword=zabbixpass

Zabbix Agent
Zabbix Agentの設定を行いますしていきます。

[root@lifebook ~]# vi /etc/zabbix/zabbix_agentd.conf

DBの設定
DBの設定を行っていきます。
自分の場合作成したDBユーザーZabbixにログインできないという事象が発生しました・・・
同じような事象が発生したら回避方法のところを見てみてください。
また、もっといい回避方法があったら教えてください・・・

まず、MariaDBサービスを自動起動設定と開始させます。

[root@lifebook ~]# systemctl enable mariadb
[root@lifebook ~]# systemctl start mariadb

続いてMariaDBの初期設定をしていきます

[root@lifebook ~]# 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):  何も入力せずエンター
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 yを入力して
New password:  DBユーザー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 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 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 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 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!
[root@lifebook ~]#

Zabbix用のデータベースとDBユーザーを作成していきますが、
ここのコマンドでユーザーを作成することは私はあまりお勧めしません。
Zabbix用のスキーマファイル読み込み時権限がないとのエラーが発生して読み込みができません。
コマンドを実行してもデータベース作成のみのほうがいいです。

[root@lifebook ~]# mysql -u root -p
Enter password: 
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.64-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)]> CREATE DATABASE zabbix character set utf8 collate utf8_bin;  このコマンドまで実行をお勧め。
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> CREATE USER zabbix@localhost IDENTIFIED BY '<zabbixpass>';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT ALL ON zabbix.* TO zabbix@localhost;
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> quit;
Bye
[root@lifebook ~]# 

上のコマンドを実行して念のためmysqlにzabbixでログインしてみましょう。
問題なくログイン出来たら・・・

おめでとうございます!:tada:
回避方法の後のZabbix用のスキーマファイルをデータベースに読み込ませてください。

ログインできずに下記のようなエラーが発生したらphpmyAdminで対処します。

[root@lifebook ~]# mysql -u zabbix -p zabbixpass
Enter password: 
ERROR 1045 (28000): Access denied for user 'zabbix'@'localhost' (using password: YES)

phpmyAdminを使用しての回避法

自分の場合phpmyAdminを使用しての対処できましたが、正しい方法とは思えませんが一つの対処法として記載しておきます。

phpmyAdminのインストール

まずphpmyAdminをインストールします。

[root@lifebook ~]# yum -y install phpMyAdmin

インストール完了後、phpMyAdminの設定ファイルを編集します。

[root@lifebook ~]#vi /etc/httpd/conf.d/phpMyAdmin.conf

特に大きく変更するところはないですがローカルネットワーク内からのアクセスを許可する設定を追加していきます。

<Directory /usr/share/phpMyAdmin/>
   AddDefaultCharset UTF-8

   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require ip 127.0.0.1
       Require ip 192.168.0 <-ローカルIP
       Require ip ::1
     </RequireAny>
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 127.0.0.1
     Allow from 192.168.0 <-ローカルIP
     Allow from ::1
   </IfModule>
</Directory>

<Directory /usr/share/phpMyAdmin/setup/>
   <IfModule mod_authz_core.c>
     # Apache 2.4
     <RequireAny>
       Require ip 127.0.0.1
       Require ip 192.168.0 <-ローカルIP
       Require ip ::1
     </RequireAny>
   </IfModule>
   <IfModule !mod_authz_core.c>
     # Apache 2.2
     Order Deny,Allow
     Deny from All
     Allow from 127.0.0.1
     Allow from 192.168.0 <-ローカルIP
     Allow from ::1
   </IfModule>
</Directory>

上の設定を設定して保存してApacheを起動します。

[root@lifebook ~]# systemctl start httpd

SSHの時と同じようなエラーが発生したら同じコマンドで対処します。

[root@lifebook ~]# service httpd start
Redirecting to /bin/systemctl start httpd.service
Failed to get D-Bus connection: Operation not permitted

何回も同じことをするもめんどくさいので、下記のようなシェルスクリプトを書いていきます。

[root@lifebook ~]# vi procsetup.sh
# !/bin/bash
mv /usr/bin/systemctl /usr/bin/systemctl.old
echo -e yes\\n
curl https://raw.githubusercontent.com/gdraheim/docker-systemctl-replacement/master/files/docker/systemctl.py > /usr/bin/systemctl
chmod +x /usr/bin/systemctl
[root@lifebook ~]# chmod +x procsetup.sh
[root@lifebook ~]# ./procsetup.sh

上のシェルスクリプトを実行したら、Apacheを起動してphpmyAdminにブラウザよりアクセスします。

[root@lifebook ~]# service httpd start

phpmyAdminにログイン後コマンドで作成したZabbixユーザーを削除します。
画像では二つZabbixユーザーが存在していますがホストの項目がlocalhostになっているZabbixユーザーを削除します。
image.png

削除後ユーザーを追加するより以下の画像と同じように権限を設定していきます。
なお作成するユーザーのパスワードはコマンドで追加したときと同じパスワードを使用します。
画像ではエラーが発生出ていますがこれは重複するユーザー名を入力したためなので削除後は出ないと思います。
image.png

グローバル特権はすべてにチェックし実行をクリックします。

image.png

作成が終わったら再度mysqlでログインできるか確認してからZabbix用のスキーマファイルをデータベースに読み込ませます

[root@lifebook ~]# mysql -u root -pzabbixpass
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 119
Server version: 5.5.64-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)]> quit
Bye
[root@lifebook ~]# 

[root@lifebook ~]# zcat /usr/share/doc/zabbix-server-mysql-4.4.*/create.sql.gz | mysql -u zabbix -pzabbixpass
Enter password: 
[root@lifebook ~]# 

通常であればfirewallコマンドで通信の許可を追加しますがWSLCentOS7にはインストールされてはいないようです

[root@lifebook ~]# firewall-cmd --add-port=10051/tcp --zone=public --permanent
-bash: firewall-cmd: command not found
[root@lifebook ~]# firewall
-bash: firewall: command not found

インストールされていないため今回は追加しませんが通常のCentOS7にて許可設定するポートとプロトコルを記載しておきます。
10050/tcp : Zabbix Server:Zabbix Agentのパッシブ監視通信
10051/tcp : Zabbix Agent:Serverのアクティブ監視通信
http : クライアント:Zabbix WebUIのHTTP通信

ここまで来たらサービスを起動するだけ。

[root@lifebook ~]# systemctl enable httpd
[root@lifebook ~]# systemctl enable zabbix-server
[root@lifebook ~]# systemctl start httpd
[root@lifebook ~]# systemctl start zabbix-server
[root@lifebook ~]# systemctl enable zabbix-agent
[root@lifebook ~]# systemctl start zabbix-agent
[root@lifebook ~]#

起動したらhttp://localhost/zabbixか<サーバーのIP/zabbix>でアクセスする。
image.png
インストールされているモジュールすべてOKになっているか確認
image.png
Configure DB connectionの画面が出てきたら、データベース名とユーザー名、パスワードを入力してNextStep
image.png
何回かNextStepをクリックしてこの画面が出てきたらfinish
image.png

ログイン画面に遷移したら以下のログイン情報を使用してログイン
ID
ID:Admin
PW:zabbix

ダッシュボードが表示されたら構築完了です。

image.png

画像ではすでに日本語化されていますが日本語化する方法は
右上の人物アイコンをクリックして「Language」の項目を「Japanese (ja_JP)」に指定し、
「Update」をクリックします。
その後日本語化されていれば完了です。

参考にしたサイト

敬称略
電算星組:Zabbix 4.4 インストール手順(CentOS7 / Apache2.4 / PHP5.4 / MariaDB5.5)
@unbabel:はじめての MySQL
@pugiemonn:よく使うMariaDB か MySQLのSQLコマンドメモ
etuts+:MariaDB テーブル管理、バックアップ等の基本操作コマンド
DBonline:テーブルの一覧とテーブルに関する情報を取得する
@RubiLeah-com

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?