Skip to content

Instantly share code, notes, and snippets.

@gayanhewa
Created March 23, 2025 12:01
Show Gist options
  • Save gayanhewa/20aa3e29628e575e94a7691eaa764e1a to your computer and use it in GitHub Desktop.
Save gayanhewa/20aa3e29628e575e94a7691eaa764e1a to your computer and use it in GitHub Desktop.
Website Monitoring Script using checkonlinestatus.com
# List of websites to monitor (one per line)
google.com
github.com
stackoverflow.com
example.com
yourdomain.com
# This line is a comment and won't be processed
# Empty lines like the one above are skipped
amazon.com
#!/bin/bash
# Website Status Monitor for https://checkonlinestatus.com
# Periodically checks website status and displays results in a terminal UI
# Configuration variables
CHECK_INTERVAL=3600 # Check interval in seconds (1 hour)
API_BASE_URL="https://checkonlinestatus.com/check.php"
CONFIG_FILE="$HOME/.website_monitor_urls"
LOG_FILE="$HOME/.website_monitor_log"
# ANSI color codes
RED="\033[0;31m"
GREEN="\033[0;32m"
YELLOW="\033[0;33m"
BLUE="\033[0;34m"
MAGENTA="\033[0;35m"
CYAN="\033[0;36m"
BOLD="\033[1m"
RESET="\033[0m"
# Function to check if required tools are installed
check_dependencies() {
local missing_deps=()
for cmd in curl jq date clear; do
if ! command -v $cmd &> /dev/null; then
missing_deps+=($cmd)
fi
done
if [ ${#missing_deps[@]} -ne 0 ]; then
echo -e "${RED}${BOLD}Error: Missing dependencies${RESET}"
echo "Please install the following packages:"
for dep in "${missing_deps[@]}"; do
echo " - $dep"
done
echo
echo "On Debian/Ubuntu: sudo apt install curl jq coreutils"
echo "On RHEL/CentOS: sudo yum install curl jq coreutils"
echo "On macOS: brew install curl jq coreutils"
exit 1
fi
}
# Function to create default config file if it doesn't exist
setup_config() {
if [ ! -f "$CONFIG_FILE" ]; then
echo "# List of websites to monitor (one per line)" > "$CONFIG_FILE"
echo "google.com" >> "$CONFIG_FILE"
echo "github.com" >> "$CONFIG_FILE"
echo "example.com" >> "$CONFIG_FILE"
echo -e "${YELLOW}Created default config file at $CONFIG_FILE${RESET}"
echo -e "${YELLOW}Edit this file to add/remove websites to monitor${RESET}"
sleep 2
fi
}
# Function to check website status
check_website() {
local url=$1
local response
local status
local timestamp
local formatted_time
# Call the API
response=$(curl -s "$API_BASE_URL?url=$url")
# Parse JSON response
if ! echo "$response" | jq -e . &>/dev/null; then
echo "$url,error,$(date +%s)" >> "$LOG_FILE"
return 1
fi
status=$(echo "$response" | jq -r '.isUp')
timestamp=$(echo "$response" | jq -r '.timestamp')
# Convert timestamp to human-readable format
formatted_time=$(date -d "@$timestamp" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -r "$timestamp" "+%Y-%m-%d %H:%M:%S")
# Log result
echo "$url,$status,$timestamp" >> "$LOG_FILE"
# Return results
echo "$status|$formatted_time"
}
# Function to display header
display_header() {
local current_time=$(date "+%Y-%m-%d %H:%M:%S")
local next_check=$(date -d "+$CHECK_INTERVAL seconds" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -v+"$CHECK_INTERVAL"S "+%Y-%m-%d %H:%M:%S")
echo -e "${BOLD}${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo -e "${BOLD}${BLUE} WEBSITE STATUS MONITOR (checkonlinestatus.com) ${RESET}"
echo -e "${BOLD}${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo -e "${YELLOW}Current Time:${RESET} $current_time ${YELLOW}|${RESET} ${YELLOW}Next Check:${RESET} $next_check"
echo -e "${BLUE}────────────────────────────────────────────────────────────────────────────────${RESET}"
printf "${BOLD}%-30s %-10s %-30s${RESET}\n" "WEBSITE" "STATUS" "LAST CHECKED"
echo -e "${BLUE}────────────────────────────────────────────────────────────────────────────────${RESET}"
}
# Function to display footer with keybindings
display_footer() {
echo -e "${BLUE}────────────────────────────────────────────────────────────────────────────────${RESET}"
echo -e "${BOLD}Controls:${RESET} ${CYAN}q${RESET}:Quit ${CYAN}r${RESET}:Refresh ${CYAN}a${RESET}:Add Website ${CYAN}e${RESET}:Edit Config ${CYAN}c${RESET}:Clear Log"
echo -e "${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
}
# Function to add a new website
add_website() {
clear
echo -e "${BOLD}${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo -e "${BOLD}${BLUE} ADD WEBSITE TO MONITOR ${RESET}"
echo -e "${BOLD}${BLUE}━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━${RESET}"
echo
echo -e "Enter the URL to add (without http/https, e.g. ${CYAN}example.com${RESET}):"
read -r new_url
if [[ -z "$new_url" ]]; then
echo -e "${YELLOW}No URL entered. Operation canceled.${RESET}"
sleep 2
return
fi
# Check if URL already exists
if grep -q "^$new_url$" "$CONFIG_FILE"; then
echo -e "${YELLOW}URL already exists in the config file.${RESET}"
sleep 2
return
fi
# Add URL to config file
echo "$new_url" >> "$CONFIG_FILE"
echo -e "${GREEN}Added $new_url to monitoring list.${RESET}"
sleep 2
}
# Function to edit config file
edit_config() {
local editor
# Try to determine the available editor
if command -v nano &> /dev/null; then
editor="nano"
elif command -v vim &> /dev/null; then
editor="vim"
elif command -v vi &> /dev/null; then
editor="vi"
else
editor=""
fi
if [[ -z "$editor" ]]; then
echo -e "${YELLOW}No text editor found. Please edit the file manually at:${RESET}"
echo "$CONFIG_FILE"
echo -e "${YELLOW}Press any key to continue...${RESET}"
read -n 1
return
fi
$editor "$CONFIG_FILE"
}
# Function to clear the log file
clear_log() {
> "$LOG_FILE"
echo -e "${GREEN}Log file cleared.${RESET}"
sleep 2
}
# Main function to run the monitor
run_monitor() {
local running=true
local force_refresh=false
local websites
local result
local status
local timestamp
# Enable non-blocking input
stty -echo
while $running; do
clear
display_header
# Read websites from config file
websites=()
while IFS= read -r line; do
# Skip comments and empty lines
if [[ ! "$line" =~ ^[[:space:]]*# && -n "$line" ]]; then
websites+=("$line")
fi
done < "$CONFIG_FILE"
# Check each website
for site in "${websites[@]}"; do
if $force_refresh; then
# Force new check
result=$(check_website "$site")
status=${result%|*}
timestamp=${result#*|}
else
# Try to get the latest result from log file
latest_log=$(grep "^$site," "$LOG_FILE" | tail -n 1)
if [[ -n "$latest_log" ]]; then
# Parse log entry
status=$(echo "$latest_log" | cut -d',' -f2)
timestamp_epoch=$(echo "$latest_log" | cut -d',' -f3)
timestamp=$(date -d "@$timestamp_epoch" "+%Y-%m-%d %H:%M:%S" 2>/dev/null || date -r "$timestamp_epoch" "+%Y-%m-%d %H:%M:%S")
# Check if we need a refresh (older than check interval)
current_epoch=$(date +%s)
if (( current_epoch - timestamp_epoch >= CHECK_INTERVAL )); then
result=$(check_website "$site")
status=${result%|*}
timestamp=${result#*|}
fi
else
# No log entry found, do a fresh check
result=$(check_website "$site")
status=${result%|*}
timestamp=${result#*|}
fi
fi
# Display status with color
if [[ "$status" == "true" ]]; then
status_display="${GREEN}${BOLD}UP${RESET}"
elif [[ "$status" == "false" ]]; then
status_display="${RED}${BOLD}DOWN${RESET}"
else
status_display="${YELLOW}${BOLD}ERROR${RESET}"
fi
printf "%-30s %-10s %-30s\n" "$site" "$status_display" "$timestamp"
done
display_footer
# Reset force refresh flag
force_refresh=false
# Wait for user input or timeout
read -t $CHECK_INTERVAL -n 1 key
# Process user input
case "$key" in
q|Q)
running=false
;;
r|R)
force_refresh=true
;;
a|A)
add_website
;;
e|E)
edit_config
;;
c|C)
clear_log
;;
esac
done
# Restore terminal settings
stty echo
clear
echo -e "${GREEN}Website monitoring stopped.${RESET}"
}
# Main script execution
check_dependencies
setup_config
touch "$LOG_FILE"
run_monitor
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment