From 12e46c60c45c66080ff48a42ffac7ddc92b74d83 Mon Sep 17 00:00:00 2001 From: stirelshka8 Date: Sun, 14 Jul 2024 07:16:38 +0300 Subject: [PATCH] Update script --- install_agent.sh | 58 ++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 49 insertions(+), 9 deletions(-) diff --git a/install_agent.sh b/install_agent.sh index 7916509..73c3d54 100644 --- a/install_agent.sh +++ b/install_agent.sh @@ -1,31 +1,71 @@ #!/bin/bash -#Install Zabbix Agent +# Install Zabbix Agent + +# Define color codes for output +RED='\033[0;31m' +BLUE='\033[0;34m' +GREEN='\033[0;32m' +DEFAULT='\033[0m' ROOT_UID=0 +ZABBIX_SERVER_IP="192.168.1.56" +ZABBIX_CONFIG_FILE="/etc/zabbix/zabbix_agentd.conf" if [ "$UID" -ne "$ROOT_UID" ]; then echo -e "${RED}Please run script as root user.${DEFAULT}"; exit 1 fi -if [ -f /etc/zabbix ]; then - echo "Zabbix Agent Installed" +# Function to check the success of a command +check_success() { + if [ $? -ne 0 ]; then + echo -e "${RED}Error: $1 failed.${DEFAULT}" + exit 1 + fi +} - sed -i 's/Server=.*/Server=192.168.1.56/g' /etc/zabbix/zabbix_agentd.conf - sed -i 's/ServerActive=.*/ServerActive=192.168.1.56/g' /etc/zabbix/zabbix_agentd.conf +# Check if Zabbix Agent is already installed +if [ -f "$ZABBIX_CONFIG_FILE" ]; then + echo -e "${BLUE}Zabbix Agent already installed, resetting config.${DEFAULT}" + + # Update server configuration in Zabbix Agent config file + sed -i "s/^Server=.*/Server=$ZABBIX_SERVER_IP/g" $ZABBIX_CONFIG_FILE + check_success "Updating Server in config file" + + sed -i "s/^ServerActive=.*/ServerActive=$ZABBIX_SERVER_IP/g" $ZABBIX_CONFIG_FILE + check_success "Updating ServerActive in config file" else + # Download and install Zabbix repository package wget https://repo.zabbix.com/zabbix/7.0/ubuntu/pool/main/z/zabbix-release/zabbix-release_7.0-2+ubuntu22.04_all.deb + check_success "Downloading Zabbix repository package" + dpkg -i zabbix-release_7.0-2+ubuntu22.04_all.deb + check_success "Installing Zabbix repository package" + + # Update package lists and install Zabbix Agent apt update + check_success "Updating package lists" + apt install zabbix-agent -y + check_success "Installing Zabbix Agent" + + # Enable and restart Zabbix Agent service + systemctl enable zabbix-agent + check_success "Enabling Zabbix Agent service" systemctl restart zabbix-agent - systemctl enable zabbix-agent + check_success "Restarting Zabbix Agent service" - sed -i 's/Server=127.0.0.1/Server=192.168.1.56/g' /etc/zabbix/zabbix_agentd.conf - sed -i 's/ServerActive=127.0.0.1/ServerActive=192.168.1.56/g' /etc/zabbix/zabbix_agentd.conf + # Update server configuration in Zabbix Agent config file + sed -i "s/^Server=127.0.0.1/Server=$ZABBIX_SERVER_IP/g" $ZABBIX_CONFIG_FILE + check_success "Updating Server in config file" + + sed -i "s/^ServerActive=127.0.0.1/ServerActive=$ZABBIX_SERVER_IP/g" $ZABBIX_CONFIG_FILE + check_success "Updating ServerActive in config file" fi +# Restart Zabbix Agent service to apply changes systemctl restart zabbix-agent +check_success "Restarting Zabbix Agent service" -echo "Zabbix Agent Installed" \ No newline at end of file +echo -e "${GREEN}Zabbix Agent Installed and Configured Successfully${DEFAULT}"