Skip to content

Commit

Permalink
Improve shell script by shellcheck
Browse files Browse the repository at this point in the history
Signed-off-by: Teddysun <i@teddysun.com>
  • Loading branch information
teddysun committed Oct 24, 2023
1 parent 5d975e9 commit 9d94a26
Showing 1 changed file with 60 additions and 53 deletions.
113 changes: 60 additions & 53 deletions bench.sh
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,16 @@ next() {

speed_test() {
local nodeName="$2"
[ -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
if [ -z "$1" ];then
./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
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 $3" "$4}' ./speedtest-cli/speedtest.log)
local dl_speed up_speed latency
dl_speed=$(awk '/Download/{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
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
Expand All @@ -86,7 +90,7 @@ speed() {
}

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() {
Expand All @@ -111,22 +115,23 @@ calc_size() {
echo "${total_size}"
return
fi
total_size=$(awk 'BEGIN{printf "%.1f", '$raw' / '$num'}')
total_size=$(awk 'BEGIN{printf "%.1f", '"$raw"' / '$num'}')
echo "${total_size} ${unit}"
}

# since calc_size converts kilobyte to MB, GB and TB
# to_kibyte converts zfs size from bytes to kilobyte
to_kibyte() {
local raw=$1
echo ${raw} / 1024
awk 'BEGIN{printf "%.0f", '"$raw"' / 1024}'
}

sum() {
calc_sum() {
local arr=("$@")
local s=0
local s
s=0
for i in "${arr[@]}"; do
s=$(($s + $i))
s=$((s + i))
done
echo ${s}
}
Expand Down Expand Up @@ -188,30 +193,32 @@ check_virt() {
}

ipv4_info() {
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)"
if [[ -n "$org" ]]; then
echo " Organization : $(_blue "$org")"
local org city country region
org="$(wget -q -T10 -O- ipinfo.io/org)"
city="$(wget -q -T10 -O- ipinfo.io/city)"
country="$(wget -q -T10 -O- ipinfo.io/country)"
region="$(wget -q -T10 -O- ipinfo.io/region)"
if [[ -n "${org}" ]]; then
echo " Organization : $(_blue "${org}")"
fi
if [[ -n "$city" && -n "country" ]]; then
echo " Location : $(_blue "$city / $country")"
if [[ -n "${city}" && -n "${country}" ]]; then
echo " Location : $(_blue "${city} / ${country}")"
fi
if [[ -n "$region" ]]; then
echo " Region : $(_yellow "$region")"
if [[ -n "${region}" ]]; then
echo " Region : $(_yellow "${region}")"
fi
if [[ -z "$org" ]]; then
if [[ -z "${org}" ]]; then
echo " Region : $(_red "No ISP detected")"
fi
}

install_speedtest() {
if [ ! -e "./speedtest-cli/speedtest" ]; then
sys_bit=""
local sysarch="$(uname -m)"
local sysarch
sysarch="$(uname -m)"
if [ "${sysarch}" = "unknown" ] || [ "${sysarch}" = "" ]; then
local sysarch="$(arch)"
sysarch="$(arch)"
fi
if [ "${sysarch}" = "x86_64" ]; then
sys_bit="x86_64"
Expand All @@ -231,10 +238,10 @@ install_speedtest() {
[ -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"
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 [ $? -ne 0 ]; then
wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}
[ $? -ne 0 ] && _red "Error: Failed to download speedtest-cli.\n" && exit 1
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url1}; then
if ! wget --no-check-certificate -q -T10 -O speedtest.tgz ${url2}; then
_red "Error: Failed to download speedtest-cli.\n" && exit 1
fi
fi
mkdir -p speedtest-cli && tar zxf speedtest.tgz -C ./speedtest-cli && chmod +x ./speedtest-cli/speedtest
rm -f speedtest.tgz
Expand All @@ -260,22 +267,22 @@ get_system_info() {
LANG=C
free | awk '/Mem/ {print $2}'
)
tram=$(calc_size $tram)
tram=$(calc_size "$tram")
uram=$(
LANG=C
free | awk '/Mem/ {print $3}'
)
uram=$(calc_size $uram)
uram=$(calc_size "$uram")
swap=$(
LANG=C
free | awk '/Swap/ {print $2}'
)
swap=$(calc_size $swap)
swap=$(calc_size "$swap")
uswap=$(
LANG=C
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)
if _exists "w"; then
load=$(
Expand All @@ -293,23 +300,23 @@ get_system_info() {
if _exists "getconf"; then
lbit=$(getconf LONG_BIT)
else
echo ${arch} | grep -q "64" && lbit="64" || lbit="32"
echo "${arch}" | grep -q "64" && lbit="64" || lbit="32"
fi
kern=$(uname -r)
in_kernel_no_swap_total_size=$(
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 $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)))
disk_total_size=$(calc_size $(($swap_total_size + $in_kernel_no_swap_total_size + $zfs_total_size)))
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)))
in_kernel_no_swap_used_size=$(
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 }'
)
swap_used_size=$(free -k | grep Swap | awk '{print $3}')
zfs_used_size=$(to_kibyte $(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)))
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)))
tcpctrl=$(sysctl net.ipv4.tcp_congestion_control | awk -F ' ' '{print $3}')
}
# Print System information
Expand Down Expand Up @@ -357,22 +364,22 @@ print_io_test() {
if [ -z "${freespace}" ]; then
freespace=$(df -m . | awk 'NR==3 {print $3}')
fi
if [ ${freespace} -gt 1024 ]; then
if [ "${freespace}" -gt 1024 ]; then
writemb=2048
io1=$(io_test ${writemb})
echo " I/O Speed(1st run) : $(_yellow "$io1")"
io2=$(io_test ${writemb})
echo " I/O Speed(2nd run) : $(_yellow "$io2")"
io3=$(io_test ${writemb})
echo " I/O Speed(3rd run) : $(_yellow "$io3")"
ioraw1=$(echo $io1 | awk 'NR==1 {print $1}')
[ "$(echo $io1 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw1=$(awk 'BEGIN{print '$ioraw1' * 1024}')
ioraw2=$(echo $io2 | awk 'NR==1 {print $1}')
[ "$(echo $io2 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw2=$(awk 'BEGIN{print '$ioraw2' * 1024}')
ioraw3=$(echo $io3 | awk 'NR==1 {print $1}')
[ "$(echo $io3 | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw3=$(awk 'BEGIN{print '$ioraw3' * 1024}')
ioall=$(awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}')
ioavg=$(awk 'BEGIN{printf "%.1f", '$ioall' / 3}')
ioraw1=$(echo "$io1" | awk 'NR==1 {print $1}')
[ "$(echo "$io1" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw1=$(awk 'BEGIN{print '"$ioraw1"' * 1024}')
ioraw2=$(echo "$io2" | awk 'NR==1 {print $1}')
[ "$(echo "$io2" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw2=$(awk 'BEGIN{print '"$ioraw2"' * 1024}')
ioraw3=$(echo "$io3" | awk 'NR==1 {print $1}')
[ "$(echo "$io3" | awk 'NR==1 {print $2}')" == "GB/s" ] && ioraw3=$(awk 'BEGIN{print '"$ioraw3"' * 1024}')
ioall=$(awk 'BEGIN{print '"$ioraw1"' + '"$ioraw2"' + '"$ioraw3"'}')
ioavg=$(awk 'BEGIN{printf "%.1f", '"$ioall"' / 3}')
echo " I/O Speed(average) : $(_yellow "$ioavg MB/s")"
else
echo " $(_red "Not enough space for I/O Speed test!")"
Expand All @@ -381,10 +388,10 @@ print_io_test() {

print_end_time() {
end_time=$(date +%s)
time=$((${end_time} - ${start_time}))
time=$((end_time - start_time))
if [ ${time} -gt 60 ]; then
min=$(expr $time / 60)
sec=$(expr $time % 60)
min=$((time / 60))
sec=$((time % 60))
echo " Finished in : ${min} min ${sec} sec"
else
echo " Finished in : ${time} sec"
Expand All @@ -396,11 +403,11 @@ print_end_time() {
! _exists "wget" && _red "Error: wget command not found.\n" && exit 1
! _exists "free" && _red "Error: free command not found.\n" && exit 1
# 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
[[ ! -z ${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)
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)
[[ -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)
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
_yellow "Warning: Both IPv4 and IPv6 connectivity were not detected.\n"
fi
Expand Down

4 comments on commit 9d94a26

@meliber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks the zfs usage calculation.

@teddysun
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This commit breaks the zfs usage calculation.

Please paste a screenshot or test result

@meliber
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Test os: Proxmox VE 8 with root filesystem on ZFS
ISP info is redacted.

current version:

-------------------- A Bench.sh Script By Teddysun -------------------
 Version            : v2023-10-15
 Usage              : wget -qO- bench.sh | bash
----------------------------------------------------------------------
 CPU Model          : Intel(R) Celeron(R) N5105 @ 2.00GHz
 CPU Cores          : 4 @ 2799.988 MHz
 CPU Cache          : 4096 KB
 AES-NI             : ✓ Enabled
 VM-x/AMD-V         : ✓ Enabled
 Total Disk         : 0 (0 Used)
 Total Mem          : 31.2 GB (9.8 GB Used)
 System uptime      : 4 days, 18 hour 53 min
 Load average       : 0.09, 0.09, 0.05
 OS                 : Debian GNU/Linux 12
 Arch               : x86_64 (64 Bit)
 Kernel             : 6.2.16-15-pve
 TCP CC             : cubic
 Virtualization     : Dedicated
 IPv4/IPv6          : ✓ Online / ✗ Offline
 Organization       : 
 Location           : 
 Region             : 
----------------------------------------------------------------------
 I/O Speed(1st run) : 2.5 GB/s
 I/O Speed(2nd run) : 2.5 GB/s
 I/O Speed(3rd run) : 2.6 GB/s
 I/O Speed(average) : 2594.1 MB/s
----------------------------------------------------------------------

reset one commit:

HEAD is now at 5d975e9 Improve disk usage calculation (#115)
-------------------- A Bench.sh Script By Teddysun -------------------
 Version            : v2023-10-15
 Usage              : wget -qO- bench.sh | bash
----------------------------------------------------------------------
 CPU Model          : Intel(R) Celeron(R) N5105 @ 2.00GHz
 CPU Cores          : 4 @ 2799.955 MHz
 CPU Cache          : 4096 KB
 AES-NI             : ✓ Enabled
 VM-x/AMD-V         : ✓ Enabled
 Total Disk         : 2.7 TB (702.9 GB Used)
 Total Mem          : 31.2 GB (9.8 GB Used)
 System uptime      : 4 days, 18 hour 55 min
 Load average       : 0.02, 0.06, 0.04
 OS                 : Debian GNU/Linux 12
 Arch               : x86_64 (64 Bit)
 Kernel             : 6.2.16-15-pve
 TCP CC             : cubic
 Virtualization     : Dedicated
 IPv4/IPv6          : ✓ Online / ✗ Offline
 Organization       : 
 Location           : 
 Region             : 
----------------------------------------------------------------------
 I/O Speed(1st run) : 2.6 GB/s
 I/O Speed(2nd run) : 2.5 GB/s
 I/O Speed(3rd run) : 2.6 GB/s
 I/O Speed(average) : 2628.3 MB/s
----------------------------------------------------------------------

@meliber
Copy link
Contributor

@meliber meliber commented on 9d94a26 Nov 12, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.