ArchLinux 2017.10.01: Install Zabbix for web base system monitor

This article will describe installing Zabbix which is a web-base system monitor tool.

1 Install Zabbix

The following script will install Zabbix server and Zabbix agent.

  • MYSQL_PASSWD is a password of root user in MySQL.
  • ZABBIX_PASSWD is a password of zabbix user in MySQL.
  • You need to use "localhost" as Zabbix agent host name (Configuration -> Hosts -> Create Host).
#!/bin/sh

set -e

[ -z "${MYSQL_PASSWD}" ] && MYSQL_PASSWD=mysql
[ -z "${ZABBIX_PASSWD}" ] && ZABBIX_PASSWD=zabbix

mysql_install()
{
  sudo pacman -Sy --noconfirm mariadb

  # Install database.
  sudo mysql_install_db --user=mysql --basedir=/usr --datadir=/var/lib/mysql

  sudo systemctl enable mariadb
  sudo systemctl start mariadb

  # Password configuration.
  cat <<EOF | sudo mysql_secure_installation

y
${MYSQL_PASSWD}
${MYSQL_PASSWD}
n
y
y
y
EOF
}

php_install()
{
  # Enable PHP extension. But I have only tested creating host,
  # creating item, creating graph, and adding graph to dashboard.
  # If you think something wrong or need more feature like SNMP,
  # please enable extension and install php-xxx package.
  sudo pacman -Sy --noconfirm php
  sudo sed -i /etc/php/php.ini \
       -e 's/^;extension=pdo_mysql.so/extension=pdo_mysql.so/g' \
       -e 's/^;extension=mysqli.so/extension=mysqli.so/g' \
       -e 's/^;extension=bcmath.so/extension=bcmath.so/g' \
       -e 's/^;extension=gd.so/extension=gd.so/g' \
       -e 's/^;extension=sockets.so/extension=sockets.so/g'
}

zabbix_server_install()
{
  sudo pacman -Sy --noconfirm zabbix-server zabbix-frontend-php
  sudo systemctl enable zabbix-server-mysql

  # Create database table.
  cat <<EOF | sudo mysql -uroot -p${MYSQL_PASSWD}
create database zabbix character set utf8 collate utf8_bin;
grant all privileges on zabbix.* to zabbix@localhost identified by '${ZABBIX_PASSWD}';
exit
EOF
  for sql in schema.sql images.sql data.sql; do
    # shellcheck disable=SC2002
    cat /usr/share/zabbix-server/mysql/"${sql}" | \
      sudo mysql -uzabbix -p${ZABBIX_PASSWD} zabbix
  done

  sudo sed -e 's/# ListenPort=.*/ListenPort=10051/g' \
       -e "s/# DBPassword=.*/DBPassword=${ZABBIX_PASSWD}/g" \
       -i /etc/zabbix/zabbix_server.conf

  # For duplicated localhost definition of IPv4 and IPv6, disable IPv6.
  sudo sed -e 's;::1.*localhost.localdomain.*localhost;;g' -i /etc/hosts

  # Skip setup.php
  cat <<EOF | sudo tee /usr/share/webapps/zabbix/conf/zabbix.conf.php
<?php
// Zabbix GUI configuration file.
global \$DB;

\$DB['TYPE']     = 'MYSQL';
\$DB['SERVER']   = 'localhost';
\$DB['PORT']     = '0';
\$DB['DATABASE'] = 'zabbix';
\$DB['USER']     = 'zabbix';
\$DB['PASSWORD'] = '${ZABBIX_PASSWD}';

// Schema name. Used for IBM DB2 and PostgreSQL.
\$DB['SCHEMA'] = '';

\$ZBX_SERVER      = 'localhost';
\$ZBX_SERVER_PORT = '10051';
\$ZBX_SERVER_NAME = '';

\$IMAGE_FORMAT_DEFAULT = IMAGE_FORMAT_PNG;
?>
EOF

  sudo systemctl restart zabbix-server-mysql
}

apache_install()
{
  sudo pacman -Sy --noconfirm apache php-apache
  sudo systemctl enable httpd

  # PHP configuration.
  sudo sed -e 's/^LoadModule mpm_event_module/#LoadModule mpm_event_module/g' \
       -e 's/^#LoadModule mpm_prefork_module/LoadModule mpm_prefork_module/g' \
       -i /etc/httpd/conf/httpd.conf
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
LoadModule php7_module modules/libphp7.so
AddHandler php7-script php
Include conf/extra/php7_module.conf
EOF

  # https configuration.
  yes "" | sudo openssl req -new -x509 -nodes -newkey rsa:4096 -days 1095 \
                -keyout /etc/httpd/conf/server.key \
                -out /etc/httpd/conf/server.crt
  sudo sed -e 's/^#LoadModule ssl_module/LoadModule ssl_module/g' \
       -e 's/^#LoadModule socache_shmcb_module/LoadModule socache_shmcb_module/g' \
       -i /etc/httpd/conf/httpd.conf
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/httpd-ssl.conf
EOF

  # Zabbix server configuration.
  cat <<EOF | sudo tee -a /etc/httpd/conf/httpd.conf
Include conf/extra/zabbix-frontend-php.conf
EOF
  timezone=$(timedatectl | grep "Time zone:" | \
                awk -F':' '{ print $2 }' | awk '{ print $1 }')
  cat <<EOF | sudo tee /etc/httpd/conf/extra/zabbix-frontend-php.conf
Alias /zabbix /usr/share/webapps/zabbix

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

    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 date.timezone ${timezone}
</Directory>

<Directory ~ "^/usr/share/webapps/zabbix/(conf|app|include|local)/">
    Require all denied

    <files *.php>
        Require all denied
    </files>
</Directory>
EOF

  sudo systemctl restart httpd
}

zabbix_agent_install()
{
  # This Hostname is used for Host name in Configuration -> Hosts -> Create Host.
  sudo pacman -Sy --noconfirm zabbix-agent
  sudo sed -e "s/^Hostname=.*/Hostname=localhost/g" \
       -i /etc/zabbix/zabbix_agentd.conf
  sudo systemctl enable zabbix-agent
  sudo systemctl start zabbix-agent
}

zabbix_main()
{
  mysql_install
  php_install
  zabbix_server_install
  apache_install
  zabbix_agent_install
}

zabbix_main

2 Access to Zabbix

Access to Zabbix via http or https. If you use https, please allow certification file to exception.

http://<server>/zabbix
https://<server>/zabbix

Input "Admin" to Username, "zabbix" to Passowrd (This passowrd is not ZABBIX_PASSWD).

0001_Zabbix.png