脚本宝典收集整理的这篇文章主要介绍了一个用了统计CPU 内存 硬盘 使用率的shell脚本,脚本宝典觉得挺不错的,现在分享给大家,也给大家做个参考。
在开发CFS过程中需要对机器实时的内存,CPU,硬盘使用率进行监控,因为CFS跑在Linux机器上,可以利用Linux中一些相应虚拟文件进行内存CPU使用率的计算,于是编写了以下脚本,很简单,但是很实用。
包含三个部分,分别为硬盘使用率,内存使用率和CPU使用率。
#count cpu_used_rate,memory_used_rate,disk_used_rate
#@Jayson 2012-5
#disk_used_rate
#Depend on real storage place the parameter 'Location' need to alter.
#本人文件主要存储于sda8分区,所以以此提取。
Location=/dev/sda8
Disk_Used_Rate=$(df -h | grep $Location | awk '{print $5}')
echo $Disk_Used_Rate
#memory_used_rate
LoadMemory=$(cat /proc/meminfo | awk '{print $2}')
Total=$(echo $LoadMemory | awk '{print $1}')
Free1=$(echo $LoadMemory | awk '{print $2}')
Free2=$(echo $LoadMemory | awk '{print $3}')
Free3=$(echo $LoadMemory | awk '{print $4}')
Used=`expr $Total - $Free1 - $Free2 - $Free3`
Used_Rate=`expr $Used/$Total*100 | bc -l`
Memory_Used_Rate=`expr $Used_Rate/1 | bc`
echo $Memory_Used_Rate%
#cpu_used_rate
Log1=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
Sys1=$(echo $Log1 | awk '{print $4}')
Total1=$(echo $Log1 | awk '{print $1+$2+$3+$4+$5+$6+$7}')
sleep 0.5
Log2=$(cat /proc/stat | grep 'cpu ' | awk '{print $2" "$3" "$4" "$5" "$6" "$7" "$8}')
Sys2=$(echo $Log2 | awk '{print $4}')
Total2=$(echo $Log2 | awk '{print $1+$2+$3+$4+$5+$6+$7}')
Sys=`expr $Sys2 - $Sys1`
以上是脚本宝典为你收集整理的一个用了统计CPU 内存 硬盘 使用率的shell脚本全部内容,希望文章能够帮你解决一个用了统计CPU 内存 硬盘 使用率的shell脚本所遇到的问题。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。