Linux--shell監控CPU和記憶體

來源:互聯網
上載者:User

標籤:linux   io   res   file   user   shell   

*************************************************************************************************


#! /bin/bash 
#By [email protected]




t=`date | awk ‘{print $4}‘ |awk -F ‘:‘ ‘{print $1$2$3}‘`
d=`date +%m/%d/%Y  |  awk -F ‘/‘ ‘{print $1$2}‘`
memfile=/home/walletUser/qhd/$d"-"$t"Mem".txt
cpufile=/home/walletUser/qhd/$d"-"$t"Cpu".txt
max_min_avgfile=/home/walletUser/qhd/$d"-"$t"calculation".txt




#calculate the cpu max
cpu_calc()
{
cpu_max=`cat $cpufile| awk -F ‘ ‘ ‘BEGIN {max = 0} {if ($6>max) max=$6 fi} END {print "CPU_Max=", max}‘`
cpu_min=`cat $cpufile|awk -F ‘ ‘ ‘BEGIN {min = 1000} {if ($6<min) min=$6 fi} END {print "CPU_Min=", min}‘`
cpu_avg=`cat $cpufile| awk -F ‘ ‘  ‘{sum+=$6} END {print "CPU_Average = ", sum/NR}‘`
echo   $cpu_max" "$cpu_min" "$cpu_avg"%"
}


#calculate the mem max
mem_calc()
{
mem_max=`cat $memfile| awk -F ‘ ‘ ‘BEGIN {max = 0} {if ($6>max) max=$6 fi} END {print "MEM_Max=", max}‘`
mem_min=`cat $memfile|awk -F ‘ ‘ ‘BEGIN {min = 999000} {if ($6<min) min=$6 fi} END {print "MEM_Min=", min}‘`
mem_avg=`cat $memfile| awk -F ‘ ‘  ‘{sum+=$6} END {print "MEM_Average = ", sum/NR}‘`
echo   $mem_max" "$mem_min" "$mem_avg"%"
}




# watch memory usage 
watch_mem()
{
dd1=`date | awk ‘{print $2,$3,$4}‘`


memtotal=`cat /proc/meminfo |grep "MemTotal"|awk ‘{print $2}‘` 
memfree=`cat /proc/meminfo |grep "MemFree"|awk ‘{print $2}‘` 
cached=`cat /proc/meminfo |grep "^Cached"|awk ‘{print $2}‘` 
buffers=`cat /proc/meminfo |grep "Buffers"|awk ‘{print $2}‘` 


mem_usage=$((100-memfree*100/memtotal-buffers*100/memtotal-cached*100/memtotal)) 
mem_message=$dd1" Memory usage: $mem_usage%" 
echo $mem_message >>$memfile


trap "echo ‘**********The results are as follows***********‘;echo `cpu_calc`|tee >>$max_min_avgfile;echo `mem_calc`|tee >>$max_min_avgfile;cat $max_min_avgfile;echo ‘*****************`date`*************‘;exit" 2
sleep 10s
}


# watch cpu 
get_cpu_info() 

  cat /proc/stat|grep ‘^cpu[0-9]‘|awk ‘{used+=$2+$3+$4;unused+=$5+$6+$7+$8} END{print used,unused}‘ 
}


watch_cpu() 
{
time_point_1=`get_cpu_info` 
sleep 5s 
time_point_2=`get_cpu_info`
dd2=`date | awk ‘{print $2,$3,$4}‘` 
cpu_usage=`echo $time_point_1 $time_point_2|awk ‘{used=$3-$1;total=$3+$4-$1-$2;print used*100/total}‘` 
cpu_message=$dd2" CPU Usage: $cpu_usage%" 
echo $cpu_message >>$cpufile





while :
do
#trap "echo `cpu_max`;exit" 2
watch_cpu
watch_mem
#trap "echo `cpu_max`;exit" 2
done
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.