#!/bin/bash #################################################################################################### # First Created: 11012021 Author: Allan Desc: Installs Zabbix on Ubuntu requires Nginx and MariaDB # #################################################################################################### # # Are we root # if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit ; fi # # Are we in the right directory # scriptdir="zabbix-install" && whereami=$(pwd |awk -F'/' '{print $NF}') if [ "$whereami" != "$scriptdir" ]; then printf "\nWrong directory! Script must be run from %s\n\n" "$scriptdir"; exit 1; fi # # Define variables and functions # line () { printf -- '-%.0s' {1..50}; printf '\n'; } version=$(tail -1 version) ubuntuversion=$(lsb_release -rs |tail -1) downloadurl="https://repo.zabbix.com/zabbix/$version/ubuntu/pool/main/z/zabbix-release/" release=$(curl -s https://repo.zabbix.com/zabbix/$version/ubuntu/pool/main/z/zabbix-release/ |grep .deb |grep -v latest |sed 's/>/y/g' |awk -F 'y' '{print $2}' |sed 's/<\/a//g' |grep $ubuntuversion) nginxfiles="/etc/nginx/nginxsnippets" # # Function title # title () { printf "\nZabbix install script V1.0\n\n"; } # # Function usage # usage () { clear ; printf -- "\n" printf -- "zabbixinstall \n\n" printf -- "Usage: \n" printf -- "./zabbixinstall [-n] \n" printf -- " [-p] \n" printf -- " [-help] \n\n" printf -- "Examples: \n" printf -- "./zabbixinstall -n zabbix.something.xyz -p zabbixdatabsepwd\n\n"; } # # Function installation check for success # serviceno () { printf "\n%s" $service ; printf " is not running cannot continue...\n\n"; } servicedead () { status=$(systemctl is-active $service); if [[ "$status" != "active" ]]; then serviceno ; exit; fi; } # # Function to check if a service is already running or not # inputcheck () { if [[ -z "$hostname" ]]; then usage ; printf "Error! Hostname Empty...\n\n" ; exit; fi if [[ -z "$zabbixpwd" ]]; then usage ; printf "Error! Database Password Empty...\n\n" ; exit; fi; } # # If Nginx and MariaDB is not running then die # service="mariadb" ; servicedead ; service="nginx" ; servicedead # # Display title # clear ; title # # Configure command line options # # Removed due to SC2199 and not really needed start # if [[ ! $@ =~ ^\-.+ ]]; then usage; fi # Removed due to SC2199 and not really needed stop while getopts "n:p:h:" option; do case $option in n) # hostname hostname=$(echo ${OPTARG} | tr '[:upper:]' '[:lower:]');; p) # database password zabbixpwd=$OPTARG;; h) # display help usage ; exit;; \?) # invalid option printf "\nType sudo " ; printf "%s" "$0" ; printf " -help for help\n\n" ; exit;; esac done # # Check if input conditions are met # inputcheck "$hostname" "$zabbixpwd" # # Install the Zabbix repository # wget --no-verbose $downloadurl$release dpkg -i $release apt update # # Install Zabbix components # apt install -y zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent fping # # Check if nginxsnippets exist if not download them # if [[ ! -d "$nginxfiles" ]]; then git clone --quiet https://git.x-files.dk/ubuntu-web-server/nginx-install.git ; cp -R nginx-install/nginxsnippets /etc/nginx fi # # Create Zabbix database # zabbixsql=" create database zabbix character set utf8mb4 collate utf8mb4_bin; create user zabbix@localhost identified by '$zabbixpwd'; grant all privileges on zabbix.* to zabbix@localhost; set global log_bin_trust_function_creators = 1; FLUSH PRIVILEGES; " printf '%s\n' "${zabbixsql[@]}" |sed '1d; $d' > zabbixsql.sql mysql < zabbixsql.sql # # Import Zabbix database shema # zcat /usr/share/zabbix-sql-scripts/mysql/server.sql.gz | mysql --default-character-set=utf8mb4 -uzabbix -p$zabbixpwd zabbix # # Disable trust # notrustsql=" set global log_bin_trust_function_creators = 0; FLUSH PRIVILEGES; " printf '%s\n' "${notrustsql[@]}" |sed '1d; $d' > notrustsql.sql mysql < notrustsql.sql # # Configure Nxinx # curl --silent https://git.x-files.dk/ubuntu-web-server/nginx-install/raw/branch/main/cfg-apps/zabbix.80.conf > /etc/nginx/conf.d/"$hostname".conf sed -i "s/# DBPassword=/DBPassword=$zabbixpwd/" /etc/zabbix/zabbix_server.conf sed -i "s/DOMAIN/$hostname/" /etc/nginx/conf.d/"$hostname.conf" rm /etc/nginx/conf.d/zabbix.conf # # Enable and start Zabbix # systemctl enable zabbix-server zabbix-agent systemctl restart zabbix-server zabbix-agent systemctl restart nginx # # Zabbix postinstall notice # zabbixnotice=" --------------------------------------------------------------------------------------------------- IMPORTANT: Go to http://$hostname and modify the following settings: DATABASE SETTINGS --------------------------------------------------------------------------------- DATABASE PASSWORD = $zabbixpwd SETTINGS ------------------------------------------------------------------------------------------ ZABBIX SERVER NAME = Your Sitename LOGIN --------------------------------------------------------------------------------------------- DEFAULT USER = Admin DEFAULT PASSWORD = zabbix --------------------------------------------------------------------------------------------------- " # # Display post install note # clear ; printf '%s\n' "${zabbixnotice[@]}" # # Cleaning up # rm -Rf zabbixsql.sql notrustsql.sql nginx-install *.deb # # End of script #