Added first version of the script
This commit is contained in:
Executable
+98
@@ -0,0 +1,98 @@
|
|||||||
|
#!/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/your-endpoint): " 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=55
|
||||||
|
|
||||||
|
# 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 55 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 55 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"
|
||||||
Reference in New Issue
Block a user