99 lines
2.4 KiB
Bash
Executable File
99 lines
2.4 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# Monitor Check-in Setup Script
|
|
# This script sets up automated URL check-ins with systemd
|
|
|
|
set -e # Exit on error
|
|
|
|
# Check if running as root
|
|
if [[ $EUID -ne 0 ]]; then
|
|
echo "Error: This script must be run as root"
|
|
echo "Please run with: sudo $0"
|
|
exit 1
|
|
fi
|
|
|
|
echo "=========================================="
|
|
echo "Monitor Check-in Setup Script"
|
|
echo "=========================================="
|
|
echo ""
|
|
|
|
# Prompt for endpoint URL
|
|
read -p "Enter the endpoint URL (e.g., https://example.com/api/push/xxxxxxxxxxxxxxxxxxxxx): " endpoint_url
|
|
|
|
# Validate URL format
|
|
if [[ ! "$endpoint_url" =~ ^https?:// ]]; then
|
|
echo "Error: URL must start with http:// or https://"
|
|
exit 1
|
|
fi
|
|
|
|
# Create the monitor script
|
|
echo "Creating monitor script at /opt/monitor_checkin.sh..."
|
|
tee /opt/monitor_checkin.sh > /dev/null <<EOF
|
|
#!/bin/bash
|
|
|
|
url="$endpoint_url"
|
|
interval=45
|
|
|
|
# Wait for network connectivity
|
|
echo "Waiting for network connectivity..."
|
|
while ! curl -s --connect-timeout 5 "\$url" > /dev/null 2>&1; do
|
|
echo "Network not ready, waiting..."
|
|
sleep 5
|
|
done
|
|
|
|
echo "Network is ready. Script started. Calling URL every 45 seconds..."
|
|
|
|
while true; do
|
|
curl -s "\$url" > /dev/null
|
|
echo "Called \$url at \$(date '+%Y-%m-%d %H:%M:%S')" >> /var/log/call-url.log
|
|
sleep \$interval
|
|
done
|
|
EOF
|
|
|
|
# Make the script executable
|
|
echo "Making script executable..."
|
|
chmod +x /opt/monitor_checkin.sh
|
|
|
|
# Create the systemd service
|
|
echo "Creating systemd service..."
|
|
tee /etc/systemd/system/monitor_checkin.service > /dev/null <<EOF
|
|
[Unit]
|
|
Description=Call URL Every 45 Seconds to check in with the monitoring server
|
|
After=network-online.target
|
|
Wants=network-online.target
|
|
|
|
[Service]
|
|
Type=simple
|
|
User=nobody
|
|
ExecStart=/opt/monitor_checkin.sh
|
|
Restart=on-failure
|
|
RestartSec=10
|
|
StandardOutput=journal
|
|
StandardError=journal
|
|
|
|
[Install]
|
|
WantedBy=multi-user.target
|
|
EOF
|
|
|
|
# Enable and start the service
|
|
echo "Reloading systemd daemon..."
|
|
systemctl daemon-reload
|
|
|
|
echo "Enabling monitor_checkin service..."
|
|
systemctl enable monitor_checkin.service
|
|
|
|
echo "Starting monitor_checkin service..."
|
|
systemctl start monitor_checkin.service
|
|
|
|
echo ""
|
|
echo "=========================================="
|
|
echo "Setup Complete!"
|
|
echo "=========================================="
|
|
echo ""
|
|
echo "Service Status:"
|
|
systemctl status monitor_checkin.service
|
|
|
|
echo ""
|
|
echo "View logs with: journalctl -u monitor_checkin.service -f"
|
|
echo "Check URL log with: tail -f /var/log/call-url.log"
|