Update docker/bench/run-ab.sh

This commit is contained in:
colin 2024-02-07 02:23:08 +00:00
parent 9041f7dcbc
commit 9734935762
1 changed files with 50 additions and 44 deletions

View File

@ -17,6 +17,49 @@ CONCURRENCY=${CONCURRENCY:-$DEFAULT_CONCURRENCY}
declare -a failed_urls declare -a failed_urls
upload_to_hastebin() {
local file=$log_csv
local haste_url="https://haste.nixc.us/documents"
local response=$(curl -X POST -s --data-binary @"$file" $haste_url)
local key=$(echo $response | awk -F '"' '{print $4}')
if [[ ! -z "$key" ]]; then
echo "Uploaded to hastebin: https://haste.nixc.us/$key"
else
echo "Failed to upload to hastebin."
fi
}
parse_to_csv() {
local input=$log_file
local output=/log/scan.csv
local output_dir=$(dirname "$output")
# Ensure the output directory exists
if [[ ! -d "$output_dir" ]]; then
mkdir -p "$output_dir" || { echo "Failed to create directory $output_dir"; exit 1; }
fi
# Check if input file exists
if [[ ! -f "$input" ]]; then
echo "Input file $input does not exist."
exit 1
fi
: > "$output" # Clear output file before appending
grep -Eo 'https?://[^ ]+' "$input" | sort -u | while read -r url; do
local ttfb_line=$(grep -m 1 "$url" "$input" | grep 'TTFB test passed' | awk -F'Mean TTFB: ' '{print $2}')
local ab_line=$(grep -m 1 "$url" "$input" | grep 'ApacheBench test passed' | awk -F'Average time: ' '{print $2}')
local ttfb_test=${ttfb_line%ms.*}
local ab_test=${ab_line% milliseconds.*}
if [[ -n "$ttfb_test" || -n "$ab_test" ]]; then
if [[ -z "$ttfb_test" ]]; then ttfb_test="N/A"; fi
if [[ -z "$ab_test" ]]; then ab_test="N/A"; fi
echo "$url,$ttfb_test,$ab_test" >> "$output"
fi
done
}
process_sitemap() { process_sitemap() {
IFS=',' read -r -a domains <<< "$TARGET_URLS" IFS=',' read -r -a domains <<< "$TARGET_URLS"
for domain in "${domains[@]}"; do for domain in "${domains[@]}"; do
@ -70,6 +113,7 @@ run_ab() {
fi fi
} }
echo Clearing old log file.
log_file="/log/scan.log" log_file="/log/scan.log"
mkdir -p $(dirname "$log_file") mkdir -p $(dirname "$log_file")
cat /dev/null > $"$log_file" cat /dev/null > $"$log_file"
@ -97,52 +141,14 @@ for url in "${URLS[@]}"; do
fi fi
done done
parse_to_csv() { echo Clearing old csv file.
local input="$1" log_csv="/log/scan.csv"
local output="$2" mkdir -p $(dirname "$log_csv")
local output_dir=$(dirname "$output") cat /dev/null > "$log_csv"
# Ensure the output directory exists parse_to_csv
if [[ ! -d "$output_dir" ]]; then
mkdir -p "$output_dir" || { echo "Failed to create directory $output_dir"; exit 1; }
fi
# Check if input file exists upload_to_hastebin $log_csv
if [[ ! -f "$input" ]]; then
echo "Input file $input does not exist."
exit 1
fi
: > "$output" # Clear output file before appending
grep -Eo 'https?://[^ ]+' "$input" | sort -u | while read -r url; do
local ttfb_line=$(grep -m 1 "$url" "$input" | grep 'TTFB test passed' | awk -F'Mean TTFB: ' '{print $2}')
local ab_line=$(grep -m 1 "$url" "$input" | grep 'ApacheBench test passed' | awk -F'Average time: ' '{print $2}')
local ttfb_test=${ttfb_line%ms.*}
local ab_test=${ab_line% milliseconds.*}
if [[ -n "$ttfb_test" || -n "$ab_test" ]]; then
if [[ -z "$ttfb_test" ]]; then ttfb_test="N/A"; fi
if [[ -z "$ab_test" ]]; then ab_test="N/A"; fi
echo "$url,$ttfb_test,$ab_test" >> "$output"
fi
done
}
parse_to_csv "/logs/scan.log" "/logs/scan.csv"
upload_to_hastebin() {
local file=$1
local haste_url="https://haste.nixc.us/documents"
local response=$(curl -X POST -s --data-binary @"$file" $haste_url)
local key=$(echo $response | awk -F '"' '{print $4}')
if [[ ! -z "$key" ]]; then
echo "Uploaded to hastebin: https://haste.nixc.us/$key"
else
echo "Failed to upload to hastebin."
fi
}
upload_to_hastebin "/logs/scan.csv"
if [ ${#failed_urls[@]} -gt 0 ]; then if [ ${#failed_urls[@]} -gt 0 ]; then
echo "Summary of failed URLs:" | tee -a "$log_file" echo "Summary of failed URLs:" | tee -a "$log_file"