Files
SnarfCode/scripts/deploy_zabbix_agent2.py

80 lines
3.3 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
"""
TARGETS = [
("sninfr0lnxctl01", "140.44.4.41", "root"),
("sninfr0lnxctl02", "140.44.4.42", "root"),
("sninfr0lnxctl03", "140.44.4.43", "root"),
("sninfr0lnxctl04", "140.44.4.44", "root"),
("sninfr2lnxwrk05", "140.44.4.25", "root"),
("sninfr2lnxwrk07", "140.44.4.27", "root"),
("sninfr4lnxaih01", "140.44.4.7", "snarf"),
("sninfr4lnxaih02", "140.44.4.8", "snarf"),
]
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 ""
# 1. Check if zabbix-agent2 is installed, install if missing
stdin, stdout, stderr = ssh.exec_command(f"{sudo}which zabbix_agent2")
out = stdout.read().decode().strip()
if not out:
print(" zabbix-agent2 not found. Installing via apt...")
stdin, stdout, stderr = ssh.exec_command(f"{sudo}apt-get update -y && {sudo}apt-get install -y zabbix-agent2")
print(" Apt output:", stdout.read().decode()[-200:])
# 2. Write PSK file
stdin, stdout, stderr = ssh.exec_command(f"{sudo}mkdir -p /etc/zabbix")
stdin, stdout, stderr = ssh.exec_command(f"echo '{PSK_VALUE}' | {sudo}tee /etc/zabbix/zabbix_agent2.psk > /dev/null")
stdin, stdout, stderr = ssh.exec_command(f"{sudo}chmod 600 /etc/zabbix/zabbix_agent2.psk")
stdin, stdout, stderr = ssh.exec_command(f"{sudo}chown -R zabbix:zabbix /etc/zabbix/zabbix_agent2.psk 2>/dev/null || true")
# 3. Write conf file
# Using base64 to avoid escaping issues
import base64
conf_b64 = base64.b64encode(AGENT_CONF.encode('utf-8')).decode('ascii')
stdin, stdout, stderr = ssh.exec_command(f"echo '{conf_b64}' | base64 -d | {sudo}tee /etc/zabbix/zabbix_agent2.conf > /dev/null")
# 4. Restart and enable service
stdin, stdout, stderr = ssh.exec_command(f"{sudo}systemctl daemon-reload && {sudo}systemctl enable --now zabbix-agent2.service && {sudo}systemctl restart zabbix-agent2.service")
stdout.read()
# 5. Verify status
time.sleep(2)
stdin, stdout, stderr = ssh.exec_command(f"{sudo}systemctl is-active zabbix-agent2.service")
status = stdout.read().decode().strip()
print(f" [SUCCESS] {name} ({ip}) zabbix-agent2 service is: {status}")
ssh.close()
except Exception as e:
print(f" [ERROR] Failed on {name} ({ip}): {e}")