Shell script case (iv) Use the free command to accurately monitor RAM utilization

Source: Internet
Author: User

Requirements: Use the free command to accurately monitor RAM utilization
Knowledge: GREP,FREE,AWK,BC

The script is as follows

[[email protected] scripts]# cat mem.sh #!/bin/bash-# free-m represents output in m units, and if the memory is large enough, you should use the-g parameter and the capacity is output in gigabytes. Mem_total= ' Free-m | grep Mem | awk ' {print $} ' mem_used= ' free-m | grep Mem | awk ' {print $} ' mem_free= ' free-m | grep Mem | awk ' {print $4} ' # First bash Shell is not supported by default in decimal, so here I take the use of awk calculation, of course, can also use BC to calculate. # I've screened out the total capacity and the capacity in use to calculate how much RAM is currently used. Sum_used= ' Free-m | grep Mem | awk ' {print ($3/$2) *100} ' # is output, where I add an M display after each value to make the output more understandable. Variables and the characters that need to be added after the variable should be separated by a {} number, such as ${a}m, which indicates the addition of M after the output variable A. echo "Mem_total: ${mem_total}m" echo "mem_used: ${mem_used}m" echo "Men_free: ${mem_free}m" echo "sum_used: $sum _used%" # Similarly, the Bash shell does not support the comparison of the decimal point, where I use the BC to compare the values, if the result is true, the return value is 1,false return value is 0. # like echo "1.1>1.2" |BC then output is 0, if echo "1.1>1" |BC then the output is 1.warning= ' echo ' $sum _used>90 ' |BC ' # The value returned by the comparison is used as the condition for whether the message is sent, if it is not 1, and the usage is greater than 90, then the message is sent. If [$warning-ne 1];then echo "Warning:mem use ${sum_sed}%" | Mail-s "MEM status warning." [email protected]fi[[email protected] scripts]# can finally be added to the scheduled task.

Script Run

[[email protected] scripts]# bash mem.sh mem_total: 1006Mmem_used: 986Mmen_free: 19Msum_used: 98.0119%[[email protected] scripts]#

At this point, the end.

Shell script case (iv) Use the free command to accurately monitor RAM utilization

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.