mirror of
https://github.com/teddysun/across.git
synced 2025-01-18 22:09:35 +08:00
Improve shell script by shellcheck
Signed-off-by: Teddysun <i@teddysun.com>
This commit is contained in:
parent
5d975e90ef
commit
9d94a26b12
113
bench.sh
113
bench.sh
@ -57,12 +57,16 @@ next() {
|
|||||||
|
|
||||||
speed_test() {
|
speed_test() {
|
||||||
local nodeName="$2"
|
local nodeName="$2"
|
||||||
[ -z "$1" ] && ./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1 || \
|
if [ -z "$1" ];then
|
||||||
./speedtest-cli/speedtest --progress=no --server-id=$1 --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
|
./speedtest-cli/speedtest --progress=no --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
|
||||||
|
else
|
||||||
|
./speedtest-cli/speedtest --progress=no --server-id="$1" --accept-license --accept-gdpr >./speedtest-cli/speedtest.log 2>&1
|
||||||
|
fi
|
||||||
if [ $? -eq 0 ]; then
|
if [ $? -eq 0 ]; then
|
||||||
local dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
local dl_speed up_speed latency
|
||||||
local up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
dl_speed=$(awk '/Download/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
||||||
local latency=$(awk '/Latency/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
up_speed=$(awk '/Upload/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
||||||
|
latency=$(awk '/Latency/{print $3" "$4}' ./speedtest-cli/speedtest.log)
|
||||||
if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
|
if [[ -n "${dl_speed}" && -n "${up_speed}" && -n "${latency}" ]]; then
|
||||||
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}"
|
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}"
|
||||||
fi
|
fi
|
||||||
@ -86,7 +90,7 @@ speed() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
io_test() {
|
io_test() {
|
||||||
(LANG=C dd if=/dev/zero of=benchtest_$$ bs=512k count=$1 conv=fdatasync && rm -f benchtest_$$) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
|
(LANG=C dd if=/dev/zero of=benchtest_$$ bs=512k count="$1" conv=fdatasync && rm -f benchtest_$$) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
|
||||||
}
|
}
|
||||||
|
|
||||||
calc_size() {
|
calc_size() {
|
||||||
@ -111,7 +115,7 @@ calc_size() {
|
|||||||
echo "${total_size}"
|
echo "${total_size}"
|
||||||
return
|
return
|
||||||
fi
|
fi
|
||||||
total_size=$(awk 'BEGIN{printf "%.1f", '$raw' / '$num'}')
|
total_size=$(awk 'BEGIN{printf "%.1f", '"$raw"' / '$num'}')
|
||||||
echo "${total_size} ${unit}"
|
echo "${total_size} ${unit}"
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -119,14 +123,15 @@ calc_size() {
|
|||||||
# to_kibyte converts zfs size from bytes to kilobyte
|
# to_kibyte converts zfs size from bytes to kilobyte
|
||||||
to_kibyte() {
|
to_kibyte() {
|
||||||
local raw=$1
|
local raw=$1
|
||||||
echo ${raw} / 1024
|
awk 'BEGIN{printf "%.0f", '"$raw"' / 1024}'
|
||||||
}
|
}
|
||||||
|
|
||||||
sum() {
|
calc_sum() {
|
||||||
local arr=("$@")
|
local arr=("$@")
|
||||||
local s=0
|
local s
|
||||||
|
s=0
|
||||||
for i in "${arr[@]}"; do
|
for i in "${arr[@]}"; do
|
||||||
s=$(($s + $i))
|
s=$((s + i))
|
||||||
done
|
done
|
||||||
echo ${s}
|
echo ${s}
|
||||||
}
|
}
|
||||||
@ -188,20 +193,21 @@ check_virt() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ipv4_info() {
|
ipv4_info() {
|
||||||
local org="$(wget -q -T10 -O- ipinfo.io/org)"
|
local org city country region
|
||||||
local city="$(wget -q -T10 -O- ipinfo.io/city)"
|
org="$(wget -q -T10 -O- ipinfo.io/org)"
|
||||||
local country="$(wget -q -T10 -O- ipinfo.io/country)"
|
city="$(wget -q -T10 -O- ipinfo.io/city)"
|
||||||
local region="$(wget -q -T10 -O- ipinfo.io/region)"
|
country="$(wget -q -T10 -O- ipinfo.io/country)"
|
||||||
if [[ -n "$org" ]]; then
|
region="$(wget -q -T10 -O- ipinfo.io/region)"
|
||||||
echo " Organization : $(_blue "$org")"
|
if [[ -n "${org}" ]]; then
|
||||||
|
echo " Organization : $(_blue "${org}")"
|
||||||
fi
|
fi
|
||||||
if [[ -n "$city" && -n "country" ]]; then
|
if [[ -n "${city}" && -n "${country}" ]]; then
|
||||||
echo " Location : $(_blue "$city / $country")"
|
echo " Location : $(_blue "${city} / ${country}")"
|
||||||
fi
|
fi
|
||||||
if [[ -n "$region" ]]; then
|
if [[ -n "${region}" ]]; then
|
||||||
echo " Region : $(_yellow "$region")"
|
echo " Region : $(_yellow "${region}")"
|
||||||
fi
|
fi
|
||||||
if [[ -z "$org" ]]; then
|
if [[ -z "${org}" ]]; then
|
||||||
echo " Region : $(_red "No ISP detected")"
|
echo " Region : $(_red "No ISP detected")"
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
@ -209,9 +215,10 @@ ipv4_info() {
|
|||||||
install_speedtest() {
|
install_speedtest() {
|
||||||
if [ ! -e "./speedtest-cli/speedtest" ]; then
|
if [ ! -e "./speedtest-cli/speedtest" ]; then
|
||||||
sys_bit=""
|
sys_bit=""
|
||||||
local sysarch="$(uname -m)"
|
local sysarch
|
||||||
|
sysarch="$(uname -m)"
|
||||||
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
|
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
|
||||||
local sysarch="$(arch)"
|
sysarch="$(arch)"
|
||||||
fi
|
fi
|
||||||
if [ "${sysarch}" = "x86_64" ]; then
|
if [ "${sysarch}" = "x86_64" ]; then
|
||||||
sys_bit="x86_64"
|
sys_bit="x86_64"
|
||||||
@ -231,10 +238,10 @@ install_speedtest() {
|
|||||||
[ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1
|
[ -z "${sys_bit}" ] && _red "Error: Unsupported system architecture (${sysarch}).\n" && exit 1
|
||||||
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
|
url1="https://install.speedtest.net/app/cli/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
|
||||||
url2="https://dl.lamp.sh/files/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
|
url2="https://dl.lamp.sh/files/ookla-speedtest-1.2.0-linux-${sys_bit}.tgz"
|
||||||
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}
|
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}; then
|
||||||
if [ $? -ne 0 ]; then
|
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}; then
|
||||||
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}
|
_red "Error: Failed to download speedtest-cli.\n" && exit 1
|
||||||
[ $? -ne 0 ] && _red "Error: Failed to download speedtest-cli.\n" && exit 1
|
fi
|
||||||
fi
|
fi
|
||||||
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
|
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
|
||||||
rm -f speedtest.tgz
|
rm -f speedtest.tgz
|
||||||
@ -260,22 +267,22 @@ get_system_info() {
|
|||||||
LANG=C
|
LANG=C
|
||||||
free | awk '/Mem/ {print $2}'
|
free | awk '/Mem/ {print $2}'
|
||||||
)
|
)
|
||||||
tram=$(calc_size $tram)
|
tram=$(calc_size "$tram")
|
||||||
uram=$(
|
uram=$(
|
||||||
LANG=C
|
LANG=C
|
||||||
free | awk '/Mem/ {print $3}'
|
free | awk '/Mem/ {print $3}'
|
||||||
)
|
)
|
||||||
uram=$(calc_size $uram)
|
uram=$(calc_size "$uram")
|
||||||
swap=$(
|
swap=$(
|
||||||
LANG=C
|
LANG=C
|
||||||
free | awk '/Swap/ {print $2}'
|
free | awk '/Swap/ {print $2}'
|
||||||
)
|
)
|
||||||
swap=$(calc_size $swap)
|
swap=$(calc_size "$swap")
|
||||||
uswap=$(
|
uswap=$(
|
||||||
LANG=C
|
LANG=C
|
||||||
free | awk '/Swap/ {print $3}'
|
free | awk '/Swap/ {print $3}'
|
||||||
)
|
)
|
||||||
uswap=$(calc_size $uswap)
|
uswap=$(calc_size "$uswap")
|
||||||
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)
|
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)
|
||||||
if _exists "w"; then
|
if _exists "w"; then
|
||||||
load=$(
|
load=$(
|
||||||
@ -293,7 +300,7 @@ get_system_info() {
|
|||||||
if _exists "getconf"; then
|
if _exists "getconf"; then
|
||||||
lbit=$(getconf LONG_BIT)
|
lbit=$(getconf LONG_BIT)
|
||||||
else
|
else
|
||||||
echo ${arch} | grep -q "64" && lbit="64" || lbit="32"
|
echo "${arch}" | grep -q "64" && lbit="64" || lbit="32"
|
||||||
fi
|
fi
|
||||||
kern=$(uname -r)
|
kern=$(uname -r)
|
||||||
in_kernel_no_swap_total_size=$(
|
in_kernel_no_swap_total_size=$(
|
||||||
@ -301,15 +308,15 @@ get_system_info() {
|
|||||||
df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $2 }'
|
df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $2 }'
|
||||||
)
|
)
|
||||||
swap_total_size=$(free -k | grep Swap | awk '{print $2}')
|
swap_total_size=$(free -k | grep Swap | awk '{print $2}')
|
||||||
zfs_total_size=$(to_kibyte $(sum $(zpool list -o size -Hp 2> /dev/null)))
|
zfs_total_size=$(to_kibyte "$(calc_sum "$(zpool list -o size -Hp 2> /dev/null)")")
|
||||||
disk_total_size=$(calc_size $(($swap_total_size + $in_kernel_no_swap_total_size + $zfs_total_size)))
|
disk_total_size=$(calc_size $((swap_total_size + in_kernel_no_swap_total_size + zfs_total_size)))
|
||||||
in_kernel_no_swap_used_size=$(
|
in_kernel_no_swap_used_size=$(
|
||||||
LANG=C
|
LANG=C
|
||||||
df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $3 }'
|
df -t simfs -t ext2 -t ext3 -t ext4 -t btrfs -t xfs -t vfat -t ntfs --total 2>/dev/null | grep total | awk '{ print $3 }'
|
||||||
)
|
)
|
||||||
swap_used_size=$(free -k | grep Swap | awk '{print $3}')
|
swap_used_size=$(free -k | grep Swap | awk '{print $3}')
|
||||||
zfs_used_size=$(to_kibyte $(sum $(zpool list -o allocated -Hp 2> /dev/null)))
|
zfs_used_size=$(to_kibyte "$(calc_sum "$(zpool list -o allocated -Hp 2> /dev/null)")")
|
||||||
disk_used_size=$(calc_size $(($swap_used_size + $in_kernel_no_swap_used_size + $zfs_used_size)))
|
disk_used_size=$(calc_size $((swap_used_size + in_kernel_no_swap_used_size + zfs_used_size)))
|
||||||
tcpctrl=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}')
|
tcpctrl=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}')
|
||||||
}
|
}
|
||||||
# Print System information
|
# Print System information
|
||||||
@ -357,7 +364,7 @@ print_io_test() {
|
|||||||
if [ -z "${freespace}" ]; then
|
if [ -z "${freespace}" ]; then
|
||||||
freespace=$(df -m . | awk 'NR==3 {print $3}')
|
freespace=$(df -m . | awk 'NR==3 {print $3}')
|
||||||
fi
|
fi
|
||||||
if [ ${freespace} -gt 1024 ]; then
|
if [ "${freespace}" -gt 1024 ]; then
|
||||||
writemb=2048
|
writemb=2048
|
||||||
io1=$(io_test ${writemb})
|
io1=$(io_test ${writemb})
|
||||||
echo " I/O Speed(1st run) : $(_yellow "$io1")"
|
echo " I/O Speed(1st run) : $(_yellow "$io1")"
|
||||||
@ -365,14 +372,14 @@ print_io_test() {
|
|||||||
echo " I/O Speed(2nd run) : $(_yellow "$io2")"
|
echo " I/O Speed(2nd run) : $(_yellow "$io2")"
|
||||||
io3=$(io_test ${writemb})
|
io3=$(io_test ${writemb})
|
||||||
echo " I/O Speed(3rd run) : $(_yellow "$io3")"
|
echo " I/O Speed(3rd run) : $(_yellow "$io3")"
|
||||||
ioraw1=$(echo $io1 | awk 'NR==1 {print $1}')
|
ioraw1=$(echo "$io1" | awk 'NR==1 {print $1}')
|
||||||
[ "$(echo $io1 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw1=$(awk 'BEGIN{print '$ioraw1' * 1024}')
|
[ "$(echo "$io1" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw1=$(awk 'BEGIN{print '"$ioraw1"' * 1024}')
|
||||||
ioraw2=$(echo $io2 | awk 'NR==1 {print $1}')
|
ioraw2=$(echo "$io2" | awk 'NR==1 {print $1}')
|
||||||
[ "$(echo $io2 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw2=$(awk 'BEGIN{print '$ioraw2' * 1024}')
|
[ "$(echo "$io2" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw2=$(awk 'BEGIN{print '"$ioraw2"' * 1024}')
|
||||||
ioraw3=$(echo $io3 | awk 'NR==1 {print $1}')
|
ioraw3=$(echo "$io3" | awk 'NR==1 {print $1}')
|
||||||
[ "$(echo $io3 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw3=$(awk 'BEGIN{print '$ioraw3' * 1024}')
|
[ "$(echo "$io3" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw3=$(awk 'BEGIN{print '"$ioraw3"' * 1024}')
|
||||||
ioall=$(awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}')
|
ioall=$(awk 'BEGIN{print '"$ioraw1"' + '"$ioraw2"' + '"$ioraw3"'}')
|
||||||
ioavg=$(awk 'BEGIN{printf "%.1f", '$ioall' / 3}')
|
ioavg=$(awk 'BEGIN{printf "%.1f", '"$ioall"' / 3}')
|
||||||
echo " I/O Speed(average) : $(_yellow "$ioavg MB/s")"
|
echo " I/O Speed(average) : $(_yellow "$ioavg MB/s")"
|
||||||
else
|
else
|
||||||
echo " $(_red "Not enough space for I/O Speed test!")"
|
echo " $(_red "Not enough space for I/O Speed test!")"
|
||||||
@ -381,10 +388,10 @@ print_io_test() {
|
|||||||
|
|
||||||
print_end_time() {
|
print_end_time() {
|
||||||
end_time=$(date +%s)
|
end_time=$(date +%s)
|
||||||
time=$((${end_time} - ${start_time}))
|
time=$((end_time - start_time))
|
||||||
if [ ${time} -gt 60 ]; then
|
if [ ${time} -gt 60 ]; then
|
||||||
min=$(expr $time / 60)
|
min=$((time / 60))
|
||||||
sec=$(expr $time % 60)
|
sec=$((time % 60))
|
||||||
echo " Finished in : ${min} min ${sec} sec"
|
echo " Finished in : ${min} min ${sec} sec"
|
||||||
else
|
else
|
||||||
echo " Finished in : ${time} sec"
|
echo " Finished in : ${time} sec"
|
||||||
@ -396,11 +403,11 @@ print_end_time() {
|
|||||||
! _exists "wget" && _red "Error: wget command not found.\n" && exit 1
|
! _exists "wget" && _red "Error: wget command not found.\n" && exit 1
|
||||||
! _exists "free" && _red "Error: free command not found.\n" && exit 1
|
! _exists "free" && _red "Error: free command not found.\n" && exit 1
|
||||||
# check for curl/wget
|
# check for curl/wget
|
||||||
command -v curl >/dev/null 2>&1 && local_curl=true || unset local_curl
|
_exists "curl" && local_curl=true
|
||||||
# test if the host has IPv4/IPv6 connectivity
|
# test if the host has IPv4/IPv6 connectivity
|
||||||
[[ ! -z ${local_curl} ]] && ip_check_cmd="curl -s -m 4" || ip_check_cmd="wget -qO- -T 4"
|
[[ -n ${local_curl} ]] && ip_check_cmd="curl -s -m 4" || ip_check_cmd="wget -qO- -T 4"
|
||||||
ipv4_check=$((ping -4 -c 1 -W 4 ipv4.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -4 icanhazip.com 2> /dev/null)
|
ipv4_check=$( (ping -4 -c 1 -W 4 ipv4.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -4 icanhazip.com 2> /dev/null)
|
||||||
ipv6_check=$((ping -6 -c 1 -W 4 ipv6.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -6 icanhazip.com 2> /dev/null)
|
ipv6_check=$( (ping -6 -c 1 -W 4 ipv6.google.com >/dev/null 2>&1 && echo true) || ${ip_check_cmd} -6 icanhazip.com 2> /dev/null)
|
||||||
if [[ -z "$ipv4_check" && -z "$ipv6_check" ]]; then
|
if [[ -z "$ipv4_check" && -z "$ipv6_check" ]]; then
|
||||||
_yellow "Warning: Both IPv4 and IPv6 connectivity were not detected.\n"
|
_yellow "Warning: Both IPv4 and IPv6 connectivity were not detected.\n"
|
||||||
fi
|
fi
|
||||||
|
Loading…
x
Reference in New Issue
Block a user