-
Notifications
You must be signed in to change notification settings - Fork 1
/
check_mem
executable file
·57 lines (46 loc) · 1.59 KB
/
check_mem
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#!/bin/bash
. $(dirname $0)/lib.sh
while getopts "c:w:" OPT; do
case $OPT in
w) WARNING_PC=$OPTARG ;;
c) CRITICAL_PC=$OPTARG ;;
esac
done
if [ -z "$WARNING_PC" -o -z "$CRITICAL_PC" ]; then
echo "Usage: $0 -w <warning percentage> -c <critical percentage>"
exit $STATE_UNKNOWN
fi
# The values from /proc/meminfo are in KiB (even though the output prints kB),
# convert them to B to prevent ambiguity later on, and insert them as variables.
eval "$(sed -r '
y/()/_ /
s/ *: *([0-9]*).*/=$((\1 * 1024))/
s/([a-z])([A-Z])/\1_\2/g
s/.*/\U\0/
' /proc/meminfo)"
# Some systems don't have these values, default them
SRECLAIMABLE=${SRECLAIMABLE:-0}
# The idiomatic 'used memory' computation
USED=$((MEM_TOTAL - MEM_FREE - BUFFERS - CACHED))
USED_PC=$((100 * USED / MEM_TOTAL))
# And some more components for reporting purposes
USED_TOTAL=$((MEM_TOTAL - MEM_FREE))
SWAP_USED=$((SWAP_TOTAL - SWAP_FREE))
REST=$((USED - SRECLAIMABLE))
INFO="$USED_PC% used - $(bytes_to_human_bi $USED) ($(bytes_to_human_bi $REST) \
excl. reclaimable slab, $(bytes_to_human_bi $USED_TOTAL) incl. buffers/cache) \
of $(bytes_to_human_bi $MEM_TOTAL) total - $(bytes_to_human_bi $COMMITTED_AS) \
committed"
PERF="rest=${REST}B sReclaimable=${SRECLAIMABLE}B buffers=${BUFFERS}B \
cached=${CACHED}B free=${MEM_FREE}B swap=${SWAP_USED}B used=${USED}B;\
$((MEM_TOTAL * WARNING_PC / 100));$((MEM_TOTAL * CRITICAL_PC / 100)) \
committed=${COMMITTED_AS}B"
if [ $USED_PC -ge $CRITICAL_PC ]; then
STATE="CRITICAL";
elif [ $USED_PC -ge $WARNING_PC ]; then
STATE="WARNING";
else
STATE="OK";
fi
echo "$STATE - $INFO|$PERF"
eval exit \$STATE_$STATE