#!/usr/bin/env bash # Author : Allan Christensen # First Created : 11012021 (DD-MM-YYYY) # Description : Installs Zabbix on Ubuntu 24.04 # License : MIT License (see LICENSE file for details) # # Are we root # if [[ $(id -u) -ne 0 ]]; then echo "" && echo "Must be root or use sudo" && echo "" ; exit 1 ; fi # # Define variables and functions # phpver=$(php -r 'echo PHP_MAJOR_VERSION.".".PHP_MINOR_VERSION;' 2>/dev/null || echo "8.3") ; phpfpm="php$phpver-fpm" ubuntuversion="24.04" fallbackversion="7.0" # # Function usage # usage () { printf -- "\nzabbixinstall (Ubuntu 24.04 + Zabbix 7.0 LTS)\n\n" printf -- "%s\n\n" "$socket" printf -- "Usage:\n" printf -- "sudo ./zabbixinstall -n -p [options]\n\n" printf -- "Options:\n" printf -- " -a Optional admin username $socketusage\n" printf -- " -m Optional admin password $socketusage\n" printf -- " -h | -help | --help Show this help screen\n\n" printf -- "Examples:\n" printf -- " sudo ./zabbixinstall -n zabbix.something.xyz -p zabbixdbpwd\n" printf -- " sudo ./zabbixinstall -n zabbix.something.xyz -p zabbixdbpwd -a admin -m adminpwd\n\n" } # # Function to check if a service is already running or not # 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 1 ; fi; } # # Function inputcheck # inputcheck () { [[ -z "$hostname" ]] && { usage; echo ""; echo "Error! Hostname empty."; echo ""; exit 1; } [[ -z "$dbpass" ]] && { usage; echo ""; echo "Error! Database password empty."; echo ""; exit 1; } } # # If Nginx and MariaDB is not running then die # service="mariadb" ; servicedead ; service="nginx" ; servicedead # # Check MariaDB authentication method (socket or not) # if mysql -u root -e ";" 2>/dev/null; then socket="SOCKET DETECTED — no need for -a or -m" socketusage="SOCKET DETECTED — this flag is not needed" socketauth="yes" else socket="NO SOCKET DETECTED — you must use -a and -m" socketusage="NO SOCKET DETECTED — these flags are required" socketauth="no" fi # # Let's go # clear # # Try to determine the latest Zabbix version # zabbix_version=$(curl -s https://repo.zabbix.com/zabbix/ | grep -oP '(?<=href=")[0-9]+\.[0]' | sort -V | tail -1) # Use fallback version if fetch failed if [[ -z "$zabbix_version" ]]; then printf "Could not determine latest Zabbix version. Falling back to version %s\n\n" "$fallbackversion" ; zabbix_version="$fallbackversion" ; fi printf "Using Zabbix version: %s\n\n" "$zabbix_version" # # Configure command line options # if [[ "$1" == "-help" || "$1" == "--help" ]]; then usage ; exit 0 ; fi if [[ $# -eq 0 || ! $1 =~ ^- ]]; then usage ; exit 1 ; fi while getopts ":n:p:m:a:h" option; do case "$option" in n) hostname=$(echo "$OPTARG" | tr '[:upper:]' '[:lower:]');; p) dbpass="$OPTARG";; m) mariadbpwd="$OPTARG";; a) mariadbadmin="$OPTARG";; h) usage; exit 0;; :) usage; echo ""; echo "Error! Option -$OPTARG requires an argument."; echo ""; exit 1;; \?) usage; echo ""; echo "Error! Invalid option: -$OPTARG"; echo ""; exit 1;; esac done inputcheck "$hostname" "$dbpass" # # Download and install the Zabbix repository # tmpfile="/tmp/zabbix-release_latest_${zabbix_version}+ubuntu${ubuntuversion}_all.deb" download="https://repo.zabbix.com/zabbix/${zabbix_version}/ubuntu/pool/main/z/zabbix-release/zabbix-release_latest_${zabbix_version}+ubuntu${ubuntuversion}_all.deb" wget --no-verbose -O "$tmpfile" "$download" dpkg -i "$tmpfile" apt update # # Install Zabbix components # apt install -y zabbix-server-mysql zabbix-frontend-php zabbix-nginx-conf zabbix-sql-scripts zabbix-agent fping # # Clone nginx-snippets; if nginx-snippets exists then just pull latest changes # nginxsnippets="/etc/nginx/nginx-snippets" repo="https://git.x-files.dk/webserver/nginx-snippets.git" if [[ -d "$nginxsnippets/.git" ]]; then git -C "$nginxsnippets" pull --quiet; else git clone --quiet "$repo" "$nginxsnippets"; fi # # Determine MariaDB login method # mariadbadmin="${mariadbadmin:-root}" printf "\nChecking MariaDB access method...\n" if [[ "$socketauth" == "yes" ]]; then dbmethod="socket" printf "Socket authentication detected (root)\n" elif [[ -n "$mariadbpwd" && -n "$mariadbadmin" ]]; then dbmethod="admin" printf "Using admin user authentication (%s)\n" "$mariadbadmin" else printf "\nERROR: No valid MariaDB authentication method found.\n" printf "Tried socket, admin user, and root password.\n\n" exit 1 fi # # Create Zabbix database # case "$dbmethod" in socket) mysql -u root <