(CentOS7)インターネット上のWebサーバを自宅のZabbxサーバから監視する

何をするか

自宅環境にZabbixサーバを構築して、
インターネット(AWS)上のWebサーバを監視する。

Zabbixサーバ:CentOS 7.7
Zabbxエージェント:Ubuntu 16.04.5、CentOS 7.3
Zabbixバージョン:3.4
↓ざっくり構成図

インターネット上にZabbixサーバを置く上での知っておくこと

上記の図の通りインターネット経由で監視を行うので下記のセキュリティ対策を実施。

・インターネット上のzabbxエージェントはFW(AWSならセキュリティグループ)の受信ルールで、zabbixの通信については自宅グローバルIPからのみ許可する設定を追加する。
・自宅のzabbixサーバからの監視についてはFWのステートフルインスペクションによって外部からのアクセス許可をする必要がなくなる。
・zabbixエージェントからの通信(トラップ等)が必要な場合は、FW(AWSならセキュリティグループ)の送信ルールで、zabbixの通信については自宅グローバルIPへのみ許可する設定を追加する。

zabbixサーバ(CentOS7)

zabbixサーバを動かすのに必要なパッケージをインストール
バージョンによってはzabbix-getは個別でインストールする必要ないかも。
3.4ではなかったので個別にインストールしてます。

yum -y update
yum -y install httpd-devel php php-devel php-pdo php-mysqlnd php-mbstring php-mcrypt php-gd php-pear php-pecl-apc-devel zlib-devel mariadb-devel mariadb-server
rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm
yum -y install zabbix-server-mysql zabbix-web-mysql zabbix-web-japanese
yum -y install zabbix-agent
wget http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-get-3.4.15-1.el7.x86_64.rpm
rpm -ivh zabbix-get-3.4.15-1.el7.x86_64.rpm

MySQLのパスワードポリシーの無効化と初期設定

vi /etc/my.cnf
----
validate-password=OFF
----
mysql_secure_installation
Change the root password? [Y/n] Y
Enter password:
Retype new password:
Remove anonymous users? [Y/n] Y
Disallow root login remotely? [Y/n] n
Remove test database and access to it? [Y/n] Y
Reload privilege tables now? [Y/n] Y

zabbixDBの作成、zabbixユーザへの権限割当

systemctl start mariadb
mysql -u root -p
> CREATE DATABASE zabbix character set utf8 collate utf8_bin;
> grant all on zabbix.* to zabbix@localhost identified by ‘pass’;
> FLUSH PRIVILEGES;
> \q

MySQLのPWを記載

vi /etc/zabbix/zabbix_server.conf
----
DBPassword=password
----

起動、自動起動設定

systemctl enable mariadb
systemctl start zabbix-server
systemctl enable zabbix-server
systemctl start httpd
systemctl enable httpd

zabbixデータベースをインポート

cd /usr/share/doc/zabbix-server-mysql-3.4.15/
zcat create.sql.gz | mysql -uzabbix -p zabbix
Enter password:

zabbix→mysqlのアクセス権割当

chmod 777 /var/lib/mysql/
chmod 777 /var/lib/mysql/mysql.sock

FWにてhttpを許可する(WebUIへのアクセス用)

firewall-cmd --add-service=http --zone=public --permanent
firewall-cmd --reload

zabbixエージェント(Ubuntu)

zabbixエージェントを動かすのに必要なパッケージをダウンロード

wget http://repo.zabbix.com/zabbix/3.4/ubuntu/pool/main/z/zabbix-release/zabbix-release_3.4-1+bionic_all.deb
ls -l
----
-rw-r--r--  1 root    root    4120 Mar 23 09:43 zabbix-release_3.4-1+bionic_all.deb
----

パッケージのダウンロード元を追加

vi /etc/apt/sources.list.d/zabbix.list
----
deb http://us.archive.ubuntu.com/ubuntu/ bionic main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ bionic main universe
deb http://us.archive.ubuntu.com/ubuntu/ bionic-security main universe
deb http://us.archive.ubuntu.com/ubuntu/ bionic-updates main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-security main universe
deb-src http://us.archive.ubuntu.com/ubuntu/ bionic-updates main universe
----

zabbixエージェントをインストール

dpkg -i zabbix-release_3.4-1+bionic_all.deb
apt update
apt -y install zabbix-agent

zabbixサーバのIP、ホスト名を記載
※ここで設定するHostnameはWebUI側でも同じホスト名にしないと動作しないので注意!!

vi /etc/zabbix/zabbix_agentd.conf
----
Server=1.1.1.1
ServerActive=1.1.1.1
Hostname=WebSV
----

起動、自動起動設定

systemctl start zabbix-agent
systemctl enable zabbix-agent
firewall-cmd --add-port=10050/tcp --zone=public --permanent
firewall-cmd --reload

必要に応じてルート権限割り当て(不要なら0)

vi /etc/zabbix/zabbix_agentd.conf
----
AllowRoot=1
----

zabbixエージェント(CentOS7)

zabbixエージェントのパッケージのダウンロード元を追加、インストール

rpm -ivh http://repo.zabbix.com/zabbix/3.4/rhel/7/x86_64/zabbix-release-3.4-1.el7.centos.noarch.rpm
yum install zabbix-agent

zabbixサーバのIP、ホスト名を記載
※ここで設定するHostnameはWebUI側でも同じホスト名にしないと動作しないので注意!!

vi /etc/zabbix/zabbix_agentd.conf
----
Server=1.1.1.1
ServerActive=1.1.1.1
Hostname=WebSV
----

起動、自動起動設定

systemctl start zabbix-agent
systemctl enable zabbix-agent
firewall-cmd --add-port=10050/tcp --zone=public --permanent
firewall-cmd --reload

必要に応じてルート権限割り当て(不要なら0)

vi /etc/zabbix/zabbix_agentd.conf
----
AllowRoot=1
----

その他、ログ関連

インターネット上のWebサーバでポート番号10050を開ける必要があります。
ZabbixのWebUI設定方法などはネット上に沢山あるので割愛。
うまく動作しない場合は各種ログを確認すること

/var/log/zabbix-server/zabbix_server.log
/var/log/zabbix-agent/zabbix_agentd.log