Skip to content

Instantly share code, notes, and snippets.

@unleftie
Last active April 25, 2024 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unleftie/fd30c5be74c46909bb3486e6bc29d489 to your computer and use it in GitHub Desktop.
Save unleftie/fd30c5be74c46909bb3486e6bc29d489 to your computer and use it in GitHub Desktop.
ping with curl
#!/bin/bash
# example:
# ./http_test.sh --hostname https://google.com
set -o pipefail
# Variables for curl command
SLEEP_INTERVAL=5 # Sleep interval in seconds
TIMEOUT=5 # Timeout value in seconds
EXPECTED_RESPONSE_CODE=200
# Variables to track request statistics
TOTAL_REQUESTS=0
SUCCESSFUL_REQUESTS=0
UNSUCCESSFUL_REQUESTS=0
WRONG_CODE_REQUESTS=0
TIMEOUT_REQUESTS=0
function print_request_success() {
printf '%s%s [OK]%s\n' "$(printf '\033[32m')" "$*" "$(printf '\033[m')" >&2
}
function print_request_error() {
printf '%s%s [NOT OK]%s\n' "$(printf '\033[31m')" "$*" "$(printf '\033[m')" >&2
}
function print_error() {
printf '%sERROR: %s%s\n' "$(printf '\033[31m')" "$*" "$(printf '\033[m')" >&2
exit 1
}
# Function to check response code
function check_response() {
# Perform curl request with timeout and extract response code
RESPONSE_CODE=$(curl -L -s -o /dev/null -w "%{http_code}" --max-time $TIMEOUT "$HOSTNAME" 2>&1)
# Check if curl command failed (indicating a timeout)
if [[ $? -ne 0 ]]; then
((TIMEOUT_REQUESTS++))
print_request_error "Request timed out after $TIMEOUT seconds"
else
# Check if response code is $EXPECTED_RESPONSE_CODE
if [[ $RESPONSE_CODE -eq $EXPECTED_RESPONSE_CODE ]]; then
((SUCCESSFUL_REQUESTS++))
print_request_success "Response code is $RESPONSE_CODE"
else
((WRONG_CODE_REQUESTS++))
print_request_error "Response code is $RESPONSE_CODE, expected $EXPECTED_RESPONSE_CODE"
fi
fi
# Increment total requests count
((TOTAL_REQUESTS++))
}
# Function to calculate uptime percentage
function calculate_uptime() {
UNSUCCESSFUL_REQUESTS=$((TIMEOUT_REQUESTS + WRONG_CODE_REQUESTS))
UPTIME_PERCENTAGE=$(awk "BEGIN {print ($SUCCESSFUL_REQUESTS / $TOTAL_REQUESTS) * 100}")
if ((TIMEOUT_REQUESTS > 0)); then
DOWNTIME_SECONDS=$(echo "scale=0; ($TIMEOUT_REQUESTS * $TIMEOUT)" | bc -l)
else
DOWNTIME_SECONDS=0
fi
if ((WRONG_CODE_REQUESTS > 0)); then
DOWNTIME_SECONDS=$(echo "scale=0; ($DOWNTIME_SECONDS + ($WRONG_CODE_REQUESTS * $SLEEP_INTERVAL))" | bc -l)
fi
echo "Uptime percentage: $UPTIME_PERCENTAGE%"
if [[ $UPTIME_PERCENTAGE -ne 100 ]]; then
echo "Downtime: ~${DOWNTIME_SECONDS} seconds"
fi
}
# Function to print statistics
function print_statistics() {
echo "Total requests: $TOTAL_REQUESTS"
echo "Successful requests: $SUCCESSFUL_REQUESTS"
echo "Unsuccessful requests: $UNSUCCESSFUL_REQUESTS"
calculate_uptime
}
# Trap Ctrl+C and print statistics
trap 'echo -e "\n"; print_statistics; exit' INT
# Parse command line arguments
while [[ "$#" -gt 0 ]]; do
case $1 in
-h | --hostname)
HOSTNAME="$2"
shift
;;
*)
print_error "Unknown parameter passed: $1"
;;
esac
shift
done
if ! command -v curl &>/dev/null; then
print_error "Curl is not installed. Please install curl to run tests."
fi
# Check if required parameters are provided
if [[ -z $HOSTNAME ]]; then
print_error "Usage: $0 --hostname <hostname>"
fi
# Continuously check response
while true; do
check_response
sleep $SLEEP_INTERVAL
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment