zabbix installation

Zabbix Installation Script

Zabbix is an open-source monitoring software tool for networks, servers, virtual machines(VMs) and cloud services. It provides monitoring metrics of CPU load, Memory utilization disk space usage, networking and etc. It monitors operations on Linux, Windows, Mac and other popular operating systems, monitoring is only possible through agents. In Zabbix datum are stored in MySQL, MariaDB, Oracle, PostgreSQL, SQLite databases. In this post, Zabbix server and agent installation can be done through the bash script. This script is tested in Ubuntu 16.04(Xenial) and Ubuntu 18.04(bionic), and this will install Zabbix 4.2.1.

To view this in GitHub by
https://github.com/Eranachandran/Zabbix-Server-Installation

Zabbix-server installation and Configuring Settings for the Zabbix Web Interface

Run the following script to install a Zabbix agent on Zabbix server. after running that script, the Zabbix agent will be installed. After the Zabbix web interface configuration should be configured manually. Ref:
https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-zabbix-to-securely-monitor-remote-servers-on-ubuntu-16-04

################################################################################################################################################
# Script Name: zabbix_server_install.sh
# Author: Eranachandran
# Date : 30-04-2019
# Description: The following script will install zabbix server
# In machine already mysql installed, usage: sudo bash zabbix_server_install.sh --host_password --mysql_db_user_name --mysql_db_password  --zabbix_database_new_password 
# In machine already mariadb installed, usage: sudo bash zabbix_server_install.sh --host_password --mariadb_db_user_name --maria_db_password  --zabbix_database_new_password 
# In machine already mariadb or not mysql installed usage:sudo bash zabbix_server_install.sh --host_password --database_new_root_password  --zabbix_database_new_password 
################################################################################################################################################
#! /bin/bash

ARGS=$(getopt -o a:b:c:d:e:f:g -l "host_password:,mysql_db_user_name:,mysql_db_password:,mariadb_db_user_name:,maria_db_password:,database_new_root_password:,zabbix_database_new_password:" -- "[email protected]");

eval set -- "$ARGS";

while true; do
  case "$1" in
    -a|--host_password)
      shift;
      if [ -n "$1" ]; then
        host_password=$1;
        shift;
      fi
      ;;
    -b|--mysql_db_user_name)
      shift;
      if [ -n "$1" ]; then
        mysql_db_user_name=$1;
        shift;
      fi
      ;;
   -c|--mysql_db_password)
      shift;
      if [ -n "$1" ]; then
        mysql_db_password=$1;
        shift;
      fi
      ;;
   -d|--mariadb_db_user_name)
      shift;
      if [ -n "$1" ]; then
        mariadb_db_user_name=$1;
        shift;
      fi
      ;;
   -e|--maria_db_password)
      shift;
      if [ -n "$1" ]; then
        maria_db_password=$1;
        shift;
      fi
      ;;
   -f|--database_new_root_password)
      shift;
      if [ -n "$1" ]; then
        database_new_root_password=$1;
        shift;
      fi
      ;;
   -g|--zabbix_database_new_password)
      shift;
      if [ -n "$1" ]; then
        zabbix_database_new_password=$1;
        shift;
      fi
      ;;
    --)
      shift;
      break;
      ;;
  esac
done



echo "$host_password" | sudo -S sudo apt -y update

# install the PHP modules Zabbix needs
echo "$host_password" | sudo -S apt -y install php7.0-xml php7.0-bcmath php7.0-mbstring

#checking ubuntu Version 16.04 or 18.04 and  Install Zabbix repository
if [ $(cat /etc/os-release  | awk 'NR==2 {print $3}'| grep -i -o xenial) ==  "Xenial" ]; then
  echo "$host_password" | sudo -S wget https://repo.zabbix.com/zabbix/4.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.2-1+xenial_all.deb
  echo "$host_password" | sudo -S dpkg -i zabbix-release_4.2-1+xenial_all.deb
elif [ $(cat /etc/os-release  | awk 'NR==2 {print $3}'| grep -i -o bionic) ==  "Bionic" ]; then
  echo "$host_password" | sudo -S wget https://repo.zabbix.com/zabbix/4.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.2-1+bionic_all.deb
  echo "$host_password" | sudo -S dpkg -i zabbix-release_4.2-1+bionic_all.deb
fi

#updating packages
echo "$host_password" | sudo -S sudo apt -y update

# Checking Mysql Installed or not, if installed zabbix database created in mysql
mysql=$(dpkg -l | grep "mysql-server")

if [ "$?" ==  0 ]; then
#Install Zabbix server, frontend agent
echo "$host_password" | sudo -S apt -y install zabbix-server-mysql zabbix-frontend-php zabbix-agent
echo "create database zabbix character set utf8 collate utf8_bin;" | mysql -h localhost -u $mysql_db_user_name -p$mysql_db_password

