#!/usr/bin/env bash # Variables SCRIPT_PATH="/usr/local/bin/sitechecks.sh" RC_FILE="/etc/rc.d/sitechecks" JSON_FILE="/etc/sitechecks/sites.json" LOG_FILE="/var/log/sitechecks.log" # Function: Install the monitoring script install_monitor_script() { echo "Installing the monitoring script to ${SCRIPT_PATH}..." cat << 'EOF' > "$SCRIPT_PATH" #!/usr/bin/env bash # Files INPUT_FILE="/etc/sitechecks/sites.json" LOG_FILE="/var/log/sitechecks.log" # Curl options CURL_OPTS=( --silent --max-time 10 --location --max-redirs 10 --fail --output /dev/null ) # Function: Describe HTTP status codes http_status_description() { local status="$1" case "$status" in 200) echo "OK" ;; 301) echo "Moved Permanently" ;; 302) echo "Found" ;; 400) echo "Bad Request" ;; 401) echo "Unauthorized" ;; 403) echo "Forbidden" ;; 404) echo "Not Found" ;; 408) echo "Request Timeout" ;; 429) echo "Too Many Requests" ;; 500) echo "Internal Server Error" ;; 502) echo "Bad Gateway" ;; 503) echo "Service Unavailable" ;; 504) echo "Gateway Timeout" ;; *) echo "Unknown Status" ;; esac } # Function: Check website availability check_site() { local site="$1" curl "${CURL_OPTS[@]}" --write-out "%{http_code}" "$site" 2>/dev/null } # Main function: Monitor websites monitor_sites() { local urls=$(jq -r '.urls[]' "$INPUT_FILE") if [[ -z "$urls" ]]; then echo "[$(date)] Error: No URLs found in the input file." >> "$LOG_FILE" exit 1 fi for site in $urls; do status=$(check_site "$site") description=$(http_status_description "$status") # Log the result echo "[$(date)] URL: $site - Status: $status ($description)" >> "$LOG_FILE" # Log errors only if [[ "$status" != "200" ]]; then echo "[$(date)] Error with URL: $site - Status: $status ($description)" >> "$LOG_FILE" fi done } monitor_sites EOF chmod +x "$SCRIPT_PATH" echo "Monitoring script installed." } # Function: Create JSON configuration file create_json_config() { echo "Creating JSON configuration file at ${JSON_FILE}..." mkdir -p "$(dirname "$JSON_FILE")" cat << 'EOF' > "$JSON_FILE" { "urls": [ "https://example.com", "https://example.org" ] } EOF echo "JSON configuration file created." } # Function: Create RC service file create_rc_service_file() { echo "Creating RC service file at ${RC_FILE}..." cat << EOF > "$RC_FILE" #!/bin/sh # PROVIDE: sitechecks # REQUIRE: DAEMON # KEYWORD: shutdown . /etc/rc.subr name="sitechecks" rcvar="sitechecks_enable" command="$SCRIPT_PATH" output_file="$LOG_FILE" pidfile="/var/run/sitechecks.pid" start_precmd="mkdir -p /var/run" load_rc_config \$name : \${sitechecks_enable:=NO} run_rc_command "\$1" EOF chmod +x "$RC_FILE" echo "RC service file created." } # Function: Enable and start the RC service enable_and_start_service() { echo "Enabling and starting the service..." echo "sitechecks_enable=YES" >> /etc/rc.conf service sitechecks start echo "Service started and enabled." } # Main installation process install_monitor_script create_json_config create_rc_service_file enable_and_start_service echo "Installation completed. The monitoring service is now running."