captaincore/app/monitor
2024-05-08 17:20:13 -04:00

157 lines
4 KiB
Bash

#!/usr/bin/env bash
#
# Monitor check one or more sites.
#
# `captaincore monitor`
#
# [<site>...]
# One or more sites to check.
#
# [@<target>]
# Target groups of sites like @all @production or @staging.
#
# [--urls="<url1> <url2> <url3>"]
#
# [--retry=<number-of-retries>]
# Number of retries for failures. Defaults to 3.
#
# [--parallel=<number-of-checks>]
# Number of monitor checks to run at same time. Defaults to 15.
#
# [--page=<page-url>]
# Check a specific page. Example `--page=/wp-admin/`. Defaults to home page.
#
while read config; do
if [[ "$config" == "Error:"* ]]; then
continue
fi
declare "$config"
done <<< "$(php ${CAPTAINCORE_PATH}/lib/local-scripts/configs.php fetch)"
run_command() {
if [[ ${#@} != "1" ]]; then
echo -e "${COLOR_RED}Error:${COLOR_NORMAL} Required <site> or <target>."
return 1
fi
urls_to_check=()
# Assign default retry
if [[ $RETRY == "" ]]; then
RETRY=3
fi
# Assign default parallel
if [[ $FLAG_PARALLEL == "" ]]; then
FLAG_PARALLEL=15
fi
# See if any sites are specifed
if [[ $@ != "@all"* && $@ != "@production" && $@ != "@staging" ]]; then
# Runs on specific sites
for site in $@; do
url=$( captaincore site get $site --field=home_url --captain-id=$CAPTAIN_ID )
if [[ "$url" == "" ]]; then
continue
fi
urls_to_check+=( ${url}${page},${site} )
done
urls_to_check=${urls_to_check[@]}
fi
if [[ ${#urls} != 0 ]]; then
urls_to_check=${urls}
fi
if [[ $@ == "@all"* || $@ == "@production" || $@ == "@staging" ]]; then
urls=$( captaincore site list $@ --field=home_url,site --captain-id=$CAPTAIN_ID )
for item in ${urls[*]}; do
url=${item%%,*}
site_name=${item##*,}
if [[ "$url" == "" ]] || [[ "$site_name" == "" ]]; then
continue
fi
urls_to_check+=( ${url}${page},${site_name} )
done
urls_to_check=${urls_to_check[@]}
fi
orignal_urls_to_check=$urls_to_check
# Generate random auth
auth=''; for count in {0..6}; do auth+=$(printf "%x" $(($RANDOM%16)) ); done;
# Begin time tracking
overalltimebegin=$(date +"%s")
backup_date=$(date +'%Y-%m-%d')
backup_time=$(date +'%H-%M')
# Define log file format
log_file=$logs/${backup_date}_${backup_time}_$auth.txt
# Define monitor.json location
monitor_file="$path/monitor.json"
if [[ "$urls_to_check" == "" ]]; then
echo "${COLOR_RED}Error:${COLOR_NORMAL} Nothing to check"
exit
fi
echo "logging to $log_file"
cd "$HOME/.captaincore/data"
for attempt in $(seq 1 $RETRY); do
# Wait before retrying failures
if [[ "$attempt" != "1" ]]; then
sleep 10s
fi
# Run checks in parallel. Collect the results in log file.
( echo $urls_to_check | xargs -P $FLAG_PARALLEL -n 1 captaincore monitor-check ) 2>&1 | tee $log_file
# Have local PHP handle error count
error_count=$( php $HOME/.captaincore/lib/local-scripts/monitor.php check $log_file $monitor_file )
# If no errors then skip the rest of the attempts.
if [[ $error_count == "0" ]]; then
break
fi
# Skip last attempt
if [[ $attempt != $RETRY ]]; then
echo "Attempt #${attempt} found $error_count errors. Checking those URLs again.";
# Fetch urls with errors for another another check
urls_to_check=$( php $HOME/.captaincore/lib/local-scripts/monitor.php process $log_file $monitor_file )
# Empty space for formating
echo "" >> $log_file
else
echo "Attempt #${attempt} found $error_count errors."
fi
done
# Build emails
email_content=$( php $HOME/.captaincore/lib/local-scripts/monitor.php generate $log_file $monitor_file "$orignal_urls_to_check" $CAPTAIN_ID )
if [[ $email_content != "" ]]; then
echo "Sending Email"
echo $email_content
# output "Response code $response for $address" per each item in array
echo $email_content | mutt -e 'set content_type=text/html' -s "Monitor: $error_count errors" -- $captaincore_admin_email
fi
}
run_command $@