Linux--shell monitoring CPU and memory

Source: Internet
Author: User

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


#! /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
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.