zabbix-install/zabbixinstall

168 lines
5.6 KiB
Bash
Executable File

#!/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 $scriptdir\n\n" ; exit ; fi
#
# Define variables and functions
#
red='\033[0;31m' ; bred='\033[1;31m' ; green='\033[0;92m' ; blue='\033[0;36m' ; bold='\033[1m' ; normal='\033[0m'
line (){ for i in {1..50}; do echo -n "$1"; done && 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 -- "${bold}zabbixinstall${normal} \n\n"
printf -- "${bold}Usage:${normal} \n"
printf -- "./zabbixinstall [-n] <zabbix domain> \n"
printf -- " [-p] <zabbix database password> \n"
printf -- " [-help] <this screen> \n\n"
printf -- "${bold}Examples:${normal} \n"
printf -- "./zabbixinstall -n zabbix.something.xyz -p zabbixdatabsepwd\n\n"; }
#
# Function installation check for success
#
serviceno () { printf "\n$service 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 "${bold}Error! ${normal}Hostname Empty...\n\n" ; exit; fi
if [[ -z "$zabbixpwd" ]]; then usage ; printf "${bold}Error! ${normal}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
#
if [[ ! $@ =~ ^\-.+ ]]; then usage; fi
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 "Type $0 -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
#