欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  网络运营

一个用了统计CPU 内存 硬盘 使用率的shell脚本

程序员文章站 2023-11-10 20:19:46
复制代码 代码如下:#!/bin/bash#this script is use for describle cpu hard memery utilizationtota...

复制代码 代码如下:

#!/bin/bash
#this script is use for describle cpu hard memery utilization
total=0
idle=0
system=0
user=0
nice=0
mem=0
vmexec=/usr/bin/vmstat
which sar > /dev/null 2>&1
if [ $? -ne 0 ]
then
  ver=`vmstat -v | awk '{printf $3}'`
  nice=0
  temp=`vmstat 1 3 |tail -1`
  user=`echo $temp |awk '{printf("%s\n",$13)}'`
  system=`echo $temp |awk '{printf("%s\n",$14)}'`
  idle=`echo $temp |awk '{printf("%s\n",$15)}'`
  total=`echo|awk '{print (c1+c2)}' c1=$system c2=$user`
fi
echo "#cpu utilization#"
echo "total cpu  is already use: $total"
echo "cpu user   is already use: $user"
echo "cpu system is already use: $system"
echo "cpu nice   is already use: $nice"
echo "cpu idle   is already use: $idle"
echo
root_use=$(df -lh | awk 'nr==2' | awk '{print $5}')
dev_use=$(df -lh | awk 'nr==3' | awk '{print $5}')
dev_shm_use=$(df -lh | awk 'nr==4' | awk '{print $5}')
echo "#hard utilization#"
echo "/        is already use: $root_use"
echo "/dev     is already use: $dev_use"
echo "/dev/shm is already use: $dev_shm_use"
echo
memery_used=$(free | awk 'nr==2' | awk '{print $3}')
memery_all=$(free | awk 'nr==2' | awk '{print $2}')
memery_percent=$(echo "scale=4;$memery_used / $memery_all" | bc)
percent_part1=$(echo $memery_percent | cut -c 2-3)
percent_part2=$(echo $memery_percent | cut -c 4-5)
echo "#memery utilization#"
echo "system memery is already use: $percent_part1.$percent_part2%"
swap_used=$(free | awk 'nr==4' | awk '{print $3}')
swap_all=$(free | awk 'nr==4' | awk '{print $2}')
swap_percent=$(echo "scale=4;$swap_used / $swap_all" | bc)
swap_part1=$(echo $swap_percent | cut -c 2-3)
swap_part2=$(echo $swap_percent | cut -c 4-5)
echo "swap   memery is already use: $swap_part1.$swap_part2%"
echo

在开发cfs过程中需要对机器实时的内存,cpu,硬盘使用率进行监控,因为cfs跑在linux机器上,可以利用linux中一些相应虚拟文件进行内存cpu使用率的计算,于是编写了以下脚本,很简单,但是很实用。

包含三个部分,分别为硬盘使用率,内存使用率和cpu使用率。

复制代码 代码如下:

 #!/bin/sh

#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`


本文出自 “fighting,jayson!” 博客