一個用了統計CPU 記憶體 硬碟 使用率的shell指令碼

來源:互聯網
上載者:User

複製代碼 代碼如下:#!/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!” 部落格

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.