【備忘録】Zabbix使ってみたかったけど、時間がかかったのでとりあえず入れてみる
※追記、2020/09/06
そういえば、Zabbixが何ぞや的お話書いてなかった。 簡単に書くと、サーバ/NW装置の管理/監視のためのフリーツールです。
※追記、2020/09/07
zabbix-agent2なんてものあったんですね。 そっちの方が良さげなので情報をUpdate。(但し詳細設定は書かず)
前回の検証環境から。
【備忘録】ESXiを使って宅内検証環境つくる - すらすと’s 生活の跡
環境
- ゲストOS: ArchLinux x86_64 (SELinux無効 (おい※))、logArchと命名
- Web3層部分: LEMP Stack (Linux / nginx / MySQL (MariaDB) / PHP)
- 導入バージョンはArchのデフォルトリポジトリを使うので大体最新版
- Windows Terminal使用、Ubuntu18.04 (WSL2)にてSSH接続
※SELinuxはArchのデフォで無効、なので別の機会に勉強したい
やりたいこと
- ESXi関連のログ収集とステータス管理
- Raspberry Piのステータス管理
- ログやステータスのWebGUI表示
- SSLは今回なし、別の機会に導入を検討する (導入するとh(ry)
導入
以下パッケージを導入、何も考えず動かせる(だろう)ものを入れていく。
# pacman -S nginx mariadb zabbix-server zabbix-agent2 zabbix-frontend-php php-fpm
nginx
まずはnginxの連携やディレクトリ等の設定を修正する。 今回の修正箇所は以下の通り。
※<VM_Network-Range>: VM Network側NICに割り当てたAddress Range、"0.0.0.0/0"形式で指定
※<HostOnly_Network-Range>: HostOnly Network側NICに割り当てたAddress Range、"0.0.0.0/0"形式で指定
events { worker_connections 512; } (略) http { (略) server { listen 80; server_name logArch; allow 127.0.0.1; allow <VM_Network-Range>; allow <HostOnly_Network-Range>; deny all; root /srv/http; index index.html index.htm index.php; (略) location / { } location /zabbix/ { root /srv/http/zabbix; } location ~ [^/]\.php(/|$) { fastcgi_pass unix:/run/php-fpm/php-fpm.sock; fastcgi_index index.php; include fastcgi.conf; } (略) } (略) }
設定が終わったらnginxを起動して、怒られないことを確認する。
systemctl start nginx.service
※設定がおかしければ起動できないので、journalctl -xe
で何が問題かを確認する。
最後に、/srv/http/index.php
に以下のコードを入れてPHPの動作を確認する。
タグにphpを入れ忘れると動かないので注意!! (n敗 | 5 < n < 20) なぜ入れ忘れるのか…。
<?php phpinfo(); ?>
MariaDB
次に、MariaDBの設定をArchWikiに沿って実施していく。 まずは初期設定投入と起動から。
引数のディレクトリを変えたりすると動かなくなることがあるのでコピペで投入したいところ。(1敗)
# mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql (略) # systemctl start mariadb.service
続いて、インストールの後処理を実施。 パスワードの変更やtest設定の削除を対話式で一度に処理できる。
# 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 haven't set the root password yet, you should just press enter here. Enter current password for root (enter for none): OK, successfully used password, moving on... Setting the root password or using the unix_socket ensures that nobody can log into the MariaDB root user without the proper authorisation. You already have your root account protected, so you can safely answer 'n'. Switch to unix_socket authentication [Y/n] n ... skipping. You already have your root account protected, so you can safely answer 'n'. Change the root password? [Y/n] y 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!
Zabbix-Frontend-PHP
ZabbixのArchWikiを見て設定をする。 まずはZabbixの格納先を整えていく。
ln -s /usr/share/webapps/zabbix /srv/http/zabbix
次にPHP設定だが、少し情報が古い / 一般的なので以下のように変更。
※open_basedir
に/tmp
を入れたくないお気持ちで外すとエラーを返されました。
そういうものですか?
※動かすための最低限の設定なのでもっとセキュアな設定にすべきとは思う。
open_basedir = /usr/share/webapps/:/srv/http/:/tmp/ cgi.fix_pathinfo=0 extension=bcmath extension=gd extension=gettext extension=mysqli extension=sockets post_max_size = 16M max_execution_time = 300 max_input_time = 300 date.timezone = Asia/Tokyo
続いてMariaDBの利用設定。エラーがなければ出力はない。
なお、create database
のcollate utf8_bin
はないと後々怒られが発生する。
Wikiにはないけど…。
※<DB_Password>: ZabbixがDB参照するときのパスワード
# mysql -u root -p -e "create database zabbix character set utf8 collate utf8_bin" # mysql -u root -p -e "grant all on zabbix.* to zabbix@localhost identified by '<DB_Password>'" # mysql -u zabbix -p zabbix < /usr/share/zabbix-server/mysql/schema.sql # mysql -u zabbix -p zabbix < /usr/share/zabbix-server/mysql/images.sql # mysql -u zabbix -p zabbix < /usr/share/zabbix-server/mysql/data.sql
なお、ここで設定した<DB_Password>は/etc/zabbix/zabbix_server.conf
にも設定した方が良さげ。
少なくともjournalのエラーは消える。
Wikiにも書いといてくださいよ…。 (他力本願)
DBPassword=<DB_Password>
続いて、ZabbixとMariaDBの連携サービスを立ち上げる。
systemctl start zabbix-server-mysql.service
適当なブラウザでhttp://<logArchのアドレス>/zabbix/
にアクセスすると、起動しているはず。
ここまでくれば、あとはCLI最終処理としてService起動設定を変更。
# systemctl enable nginx.service php-fpm.service mariadb.service zabbix-server-mysql.service zabbix-agent2.service
Zabbix WebGUI
以降、画面に従って設定を入れていく。
要件適合チェックはPHP LDAPがoffでWarningとなっているが、そもそもLDAPは使わないので無視でOK。
LDAPを使う人はPHPのモジュール設定を変えたりとか? 設定方法がわからない…。
MySQLの接続設定は、MariaDB設定で使ったDatabase Name / User / Passwordを設定する。 間違えるとここで怒られます。
なお、フラグは回収した模様。(1敗)
次に、バックエンドへ接続するための設定を実施する。 ここはデフォルトで問題ない。
設定が正常に入れればこの画面へ。 ここで最終確認し初期設定を完了、
しない。(大敗)
Serviceファイルの編集
ここでSystemdの仕様に転がされます。
php-fpm /usr read only
でgoogle先生に問い合わせると、どうやらSystemdの仕様で/usr
等々への書き込みがブロックされるらしい。
このせいでphp-fpmによるPHP関数の実行が阻害されていた模様。
[Service]
のProtectSystem=full
がこの仕様に該当するらしい。
セキュリティ上これは外すのは良くないのでどうにかしなければいけない。
だったら/usr
に作ってくれるなよな…とも思う。
ちなみに、journalログはこんな感じ。
※IPアドレスとか日時はぼかした。改行も適宜入れた。
mmm dd HH:MM:SS logArch nginx[2456]: yyyy/mm/dd MM:HH:SS [error] 2456#2456: *46 FastCGI sent in stderr: "PHP message: PHP Warning: file_put_contents(/usr/share/webapps/zabbix/conf/zabbix.conf.php): failed to open stream: Read-only file system in /usr/share/webapps/zabbix/include/classes/core/CConfigFile.php on line 190" while reading response header from upstream, client: <Client IP Address>, server: logarch, request: "POST /zabbix/setup.php HTTP/1.1", upstream: "fastcgi://unix:/run/php-fpm/php-fpm.sock:", host: "<Server IP Address>", referrer: "http://<Server IP Address>/zabbix/setup.php"
仕方がないので、Serviceファイルの上書きを実施する。
コマンドはsystemctl edit php-fpm
で、上書きしたい設定だけを記載していく。
※今回の場合、上書き設定のファイル実体は/etc/systemd/system/php-fpm.service.d/override.conf
に保存される。
[Service] ReadWritePaths=/usr/share/webapps/zabbix/conf /usr/share/webapps/zabbix/local/conf
無事完了。
ようやくログイン
ユーザは、Admin
、初期パスワードはzabbix
。
いえい。
agentが応答なしなのはまた別のお話。設定ろくに入れてないからね…。
所感
PHPが罠すぎるのでやっぱり好きになれない。
あと、日本語Wikiとかの情報が古い…。(他力本願)
ソース等
※(一部含まれていないかも、いろいろ調べたので…。)