mirror of
https://ghproxy.net/https://github.com/CaptainCore/captaincore.git
synced 2025-10-04 03:00:07 +08:00
60 lines
1.4 KiB
Bash
60 lines
1.4 KiB
Bash
#!/usr/bin/env bash
|
|
|
|
#
|
|
# Monitor check on a single valid HTTP url.
|
|
#
|
|
# `captaincore monitor-check <url,name>`
|
|
#
|
|
# [--debug]
|
|
# Extra info for troubleshooting
|
|
#
|
|
|
|
# Vars
|
|
user_agent="captaincore/1.0 (CaptainCore Health Check by CaptainCore.io)"
|
|
url=${1%%,*}
|
|
name=${1##*,}
|
|
flag=$2
|
|
|
|
run_command() {
|
|
|
|
# Run the health check. Return http_code and body.
|
|
response=$(curl --location --max-redirs 7 --user-agent "$user_agent" --write-out "|%{num_redirects}|%{http_code}" --max-time 30 --compressed --silent $url)
|
|
response_last_line=$( printf "%s\n" "$response" | tail -n1 )
|
|
|
|
# Pull out number of redirects
|
|
num_redirects=$( echo -e "$response_last_line" | perl -ne '/.*\|(\d+)\|(\d+)$/&& print $1' )
|
|
|
|
# Pull out http code
|
|
http_code=$( echo -e "$response_last_line" | perl -ne '/.*\|(\d+)\|(\d+)$/&& print $2' )
|
|
|
|
# Pull out </html> from bottom if found. Valid code should contain </html> near the bottom.
|
|
html_end_tag=$( printf "%s\n" "$response"| tail -n12 | perl -wnE'say for /<\/html>/g' )
|
|
|
|
# check if </html> found
|
|
if [[ $html_end_tag == "</html>" ]]; then
|
|
html_end_tag_check="true"
|
|
else
|
|
html_end_tag_check="false"
|
|
fi
|
|
|
|
# Build json for output
|
|
read -r -d '' json_output << EOM
|
|
{
|
|
"http_code":"$http_code",
|
|
"num_redirects":"$num_redirects",
|
|
"url":"$url",
|
|
"name":"$name",
|
|
"html_valid":"$html_end_tag_check"
|
|
}
|
|
EOM
|
|
|
|
echo $json_output
|
|
|
|
if [[ "$flag" == "--debug" ]]; then
|
|
echo "Raw response"
|
|
echo "$response"
|
|
fi
|
|
|
|
}
|
|
|
|
run_command $1
|