#granting Preivileges for zabbix database, Here '%' is given, so all remote host will access zabbix database with password. So don't grant like this. try to allow whitelisted hosts only
echo  "grant all privileges on zabbix.* to [email protected]'%' identified by '$zabbix_database_new_password';"  | mysql -h localhost -u $mysql_db_user_name -p$mysql_db_password
echo "flush privileges;" | mysql -h localhost -u $mysql_db_user_name -p$mysql_db_password

#here bind-address is 0.0.0.0, so all remote host will access database server with password. So don't grant like this. try to allow whitelisted hosts only 
echo "$host_password" | sudo -S sed -i  "s/^\(bind-address\s*=\).*/\1 0.0.0.0/" /etc/mysql/mysql.conf.d/mysqld.cnf
echo "$host_password" | sudo -S service mysql restart

#Run the following command to set up the schema and import the data into the zabbix database
echo "$host_password" | sudo -S zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix -p$zabbix_database_new_password
echo "$host_password" | sudo -S sed -i  "s/^\(\s*#\s*DBPassword=\).*/\DBPassword=/"  /etc/zabbix/zabbix_server.conf
echo "$host_password" | sudo -S sed -i  "s/^\(DBPassword\s*=\).*/\1 ${zabbix_database_new_password}/" /etc/zabbix/zabbix_server.conf

#Edit file /etc/zabbix/apache.conf, uncomment and set the right timezone for you.
echo "$host_password" | sudo -S  sed -i  "s/^\(\s*#\s*php_value date.timezone Europe\/Riga\).*/\\tphp_value date.timezone Asia\/Kolkata/" /etc/zabbix/apache.conf
fi

# Checking MariaDB Installed or not, if installed zabbix database created in mariadb
mariadb=$(dpkg -l | grep mariadb-server)
if [ "$?" ==  0 ]; then
#Install Zabbix server, frontend agent
echo "$host_password" | sudo -S apt -y install zabbix-server-mysql zabbix-frontend-php zabbix-agent
echo "create database zabbix character set utf8 collate utf8_bin;" | mysql -h localhost -u $mariadb_db_user_name -p$maria_db_password

#granting Preivileges for zabbix database, Here '%' is given, so all remote host will access zabbix database with password. So don't grant like this. try to allow whitelisted hosts only
echo  "grant all privileges on zabbix.* to [email protected]'%' identified by '$zabbix_database_new_password';"  | mysql -h localhost -u $mariadb_db_user_name -p$maria_db_password
echo "flush privileges;" | mysql -h localhost -u $mariadb_db_user_name -p$maria_db_password

#here bind-address is 0.0.0.0, so all remote host will access database server with password. So don't grant like this. try to allow whitelisted hosts only 
echo "$host_password" | sudo -S sed -i  "s/^\(bind-address\s*=\).*/\1 0.0.0.0/" /etc/mysql/mariadb.conf.d/50-server.cnf
echo "$host_password" | sudo -S service mysql restart

#Run the following command to set up the schema and import the data into the zabbix database
echo "$host_password" | sudo -S zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix -p$zabbix_database_new_password
echo "$host_password" | sudo -S sed -i  "s/^\(\s*#\s*DBPassword=\).*/\DBPassword=/"  /etc/zabbix/zabbix_server.conf
echo "$host_password" | sudo -S sed -i  "s/^\(DBPassword\s*=\).*/\1 ${zabbix_database_new_password}/" /etc/zabbix/zabbix_server.conf

#Edit file /etc/zabbix/apache.conf, uncomment and set the right timezone for you.
echo "$host_password" | sudo -S  sed -i  "s/^\(\s*#\s*php_value date.timezone Europe\/Riga\).*/\\tphp_value date.timezone Asia\/Kolkata/" /etc/zabbix/apache.conf
fi

#if mysql or mariadb not installed mariadb will be installed and zabbix database created in mariadb 
db_install_check=$(dpkg -l | grep mariadb-server || dpkg -l | grep mysql-server)

if [ "$?" !=  0 ]; then
#Install Zabbix server, frontend agent
echo "$host_password" | sudo -S debconf-set-selections <<< 'mysql-server mysql-server/root_password password $database_new_root_password'
echo "$host_password" | sudo -S debconf-set-selections <<< 'mysql-server mysql-server/root_password_again password  $database_new_root_password'
echo "$host_password" | sudo -S apt -y install zabbix-server-mysql zabbix-frontend-php zabbix-agent
echo "create database zabbix character set utf8 collate utf8_bin;" | mysql -h localhost -u root -p$database_new_root_password

#granting Preivileges for zabbix database, Here '%' is given, so all remote host will access zabbix database with password. So don't grant like this. try to allow whitelisted hosts only 
echo  "grant all privileges on zabbix.* to [email protected]'%' identified by '$zabbix_database_new_password';"  | mysql -h localhost -u root -p$database_new_root_password
echo "flush privileges;" | mysql -h localhost -u root -p$database_new_root_password

