Created
April 28, 2023 15:56
-
-
Save digidigo/9d73858057e2e58aebea91d5b4ab4a44 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# Check if a URL is provided as an argument | |
if [ -z "$1" ]; then | |
echo "Usage: $0 <URL>" | |
exit 1 | |
fi | |
# Set the URL from the argument | |
url="$1" | |
# Extract the domain and path from the URL, remove 'http://' or 'https://', and replace slashes with dashes | |
domain_and_path=$(echo "$url" | sed -e 's~http[s]*://~~' -e 's~/~_~g') | |
# Name of the output CSV file | |
output_file="${domain_and_path}_monitor_log.csv" | |
# Write the header row of the CSV file | |
echo "URL,Response Time,Status" > "$output_file" | |
# Define colors for good and error responses | |
good_response_color="\033[32m" # Green | |
error_response_color="\033[31m" # Red | |
reset_color="\033[0m" | |
while true; do | |
# Send a request and capture the response time and status | |
response=$(curl -o /dev/null -s -w "%{http_code},%{time_total}" "$url") | |
status=$(echo "$response" | cut -d ',' -f 1) | |
# Append the response time and status to the CSV file | |
echo "$url,$response" >> "$output_file" | |
# Display the result in the terminal | |
if [[ $status -ge 200 ]] && [[ $status -lt 300 ]]; then | |
echo -e "${good_response_color}${url}, ${response}${reset_color}" | |
else | |
echo -e "${error_response_color}${url}, ${response}${reset_color}" | |
fi | |
# Wait for 5 seconds before sending the next request | |
sleep 5 | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment