Files
workspace/projects/feed-hunter/setup_monitor.sh

186 lines
5.1 KiB
Bash
Executable File

#!/bin/bash
# Feed Hunter Portal Monitor Setup Script
set -e
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
SYSTEMD_USER_DIR="$HOME/.config/systemd/user"
echo "=== Feed Hunter Portal Monitor Setup ==="
echo "Script directory: $SCRIPT_DIR"
echo "Systemd user directory: $SYSTEMD_USER_DIR"
echo
# Function to show status
show_status() {
echo "=== Current Status ==="
echo "Portal Monitor Service:"
systemctl --user status feed-hunter-portal-monitor.service --no-pager -l || echo " Not found or inactive"
echo
echo "Business Hours Timer:"
systemctl --user status feed-hunter-portal-monitor-business.timer --no-pager -l || echo " Not found or inactive"
echo
echo "Off-Hours Timer:"
systemctl --user status feed-hunter-portal-monitor-offhours.timer --no-pager -l || echo " Not found or inactive"
echo
echo "Active Timers:"
systemctl --user list-timers feed-hunter-portal-monitor-* || echo " No active timers found"
echo
}
# Function to install/update services
install_services() {
echo "Installing systemd services..."
# Reload systemd to pick up any changes
systemctl --user daemon-reload
# Enable the timers (this also enables the service as a dependency)
echo "Enabling business hours timer..."
systemctl --user enable feed-hunter-portal-monitor-business.timer
echo "Enabling off-hours timer..."
systemctl --user enable feed-hunter-portal-monitor-offhours.timer
echo "Services installed and enabled."
}
# Function to start services
start_services() {
echo "Starting monitoring services..."
systemctl --user start feed-hunter-portal-monitor-business.timer
systemctl --user start feed-hunter-portal-monitor-offhours.timer
echo "Services started."
}
# Function to stop services
stop_services() {
echo "Stopping monitoring services..."
systemctl --user stop feed-hunter-portal-monitor-business.timer 2>/dev/null || true
systemctl --user stop feed-hunter-portal-monitor-offhours.timer 2>/dev/null || true
echo "Services stopped."
}
# Function to restart services
restart_services() {
stop_services
sleep 2
start_services
}
# Function to run a manual test
test_monitor() {
echo "Running manual monitor test..."
echo "Output will be shown below:"
echo "================================"
cd "$SCRIPT_DIR"
python3 portal_monitor.py
echo "================================"
echo "Test completed. Check the output above for any issues."
}
# Function to show logs
show_logs() {
echo "Recent monitor logs (last 50 lines):"
echo "================================"
journalctl --user -u feed-hunter-portal-monitor.service -n 50 --no-pager || echo "No logs found"
echo "================================"
echo "To follow logs in real-time, run:"
echo "journalctl --user -u feed-hunter-portal-monitor.service -f"
}
# Function to show monitor status
show_monitor_status() {
echo "Running monitor status check..."
cd "$SCRIPT_DIR"
python3 monitor_status.py
}
# Function to uninstall
uninstall_services() {
echo "Uninstalling monitoring services..."
stop_services
systemctl --user disable feed-hunter-portal-monitor-business.timer 2>/dev/null || true
systemctl --user disable feed-hunter-portal-monitor-offhours.timer 2>/dev/null || true
echo "Services disabled and stopped."
echo "Note: Service files are still in $SYSTEMD_USER_DIR"
echo "Remove them manually if desired."
}
# Main menu
case "${1:-menu}" in
"install")
install_services
echo
echo "✅ Installation completed!"
echo "Run '$0 start' to start the monitoring services."
;;
"start")
start_services
echo
echo "✅ Services started!"
echo "Run '$0 status' to check status or '$0 logs' to view logs."
;;
"stop")
stop_services
echo
echo "✅ Services stopped!"
;;
"restart")
restart_services
echo
echo "✅ Services restarted!"
;;
"status")
show_status
;;
"monitor-status")
show_monitor_status
;;
"test")
test_monitor
;;
"logs")
show_logs
;;
"uninstall")
uninstall_services
echo
echo "✅ Services uninstalled!"
;;
"menu"|*)
echo "Feed Hunter Portal Monitor Management"
echo
echo "Usage: $0 [command]"
echo
echo "Commands:"
echo " install Install and enable systemd services"
echo " start Start monitoring services"
echo " stop Stop monitoring services"
echo " restart Restart monitoring services"
echo " status Show systemd service status"
echo " monitor-status Show detailed monitor status"
echo " test Run manual monitor test"
echo " logs Show recent logs"
echo " uninstall Stop and disable services"
echo
echo "Quick setup:"
echo " $0 install && $0 start"
echo
;;
esac