-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_mem
executable file
·185 lines (169 loc) · 6.43 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
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
#!/bin/sh
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License only.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
ST_OK=0
ST_WR=1
ST_CR=2
ST_UK=3
wcdiff=0
wclvls=0
PROGNAME=`basename $0`
VERSION="Version 1.0,"
AUTHOR="2009, Mike Adolphs (http://www.matejunkie.com/)"
print_version() {
echo "$VERSION $AUTHOR"
}
print_help() {
print_version $PROGNAME $VERSION
echo ""
echo "$PROGNAME is a Nagios plugin to check the current memory usage via"
echo "/proc/meminfo. It should work on almost every system where you're"
echo "able to cat /proc/meminfo. The script itself is written sh-compliant"
echo "and free software under the terms of the GPLv2."
echo ""
echo "$PROGNAME -s/--style kb/perc [-w/--warning] [-c/--critical]"
echo "Options:"
echo " -s/--style)"
echo " You're able to set the output to kilobyte (kb) or percentage"
echo " (perc) values. Please note that other values will result in"
echo " termination. Default is: kb"
echo " -w/--warning)"
echo " Warning level in kb or percent for the amount of used memory."
echo " Note that when the style is set to percentage, only values"
echo " betweeen 0 and 100 are allowed. Default is: Off."
echo " -c/--critical)"
echo " Critical level in kb or percent for the amount of used memory."
echo " Note that when the style is set to percentage, only values"
echo " between 0 and 100 are allowed. Default is: Off."
exit $ST_UK
}
val_wcdiff() {
if [ ${lv_wr} -gt ${lv_cr} ]
then
wcdiff=1
fi
}
val_wclvls() {
if [ $style = "perc" ]
then
if [ "$lv_wr" -lt 0 -o "$lv_wr" -gt 100 -o "$lv_cr" -lt 0 -o "$lv_cr" -gt 100 ]
then
wclvls=1
val_wcdiff
fi
fi
}
style="kb"
while test -n "$1"; do
case "$1" in
--help|-h)
print_help
exit $ST_UK
;;
--version|-v)
print_version $PROGNAME $VERSION
exit $ST_UK
;;
--style|-s)
style=$2
shift
;;
--warning|-w)
lv_wr=$2
shift
;;
--critical|-c)
lv_cr=$2
shift
;;
*)
echo "Unknown argument: $1"
print_help
exit $ST_UK
;;
esac
shift
done
if [ ! -z "$lv_wr" -a ! -z "$lv_cr" ]
then
val_wcdiff
val_wclvls
fi
if [ $wcdiff = 1 ]
then
echo "Please adjust levels. The critical level must be higher than the warning level!"
if [ $wclvls = 1 ]
then
echo "Warning and critical level values must be between 0 and 100."
fi
exit $ST_UK
fi
if [ $wclvls = 1 ]
then
echo "Warning and critical level values must be between 0 and 100."
exit $ST_UK
fi
MEM_TOTAL=`grep "^MemTotal" /proc/meminfo|awk '{print $2}'`
TMP_MEM_FREE=`grep "^MemFree" /proc/meminfo|awk '{print $2}'`
BUFFERS=`grep "^Buffers" /proc/meminfo|awk '{print $2}'`
CACHED=`grep "^Cached" /proc/meminfo|awk '{print $2}'`
TMP_MEM_USED=`expr $MEM_TOTAL - $TMP_MEM_FREE - $CACHED`
P_MEM_USED=`echo "scale=2; $TMP_MEM_USED / $MEM_TOTAL * 100" | bc -l | sed 's/.[0-9][0-9]//'`
P_MEM_FREE=`echo "scale=0; 100 - $P_MEM_USED" | bc -l`
if [ $style = "kb" ]
then
if [ ! -z "$lv_wr" -a ! -z "$lv_cr" ]
then
if [ ${TMP_MEM_USED} -ge ${lv_wr} -a ${TMP_MEM_USED} -lt ${lv_cr} ]
then
echo "WARNING - Total: $MEM_TOTAL kb, Used: $TMP_MEM_USED kb, Free: $TMP_MEM_FREE kb | 'mem_total'=$MEM_TOTAL 'mem_used'=$TMP_MEM_USED;$lv_wr;$lv_cr 'mem_free'=$TMP_MEM_FREE"
exit $ST_WR
elif [ ${TMP_MEM_USED} -ge ${lv_cr} ]
then
echo "CRITICAL - Total: $MEM_TOTAL kb, Used: $TMP_MEM_USED kb, Free: $TMP_MEM_FREE kb | 'mem_total'=$MEM_TOTAL 'mem_used'=$TMP_MEM_USED;$lv_wr;$lv_cr 'mem_free'=$TMP_MEM_FREE"
exit $ST_CR
else
echo "OK - Total: $MEM_TOTAL kb, Used: $TMP_MEM_USED kb, Free: $TMP_MEM_FREE kb | 'mem_total'=$MEM_TOTAL 'mem_used'=$TMP_MEM_USED;$lv_wr;$lv_cr 'mem_free'=$TMP_MEM_FREE"
exit $ST_OK
fi
else
echo "OK - Total: $MEM_TOTAL kb, Used: $TMP_MEM_USED kb, Free: $TMP_MEM_FREE kb | 'mem_total'=$MEM_TOTAL 'mem_used'=$TMP_MEM_USED 'mem_free'=$TMP_MEM_FREE"
exit $ST_OK
fi
elif [ $style = "perc" ]
then
if [ ! -z "$lv_wr" -a ! -z "$lv_cr" ]
then
if [ ${P_MEM_USED} -ge ${lv_wr} -a ${P_MEM_USED} -lt ${lv_cr} ]
then
echo "WARNING - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$lv_wr;$lv_cr 'mem_free'=$P_MEM_FREE"
exit $ST_WR
elif [ ${P_MEM_USED} -ge ${lv_cr} ]
then
echo "CRITICAL - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$lv_wr;$lv_cr 'mem_free'=$P_MEM_FREE"
exit $ST_CR
else
echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED;$lv_wr;$lv_cr 'mem_free'=$P_MEM_FREE"
exit $ST_OK
fi
else
echo "OK - Used: $P_MEM_USED%, Free: $P_MEM_FREE% | 'mem_used'=$P_MEM_USED 'mem_free'=$P_MEM_FREE"
exit $ST_OK
fi
else
echo "Style (-s) must be either kb (kilobyte) or perc (for percent). kb is"
echo "being used as the default behavior when you don't provide the -s option."
echo ""
echo "For more information try -h or --help!"
exit $ST_UK
fi