WBITT

We Bring In Tomorrows Technology

  • Full Screen
  • Wide Screen
  • Narrow Screen
  • Increase font size
  • Default font size
  • Decrease font size

Script to calculate the CPU and MEM load Average

E-mail Print PDF
User Rating: / 10
PoorBest 

#######Script Created By  Asim Abbas
DATE: AUG 31 2008
######### Script to Calculate the CPU and MEM Average Usage


#!/bin/bash
export TERM=xterm

home_dir=/root/script
date  >> $home_dir/cpu.txt
/usr/bin/top -b -n 1 |grep "Cpu" > $home_dir/cpu.txt

for i in `date +%Y%m%d` ;  do cat $home_dir/cpu.txt | cut -d ',' -f1 | tr -d 'us'  | tr -d ' (Cp):' | sed -e s/^0+// |grep % |tr -d '%' >> $home_dir/avg$i.txt ; done


/usr/bin/top -b -n 1 |grep "Mem" > $home_dir/mem.txt


for i in `date +%Y%m%d` ; do cat mem.txt |cut -d ',' -f2 | tr -d 'used' |tr -d 'k' |tr -d ' ' >>  $home_dir/memavg$i.txt ; done


for i in `date +%Y%m%d` ; do awk '{ s += $1 } END { print "sum: ", s, " average CPU Use: ", s/NR, " samples: ", NR }' $home_dir/avg$i.txt > $home_dir/cpuavg$i.txt ; done

for i in `date +%Y%m%d` ; do awk '{ s += $1 } END { print "sum: ", s, " average Mem Use: ", s/NR, " samples: ", NR }' $home_dir/memavg$i.txt >$home_dir/memavgres$i.txt ; done


echo 'CPU & MEM average calcultion script completed'

 

You are here How To / Tutorials Script to calculate the CPU and MEM load Average