Alarm memory usage

The full text is divided into five chapters:

  • The effect of this script and precautions

  • The content of the script, text and picture edition version (when it is convenient to work directly copied to the production environment)

  • Implementation of the results of the script

  • Script content explanation

  • Abstract summary

Role: Use this script is to send a warning message to the root mailbox when memory usage reaches 80%, suggesting that root as soon as possible.

Note: When this script alone is not obvious, and need to have a regular job with remarkable effect!

Script content (CRT):

#!/bin/bash
#When Memory Use 80% Alarm
#Author:zhanghe QQ:746620446 Version:0.1
USED=$(free -m | sed -n 2p | awk '{print $3}')
TOTAL=$(free -m | sed -n 2p | awk '{print $2}')
USED_Percent=$[$USED*100/$TOTAL]
if [ $USED_Percent -ge 3 ];then
 echo "$(date +%F-%H-%M) current memory used $USED_Percent%,please handle in it time" | mail -s "MEM_alarm" root
fi

[root@linuxprobe ~]# crontab -e

* / 1 * * * * /root/scripts/1.sh # once per minute

Script implementation of the results:

image.png

Explain the contents of the script (CRT):

 
[root @ linuxprobe scripts] # CAT -n MEM-arm.sh 
     ! 1 # / bin / bash 
     2 #when the Use Memory 80% Alarm 
     3 #Author: zhanghe QQ: 746 620 446 Version: 0.1 
     4 USED = $ (as Free -m | sed -n 2p | awk '{print $ 3}') # how much memory is currently removed M 
     . 5 tOTAL = $ (Free -m | -n Sed 2P | awk '{} Print $ 2') taken total memory # 
     6 USED_Percent = $ [$ uSED * 100 / $ tOTAL] # multiplied by 100 divided by the total amount to give a percentage of 
     7 if [$ USED_Percent -ge 80] ; then # 80 is timed out when the percentage of the amount of 
     8 echo "$ (date +% H-% M% F-) Current Memory Used $ USED_Percent%, Please handle in IT Time "| mail -s" MEM_alarm "root 
     9 fi

Ideas:

Idea of ​​this script is very simple, first of all use of the current contents of the percentage taken out, and then do a comparison with the 80%, if the memory usage is greater than 80%, then give root mail tips, nothing if not exceeded, then also do not do.

difficulty:

The first difficulty is to remove the current memory usage percentage, this percentage does not directly appear in the free command of them, but we need to be calculated, the formula is not difficult, is to use the memory divided by the total has been spent the content can then multiplied by 100, that students will do, but, but, but, linux value of which is not less than 1 display, it displays less than 1 becomes 0, and Qiao Qiao currently in use when we have used when divided by the total amount of RAM memory is less than one, then less than linux 1 will be displayed as 0, and 0 multiplied by 100, or equal to 0, so the result is that we get no sense of 0, how to solve it? It is also very simple to solve it! This is the subject of the pupils (From this primary knowledge of how important it is), "one multiplied by two multiplied by 3" and "a multiplied by three and then divided by two," the result is the same, we use the who then get the first data is no longer less than 1, so linux can be correctly identified, solved this problem just fine, which is generated on line 6.

The second difficulty is the test conditions and numerical format parentheses format issues, let us look dictation, the condition determination There are three methods, three methods are more wonderful, namely Test, a single bracket, double brackets, usually we the habit of using single quotes, either single or double brackets in brackets must have spaces, for reasons of space but also people speechless! Linux command line format is: Command [Option] [parameters], separated by a space from each other, and the front half portion is bracketed commands! You do not believe? Here is the evidence:

[root @ linuxprobe ~] # of the type -a [ 
[IS A shell the BUILTIN 
[IS / usr / bin / [ 
[root @ linuxprobe ~] # of the type -a [[ 
[[IS A shell keyword 

Well, Closer to home, talk about the conditions test three uses: 

[root @ linuxprobe ~] # # 9 -gt the test 1 the first test, simple and crude, using a relatively small 
[root @ linuxprobe ~] # echo $? 
1 
[root @ linuxprobe ~] # [9 -gt 8] # second single brackets, the most used 
bash: [9: command not found ... # look! There are no spaces on either side is not 
[root @ linuxprobe ~] # [9 -gt 8] 
[root @ linuxprobe ~] # echo $? 
0 
[root @ linuxprobe ~] # [[-e / etc / passwd]] # third and also the Mercedes Benz! 
bash: [[-e: command not found ... # you look, there is no space on either side is not! 
[root @ linuxprobe ~] # [[-e / etc / passwd]] 
[root @ linuxprobe ~] # echo $? 
0

Let's look, numerical computation format:

Conditions tested wholeheartedly brackets, there is one, two, but also keep their distance, while the numerical calculation is Jiaotaliangzhichuan! A bracket, one pair of parentheses and preceded by $ symbol, the end is a shame! !

Data operation there are a variety of formats, namely the let, $ [], $ (())

[root @ linuxprobe ~] # a = 2 # The first method with and without $ $ no difference 
[root @ linuxprobe ~] # b = 3 # Although simple and crude, but they have few 
[root @ linuxprobe ~] # the let O = $ A + $ B 
[the root @ linuxprobe ~] # echo $ O 
. 5 
[the root @ linuxprobe ~] # the let P = A + B + A 
[the root @ linuxprobe ~] # echo $ P 
. 7 
[the root @ linuxprobe ~ ] # a = 3 # $ second method with and without $ no difference 
[linuxprobe the root @ ~] #. 4 B = 
[the root linuxprobe @ ~] # $ D = ((A + $ B $)) # brackets with $ 
[linuxprobe the root @ ~] # $ D echo 
. 7 
[linuxprobe the root @ ~] D = # $ ((ab &)) # $ brackets and normal use without 
[linuxprobe the root @ ~] # echo $ D 
-1 
[ root @ linuxprobe ~] # a = 2 # in parentheses is the same, with or without $ brackets are 
[root @ linuxprobe ~] # b = 3 # this script "patronize" once this method, the impression is not bad! 
[root @ linuxprobe ~] # c = $ [$ a + $ b]
[root @ linuxprobe ~] # echo $ c
[root@linuxprobe ~]# c=$[a+b]
[root@linuxprobe ~]# echo $c
5

Abstract Summary:

First is that the test conditions, test condition request is simple, loyal, only soft spot in parentheses! test, single bracket, double parentheses, the only place to note is that both sides have spaces.

Jiaotaliangzhichuan the data is calculated, will be a single bracket, would be a double parentheses, but fortunately not a little merit has not, whether it is let, a single bracket, parentheses numerical bis They are treated equally, which may have a $ symbol in parentheses, or may not, but outside of the $ brackets have to have.

Guess you like

Origin www.cnblogs.com/yizhangheka/p/11038973.html