Last active
January 2, 2025 15:33
-
-
Save tranphuquy19/b722725039ed61fe821fcc6299003a5c to your computer and use it in GitHub Desktop.
speedtest.sh
This file contains hidden or 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 | |
# Default configuration from environment variables | |
THRESHOLD_DOWNLOAD=${THRESHOLD_DOWNLOAD:-300} | |
THRESHOLD_UPLOAD=${THRESHOLD_UPLOAD:-200} | |
IFS=',' read -ra SERVER_IDS <<<"${SERVER_IDS:-44677,9903,26853,1536,3865,22207,50467,13623}" | |
# Telegram configuration | |
TELEGRAM_BOT_TOKEN=${TELEGRAM_BOT_TOKEN:-""} | |
TELEGRAM_CHAT_ID=${TELEGRAM_CHAT_ID:-"-"} | |
TELEGRAM_THREAD_ID=${TELEGRAM_THREAD_ID:-""} | |
# Function to send message via Telegram | |
send_telegram_message() { | |
local message="$1" | |
local status="$2" | |
# Prepare message for Telegram | |
local telegram_message="" | |
# Add alert header if needed | |
[ "$status" != "success" ] && telegram_message+=$'π @all\n\n' | |
# Add title and timestamp | |
telegram_message+=$'π Speed Test Results\n' | |
telegram_message+=$"π $(date '+%Y-%m-%d %H:%M:%S')" | |
telegram_message+=$'\n' | |
telegram_message+=$'ββββββββββββββββββββ\n\n' | |
# Process the test results line by line | |
while IFS= read -r line; do | |
# Skip header lines | |
[[ "$line" =~ ^===.*===$ ]] && continue | |
[[ "$line" =~ ^π .*$ ]] && continue | |
# Replace ASCII separator with Unicode | |
if [[ "$line" =~ ^-+$ ]]; then | |
telegram_message+=$'ββββββββββββββββββββ\n' | |
continue | |
fi | |
# Add other lines | |
telegram_message+="$line"$'\n' | |
done <<<"$message" | |
# Prepare payload | |
local payload | |
if [ -n "$TELEGRAM_THREAD_ID" ]; then | |
payload=$(jq -n \ | |
--arg chat_id "$TELEGRAM_CHAT_ID" \ | |
--arg thread_id "$TELEGRAM_THREAD_ID" \ | |
--arg text "$telegram_message" \ | |
'{ | |
chat_id: $chat_id, | |
message_thread_id: $thread_id, | |
text: $text, | |
parse_mode: "HTML", | |
disable_web_page_preview: true | |
}') | |
else | |
payload=$(jq -n \ | |
--arg chat_id "$TELEGRAM_CHAT_ID" \ | |
--arg text "$telegram_message" \ | |
'{ | |
chat_id: $chat_id, | |
text: $text, | |
parse_mode: "HTML", | |
disable_web_page_preview: true | |
}') | |
fi | |
# Send message with retry | |
local max_retries=3 | |
local retry_count=0 | |
while [ $retry_count -lt $max_retries ]; do | |
if response=$(curl -s -X POST \ | |
"https://api.telegram.org/bot${TELEGRAM_BOT_TOKEN}/sendMessage" \ | |
-H 'Content-Type: application/json' \ | |
-d "$payload"); then | |
if echo "$response" | jq -e '.ok == true' >/dev/null; then | |
echo "β Telegram message sent successfully" | |
return 0 | |
fi | |
fi | |
((retry_count++)) | |
[ $retry_count -lt $max_retries ] && sleep 5 | |
done | |
echo "β Failed to send Telegram message after $max_retries attempts" | |
return 1 | |
} | |
# Install speedtest CLI | |
install_speedtest() { | |
echo "π Installing speedtest CLI..." | |
rm -f speedtest* | |
if ! wget -q https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-x86_64.tgz; then | |
echo "β Failed to download speedtest CLI" | |
exit 1 | |
fi | |
if ! tar xzf ookla-speedtest-1.2.0-linux-x86_64.tgz; then | |
echo "β Failed to extract speedtest files" | |
rm ookla-speedtest-1.2.0-linux-x86_64.tgz | |
exit 1 | |
fi | |
rm ookla-speedtest-1.2.0-linux-x86_64.tgz | |
chmod +x speedtest | |
mkdir -p ~/.config/ookla | |
cat >~/.config/ookla/speedtest-cli.json <<EOF | |
{ | |
"Settings": { | |
"LicenseAccepted": "604ec27f828456331ebf441826292c49276bd3c1bee1a2f65a6452f505c4061c", | |
"GDPRAccepted": true | |
} | |
} | |
EOF | |
echo "β Speedtest CLI installed successfully" | |
} | |
# Check speedtest binary | |
check_speedtest() { | |
if [ ! -f "./speedtest" ]; then | |
echo "β Speedtest binary not found in current directory" | |
echo "π Attempting to install speedtest..." | |
install_speedtest | |
fi | |
chmod +x ./speedtest | |
} | |
# Test speed for a single server | |
test_speed() { | |
local server_id=$1 | |
local result | |
echo "π Testing server ID: $server_id..." >&2 | |
result=$(./speedtest -f json -s "$server_id" 2>/dev/null) | |
if [ $? -ne 0 ] || [ -z "$result" ]; then | |
echo "β Failed to test server $server_id" >&2 | |
return 1 | |
fi | |
echo "$result" | |
return 0 | |
} | |
# Analyze JSON result | |
analyze_result() { | |
local json_result=$1 | |
local has_alert="false" | |
# Get values from JSON | |
local download_bytes=$(echo "$json_result" | jq -r '.download.bandwidth') | |
local upload_bytes=$(echo "$json_result" | jq -r '.upload.bandwidth') | |
local ping=$(echo "$json_result" | jq -r '.ping.latency') | |
local server_name=$(echo "$json_result" | jq -r '.server.name') | |
local server_location=$(echo "$json_result" | jq -r '.server.location') | |
local server_country=$(echo "$json_result" | jq -r '.server.country') | |
local server_id=$(echo "$json_result" | jq -r '.server.id') | |
# Convert bytes/s to Mbps | |
local download_mbps=$(echo "scale=2; $download_bytes * 8 / 1000000" | bc) | |
local upload_mbps=$(echo "scale=2; $upload_bytes * 8 / 1000000" | bc) | |
# Format output | |
echo "π Server ID: $server_id - $server_name ($server_location, $server_country)" | |
# Check download threshold | |
if (($(echo "$download_mbps < $THRESHOLD_DOWNLOAD" | bc -l))); then | |
echo "β¬οΈ Download: $download_mbps Mbps β οΈ" | |
has_alert="true" | |
else | |
echo "β¬οΈ Download: $download_mbps Mbps" | |
fi | |
# Check upload threshold | |
if (($(echo "$upload_mbps < $THRESHOLD_UPLOAD" | bc -l))); then | |
echo "β¬οΈ Upload: $upload_mbps Mbps β οΈ" | |
has_alert="true" | |
else | |
echo "β¬οΈ Upload: $upload_mbps Mbps" | |
fi | |
echo "π Ping: $ping ms" | |
echo "ββββββββββββββββββββ" | |
[ "$has_alert" = "true" ] && return 1 || return 0 | |
} | |
# Main function | |
main() { | |
if [ ${#SERVER_IDS[@]} -eq 0 ]; then | |
echo "β Error: No server IDs defined" | |
exit 1 | |
fi | |
local output="" | |
local overall_alert=false | |
local test_results=() | |
local failed_servers=() | |
# Add threshold info at the beginning | |
output+="π Threshold - Download: $THRESHOLD_DOWNLOAD Mbps, Upload: $THRESHOLD_UPLOAD Mbps"$'\n' | |
output+="ββββββββββββββββββββ"$'\n\n' | |
for server_id in "${SERVER_IDS[@]}"; do | |
local result | |
if result=$(test_speed "$server_id"); then | |
test_results+=("$result") | |
output+="$(analyze_result "$result")"$'\n' | |
[ $? -eq 1 ] && overall_alert=true | |
else | |
failed_servers+=("$server_id") | |
output+="β Failed to test server $server_id"$'\n\n' | |
fi | |
done | |
output+="π Summary | |
Total servers tested: ${#SERVER_IDS[@]} | |
Successful tests: ${#test_results[@]}"$'\n' | |
if [ ${#failed_servers[@]} -gt 0 ]; then | |
output+="β Failed servers: ${failed_servers[*]}"$'\n' | |
fi | |
local status="success" | |
if $overall_alert; then | |
output+="β οΈ Alert: One or more servers below threshold!"$'\n' | |
status="alert" | |
else | |
if [ ${#test_results[@]} -eq 0 ]; then | |
output+="β Error: All tests failed"$'\n' | |
status="error" | |
else | |
output+="β All successful tests are above thresholds"$'\n' | |
fi | |
fi | |
echo -e "$output" | |
if [ -n "$TELEGRAM_BOT_TOKEN" ] && [ -n "$TELEGRAM_CHAT_ID" ]; then | |
send_telegram_message "$output" "$status" | |
fi | |
[ "$status" != "success" ] && exit 1 || exit 0 | |
} | |
# Check dependencies | |
check_dependencies() { | |
local missing_deps=() | |
if ! command -v jq &>/dev/null; then | |
missing_deps+=("jq") | |
fi | |
if ! command -v bc &>/dev/null; then | |
missing_deps+=("bc") | |
fi | |
if [ ${#missing_deps[@]} -ne 0 ]; then | |
echo "β Error: Missing required dependencies:" | |
printf '%s\n' "${missing_deps[@]}" | |
echo "Please install them using:" | |
echo "sudo apt-get install ${missing_deps[*]}" | |
exit 1 | |
fi | |
} | |
# Parse arguments | |
while [[ $# -gt 0 ]]; do | |
case $1 in | |
--install) | |
install_speedtest | |
exit 0 | |
;; | |
--help) | |
echo "Usage: $0 [--install] [--help]" | |
echo " --install: Force reinstall speedtest CLI" | |
echo " --help: Show this help message" | |
echo "" | |
echo "Environment variables:" | |
echo " THRESHOLD_DOWNLOAD: Download threshold in Mbps (default: 300)" | |
echo " THRESHOLD_UPLOAD: Upload threshold in Mbps (default: 200)" | |
echo " SERVER_IDS: Comma-separated list of server IDs (default: 44677,22207)" | |
echo " TELEGRAM_BOT_TOKEN: Telegram bot token" | |
echo " TELEGRAM_CHAT_ID: Telegram chat ID" | |
echo " TELEGRAM_THREAD_ID: Telegram thread ID (optional)" | |
exit 0 | |
;; | |
*) | |
echo "Unknown option: $1" | |
exit 1 | |
;; | |
esac | |
shift | |
done | |
# Run checks and main function | |
# check_dependencies | |
check_speedtest | |
main |
This file contains hidden or 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
π @all | |
π Speed Test Results | |
π 2025-01-02 20:55:32 | |
ββββββββββββββββββββ | |
π Threshold - Download: 300 Mbps, Upload: 200 Mbps | |
ββββββββββββββββββββ | |
π Server ID: 44677 - FPT Telecom (Da Nang, Vietnam) | |
β¬οΈ Download: 942.29 Mbps | |
β¬οΈ Upload: 940.62 Mbps | |
π Ping: 4.115 ms | |
ββββββββββββββββββββ | |
π Server ID: 9903 - Viettel Network (Ha Noi, Vietnam) | |
β¬οΈ Download: 937.18 Mbps | |
β¬οΈ Upload: 941.11 Mbps | |
π Ping: 13.796 ms | |
ββββββββββββββββββββ | |
π Server ID: 26853 - Viettel Network (Ho Chi Minh, Vietnam) | |
β¬οΈ Download: 948.27 Mbps | |
β¬οΈ Upload: 941.48 Mbps | |
π Ping: 23.259 ms | |
ββββββββββββββββββββ | |
π Server ID: 1536 - STC (Hong Kong, Hong Kong) | |
β¬οΈ Download: 943.13 Mbps | |
β¬οΈ Upload: 939.17 Mbps | |
π Ping: 43.794 ms | |
ββββββββββββββββββββ | |
π Server ID: 3865 - KABEL-TV-BINZ (Binz, Germany) | |
β¬οΈ Download: 942.79 Mbps | |
β¬οΈ Upload: 7.89 Mbps β οΈ | |
π Ping: 230.977 ms | |
ββββββββββββββββββββ | |
π Server ID: 22207 - BCNET (Vancouver, BC, Canada) | |
β¬οΈ Download: 651.33 Mbps | |
β¬οΈ Upload: 450.36 Mbps | |
π Ping: 268.36 ms | |
ββββββββββββββββββββ | |
π Server ID: 50467 - Verizon (Tokyo, Japan) | |
β¬οΈ Download: .80 Mbps β οΈ | |
β¬οΈ Upload: 275.24 Mbps | |
π Ping: 131.43 ms | |
ββββββββββββββββββββ | |
π Server ID: 13623 - Singtel (Singapore, Singapore) | |
β¬οΈ Download: 948.96 Mbps | |
β¬οΈ Upload: 533.02 Mbps | |
π Ping: 73.599 ms | |
ββββββββββββββββββββ | |
π Summary | |
Total servers tested: 8 | |
Successful tests: 8 | |
β οΈ Alert: One or more servers below threshold! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment