2016-11-29 14:25:31 +09:00
|
|
|
#!/usr/bin/env bash
|
|
|
|
#
|
|
|
|
# Description: Auto test download & I/O speed script
|
|
|
|
#
|
2020-07-28 17:17:30 +09:00
|
|
|
# Copyright (C) 2015 - 2020 Teddysun <i@teddysun.com>
|
2016-11-29 14:25:31 +09:00
|
|
|
# Thanks: LookBack <admin@dwhd.org>
|
|
|
|
# URL: https://teddysun.com/444.html
|
|
|
|
#
|
2020-07-28 21:12:13 +09:00
|
|
|
trap _exit INT QUIT TERM
|
2015-09-09 10:39:03 +08:00
|
|
|
|
2020-07-28 17:17:30 +09:00
|
|
|
_red() {
|
|
|
|
printf '\033[0;31;31m%b\033[0m' "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
_green() {
|
|
|
|
printf '\033[0;31;32m%b\033[0m' "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
_yellow() {
|
|
|
|
printf '\033[0;31;33m%b\033[0m' "$1"
|
|
|
|
}
|
|
|
|
|
|
|
|
_blue() {
|
|
|
|
printf '\033[0;31;36m%b\033[0m' "$1"
|
|
|
|
}
|
2016-11-24 22:23:24 +09:00
|
|
|
|
2020-07-28 17:17:30 +09:00
|
|
|
_exists() {
|
|
|
|
local cmd="$1"
|
|
|
|
if eval type type > /dev/null 2>&1; then
|
|
|
|
eval type "$cmd" > /dev/null 2>&1
|
|
|
|
elif command > /dev/null 2>&1; then
|
|
|
|
command -v "$cmd" > /dev/null 2>&1
|
|
|
|
else
|
|
|
|
which "$cmd" > /dev/null 2>&1
|
|
|
|
fi
|
|
|
|
local rt=$?
|
|
|
|
return ${rt}
|
|
|
|
}
|
2016-11-30 11:24:07 +09:00
|
|
|
|
2020-07-28 20:05:32 +09:00
|
|
|
_64bit(){
|
|
|
|
if [ $(getconf WORD_BIT) = '32' ] && [ $(getconf LONG_BIT) = '64' ]; then
|
|
|
|
return 0
|
|
|
|
else
|
|
|
|
return 1
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
2020-07-28 21:12:13 +09:00
|
|
|
_exit() {
|
|
|
|
_red "\nThe script has been terminated.\n"
|
|
|
|
# clean up
|
2021-02-10 14:59:39 +09:00
|
|
|
rm -fr speedtest.tgz speedtest-cli benchtest_*
|
2020-07-28 21:12:13 +09:00
|
|
|
exit 1
|
|
|
|
}
|
|
|
|
|
2015-12-14 22:15:18 +08:00
|
|
|
get_opsy() {
|
2021-02-11 15:09:56 +09:00
|
|
|
[ -f /etc/redhat-release ] && awk '{print $0}' /etc/redhat-release && return
|
2015-12-25 20:16:48 +08:00
|
|
|
[ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
|
2015-12-14 22:15:18 +08:00
|
|
|
[ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
|
|
|
|
}
|
2015-09-09 10:39:03 +08:00
|
|
|
|
|
|
|
next() {
|
2015-09-23 10:04:56 +08:00
|
|
|
printf "%-70s\n" "-" | sed 's/\s/-/g'
|
2015-09-09 10:39:03 +08:00
|
|
|
}
|
|
|
|
|
2020-07-28 20:05:32 +09:00
|
|
|
speed_test() {
|
2020-07-28 21:12:13 +09:00
|
|
|
local nodeName="$2"
|
2020-07-29 09:52:17 +09:00
|
|
|
[ -z "$1" ] && ./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr > ./speedtest-cli/speedtest.log 2>&1 || \
|
|
|
|
./speedtest-cli/speedtest --progress=no --server-id=$1 --accept-license --accept-gdpr > ./speedtest-cli/speedtest.log 2>&1
|
2020-07-28 20:05:32 +09:00
|
|
|
if [ $? -eq 0 ]; then
|
2020-07-29 09:52:17 +09:00
|
|
|
local dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
|
|
|
local up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
|
|
|
local latency=$(awk '/Latency/{print $2" "$3}' ./speedtest-cli/speedtest.log)
|
2020-07-28 20:05:32 +09:00
|
|
|
if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
|
2020-07-29 09:52:17 +09:00
|
|
|
printf "\033[0;33m%-18s\033[0;32m%-18s\033[0;31m%-20s\033[0;36m%-12s\033[0m\n" " ${nodeName}" "${up_speed}" "${dl_speed}" "${latency}"
|
2020-07-28 20:05:32 +09:00
|
|
|
fi
|
|
|
|
fi
|
2015-09-09 10:39:03 +08:00
|
|
|
}
|
|
|
|
|
2020-07-28 20:05:32 +09:00
|
|
|
speed() {
|
|
|
|
speed_test '' 'Speedtest.net'
|
2020-07-28 21:12:13 +09:00
|
|
|
speed_test '5145' 'Beijing CU'
|
|
|
|
speed_test '3633' 'Shanghai CT'
|
|
|
|
speed_test '24447' 'Shanghai CU'
|
|
|
|
speed_test '27594' 'Guangzhou CT'
|
|
|
|
speed_test '26678' 'Guangzhou CU'
|
|
|
|
speed_test '16192' 'Shenzhen CU'
|
|
|
|
speed_test '4515' 'Shenzhen CM'
|
|
|
|
speed_test '32155' 'Hongkong CN'
|
|
|
|
speed_test '13623' 'Singapore SG'
|
|
|
|
speed_test '15047' 'Tokyo JP'
|
2015-09-09 10:39:03 +08:00
|
|
|
}
|
|
|
|
|
2015-12-03 14:33:33 +08:00
|
|
|
io_test() {
|
2020-07-28 21:12:13 +09:00
|
|
|
(LANG=C dd if=/dev/zero of=benchtest_$$ bs=64k count=16k conv=fdatasync && rm -f benchtest_$$ ) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
|
2015-12-03 14:33:33 +08:00
|
|
|
}
|
|
|
|
|
2016-11-24 22:23:24 +09:00
|
|
|
calc_disk() {
|
|
|
|
local total_size=0
|
2016-11-25 00:46:43 +09:00
|
|
|
local array=$@
|
2018-06-27 08:33:04 +09:00
|
|
|
for size in ${array[@]}
|
2016-11-24 22:23:24 +09:00
|
|
|
do
|
2016-11-25 00:46:43 +09:00
|
|
|
[ "${size}" == "0" ] && size_t=0 || size_t=`echo ${size:0:${#size}-1}`
|
2017-05-26 23:41:36 +09:00
|
|
|
[ "`echo ${size:(-1)}`" == "K" ] && size=0
|
2016-11-27 16:37:14 +09:00
|
|
|
[ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' / 1024}' )
|
|
|
|
[ "`echo ${size:(-1)}`" == "T" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' * 1024}' )
|
|
|
|
[ "`echo ${size:(-1)}`" == "G" ] && size=${size_t}
|
2016-11-25 11:33:45 +09:00
|
|
|
total_size=$( awk 'BEGIN{printf "%.1f", '$total_size' + '$size'}' )
|
2016-11-24 22:23:24 +09:00
|
|
|
done
|
|
|
|
echo ${total_size}
|
|
|
|
}
|
2015-12-14 22:15:18 +08:00
|
|
|
|
2020-07-28 17:17:30 +09:00
|
|
|
check_virt(){
|
|
|
|
_exists "dmesg" && virtualx="$(dmesg 2>/dev/null)"
|
2020-07-28 20:05:32 +09:00
|
|
|
if _exists "dmidecode"; then
|
2020-07-28 17:17:30 +09:00
|
|
|
sys_manu="$(dmidecode -s system-manufacturer 2>/dev/null)"
|
|
|
|
sys_product="$(dmidecode -s system-product-name 2>/dev/null)"
|
|
|
|
sys_ver="$(dmidecode -s system-version 2>/dev/null)"
|
|
|
|
else
|
|
|
|
sys_manu=""
|
|
|
|
sys_product=""
|
|
|
|
sys_ver=""
|
|
|
|
fi
|
|
|
|
if grep -qa docker /proc/1/cgroup; then
|
|
|
|
virt="Docker"
|
|
|
|
elif grep -qa lxc /proc/1/cgroup; then
|
|
|
|
virt="LXC"
|
|
|
|
elif grep -qa container=lxc /proc/1/environ; then
|
|
|
|
virt="LXC"
|
|
|
|
elif [[ -f /proc/user_beancounters ]]; then
|
|
|
|
virt="OpenVZ"
|
|
|
|
elif [[ "${virtualx}" == *kvm-clock* ]]; then
|
|
|
|
virt="KVM"
|
|
|
|
elif [[ "${cname}" == *KVM* ]]; then
|
|
|
|
virt="KVM"
|
|
|
|
elif [[ "${cname}" == *QEMU* ]]; then
|
|
|
|
virt="KVM"
|
|
|
|
elif [[ "${virtualx}" == *"VMware Virtual Platform"* ]]; then
|
|
|
|
virt="VMware"
|
|
|
|
elif [[ "${virtualx}" == *"Parallels Software International"* ]]; then
|
|
|
|
virt="Parallels"
|
|
|
|
elif [[ "${virtualx}" == *VirtualBox* ]]; then
|
|
|
|
virt="VirtualBox"
|
|
|
|
elif [[ -e /proc/xen ]]; then
|
|
|
|
virt="Xen"
|
|
|
|
elif [[ "${sys_manu}" == *"Microsoft Corporation"* ]]; then
|
|
|
|
if [[ "${sys_product}" == *"Virtual Machine"* ]]; then
|
|
|
|
if [[ "${sys_ver}" == *"7.0"* || "${sys_ver}" == *"Hyper-V" ]]; then
|
|
|
|
virt="Hyper-V"
|
|
|
|
else
|
|
|
|
virt="Microsoft Virtual Machine"
|
|
|
|
fi
|
|
|
|
fi
|
|
|
|
else
|
|
|
|
virt="Dedicated"
|
|
|
|
fi
|
|
|
|
}
|
|
|
|
|
|
|
|
ipv4_info() {
|
2020-07-29 10:59:18 +09:00
|
|
|
local org="$(wget -q -T10 -O- ipinfo.io/org)"
|
|
|
|
local city="$(wget -q -T10 -O- ipinfo.io/city)"
|
|
|
|
local country="$(wget -q -T10 -O- ipinfo.io/country)"
|
|
|
|
local region="$(wget -q -T10 -O- ipinfo.io/region)"
|
2020-07-28 20:05:32 +09:00
|
|
|
[[ -n "$org" ]] && echo " Organization : $(_blue "$org")"
|
|
|
|
[[ -n "$city" && -n "country" ]] && echo " Location : $(_blue "$city / $country")"
|
|
|
|
[[ -n "$region" ]] && echo " Region : $(_blue "$region")"
|
|
|
|
}
|
|
|
|
|
|
|
|
install_speedtest() {
|
|
|
|
if [ ! -e "./speedtest-cli/speedtest" ]; then
|
|
|
|
_64bit && sys_bit=x86_64 || sys_bit=i386
|
2021-10-08 07:54:28 +09:00
|
|
|
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.0.0-${sys_bit}-linux.tgz"
|
2020-07-28 20:05:32 +09:00
|
|
|
url2="https://dl.lamp.sh/files/ookla-speedtest-1.0.0-${sys_bit}-linux.tgz"
|
2020-07-29 10:59:18 +09:00
|
|
|
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}
|
2020-07-28 20:05:32 +09:00
|
|
|
if [ $? -ne 0 ]; then
|
2020-07-29 10:59:18 +09:00
|
|
|
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}
|
2020-07-28 20:05:32 +09:00
|
|
|
[ $? -ne 0 ] && _red "Error: Failed to download speedtest-cli.\n" && exit 1
|
|
|
|
fi
|
|
|
|
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
|
|
|
|
rm -f speedtest.tgz
|
|
|
|
fi
|
2020-07-28 17:17:30 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
! _exists "wget" && _red "Error: wget command not found. You must be install wget command at first.\n" && exit 1
|
|
|
|
# Get System information
|
2016-11-24 22:23:24 +09:00
|
|
|
cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
|
|
|
|
cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
|
2019-05-15 08:30:08 +09:00
|
|
|
freq=$( awk -F'[ :]' '/cpu MHz/ {print $4;exit}' /proc/cpuinfo )
|
2020-07-28 20:05:32 +09:00
|
|
|
ccache=$( awk -F: '/cache size/ {cache=$2} END {print cache}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
|
2020-11-10 19:40:00 +09:00
|
|
|
tram=$( LANG=C; free -m | awk '/Mem/ {print $2}' )
|
|
|
|
uram=$( LANG=C; free -m | awk '/Mem/ {print $3}' )
|
|
|
|
swap=$( LANG=C; free -m | awk '/Swap/ {print $2}' )
|
|
|
|
uswap=$( LANG=C; free -m | awk '/Swap/ {print $3}' )
|
2016-11-25 00:58:42 +09:00
|
|
|
up=$( awk '{a=$1/86400;b=($1%86400)/3600;c=($1%3600)/60} {printf("%d days, %d hour %d min\n",a,b,c)}' /proc/uptime )
|
2020-07-31 11:58:04 +09:00
|
|
|
if _exists "w"; then
|
2020-11-10 19:40:00 +09:00
|
|
|
load=$( LANG=C; w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
|
2020-07-31 11:58:04 +09:00
|
|
|
elif _exists "uptime"; then
|
2020-11-10 19:40:00 +09:00
|
|
|
load=$( LANG=C; uptime | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
|
2020-07-31 11:58:04 +09:00
|
|
|
fi
|
2016-11-24 22:23:24 +09:00
|
|
|
opsy=$( get_opsy )
|
|
|
|
arch=$( uname -m )
|
2020-07-31 11:58:04 +09:00
|
|
|
if _exists "getconf"; then
|
|
|
|
lbit=$( getconf LONG_BIT )
|
|
|
|
else
|
|
|
|
echo ${arch} | grep -q "64" && lbit="64" || lbit="32"
|
|
|
|
fi
|
2016-11-24 22:23:24 +09:00
|
|
|
kern=$( uname -r )
|
2018-07-03 23:17:00 +09:00
|
|
|
disk_size1=($( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|devtmpfs|by-uuid|chroot|Filesystem|udev|docker' | awk '{print $2}' ))
|
|
|
|
disk_size2=($( LANG=C df -hPl | grep -wvE '\-|none|tmpfs|devtmpfs|by-uuid|chroot|Filesystem|udev|docker' | awk '{print $3}' ))
|
2018-06-03 22:27:20 +09:00
|
|
|
disk_total_size=$( calc_disk "${disk_size1[@]}" )
|
|
|
|
disk_used_size=$( calc_disk "${disk_size2[@]}" )
|
2020-07-28 17:17:30 +09:00
|
|
|
tcpctrl=$( sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}' )
|
|
|
|
check_virt
|
2016-11-24 22:23:24 +09:00
|
|
|
clear
|
|
|
|
next
|
2020-07-28 20:05:32 +09:00
|
|
|
echo " CPU Model : $(_blue "$cname")"
|
|
|
|
echo " CPU Cores : $(_blue "$cores")"
|
|
|
|
echo " CPU Frequency : $(_blue "$freq MHz")"
|
|
|
|
echo " CPU Cache : $(_blue "$ccache")"
|
|
|
|
echo " Total Disk : $(_blue "$disk_total_size GB ($disk_used_size GB Used)")"
|
|
|
|
echo " Total Mem : $(_blue "$tram MB ($uram MB Used)")"
|
|
|
|
echo " Total Swap : $(_blue "$swap MB ($uswap MB Used)")"
|
|
|
|
echo " System uptime : $(_blue "$up")"
|
|
|
|
echo " Load average : $(_blue "$load")"
|
|
|
|
echo " OS : $(_blue "$opsy")"
|
|
|
|
echo " Arch : $(_blue "$arch ($lbit Bit)")"
|
|
|
|
echo " Kernel : $(_blue "$kern")"
|
|
|
|
echo " TCP CC : $(_blue "$tcpctrl")"
|
|
|
|
echo " Virtualization : $(_blue "$virt")"
|
2020-07-28 17:17:30 +09:00
|
|
|
ipv4_info
|
2016-11-24 22:23:24 +09:00
|
|
|
next
|
2015-12-03 14:33:33 +08:00
|
|
|
io1=$( io_test )
|
2020-07-28 20:05:32 +09:00
|
|
|
echo " I/O Speed(1st run) : $(_yellow "$io1")"
|
2015-12-03 14:33:33 +08:00
|
|
|
io2=$( io_test )
|
2020-07-28 20:05:32 +09:00
|
|
|
echo " I/O Speed(2nd run) : $(_yellow "$io2")"
|
2015-12-03 14:33:33 +08:00
|
|
|
io3=$( io_test )
|
2020-07-28 20:05:32 +09:00
|
|
|
echo " I/O Speed(3rd run) : $(_yellow "$io3")"
|
2015-12-03 01:28:21 +08:00
|
|
|
ioraw1=$( echo $io1 | awk 'NR==1 {print $1}' )
|
2015-12-27 14:47:56 +08:00
|
|
|
[ "`echo $io1 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw1=$( awk 'BEGIN{print '$ioraw1' * 1024}' )
|
2015-12-03 01:28:21 +08:00
|
|
|
ioraw2=$( echo $io2 | awk 'NR==1 {print $1}' )
|
2015-12-27 14:47:56 +08:00
|
|
|
[ "`echo $io2 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw2=$( awk 'BEGIN{print '$ioraw2' * 1024}' )
|
2015-12-03 01:28:21 +08:00
|
|
|
ioraw3=$( echo $io3 | awk 'NR==1 {print $1}' )
|
2015-12-27 14:47:56 +08:00
|
|
|
[ "`echo $io3 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw3=$( awk 'BEGIN{print '$ioraw3' * 1024}' )
|
2015-12-02 22:16:44 +08:00
|
|
|
ioall=$( awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}' )
|
2016-11-25 16:24:37 +09:00
|
|
|
ioavg=$( awk 'BEGIN{printf "%.1f", '$ioall' / 3}' )
|
2020-07-28 20:05:32 +09:00
|
|
|
echo -e " Average I/O speed : $(_yellow "$ioavg MB/s")"
|
2016-11-24 22:38:36 +09:00
|
|
|
next
|
2020-07-29 09:52:17 +09:00
|
|
|
install_speedtest && printf "%-18s%-18s%-20s%-12s\n" " Node Name" "Upload Speed" "Download Speed" "Latency"
|
2020-07-28 20:05:32 +09:00
|
|
|
speed && rm -fr speedtest-cli
|
2020-07-28 17:17:30 +09:00
|
|
|
next
|