Sitechecks/sitechecks-installer.sh
hax cac4782bd5 Welcome Sidechecks-Installer :D
Signed-off-by: hax <hax@lainlounge.xyz>
2025-01-05 13:20:18 +00:00

139 lines
3.3 KiB
Bash

#!/usr/bin/env bash
# Variables
SCRIPT_PATH="/usr/local/bin/website_monitor.sh"
SERVICE_NAME="sitechecks"
SERVICE_FILE="/etc/systemd/system/${SERVICE_NAME}.service"
JSON_FILE="/etc/${SERVICE_NAME}/sites.json"
LOG_FILE="/var/log/${SERVICE_NAME}.log"
# Function: Install the monitoring script
install_monitor_script() {
echo "Copying 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 systemd service file
create_service_file() {
echo "Creating systemd service file at ${SERVICE_FILE}..."
cat << EOF > "$SERVICE_FILE"
[Unit]
Description=Website Monitoring Service
After=network.target
[Service]
ExecStart=$SCRIPT_PATH
Restart=on-failure
WorkingDirectory=/usr/local/bin
StandardOutput=append:$LOG_FILE
StandardError=append:$LOG_FILE
[Install]
WantedBy=multi-user.target
EOF
echo "Systemd service file created."
}
# Function: Enable and start the service
enable_and_start_service() {
echo "Enabling and starting the service ${SERVICE_NAME}..."
systemctl daemon-reload
systemctl enable "${SERVICE_NAME}"
systemctl start "${SERVICE_NAME}"
echo "Service ${SERVICE_NAME} is now running."
}
# Main installation process
install_monitor_script
create_json_config
create_service_file
enable_and_start_service
echo "Installation completed."