KUSANAGI for Vagrantで Zabbix 4.0を動かしてみる
祝Zabbix4.0リリース
さて、無事Zabbix4.0が10/1(現地時間)にリリースされましたね。10/2に @kuryuu タンが世界最速のリリース記念! Zabbix 4.0 体験会をやってましたね。さすが @kuryuu タン、リリースノートの和訳もバッチリです。
ということで、KUSANAGIでもZabbix4.0入れていきたいと思うんですけどね。最近触ってなかったので、KUSANAGI for Vagrantでやってみようかと思います。手動だと2時間で終わったのに、Vagrantファイルに書くと4時間くらい掛かったという大変さですが、皆様のお役に立てればと思います。
KUSANAGI for Vagrantとは
KUSANAGI for Vagrantは、Hashicorp製の仮想機械を構築するためのソフトウェアVagrant上でKUSANAGIを動かすモノです。Vagrantは、Windows10などではVirtualBoxという仮想マシンへインストールできたりしますが、LinuxではKVMに入れたりも出来ますね。
今回は、Windows上のVirtualBoxとVagrantを使います。KUSANAGI for Vagrantの手順に従い、Windows用のVirtualBoxとVagrantを入れましょう。
Vagrantの実行
では、Vagrantファイルの用意をします。みなさん、WSL(Windows Subsystem for Linux)はインストールされていると思いますので、WSLから操作します。え?WSL入れてない?Windows StoreでUbuntuで検索して、インストールして、起動して、ユーザ名とパスワードを入力するだけで大丈夫ですよ。
さて、WSL(Ubuntu)を起動して、以下のコマンドを入力してください。
mkdir -p /mnt/c/Users/ユーザ名/Vagrant/kusanagi cd /mnt/c/Users/ユーザ名/Vagrant/kusanagi vagrant init yuya_tajima/kusanagi
これで、Vagrantの設定及び、Vagrantファイルのテンプレートが作成されます。
Vagrantファイルの内容を、viなどで以下のように変更します。
# -*- mode: ruby -*-
# vi: set ft=ruby :
# All Vagrant configuration is done below. The "2" in Vagrant.configure
# configures the configuration version (we support older styles for
# backwards compatibility). Please don't change it unless you know what
# you're doing.
Vagrant.configure("2") do |config|
# The most common configuration options are documented and commented below.
# For a complete reference, please see the online documentation at
# https://docs.vagrantup.com.
# Every Vagrant development environment requires a box. You can search for
# boxes at https://vagrantcloud.com/search.
config.vm.box = "yuya_tajima/kusanagi"
# Disable automatic box update checking. If you disable this, then
# boxes will only be checked for updates when the user runs
# `vagrant box outdated`. This is not recommended.
# config.vm.box_check_update = false
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine. In the example below,
# accessing "localhost:8080" will access port 80 on the guest machine.
# NOTE: This will enable public access to the opened port
# config.vm.network "forwarded_port", guest: 80, host: 8080
config.vm.network "forwarded_port", guest: 22, host: 10022, id: "ssh"
# Create a forwarded port mapping which allows access to a specific port
# within the machine from a port on the host machine and only allow access
# via 127.0.0.1 to disable public access
# config.vm.network "forwarded_port", guest: 80, host: 8080, host_ip: "127.0.0.1"
# Create a private network, which allows host-only access to the machine
# using a specific IP.
# config.vm.network "private_network", ip: "192.168.33.10"
config.vm.network "private_network", ip: "192.168.33.10"
# Create a public network, which generally matched to bridged network.
# Bridged networks make the machine appear as another physical device on
# your network.
# config.vm.network "public_network"
# Share an additional folder to the guest VM. The first argument is
# the path on the host to the actual folder. The second argument is
# the path on the guest to mount the folder. And the optional third
# argument is a set of non-required options.
# config.vm.synced_folder "../data", "/vagrant_data"
# Provider-specific configuration so you can fine-tune various
# backing providers for Vagrant. These expose provider-specific options.
# Example for VirtualBox:
#
config.vm.provider "virtualbox" do |vb|
# Display the VirtualBox GUI when booting the machine
# vb.gui = true
# Customize the amount of memory on the VM:
vb.memory = "4096"
end
#
# View the documentation for the provider you are using for more
# information on available options.
# Enable provisioning with a shell script. Additional provisioners such as
# Puppet, Chef, Ansible, Salt, and Docker are also available. Please see the
# documentation for more information about their specific syntax and use.
# config.vm.provision "shell", inline: <<-SHELL
# apt-get update
# apt-get install -y apache2
# SHELL
config.vm.provision "shell", inline: <<-SHELL
export TERM=xterm
export KUSANAGI_PASSWORD=`mkpasswd -l 20`
export DBROOTPASS=`mkpasswd -s 0 -l 20`
export SITE_DOMAIN=zabbix.localdomain
export DBNAME=`mkpasswd -s 0 -l 10`
export DBUSER=`mkpasswd -s 0 -l 10`
export DBPASS=`mkpasswd -s 0 -l 20`
export EMAILOPTION=--no-email
sudo yum install -y https://repo.zabbix.com/zabbix/4.0/rhel/7/x86_64/zabbix-release-4.0-1.el7.noarch.rpm
sudo yum clean all
echo yum update
sudo yum -y --enablerepo=remi,remi-php56 update 2>&1 > /dev/null
yes 1 | sudo kusanagi init --tz tokyo --lang en --keyboard en --passwd $KUSANAGI_PASSWORD --no-phrase --dbrootpass "$DBROOTPASS" --php7 --nginx
sudo kusanagi provision --lamp --fqdn $SITE_DOMAIN $EMAILOPTION --dbname $DBNAME --dbuser $DBUSER --dbpass "$DBPASS" zabbix
sudo yum --enablerepo=remi,remi-php56 install -y zabbix-server-mysql zabbix-web-mysql zabbix-agent zabbix-web-japanese
sleep 1
echo "alter database $DBNAME character set utf8 collate utf8_bin;"| mysql -uroot -p$DBROOTPASS $DBNAME
SQLFILE=$(ls -t /usr/share/doc/zabbix-server-mysql-4*/create.sql.gz | head -1)
zcat $SQLFILE | mysql -uroot -p$DBROOTPASS $DBNAME
(echo php_value[max_execution_time] = 300
echo php_value[max_input_time] = 300
echo php_value[date.timezone] = Asia/Tokyo) | sudo tee -a /etc/php7-fpm.d/www.conf
sudo cp -pr /usr/share/zabbix/* /home/kusanagi/zabbix/DocumentRoot/
grep -v -e DATABASE -e USER -e PASSWORD /usr/share/zabbix/conf/zabbix.conf.php.example |
sudo tee /etc/zabbix/web/zabbix.conf.php
(echo "\\\$DB\['DATABASE'] = '$DBNAME'\;"
echo "\\\$DB\['USER'] = '$DBUSER'\;"
echo "\\\$DB\['PASSWORD'] = '$DBPASS'\;") | sudo tee -a /etc/zabbix/web/zabbix.conf.php
#sed -e "s/\(DB\[\'DATABASE\'\]\s\+=\s\).*;/\\1 \'$DBNAME\';/" \
# -e "s/\(DB\[\'USER\'\]\s\+=\s\).*;/\\1 \'$DBUSER\';/" \
# -e "s/\(DB\[\'PASSWORD\'\]\s\+=\s\).*;/\\1 \'$DBPASS\';/" \
# /usr/share/zabbix/conf/zabbix.conf.php.example | sudo tee /etc/zabbix/web/zabbix.conf.php
(echo DBName=$DBNAME
echo DBUser=$DBUSER
echo DBPassword=$DBPASS ) | sudo tee -a /etc/zabbix/zabbix_server.conf
sudo chown -R httpd:www /home/kusanagi/zabbix/DocumentRoot/ /etc/zabbix/web
sudo chown :www /var/lib/php/*
sudo systemctl start zabbix-server zabbix-agent
sudo systemctl enable zabbix-server zabbix-agent
echo "KUSANAGI password: $KUSANAGI_PASSWORD"
sudo reboot
SHELL
end
上記のファイルで変更すべきは以下の部分です。
- config.vm.network “forwarded_port”, guest: 22, host: 10022, id: “ssh”
sshのポートフォワードを行う設定になります。10022の部分は任意の使用していないポートを指定してください。 - config.vm.network “private_network”, ip: “192.168.33.10”
内部的なネットワークIPアドレスになります。任意の使用していないローカルIPアドレスを指定してください。 - SITE_DOMAIN=zabbix.localdomain
作成するZabbixサーバのホスト名となります。このホスト名を使用してZabbixサーバにアクセスする必要があります。
必要なパスワード、DBの設定などはmkpasswdコマンドを使用してランダム生成するようにしています。
その後、以下のコマンドを打って、ややしばらくお待ち下さい。
vagrant up
初回はVagrantのイメージのダウンロードを行うため、しばらく時間がかかります。
また、yum update や kusanagi initで、サーバ暗号鍵やLet’s Encryptで使用するcertbotの更新のため数十分かかります。
終了時に、以下のメッセージが出力されますが、これが自動生成されたkusanagiユーザのパスワードになりますので、コピーしておいてください。
default: KUSANAGI password: Yxh4qtxygKfwe!6asiwk
Zabbixサーバへのwebアクセス
作成したサーバにアクセスするには、hostsファイルを編集する必要があります。
Windowsボタンを右クリックして、Windows Powershell(管理者用)を起動します。

ここで、以下のコマンドを実行してhostsファイルをメモ帳で開きます。
PS C:\WINDOWS\system32> .\notepad.exe .\drivers\etc\hosts
以下のエントリを末尾に追加してください。ここで指定するIPアドレスとホスト名は、Vagrantファイルに指定したものと同じものにしてください。
192.168.33.10 zabbix.localdomain
ブラウザで、http://zabbix.localdomain/ にアクセスすると以下の画面が表示されると思います。

デフォルトのユーザ「Admin」、パスワード「zabbix」を入力してログインしてください。
ZabbixサーバへのSSHアクセス
sshでアクセスするためには、sshの設定が必要です。以下のコマンドを実行し、~/.ssh/configの内容を変更してください。
$ mkdir ~/.ssh $ chmod 700 ~/.ssh $ vagrant.exe ssh-config | tee ~/.ssh/config Host default HostName 127.0.0.1 User vagrant Port 10022 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile C:/Users/ユーザ名/.vagrant.d/boxes/yuya_tajima-VAGRANTSLASH-kusanagi/0.1/virtualbox/vagrant_private_key IdentitiesOnly yes LogLevel FATAL $ chmod 600 ~/.ssh/config $ cp /mnt/c/Users/ユーザ名/.vagrant.d/boxes/yuya_tajima-VAGRANTSLASH-kusanagi/0.1/virtualbox/vagrant_private_key ~/.ssh/kusanagi_private_key $ vi ~/.ssh/config $ cat ~/.ssh/config Host default HostName 127.0.0.1 User vagrant Port 10022 UserKnownHostsFile /dev/null StrictHostKeyChecking no PasswordAuthentication no IdentityFile /home/ユーザ名/.ssh/kusanagi_private_key IdentitiesOnly yes LogLevel FATAL
ここで、以下のコマンドを打つことで、作成したZabbixサーバにVagrantユーザで接続できます。
$ ssh default
Last login: Wed Oct 3 12:03:27 2018 from gateway
__ ____ _______ ___ _ _____ __________
/ //_/ / / / ___// | / | / / | / ____/ _/
/ ,< / / / /\__ \/ /| | / |/ / /| |/ / __ / /
/ /| / /_/ /___/ / ___ |/ /| / ___ / /_/ // /
/_/ |_\____//____/_/ |_/_/ |_/_/ |_\____/___/
Version 8.4.0-3, Powered by Prime Strategy.
[vagrant@kusanagi71 ~]$
rootになるには、sudo -i などを使用してください。
最後に
これで、KUSANAGI for VagrantでZabbix Server4.0のインストールは完了です。あとは、新機能を思う存分楽しみましょう。
また、Vagrantファイルのスクリプトを参考にすれば、他のクラウド環境で動作するKUSANAGIでも同様に構築できます。応用して楽しいZabbixライフを送りましょう。

