forked from skywalka/check-cpu-perf
-
Notifications
You must be signed in to change notification settings - Fork 3
/
check_memory.sh
127 lines (112 loc) · 3.16 KB
/
check_memory.sh
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/bin/bash
#
#
# Found on http://sharadchhetri.com/2013/07/02/check_memory-unable-to-read-output-nagios-nrpe/
#
#
#
# $Id: check_mem,v 1.3 2008/09/17 21:47:03 nagios Exp nagios $
#
# Revision 1.4 2013/07/02 02:09 PM IST nagios
# Author : sharad kumar chhetri (for revision 1.4)
#
# $Log: check_mem,v $
# Revision 1.3 2008/09/17 21:47:03 nagios
# dropped free in favor of using /proc/meminfo
#
# Revision 1.2 2008/09/17 21:00:24 nagios
# added usage statement and input validation from http://www.nagiosexchange.org/cgi-bin/page.cgi?g=2099.html;d=1
# respects to the author
#
# Revision 1.1 2008/09/17 20:57:38 nagios
# Initial revision
#
# This plugin excludes the system cache and buffer, because on systems with stable memory
# usage it is perfectly normal for system cache to fill in all available memory
# (most of it can be freed at any time if applications requires it).
# Please keep a margin for system cache and buffers when setting thresholds.
#
#!/bin/bash
USAGE="`basename $0` [-w|--warning]<percent free> [-c|--critical]<percent free>"
THRESHOLD_USAGE="WARNING threshold must be greater than CRITICAL: `basename $0` $*"
# print usage
if [ $# -lt 4 ]
then
echo ""
echo "Wrong Syntax: `basename $0` $*"
echo ""
echo "Usage: $USAGE"
echo ""
exit 3
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 3
fi
# Total physical memory
total=`cat /proc/meminfo |head -n 1 |tail -n 1| gawk '{print $2}'`
# Free physical memory
free=`cat /proc/meminfo |head -n 2 |tail -n 1| gawk '{print $2}'`
# Buffers
buffers=`cat /proc/meminfo |head -n 3 |tail -n 1| gawk '{print $2}'`
# Cached
cached=`cat /proc/meminfo |head -n 4 |tail -n 1| gawk '{print $2}'`
#Available physical memory
available=`echo "$free+$buffers+$cached" | bc`
# make it into % percent free = ((free mem / total mem) * 100)
percent=`echo "scale=2; $available/$total*100" | bc`
#echo $total
#echo $free
#echo $buffers
#echo $cached
#echo $available
#echo $percent
#echo $critical
#echo $warning
#echo `echo "$percent <= $critical"|bc`
#echo `echo "$percent <= $warning"|bc`
#echo `echo "$percent > $warning"|bc`
totalMB=`echo "$total/1024" | bc`
availableMB=`echo "$available/1024" | bc`
usedMB=`echo "$totalMB-$availableMB" | bc`
usedpercent=`echo "100-$percent" | bc`
invWarn=`echo 100-$warning | bc`
invCrit=`echo 100-$critical | bc`
perf="|RAM=$usedpercent%;$invWarn;$invCrit;"
output="- Total=${totalMB}MB, Used=${usedMB}MB ($usedpercent%), Free=${availableMB}MB ($percent%) $perf"
if [ "`echo "$percent <= $critical"|bc`" -eq 1 ]
then
echo "RAM CRITICAL $output"
exit 2
fi
if [ "`echo "$percent <= $warning"|bc`" -eq 1 ]
then
echo "RAM WARNING $output"
exit 1
fi
if [ "`echo "$percent > $warning"|bc`" -eq 1 ]
then
echo "RAM OK $output"
exit 0
fi