#!/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’