#here bind-address is 0.0.0.0, so all remote host will access database server with password. So don't grant like this. try to allow whitelisted hosts only 
echo "$host_password" | sudo -S sed -i  "s/^\(bind-address\s*=\).*/\1 0.0.0.0/"  /etc/mysql/mariadb.conf.d/50-server.cnf
echo "$host_password" | sudo -S service mysql restart

#Run the following command to set up the schema and import the data into the zabbix database
echo "$host_password" | sudo -S zcat /usr/share/doc/zabbix-server-mysql*/create.sql.gz | mysql -uzabbix -p zabbix -p$zabbix_database_new_password
echo "$host_password" | sudo -S sed -i  "s/^\(\s*#\s*DBPassword=\).*/\DBPassword=/"  /etc/zabbix/zabbix_server.conf
echo "$host_password" | sudo -S sed -i  "s/^\(DBPassword\s*=\).*/\1 ${zabbix_database_new_password}/" /etc/zabbix/zabbix_server.conf

#Edit file /etc/zabbix/apache.conf, uncomment and set the right timezone for you.
echo "$host_password" | sudo -S echo "$host_password" | sudo -S  sed -i  "s/^\(\s*#\s*php_value date.timezone Europe\/Riga\).*/\\tphp_value date.timezone Asia\/Kolkata/" /etc/zabbix/apache.conf
fi

#Zabbix server and agent processes
echo "$host_password" | sudo -S systemctl restart zabbix-server zabbix-agent apache2
echo "$host_password" | sudo -S systemctl enable zabbix-server zabbix-agent apache2

#Check zabbix status
zabbix_status=$(echo "$host_password" | sudo -S systemctl status zabbix-server |  awk 'NR==3')
echo "$zabbix_status"

Zabbix Agent Instalation and Configuration

After Configuring zabbix server, run the following script to install zabbix agent

################################################################################################################################################
# Script Name: zabbix_agent_install.sh
# Author: Eranachandran
# Date : 30-04-2019
# Description: The following script will install zabbix agent
# usage: sudo bash zabbix_agent_install.sh --host_password  --zabbix_server_ip
################################################################################################################################################
#! /bin/bash

ARGS=$(getopt -o a:b -l "host_password:,zabbix_server_ip:" -- "[email protected]");

eval set -- "$ARGS";

while true; do
  case "$1" in
    -a|--host_password)
      shift;
      if [ -n "$1" ]; then
        host_password=$1;
        shift;
      fi
      ;;
    -b|--zabbix_server_ip)
      shift;
      if [ -n "$1" ]; then
        zabbix_server_ip=$1;
        shift;
      fi
      ;;
    --)
      shift;
      break;
      ;;
  esac
done


#checking ubuntu Version 16.04 or 18.04 and  Install Zabbix repository
if [ $(cat /etc/os-release  | awk 'NR==2 {print $3}'| grep -i -o xenial) ==  "Xenial" ]; then
  echo "$host_password" | sudo -S wget https://repo.zabbix.com/zabbix/4.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.2-1+xenial_all.deb
  echo "$host_password" | sudo -S dpkg -i zabbix-release_4.2-1+xenial_all.deb
elif [ $(cat /etc/os-release  | awk 'NR==2 {print $3}'| grep -i -o bionic) ==  "Bionic" ]; then
  echo "$host_password" | sudo -S wget https://repo.zabbix.com/zabbix/4.2/ubuntu/pool/main/z/zabbix-release/zabbix-release_4.2-1+bionic_all.deb
  echo "$host_password" | sudo -S dpkg -i zabbix-release_4.2-1+bionic_all.deb
fi

#updating packages
echo "$host_password" | sudo -S sudo apt -y update

#install the Zabbix agent
echo "$host_password" | sudo -S apt-get -y install zabbix-agent

#adding zabbix server IP
echo "$host_password" | sudo -S sed -i  "s/^\(Server\s*=\).*/\1 ${zabbix_server_ip}/" /etc/zabbix/zabbix_agentd.conf


#starting Zabbix agent
echo "$host_password" | sudo -S systemctl start zabbix-agent
echo "$host_password" | sudo -S systemctl enable zabbix-agent

#check zabbix agent status
zabbix_agent_status=$(echo "$host_password" | sudo -S systemctl status zabbix-agent |  awk 'NR==3')
echo "$zabbix_agent_status"

Monitoring, getting monitor data, mail notification and other monitoring services need to be configured through Zabbix dashboard.

use the following link for configuring hosts through zabbix dashboard
https://www.zabbix.com/documentation/3.0/manual/config/hosts/host

Posts created 13

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top