#!/bin/bash
#这个脚本使用来统计CPU、磁盘、内存使用率、带宽的
total=0
system=0
user=0
i=0
#带宽使用情况
time=date "+%Y-%m-%d %k:%M"
day=date "+%Y-%m-%d"
minute=date "+%k:%M"
echo “*************************************************************************” >> 123.txt
echo “统计开始时间:$day $minute” >> 123.txt
#循环五次,避免看到的是偶然的数据
echo “#带宽的使用情况:#” >>123.txt
while (( $i<5 ))
do
#原先的ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-
方式获取网卡的信息为空,已经注释掉
#rx_before=ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-
#tx_before=ifconfig eth0|sed -n "7p"|awk '{print $6}'|cut -c7-
rx_before=$(cat /proc/net/dev | grep ‘eth’ | tr : " " | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') tx_before=(cat /proc/net/dev | grep ‘eth’ | tr : " " | awk ‘{print $10}’)
sleep 2
#rx_after=ifconfig eth0|sed -n "7p"|awk '{print $2}'|cut -c7-
#tx_after=ifconfig eth0|sed -n "7p"|awk '{print $6}'|cut -c7-
rx_after=$(cat /proc/net/dev | grep ‘eth’ | tr : " " | awk '{print KaTeX parse error: Expected 'EOF', got '}' at position 2: 2}̲') tx_after=(cat /proc/net/dev | grep ‘eth’ | tr : " " | awk ‘{print $10}’)
rx_result= [ ( r x a f t e r − r x b e f o r e ) / 1024 / 1024 / 2 ∗ 8 ] t x r e s u l t = [(rx_after-rx_before)/1024/1024/2*8] tx_result= [(rxafter−rxbefore)/1024/1024/2∗8]txresult=[(tx_after-tx_before)/1024/1024/2*8]
echo “$time Now_In_Speed: $rx_result Mbps Now_OUt_Speed: $tx_result Mbps” >>123.txt
let “i++”
done
rx_result= ( c a t 123. t x t ∣ g r e p " (cat 123.txt|grep " (cat123.txt∣grep"time"|awk '{In+=KaTeX parse error: Expected 'EOF', got '}' at position 2: 4}̲END{print In}')…(cat 123.txt|grep “$time”|awk '{Out+=KaTeX parse error: Expected 'EOF', got '}' at position 2: 7}̲END{print Out}'…(echo "scale=2; r x r e s u l t / 5 " ∣ b c ) O u t S p e e d = rx_result/5"|bc) Out_Speed= rx