From f86417b22cbadcd279c3404d66234c80b065dadd Mon Sep 17 00:00:00 2001 From: Doug Masiero Date: Wed, 23 Apr 2025 17:54:02 -0400 Subject: [PATCH] Modify zabbix ServerActive lines - Now monitoring live VMs --- .../alpine/tasks/install_zabbix.sh | 2 +- .../common/tasks/post_modify_zabbix.sh | 28 +++++++++++++++++++ .../ubuntu/tasks/install_zabbix.sh | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) create mode 100644 bolt/vm_automation/common/tasks/post_modify_zabbix.sh diff --git a/bolt/vm_automation/alpine/tasks/install_zabbix.sh b/bolt/vm_automation/alpine/tasks/install_zabbix.sh index 2387dba..f0ddbbe 100644 --- a/bolt/vm_automation/alpine/tasks/install_zabbix.sh +++ b/bolt/vm_automation/alpine/tasks/install_zabbix.sh @@ -15,7 +15,7 @@ fi ssh -o StrictHostKeyChecking=no root@${STAGING_IP} "apk add zabbix-agent2" # Configure zabbix-agent2 -ssh -o StrictHostKeyChecking=no root@${STAGING_IP} "sed -i -e 's/^Server=127\.0\.0\.1/# Server=127.0.0.1/' -e 's/^ServerActive=127\.0\.0\.1/ServerActive=10.44.0.5,zabbix.moeny.ai/' /etc/zabbix/zabbix_agent2.conf" +ssh -o StrictHostKeyChecking=no root@${STAGING_IP} "sed -i -e 's/^Server=127\.0\.0\.1/# Server=127.0.0.1/' -e 's/^ServerActive=127\.0\.0\.1/ServerActive=zabbix.moeny.ai,zabbix.moeny.internal/' /etc/zabbix/zabbix_agent2.conf" # Add zabbix-agent2 to default runlevel ssh -o StrictHostKeyChecking=no root@${STAGING_IP} "rc-update add zabbix-agent2 default" diff --git a/bolt/vm_automation/common/tasks/post_modify_zabbix.sh b/bolt/vm_automation/common/tasks/post_modify_zabbix.sh new file mode 100644 index 0000000..46dfcc9 --- /dev/null +++ b/bolt/vm_automation/common/tasks/post_modify_zabbix.sh @@ -0,0 +1,28 @@ +#!/bin/bash + +CONFIG_FILE="/etc/zabbix/zabbix_agent2.conf" +SEARCH_STRING="10.44.0.5" +REPLACE_STRING="zabbix.moeny.internal" + +# Check if the configuration file exists using sudo +if ! sudo test -f "$CONFIG_FILE"; then + echo "Error: File $CONFIG_FILE not found." + exit 1 +fi + +# Check if the search string exists in the file using sudo +if sudo grep -q -F "$SEARCH_STRING" "$CONFIG_FILE"; then + # Escape dots in the search string for sed + ESCAPED_SEARCH_STRING=$(echo "$SEARCH_STRING" | sed 's/\./\\./g') + + # Replace the string in place using sudo without creating a backup + sudo sed -i "s/$ESCAPED_SEARCH_STRING/$REPLACE_STRING/g" "$CONFIG_FILE" + if [ $? -ne 0 ]; then + echo "Error: Failed to replace string." + exit 1 + fi + + echo "Replacement done." +else + echo "No replacement needed. The string '$SEARCH_STRING' was not found." +fi \ No newline at end of file diff --git a/bolt/vm_automation/ubuntu/tasks/install_zabbix.sh b/bolt/vm_automation/ubuntu/tasks/install_zabbix.sh index 4dc5528..2217166 100644 --- a/bolt/vm_automation/ubuntu/tasks/install_zabbix.sh +++ b/bolt/vm_automation/ubuntu/tasks/install_zabbix.sh @@ -23,7 +23,7 @@ sudo apt update sudo apt install -y zabbix-agent2 # Configure the Zabbix agent -sudo sed -i -e 's/^Server=127\.0\.0\.1/# Server=127.0.0.1/' -e 's/^ServerActive=127\.0\.0\.1/ServerActive=10.44.0.5,zabbix.moeny.ai/' -e 's/^LogFileSize=0/LogFileSize=1/' -e 's/^Hostname=Zabbix server/# Hostname=Zabbix server/' -e 's/^# HostnameItem=system\.hostname/HostnameItem=system.hostname/' /etc/zabbix/zabbix_agent2.conf +sudo sed -i -e 's/^Server=127\.0\.0\.1/# Server=127.0.0.1/' -e 's/^ServerActive=127\.0\.0\.1/ServerActive=zabbix.moeny.ai,zabbix.moeny.internal/' -e 's/^LogFileSize=0/LogFileSize=1/' -e 's/^Hostname=Zabbix server/# Hostname=Zabbix server/' -e 's/^# HostnameItem=system\.hostname/HostnameItem=system.hostname/' /etc/zabbix/zabbix_agent2.conf # Enable the Zabbix agent sudo systemctl enable zabbix-agent2