Files
SnarfCode/scripts/retry_deploy_zabbix.py

72 lines
2.4 KiB
Python

import paramiko
import sys
import time
sys.stdout.reconfigure(encoding='utf-8')
PSK_VALUE = "a1142332ccfa9c53040e36171bb551ceb2865fb4051d203404df39e88ebe94f0"
AGENT_CONF = """# Standardized Antigravity Managed Zabbix Agent 2 Config
Server=zabbix.snarfnet.net
ServerActive=zabbix.snarfnet.net
HostnameItem=system.hostname
HostMetadata=Linux
TLSConnect=psk
TLSAccept=psk
TLSPSKIdentity=PSK_autoregister
TLSPSKFile=/etc/zabbix/zabbix_agent2.psk
LogFile=/var/log/zabbix/zabbix_agent2.log
LogFileSize=10
BufferSend=5
BufferSize=100
"""
import base64
conf_b64 = base64.b64encode(AGENT_CONF.encode('utf-8')).decode('ascii')
TARGETS = [
("sninfr0lnxctl01", "140.44.4.41", "root"),
("sninfr0lnxctl02", "140.44.4.42", "root"),
("sninfr2lnxwrk05", "140.44.4.25", "root"),
("sninfr2lnxwrk07", "140.44.4.27", "root"),
]
for name, ip, user in TARGETS:
print(f"\n=======================================================")
print(f"Deploying Zabbix Agent 2 to {name} ({ip}) as {user}...")
print(f"=======================================================")
try:
ssh = paramiko.SSHClient()
ssh.set_missing_host_key_policy(paramiko.AutoAddPolicy())
ssh.connect(ip, username=user, key_filename=r'C:\Users\Snarf\.ssh\id_ed25519_snarfnet', timeout=10)
sudo = "sudo " if user != "root" else ""
compound_cmd = f"""
which zabbix_agent2 || (apt-get update -y && apt-get install -y zabbix-agent2)
mkdir -p /etc/zabbix
echo '{PSK_VALUE}' > /etc/zabbix/zabbix_agent2.psk
chmod 600 /etc/zabbix/zabbix_agent2.psk
chown -R zabbix:zabbix /etc/zabbix/zabbix_agent2.psk 2>/dev/null || true
echo '{conf_b64}' | base64 -d > /etc/zabbix/zabbix_agent2.conf
systemctl daemon-reload
systemctl enable --now zabbix-agent2.service
systemctl restart zabbix-agent2.service
systemctl is-active zabbix-agent2.service
"""
stdin, stdout, stderr = ssh.exec_command(f"{sudo}bash -c \"{compound_cmd}\"")
out = stdout.read().decode().strip()
err = stderr.read().decode().strip()
lines = out.splitlines()
last_line = lines[-1] if lines else "EMPTY"
print(f" [RESULT] {name} ({ip}) output: {last_line}")
if err:
print(" STDERR (tail):", err[-200:])
ssh.close()
except Exception as e:
print(f" [ERROR] Failed on {name} ({ip}): {e}")