1 #!/bin/bash2 #这个脚本使用来统计CPU、磁盘、内存使用率、带宽的3 #初始化一些下面用到的变量4 total=0
5 system=0
6 user=0
7 i=0
8
9 time=`date "+%Y-%m-%d %k:%M"`10 day=`date "+%Y-%m-%d"`11 minute=`date "+%k:%M"`12 echo "*************************************************************************" >> 1234.txt13 echo "统计开始时间:$day $minute" >> 1234.txt14
15 #带宽使用情况16 echo "#带宽的使用情况:#" >>1234.txt17 #循环五次,避免看到的是偶然的数据18 while (( $i<5))19 do
20 #原先的`ifconfig eth0|sed -n "7p"|awk ‘{print $2}‘|cut -c7-`方式获取网卡的信息为空,已经注释掉21 #rx_before=`ifconfig eth0|sed -n "7p"|awk ‘{print $2}‘|cut -c7-`22 #tx_before=`ifconfig eth0|sed -n "7p"|awk ‘{print $6}‘|cut -c7-`23 rx_before=$(cat /proc/net/dev | grep ‘eth‘ | tr : " " | awk ‘{print $2}‘)24 tx_before=$(cat /proc/net/dev | grep ‘eth‘ | tr : " " | awk ‘{print $10}‘)25 #休眠2s26 sleep 2
27 #用sed先获取第7列,再用awk获取第2列,再cut切割,从第7个到最后,即只切割网卡流量数字部分28 #rx_after=`ifconfig eth0|sed -n "7p"|awk ‘{print $2}‘|cut -c7-`29 #tx_after=`ifconfig eth0|sed -n "7p"|awk ‘{print $6}‘|cut -c7-`30 rx_after=$(cat /proc/net/dev | grep ‘eth‘ | tr : " " | awk ‘{print $2}‘)31 tx_after=$(cat /proc/net/dev | grep ‘eth‘ | tr : " " | awk ‘{print $10}‘)32 #注意下面截取的相差2秒的两个时刻的累计和发送的bytes(即累计传送和接受的位)33 rx_result=$[(rx_after-rx_before)/1024/1024/2*8]34 tx_result=$[(tx_after-tx_before)/1024/1024/2*8]35 echo "$time Now_In_Speed: $rx_result Mbps Now_OUt_Speed: $tx_result Mbps" >>1234.txt36 let "i++"
37 done
38 #注意下面grep后面的$time变量要用双引号括起来39 rx_result=$(cat 1234.txt|grep "$time"|awk ‘{In+=$4}END{print In}‘)40 tx_result=$(cat 1234.txt|grep "$time"|awk ‘{Out+=$7}END{print Out}‘)41 In_Speed=$(echo "scale=2;$rx_result/5"|bc)42 Out_Speed=$(echo "scale=2;$tx_result/5"|bc)43 #echo "#带宽的5次的平均值是:#" >>1234.txt44 echo "$time In_Speed_average: $In_Speed Mbps Out_Speed_average: $Out_Speed Mbps" >>1234.txt45
46
47 #CPU使用情况48 #使用vmstat 15命令统计5秒内的使用情况,再计算每秒使用情况49 which sar > /dev/null 2>&1
50 if [ $? -ne 0]51 then
52 total=`vmstat 1 5|awk ‘{x+=$13;y+=$14}END{print x+y}‘`53 average=$(echo "scale=2;$total/5"|bc)54 fi
55 echo "#CPU使用率:#" >>1234.txt56 echo "Total CPU is already use: $average%" >>123.txt57
58
59 #磁盘使用情况(注意:需要用sed先进行格式化才能进行累加处理)60 disk_used=$(df -m | sed ‘1d;/ /!N;s/\n//;s/ \+/ /;‘ | awk ‘{used+=$3} END{print used}‘)61 disk_totalSpace=$(df -m | sed ‘1d;/ /!N;s/\n//;s/ \+/ /;‘ | awk ‘{totalSpace+=$2} END{print totalSpace}‘)62 disk_all=$(echo "scale=4;$disk_used/$disk_totalSpace" |bc)63 disk_percent1=$(echo $disk_all | cut -c 2-3)64 disk_percent2=$(echo $disk_all | cut -c 4-5)65 disk_warning=`df -m | sed ‘1d;/ /!N;s/\n//;s/ \+/ /;‘ | awk ‘{if ($5>85) print $5 $6;}‘`66 echo "#磁盘利用率#" >>123.txt67 echo "hard disk has used: $disk_percent1.$disk_percent2%" >>123.txt68 #echo -e "\t\t.."表示换行69 echo -e "\t\t#磁盘存在目录使用率超过85%报警#" >>123.txt70 echo -e "\t\tover used: $disk_warning" >>123.txt71
72
73 #内存使用情况74 #获得系统总内存75 memery_all=$(free -m | awk ‘NR==2‘ | awk ‘{print $2}‘)76 #获得占用内存(操作系统 角度)77 system _memery_used =$(free -m | awk ‘NR==2‘ | awk ‘{print $3}‘)78 #获得buffer、cache占用内存,当内存不够时会及时回收,所以这两部分可用于可用内存的计算79 buffer_used=$(free -m | awk ‘NR==2‘ | awk ‘{print $6}‘)80 cache_used=$(free -m | awk ‘NR==2‘ | awk ‘{print $7}‘)81 #获得被使用内存,所以这部分可用于可用内存的计算,注意计算方法82 actual_used_all =$[memery_all-(free+buffer_used+cache_used)]83 #获得实际占用的内存84 actual_used_all=`expr $memery_all - $free + $buffer_used +$cache_used `85 echo "$used_all" >> 123.txt86 memery_percent=$(echo "scale=4;$system _memery_used / $memery_all" |bc)87 memery_percent2=$(echo "scale=4; $actual_used_all / $memery_all" |bc)88 percent_part1=$(echo $memery_percent | cut -c 2-3)89 percent_part2=$(echo $memery_percent | cut -c 4-5)90 percent_part11=$(echo $memery_percent2 | cut -c 2-3)91 percent_part22=$(echo $memery_percent2 | cut -c 4-5)92 echo "#内存使用率#" >> 123.txt93 #获得占用内存(操作系统角度)94 echo "system memery is already use: $percent_part1.$percent_part2%" >>123.txt95 #获得实际内存占用率96 echo "actual memery is already use: $percent_part11.$percent_part22%" >>123.txt97 echo "buffer is already used : $buffer_used M" >>123.txt98 echo "cache is already used : $cache_used M" >>123.txt99
100
101 echo "结束本次统计:$day $minute" >> 123.txt102 echo "*************************************************************************" >> 123.txt103 echo -e "\n\n\n\n" >> 123.txt