VPS云主机测试脚本

  • A+
所属分类:资源分享

该段脚本来源于互联网,这里做了些简单的修改,主要删除了一些无用的代码,内部测试规则没有任何修改。

本段代码可以显示出云主机使用的cpu型号,主频大小,缓存大小,硬盘容量和内存等信息,并测试硬盘的读写速度,以及下载速度(一般云主机服务商的下行速度不限制)

  1. #!/usr/bin/env bash
  2. if  [ ! -e '/usr/bin/wget' ]; then
  3.     echo "Error: wget command not found. You must be install wget command at first."
  4.     exit 1
  5. fi
  6. RED='\033[0;31m'
  7. GREEN='\033[0;32m'
  8. YELLOW='\033[0;33m'
  9. PLAIN='\033[0m'
  10. get_opsy() {
  11.     [ -f /etc/redhat-release ] && awk '{print ($1,$3~/^[0-9]/?$3:$4)}' /etc/redhat-release && return
  12.     [ -f /etc/os-release ] && awk -F'[= "]' '/PRETTY_NAME/{print $3,$4,$5}' /etc/os-release && return
  13.     [ -f /etc/lsb-release ] && awk -F'[="]+' '/DESCRIPTION/{print $2}' /etc/lsb-release && return
  14. }
  15. next() {
  16.     printf "%-70s\n" "-" | sed 's/\s/-/g'
  17. }
  18. speed_test() {
  19.     local speedtest=$(wget -4O /dev/null -T300 $1 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}')
  20.     local ipaddress=$(ping -c1 -n `awk -F'/' '{print $3}' <<< $1` | awk -F'[()]' '{print $2;exit}')
  21.     local nodeName=$2
  22.     printf "${YELLOW}%-32s${GREEN}%-24s${RED}%-14s${PLAIN}\n" "${nodeName}" "${ipaddress}" "${speedtest}"
  23. }
  24. speed_test_v6() {
  25.     local speedtest=$(wget -6O /dev/null -T300 $1 2>&1 | awk '/\/dev\/null/ {speed=$3 $4} END {gsub(/\(|\)/,"",speed); print speed}')
  26.     local ipaddress=$(ping6 -c1 -n `awk -F'/' '{print $3}' <<< $1` | awk -F'[()]' '{print $2;exit}')
  27.     local nodeName=$2
  28.     printf "${YELLOW}%-32s${GREEN}%-24s${RED}%-14s${PLAIN}\n" "${nodeName}" "${ipaddress}" "${speedtest}"
  29. }
  30. speed() {
  31.     speed_test 'http://cachefly.cachefly.net/100mb.test' 'CacheFly'
  32.     speed_test 'http://speedtest.tokyo.linode.com/100MB-tokyo.bin' 'Linode, Tokyo, JP'
  33.     speed_test 'http://speedtest.singapore.linode.com/100MB-singapore.bin' 'Linode, Singapore, SG'
  34.     speed_test 'http://speedtest.london.linode.com/100MB-london.bin' 'Linode, London, UK'
  35.     speed_test 'http://speedtest.frankfurt.linode.com/100MB-frankfurt.bin' 'Linode, Frankfurt, DE'
  36.     speed_test 'http://speedtest.fremont.linode.com/100MB-fremont.bin' 'Linode, Fremont, CA'
  37.     speed_test 'http://speedtest.dal05.softlayer.com/downloads/test100.zip' 'Softlayer, Dallas, TX'
  38.     speed_test 'http://speedtest.sea01.softlayer.com/downloads/test100.zip' 'Softlayer, Seattle, WA'
  39.     speed_test 'http://speedtest.fra02.softlayer.com/downloads/test100.zip' 'Softlayer, Frankfurt, DE'
  40.     speed_test 'http://speedtest.sng01.softlayer.com/downloads/test100.zip' 'Softlayer, Singapore, SG'
  41.     speed_test 'http://speedtest.hkg02.softlayer.com/downloads/test100.zip' 'Softlayer, HongKong, CN'
  42. }
  43. io_test() {
  44.     (LANG=C dd if=/dev/zero of=test_$$ bs=64k count=16k conv=fdatasync && rm -f test_$$ ) 2>&1 | awk -F, '{io=$NF} END { print io}' | sed 's/^[ \t]*//;s/[ \t]*$//'
  45. }
  46. calc_disk() {
  47.     local total_size=0
  48.     local array=$@
  49.     for size in ${array[@]}
  50.     do
  51.         [ "${size}" == "0" ] && size_t=0 || size_t=`echo ${size:0:${#size}-1}`
  52.         [ "`echo ${size:(-1)}`" == "K" ] && size=0
  53.         [ "`echo ${size:(-1)}`" == "M" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' / 1024}' )
  54.         [ "`echo ${size:(-1)}`" == "T" ] && size=$( awk 'BEGIN{printf "%.1f", '$size_t' * 1024}' )
  55.         [ "`echo ${size:(-1)}`" == "G" ] && size=${size_t}
  56.         total_size=$( awk 'BEGIN{printf "%.1f", '$total_size' + '$size'}' )
  57.     done
  58.     echo ${total_size}
  59. }
  60. cname=$( awk -F: '/model name/ {name=$2} END {print name}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
  61. cores=$( awk -F: '/model name/ {core++} END {print core}' /proc/cpuinfo )
  62. freq=$( awk -F: '/cpu MHz/ {freq=$2} END {print freq}' /proc/cpuinfo | sed 's/^[ \t]*//;s/[ \t]*$//' )
  63. tram=$( free -m | awk '/Mem/ {print $2}' )
  64. uram=$( free -m | awk '/Mem/ {print $3}' )
  65. swap=$( free -m | awk '/Swap/ {print $2}' )
  66. uswap=$( free -m | awk '/Swap/ {print $3}' )
  67. 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 )
  68. load=$( w | head -1 | awk -F'load average:' '{print $2}' | sed 's/^[ \t]*//;s/[ \t]*$//' )
  69. opsy=$( get_opsy )
  70. arch=$( uname -m )
  71. lbit=$( getconf LONG_BIT )
  72. kern=$( uname -r )
  73. ipv6=$( wget -qO- -t1 -T2 ipv6.icanhazip.com )
  74. disk_size1=($( LANG=C df -ahPl | grep -wvE '\-|none|tmpfs|devtmpfs|by-uuid|chroot|Filesystem' | awk '{print $2}' ))
  75. disk_size2=($( LANG=C df -ahPl | grep -wvE '\-|none|tmpfs|devtmpfs|by-uuid|chroot|Filesystem' | awk '{print $3}' ))
  76. disk_total_size=$( calc_disk ${disk_size1[@]} )
  77. disk_used_size=$( calc_disk ${disk_size2[@]} )
  78. clear
  79. next
  80. echo "CPU model            : $cname"
  81. echo "Number of cores      : $cores"
  82. echo "CPU frequency        : $freq MHz"
  83. echo "Total size of Disk   : $disk_total_size GB ($disk_used_size GB Used)"
  84. echo "Total amount of Mem  : $tram MB ($uram MB Used)"
  85. echo "Total amount of Swap : $swap MB ($uswap MB Used)"
  86. echo "System uptime        : $up"
  87. echo "Load average         : $load"
  88. echo "OS                   : $opsy"
  89. echo "Arch                 : $arch ($lbit Bit)"
  90. echo "Kernel               : $kern"
  91. next
  92. io1=$( io_test )
  93. echo "I/O speed(1st run)   : $io1"
  94. io2=$( io_test )
  95. echo "I/O speed(2nd run)   : $io2"
  96. io3=$( io_test )
  97. echo "I/O speed(3rd run)   : $io3"
  98. ioraw1=$( echo $io1 | awk 'NR==1 {print $1}' )
  99. "`echo $io1 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw1=$( awk 'BEGIN{print '$ioraw1' * 1024}' )
  100. ioraw2=$( echo $io2 | awk 'NR==1 {print $1}' )
  101. "`echo $io2 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw2=$( awk 'BEGIN{print '$ioraw2' * 1024}' )
  102. ioraw3=$( echo $io3 | awk 'NR==1 {print $1}' )
  103. "`echo $io3 | awk 'NR==1 {print $2}'`" == "GB/s" ] && ioraw3=$( awk 'BEGIN{print '$ioraw3' * 1024}' )
  104. ioall=$( awk 'BEGIN{print '$ioraw1' + '$ioraw2' + '$ioraw3'}' )
  105. ioavg=$( awk 'BEGIN{printf "%.1f", '$ioall' / 3}' )
  106. echo "Average I/O speed    : $ioavg MB/s"
  107. next
  108. printf "%-32s%-24s%-14s\n" "Node Name" "IPv4 address" "Download Speed"
  109. speed && next

使用方式特别简单,在云主机使用vi命令新建个文件,将上述脚本复制到文件中并赋予执行权限,直接运行即可。

注意:在测试下载速度时,会因为网络状况的不同,测试时间不同,如果不想等了,可以直接用ctrl+c终止运行。

  • 问答中心
  • 微信小程序扫一扫
  • weinxin
  • 微信公众
  • 微信公众号扫一扫
  • weinxin

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

目前评论:33   其中:访客  33   博主  0

    • avatar Digital Marketing Course

      Leading Digital Marketing agency in India and Kolkata, Leading Web Design company. Providing web designing and Digital marketing services, Digital marketing course, and certification with an internship.

      • avatar Investments

        Good blog you have got here.. It’s hard to find good quality writing like yours nowadays. I seriously appreciate people like you! Take care!!

        • avatar Immobilienbewertung Limbach-Oberfrohna

          Some truly wonderful posts on this site, thankyou for contribution.