*************************************************************************************************
#! /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 =} {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 $} '
Memfree= ' Cat/proc/meminfo |grep "Memfree" |awk ' {print $} '
Cached= ' Cat/proc/meminfo |grep "^cached" |awk ' {print $} '
buffers= ' cat/proc/meminfo |grep "buffers" |awk ' {print $} '
mem_usage=$ ((100-memfree*100/memtotal-buffers*100/memtotal-cached*100/memtotal))
mem_message= $dd 1 "Memory usage: $mem _usage%"
echo $mem _message >> $memfile
Trap "Echo ' **********the results is 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= $dd 2 "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