-
Notifications
You must be signed in to change notification settings - Fork 11
/
check_mem
executable file
·78 lines (64 loc) · 2.28 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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#!/bin/bash
# check_mem v1.4
#
# v1.1 Copyright (C) 2012 Lukasz Gogolin ([email protected])
# v1.2 2014 Modified by Aaron Roydhouse ([email protected])
# v1.3 2015 Modified by Aaron Roydhouse ([email protected])
# v1.4 2018 Modified by @DavidGoodwin, @eacmen, @whereisaaron
# Default percentage usage limits
CRITICAL=90
WARNING=80
usage() {
echo "check_mem v1.4"
echo ""
echo "Usage: check_mem -w <warnlevel> -c <critlevel>"
echo ""
echo " 'warnlevel' and 'critlevel' are percentage values without a percent sign ('%')"
echo " e.g. check_mem -w 80 -c 90"
echo ""
}
while getopts "w:c:" o; do
case "${o}" in
c)
CRITICAL=$(( ${OPTARG} + 0 ))
;;
w)
WARNING=$(( ${OPTARG} + 0))
;;
*)
usage
exit 1
;;
esac
done
if [ $CRITICAL -lt 0 -o $CRITICAL -gt 100 -o $WARNING -lt 0 -o $WARNING -gt 100 -o $WARNING -gt $CRITICAL ]; then
usage
exit 1
fi
memTotal_k=$(awk '$1~/^MemTotal/{print $2}' /proc/meminfo )
memFree_k=$(awk '$1~/^MemFree/{print $2}' /proc/meminfo )
memBuffer_k=$(awk '$1~/^Buffers/{print $2}' /proc/meminfo )
memCache_k=$(awk '$1~/^Cached/{print $2}' /proc/meminfo )
memUsed_k=$(( $memTotal_k - $memFree_k - $memBuffer_k - $memCache_k ))
memUsedPrc=$(( ($memUsed_k * 100) / $memTotal_k ))
warn_b=$(( ((($memTotal_k*100)-($memTotal_k*(100-${WARNING})))/100)*1024 ))
crit_b=$(( ((($memTotal_k*100)-($memTotal_k*(100-${CRITICAL})))/100)*1024 ))
memTotal_b=$(($memTotal_k*1024))
memFree_b=$(($memFree_k*1024))
memUsed_b=$(($memUsed_k*1024))
memBuffer_b=$(($memBuffer_k*1024))
memCache_b=$(($memCache_k*1024))
memTotal_m=$(($memTotal_k/1024))
memUsed_m=$(($memUsed_k/1024))
minmax="0;$memTotal_b";
data="TOTAL=$memTotal_b;;;$minmax USED=$memUsed_b;$warn_b;$crit_b;$minmax CACHE=$memCache_b;;;$minmax BUFFER=$memBuffer_b;;;$minmax"
if [ "$memUsedPrc" -ge "${CRITICAL}" ]; then
echo "MEMORY CRITICAL - Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|$data"
exit 2
elif [ "$memUsedPrc" -ge "${WARNING}" ]; then
echo "MEMORY WARNING - Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used!|$data"
exit 1
else
echo "MEMORY OK - Total: $memTotal_m MB - Used: $memUsed_m MB - $memUsedPrc% used|$data"
exit 0
fi