概述
用于计算服务器出入口带宽实际速率,方便统计服务器网络信息。
脚本功能
该脚本会根据当前服务器内存剩余空间判断生成约为1.5G临时文件,并提交至指定的url地址进行出口带宽占用。通过检查默认出入口网卡流量数值来判断出入口带宽网络速率。
执行效果
脚本内容
#!/bin/bash
# 服务器出入口网络带宽速率计算
# Author: admin@ym68.cc
# Date: 2022-05-06
# Version: 0.0.3
# Description: 该脚本用于计算服务器外网出入口网络带宽速率
set +m
# 检查是否有测速请求进程未完成
ps -o pid,command|awk '/Speed_Test_File[[:space:]]/ {print $1}'|xargs -i kill {} 2> /dev/null
# 默认流量出口网卡
Default_Network_Device_Name=`route -n|awk '/^0.0.0.0/ {print $8}' |uniq`
# 临时测速文件
Temp_Speed_File="/dev/shm/Speed_Test_File"
if [ ! -f "${Temp_Speed_File}" ];then
# 剩余内存计算
Free_Mem_Info=`awk -F":[[:space:]]" '/^MemFree:/ {print $2}' /proc/meminfo |awk '{printf "%.0F",$1/1024}'`
if [ "${Free_Mem_Info}" -gt 2048 ];then
Temp_Speed_Size='2000000'
else
Temp_Speed_Size='1563000'
fi
echo "开始生成 $((${Temp_Speed_Size}/1024))M 的临时文件:${Temp_Speed_File}"
# 生成临时测速文件
dd if=/dev/zero of=${Temp_Speed_File} bs=1024 count=${Temp_Speed_Size} &> /dev/null
fi
Network_Device_Speed=`route -n|awk '/^0.0.0.0/{print $8}'|uniq |xargs -i ethtool {} 2>/dev/null|awk '/Speed/ {print $2}'`
if [ "${Network_Device_Speed}" = '100Mb/s' ];then
Network_Conf_Speed='2'
elif [ "${Network_Device_Speed}" = '1000Mb/s' ];then
Network_Conf_Speed='10'
elif [ "${Network_Device_Speed}" = '10000Mb/s' ];then
Network_Conf_Speed='20'
elif [ "${Network_Device_Speed}" = '25000Mb/s' ];then
Network_Conf_Speed='20'
else
Network_Device_Speed='100Mb/s(虚拟机)'
Network_Conf_Speed='2'
fi
Down_Speed_Url_List=('https://www.baidu.com/speed' 'https://www.aliyun.com/speed' 'https://ecloud.10086.cn/speed' 'https://www.ctyun.cn/speed' 'https://cucc.wocloud.cn/speed' 'https://lol.qq.com/speed' 'https://daoju.qq.com/speed' 'https://pay.qq.com/speed' 'https://www.migu.cn/speed')
for num in $(seq 1 "${Network_Conf_Speed}");do
for Down_Speed_Url in ${Down_Speed_Url_List[@]};do
(curl -s --interface "${Default_Network_Device_Name}" -F "data=@${Temp_Speed_File}" "${Down_Speed_Url}" &> /dev/null &)
done &
done &
sleep 4
echo "当前默认网卡 ${Default_Network_Device_Name} 上行测速将开始,等待10s..."
# 历史出口流量
Old_Network_Outlet_Flow_Sum=`awk '{print $1}' /sys/class/net/${Default_Network_Device_Name}/statistics/tx_bytes`
sleep 10
# 最新新出口流量
New_Network_Outlet_Flow_Sum=`awk '{print $1}' /sys/class/net/${Default_Network_Device_Name}/statistics/tx_bytes`
# 网卡出口流量字节速率
Network_Outlet_Flow_Sum=`echo |awk "{printf "%f", (${New_Network_Outlet_Flow_Sum}-${Old_Network_Outlet_Flow_Sum})/10}"`
# 网卡出口流量比特速率
Network_Outlet_Flow_Sum_D=`echo |awk "{printf "%f",(${New_Network_Outlet_Flow_Sum}-${Old_Network_Outlet_Flow_Sum})*8/10}"`
# 标准出口比特速率
Network_Outlet_Device_Flow_Sum_D=`echo|awk "{printf "%f",${Network_Outlet_Flow_Sum_D/.*}/1024/1024}"`
# 标准出口字节速率
Network_Outlet_Device_Flow_Sum=`echo|awk "{printf "%.2f",${Network_Outlet_Flow_Sum/.*}/1024/1024}"`
# 标准网络速率
Network_Outlet_Device_Flow_Type="Mbps"
# 标准硬盘速率
Network_Outlet_Device_Flow_Type_D="MB"
# 清理未完成进程
ps -o pid,command|awk '/Speed_Test_File[[:space:]]/ {print $1}'|xargs -i kill -9 {} 2> /dev/null
if [ -z "${1}" ];then
rm -f "${Temp_Speed_File}"
fi
# 下载速率
for num in $(seq 1 "${Network_Conf_Speed}");do
(wget -q --no-check-certificate -O /dev/null -U 'Speed_Test_Query' 'https://www.ym68.cc/api/Analysis_Storage/Down.php?path=7a84YVaQOkWWEes1SJO%2BW%2Bvsvrbis7QAd3kJbCgRjHPPR1rExqyeP5zUz1jhIbnOBNaV%2FQ68BBRSNNTR%2BV9vTezkGPFzT%2ByYL4cDA%2BIuEZ%2B8NieBZ1ShGjmUS5llevf0K8kE0BeFQpv3fGYxK8%2FimGYmEM37H8sM5q3mJEPZugEn93SktYHHO8OGyJ%2FP4l9TLMGnLFzMd52nFX%2FZz2BNuRP45wJ6Pefkt5Oa4PPXbeXmo5DviLbkX%2B4I5cg0P%2Fnzgwoe69SS5QveDXVLtSNkfSNJ9BnVX2oN2DZ4hlr0XogNqbtZjFc' &> /dev/null &)
(wget -q --no-check-certificate -O /dev/null -U 'Speed_Test_Query' 'https://www.ym68.cc/api/Analysis_Storage/Down.php?path=89ddFo%2ByNPB7JzVq%2BALyBaFd%2BaClbHBk8aoO%2Fcbt09YkE1M%2Bug8V2FZYAUVNeIuZQtLnMCMyz4dNsCTcCjCQy%2F8FcG%2Bxvoz2N2JYJ42zAJMCEm%2FnUEbgSgNaHSoKXrdhxv4zR85CEicwK5ADgOfIe6MBJF4EVZWRslbjJvgxGwfBh5dSQ41fv9h%2FXv8cYgFK9myNwWv0jq3Pic5od3dg384utImKSZc54zTsngZndPiJ25p12oLJPEOQbYceQFWYsT82xD2ofa9QRKA0NDlLSc1cORn%2Bw2NJmm6uhTjKv4%2BfwgftpUQ' &> /dev/null &)
done &
sleep 4
echo "当前默认网卡 ${Default_Network_Device_Name} 下行测速将开始,等待10s..."
Old_Network_Ingress_Flow_Sum=`awk '{print $1}' /sys/class/net/${Default_Network_Device_Name}/statistics/rx_bytes`
sleep 10
New_Network_Ingress_Flow_Sum=`awk '{print $1}' /sys/class/net/${Default_Network_Device_Name}/statistics/rx_bytes`
# 网卡入口流量字节速率
Network_Ingress_Flow_Sum=`echo |awk "{printf "%f",(${New_Network_Ingress_Flow_Sum}-${Old_Network_Ingress_Flow_Sum})/10}"`
# 网卡入口流量比特速率
Network_Ingress_Flow_Sum_D=`echo |awk "{printf "%f",(${New_Network_Ingress_Flow_Sum}-${Old_Network_Ingress_Flow_Sum})*8/10}"`
# 标准入口比特速率
Network_Ingress_Device_Flow_Sum_D=`echo|awk "{printf "%f",${Network_Ingress_Flow_Sum_D/.*}/1024/1024}"`
# 标准入口字节速率
Network_Ingress_Device_Flow_Sum=`echo|awk "{printf "%.2f",${Network_Ingress_Flow_Sum/.*}/1024/1024}"`
ps -o pid,command|awk '/Speed_Test_Query/ {print $1}'|xargs -i kill -9 {} 2> /dev/null
echo "当前默认网卡 ${Default_Network_Device_Name} 物理速率为: ${Network_Device_Speed} ,实际测试流量下行速率约为: ${Network_Ingress_Device_Flow_Sum_D/.*} ${Network_Outlet_Device_Flow_Type}/s,上行速率约为: ${Network_Outlet_Device_Flow_Sum_D/.*} ${Network_Outlet_Device_Flow_Type}/s"
echo "当前默认网卡 ${Default_Network_Device_Name} 物理速率为: ${Network_Device_Speed} ,实际测试流量下行格式化速率约为: ${Network_Ingress_Device_Flow_Sum} ${Network_Outlet_Device_Flow_Type_D}/s,出口流量格式化速率约为: ${Network_Outlet_Device_Flow_Sum} ${Network_Outlet_Device_Flow_Type_D}/s"
文章来源于互联网:[原创]Linux服务器出入口网络带宽速率计算Shell脚本