Linux memory mechanism: in linux, there is such an idea that the memory does not need to be white, so it tries its best to cache and buffer some data to facilitate the next use. But in fact, these memories can also be used immediately. So the idle memory free + buffers + cachedcheck_mem.sh monitoring script is found online.
Linux memory mechanism: in linux, there is such an idea that the memory does not need to be white, so it tries its best to cache and buffer some data to facilitate the next use. But in fact, these memories can also be used immediately. So idle memory =Free+ Buffers + cachEd
Check_mem.sh this monitoring script was found online. the obtained memory value is the three Mem: total userd free values from free-m. according to the above statement. this monitoring method is incorrect. so I modified the script. very simple. these two values are modified:
- # Total memory uSed
- Used = 'free-m | head-3 | tail-1 | awk '{print $3 }''
- # Calc total minus used
- Free = 'free-m | head-3 | tail-1 | awk '{print $4 }''
Check_mem.sh
- #! /Bin/bash
- USAGE = "'basename $ 0' [-w | -- warning] [-C | -- critical] "
- THRESHOLD_USAGE = "WARNING threshold must be greater than CRITICAL: 'basename $ 0' $ *"
- Calc =/tmp/meMcC
- Percent_free =/tmp/mempercent
- Critical = ""
- Warning = ""
- STATE_ OK = 0
- STATE_WARNING = 1
- STATE_CRITICAL = 2
- STATE_UNKNOWN = 3
- # Print usage
- If [[$ #-lt 4]
- Then
- Echo ""
- Echo "Wrong Syntax: 'basename $ 0' $ *"
- Echo ""
- Echo "Usage: $ USAGE"
- Echo ""
- ExIt 0
- Fi
- # Read input
- While [[$ #-gt 0]
- Do
- Case "$1" in
- -W | -- warning)
- Shift
- Warning = $1
- ;;
- -C | -- critical)
- Shift
- Critical = $1
- ;;
- Esac
- Shift
- Done
- # Verify input
- If [[$ warning-eq $ critical | $ warning-lt $ critical]
- Then
- Echo ""
- Echo "$ THRESHOLD_USAGE"
- Echo ""
- Echo "Usage: $ USAGE"
- Echo ""
- Exit 0
- Fi
- # Total memory available
- Total = 'free-m | head-2 | tail-1 | gawk '{print $2 }''
- # Total memory used
- Used = 'free-m | head-3 | tail-1 | awk '{print $3 }''
- # Calc total minus used
- Free = 'free-m | head-3 | tail-1 | awk '{print $4 }''
- # NoRmAl values
- # Echo "$ total" MB total
- # Echo "$ used" MB used
- # Echo "$ free" MB free
- # Make it into % percent free = (free mem/total mem) * 100)
- Echo "5"> $ calc # decimal accuracy
- Echo "k"> $ calc # commit
- Echo "100"> $ calc # multiply
- Echo "$ free"> $ calc # division integer
- Echo "$ total"> $ calc # division integer
- Echo "/"> $ calc # division sign
- Echo "*"> $ calc # multipliCatIon sign
- Echo "p"> $ calc # print
- Percent = '/usr/bin/dc $ calc |/bin/sed's/^ \./0./' |/usr/bin/Tr"." "|/Usr/bin/gawk {'print $1 '}'
- # Percent1 = '/usr/bin/dc $ calc'
- # Echo "$ percent1"
- If [["$ percent"-le $ critical]
- Then
- Echo "CRITICAL-$ free MB ($ percent %) Free Memory"
- Exit 2
- Fi
- If [["$ percent"-le $ warning]
- Then
- Echo "WARNING-$ free MB ($ percent %) Free Memory"
- Exit 1
- Fi
- If [["$ percent"-gt $ warning]
- Then
- Echo "OK-$ free MB ($ percent %) Free Memory"
- Exit 0
- Fi