#!/bin/bash # --- Configuration (Edit these before spreading) --- TARGET_SERVICE_UUID="" SERVER_URL="https://monitor.example.com" AUTH_USER="monitor_user" AUTH_PASS="monitor_user_pass" UPDATE_INTERVAL=10 # --- Script Logic --- # 1. Ensure the script is run as root if [ "$EUID" -ne 0 ]; then echo "Please run as root (use sudo)" exit 1 fi echo "Starting deployment of Monitoring Agent..." # 2. Install dependencies (libcap2-bin is needed for setcap) echo "Installing dependencies..." apt-get update -qq apt-get install -y libcap2-bin uuid-runtime -qq # 3. Create the system user if it doesn't exist if ! id "monitor-agent" &>/dev/null; then echo "Creating monitor-agent system user..." useradd -r -s /bin/false monitor-agent fi # 4. Create necessary directories echo "Creating directories..." mkdir -p /etc/monitor-agent mkdir -p /var/lib/monitor-agent chown -R monitor-agent:monitor-agent /var/lib/monitor-agent # 5. Install the binary if [ -f "./agent-go" ]; then echo "Installing binary to /usr/local/bin..." cp ./agent-go /usr/local/bin/monitor-agent chmod +x /usr/local/bin/monitor-agent # Grant ping capabilities without root setcap cap_net_raw+ep /usr/local/bin/monitor-agent else echo "Error: 'monitor-agent' binary not found in current directory!" exit 1 fi # 6. Generate a unique NODE_UUID for this specific server NEW_UUID=$(uuidgen) echo "Generated unique Node UUID: $NEW_UUID" # 7. Create the Environment File echo "Creating configuration file..." cat < /etc/monitor-agent/agent.env NODE_UUID=$NEW_UUID TARGET_SERVICE_UUID=$TARGET_SERVICE_UUID SERVER_URL=$SERVER_URL BASIC_AUTH_USERNAME=$AUTH_USER BASIC_AUTH_PASSWORD=$AUTH_PASS UPDATE_INTERVAL_SECONDS=$UPDATE_INTERVAL PEERS_FILE=/var/lib/monitor-agent/known_peers.json EOF chmod 600 /etc/monitor-agent/agent.env chown monitor-agent:monitor-agent /etc/monitor-agent/agent.env # 8. Create the Systemd Service File echo "Creating systemd service..." cat < /etc/systemd/system/monitor-agent.service [Unit] Description=Node Monitoring Agent After=network-online.target Wants=network-online.target [Service] Type=simple User=monitor-agent Group=monitor-agent WorkingDirectory=/var/lib/monitor-agent EnvironmentFile=/etc/monitor-agent/agent.env ExecStart=/usr/local/bin/monitor-agent CapabilityBoundingSet=CAP_NET_RAW AmbientCapabilities=CAP_NET_RAW Restart=always RestartSec=5 [Install] WantedBy=multi-user.target EOF # 9. Start the service echo "Reloading systemd and starting service..." systemctl daemon-reload systemctl enable monitor-agent systemctl restart monitor-agent echo "------------------------------------------------" echo "Deployment Complete!" echo "Node UUID: $NEW_UUID" echo "Check status with: systemctl status monitor-agent" echo "View logs with: journalctl -u monitor-agent -f" echo "------------------------------------------------"