diff options
Diffstat (limited to 'web/attachments/257980-check_ram.patch')
-rw-r--r-- | web/attachments/257980-check_ram.patch | 376 |
1 files changed, 376 insertions, 0 deletions
diff --git a/web/attachments/257980-check_ram.patch b/web/attachments/257980-check_ram.patch new file mode 100644 index 0000000..09e7a0a --- /dev/null +++ b/web/attachments/257980-check_ram.patch | |||
@@ -0,0 +1,376 @@ | |||
1 | --- nagios-plugins-1.4.5.orig/configure.in | ||
2 | +++ nagios-plugins-1.4.5/configure.in | ||
3 | @@ -1585,7 +1585,7 @@ fi | ||
4 | if test -n "$ac_cv_proc_meminfo"; then | ||
5 | AC_DEFINE(HAVE_PROC_MEMINFO,1,[Define if we have /proc/meminfo]) | ||
6 | AC_DEFINE_UNQUOTED(PROC_MEMINFO,"$ac_cv_proc_meminfo",[path to /proc/meminfo if name changes]) | ||
7 | - EXTRAS="$EXTRAS check_swap" | ||
8 | + EXTRAS="$EXTRAS check_swap check_ram" | ||
9 | fi | ||
10 | |||
11 | AC_PATH_PROG(PATH_TO_DIG,dig) | ||
12 | --- nagios-plugins-1.4.5.orig/plugins/Makefile.am | ||
13 | +++ nagios-plugins-1.4.5/plugins/Makefile.am | ||
14 | @@ -23,7 +23,7 @@ check_tcp_programs = check_ftp check_ima | ||
15 | check_udp check_clamd @check_tcp_ssl@ | ||
16 | |||
17 | EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \ | ||
18 | - check_swap check_fping check_ldap check_game check_dig \ | ||
19 | + check_swap check_fping check_ldap check_game check_dig check_ram \ | ||
20 | check_nagios check_by_ssh check_dns check_nt check_ide_smart \ | ||
21 | check_procs check_mysql_query check_apt | ||
22 | |||
23 | @@ -76,6 +76,7 @@ check_pgsql_LDADD = $(NETLIBS) $(PGLIBS) | ||
24 | check_ping_LDADD = $(NETLIBS) popen.o | ||
25 | check_procs_LDADD = $(BASEOBJS) popen.o | ||
26 | check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS) | ||
27 | +check_ram_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o | ||
28 | check_real_LDADD = $(NETLIBS) | ||
29 | check_snmp_LDADD = $(BASEOBJS) popen.o | ||
30 | check_smtp_LDADD = $(SSLOBJS) $(NETLIBS) | ||
31 | @@ -115,6 +116,7 @@ check_pgsql_DEPENDENCIES = check_pgsql.c | ||
32 | check_ping_DEPENDENCIES = check_ping.c $(NETOBJS) popen.o $(DEPLIBS) | ||
33 | check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
34 | check_radius_DEPENDENCIES = check_radius.c $(NETOBJS) $(DEPLIBS) | ||
35 | +check_ram_DEPENDENCIES = check_ram.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
36 | check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS) | ||
37 | check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS) | ||
38 | check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS) | ||
39 | --- /dev/null | ||
40 | +++ nagios-plugins-1.4.5/plugins/check_ram.c | ||
41 | @@ -0,0 +1,300 @@ | ||
42 | +/****************************************************************************** | ||
43 | +* | ||
44 | +* Nagios check_ram plugin | ||
45 | +* | ||
46 | +* License: GPL | ||
47 | +* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net) | ||
48 | +* Copyright (c) 2000-2006 nagios-plugins team | ||
49 | +* Copyright (c) 2007 Christian Heim (christian.heim@uni-greifswald.de) | ||
50 | +* | ||
51 | +* Last Modified: $Date: $ | ||
52 | +* | ||
53 | +* Description: | ||
54 | +* | ||
55 | +* This file contains the check_ram plugin | ||
56 | +* | ||
57 | +* License Information: | ||
58 | +* | ||
59 | +* This program is free software; you can redistribute it and/or modify | ||
60 | +* it under the terms of the GNU General Public License as published by | ||
61 | +* the Free Software Foundation; either version 2 of the License, or | ||
62 | +* (at your option) any later version. | ||
63 | +* | ||
64 | +* This program is distributed in the hope that it will be useful, | ||
65 | +* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
66 | +* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
67 | +* GNU General Public License for more details. | ||
68 | +* | ||
69 | +* You should have received a copy of the GNU General Public License | ||
70 | +* along with this program; if not, write to the Free Software | ||
71 | +* Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. | ||
72 | +* | ||
73 | +* $Id: check_ram.c $ | ||
74 | +* | ||
75 | +*****************************************************************************/ | ||
76 | + | ||
77 | +const char *progname = "check_ram"; | ||
78 | +const char *revision = "$Revision: $"; | ||
79 | +const char *copyright = "2000-2007"; | ||
80 | +const char *email = "nagiosplug-devel@lists.sourceforge.net"; | ||
81 | + | ||
82 | +#include "common.h" | ||
83 | +#include "popen.h" | ||
84 | +#include "utils.h" | ||
85 | + | ||
86 | +int check_ram (int usp, float free_ram_mb); | ||
87 | +int process_arguments (int argc, char **argv); | ||
88 | +int validate_arguments (void); | ||
89 | +void print_usage (void); | ||
90 | +void print_help (void); | ||
91 | + | ||
92 | +int warn_percent = 0; | ||
93 | +int crit_percent = 0; | ||
94 | +float warn_size_bytes = 0; | ||
95 | +float crit_size_bytes= 0; | ||
96 | +int verbose; | ||
97 | + | ||
98 | +int main (int argc, char **argv) | ||
99 | +{ | ||
100 | + int percent_used, percent; | ||
101 | + float total_ram_mb = 0, used_ram_mb = 0, free_ram_mb = 0; | ||
102 | + float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0; | ||
103 | + int result = STATE_UNKNOWN; | ||
104 | + char input_buffer[MAX_INPUT_BUFFER]; | ||
105 | +#ifdef HAVE_PROC_MEMINFO | ||
106 | + FILE *fp; | ||
107 | +#endif | ||
108 | + char str[32]; | ||
109 | + char *status; | ||
110 | + | ||
111 | + setlocale (LC_ALL, ""); | ||
112 | + bindtextdomain (PACKAGE, LOCALEDIR); | ||
113 | + textdomain (PACKAGE); | ||
114 | + | ||
115 | + status = strdup (""); | ||
116 | + | ||
117 | + if (process_arguments (argc, argv) == ERROR) | ||
118 | + usage4 (_("Could not parse arguments")); | ||
119 | + | ||
120 | +#ifdef HAVE_PROC_MEMINFO | ||
121 | + if (verbose >= 3) { | ||
122 | + printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO); | ||
123 | + } | ||
124 | + fp = fopen (PROC_MEMINFO, "r"); | ||
125 | + while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) { | ||
126 | + if (sscanf (input_buffer, "%*[M]%*[e]%*[m]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) { | ||
127 | + dsktotal_mb = dsktotal_mb / 1048576; /* Apply conversion */ | ||
128 | + dskused_mb = dskused_mb / 1048576; | ||
129 | + dskfree_mb = dskfree_mb / 1048576; | ||
130 | + total_ram_mb += dsktotal_mb; | ||
131 | + used_ram_mb += dskused_mb; | ||
132 | + free_ram_mb += dskfree_mb; | ||
133 | + | ||
134 | + percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb)); | ||
135 | + result = max_state (result, check_ram (percent, dskfree_mb)); | ||
136 | + | ||
137 | + if (verbose) | ||
138 | + asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent); | ||
139 | + } | ||
140 | + else if (sscanf (input_buffer, "%*[M]%*[e]%*[m]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) { | ||
141 | + if (verbose >= 3) { | ||
142 | + printf("Got %s with %f\n", str, tmp_mb); | ||
143 | + } | ||
144 | + /* I think this part is always in Kb, so convert to mb */ | ||
145 | + if (strcmp ("Total", str) == 0) { | ||
146 | + dsktotal_mb = tmp_mb / 1024; | ||
147 | + } | ||
148 | + else if (strcmp ("Free", str) == 0) { | ||
149 | + dskfree_mb = tmp_mb / 1024; | ||
150 | + } | ||
151 | + } | ||
152 | + } | ||
153 | + fclose(fp); | ||
154 | + dskused_mb = dsktotal_mb - dskfree_mb; | ||
155 | + total_ram_mb = dsktotal_mb; | ||
156 | + used_ram_mb = dskused_mb; | ||
157 | + free_ram_mb = dskfree_mb; | ||
158 | +#endif /* HAVE_PROC_MEMINFO */ | ||
159 | + | ||
160 | + /* if total_ram_mb == 0, let's not divide by 0 */ | ||
161 | + if(total_ram_mb) { | ||
162 | + percent_used = 100 * ((double) used_ram_mb) / ((double) total_ram_mb); | ||
163 | + } else { | ||
164 | + percent_used = 0; | ||
165 | + } | ||
166 | + | ||
167 | + result = max_state (result, check_ram (percent_used, free_ram_mb)); | ||
168 | + printf (_("RAM %s - %d%% free (%d MB out of %d MB) %s|"), | ||
169 | + state_text (result), | ||
170 | + (100 - percent_used), (int) free_ram_mb, (int) total_ram_mb, status); | ||
171 | + | ||
172 | + puts (perfdata ("ram", (long) free_ram_mb, "MB", | ||
173 | + TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_ram_mb), | ||
174 | + TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_ram_mb), | ||
175 | + TRUE, 0, | ||
176 | + TRUE, (long) total_ram_mb)); | ||
177 | + | ||
178 | + return result; | ||
179 | +} | ||
180 | + | ||
181 | +int check_ram (int usp, float free_ram_mb) | ||
182 | +{ | ||
183 | + int result = STATE_UNKNOWN; | ||
184 | + float free_ram = free_ram_mb * (1024 * 1024); /* Convert back to bytes as warn and crit specified in bytes */ | ||
185 | + if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent)) | ||
186 | + result = STATE_CRITICAL; | ||
187 | + else if (crit_size_bytes > 0 && free_ram <= crit_size_bytes) | ||
188 | + result = STATE_CRITICAL; | ||
189 | + else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent)) | ||
190 | + result = STATE_WARNING; | ||
191 | + else if (warn_size_bytes > 0 && free_ram <= warn_size_bytes) | ||
192 | + result = STATE_WARNING; | ||
193 | + else if (usp >= 0.0) | ||
194 | + result = STATE_OK; | ||
195 | + return result; | ||
196 | +} | ||
197 | + | ||
198 | +/* process command-line arguments */ | ||
199 | +int process_arguments (int argc, char **argv) | ||
200 | +{ | ||
201 | + int c = 0; /* option character */ | ||
202 | + | ||
203 | + int option = 0; | ||
204 | + static struct option longopts[] = { | ||
205 | + {"warning", required_argument, 0, 'w'}, | ||
206 | + {"critical", required_argument, 0, 'c'}, | ||
207 | + {"verbose", no_argument, 0, 'v'}, | ||
208 | + {"version", no_argument, 0, 'V'}, | ||
209 | + {"help", no_argument, 0, 'h'}, | ||
210 | + {0, 0, 0, 0} | ||
211 | + }; | ||
212 | + | ||
213 | + if (argc < 2) | ||
214 | + return ERROR; | ||
215 | + | ||
216 | + while (1) { | ||
217 | + c = getopt_long (argc, argv, "+?Vvhc:w:", longopts, &option); | ||
218 | + | ||
219 | + if (c == -1 || c == EOF) | ||
220 | + break; | ||
221 | + | ||
222 | + switch (c) { | ||
223 | + case 'w': /* warning size threshold */ | ||
224 | + if (is_intnonneg (optarg)) { | ||
225 | + warn_size_bytes = (float) atoi (optarg); | ||
226 | + break; | ||
227 | + } | ||
228 | + else if (strstr (optarg, ",") && | ||
229 | + strstr (optarg, "%") && | ||
230 | + sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) { | ||
231 | + warn_size_bytes = floorf(warn_size_bytes); | ||
232 | + break; | ||
233 | + } | ||
234 | + else if (strstr (optarg, "%") && | ||
235 | + sscanf (optarg, "%d%%", &warn_percent) == 1) { | ||
236 | + break; | ||
237 | + } | ||
238 | + else { | ||
239 | + usage4 (_("Warning threshold must be integer or percentage!")); | ||
240 | + } | ||
241 | + case 'c': /* critical size threshold */ | ||
242 | + if (is_intnonneg (optarg)) { | ||
243 | + crit_size_bytes = (float) atoi (optarg); | ||
244 | + break; | ||
245 | + } | ||
246 | + else if (strstr (optarg, ",") && | ||
247 | + strstr (optarg, "%") && | ||
248 | + sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) { | ||
249 | + crit_size_bytes = floorf(crit_size_bytes); | ||
250 | + break; | ||
251 | + } | ||
252 | + else if (strstr (optarg, "%") && | ||
253 | + sscanf (optarg, "%d%%", &crit_percent) == 1) { | ||
254 | + break; | ||
255 | + } | ||
256 | + else { | ||
257 | + usage4 (_("Critical threshold must be integer or percentage!")); | ||
258 | + } | ||
259 | + case 'v': /* verbose */ | ||
260 | + verbose++; | ||
261 | + break; | ||
262 | + case 'V': /* version */ | ||
263 | + print_revision (progname, revision); | ||
264 | + exit (STATE_OK); | ||
265 | + case 'h': /* help */ | ||
266 | + print_help (); | ||
267 | + exit (STATE_OK); | ||
268 | + case '?': /* error */ | ||
269 | + usage2 (_("Unknown argument"), optarg); | ||
270 | + } | ||
271 | + } | ||
272 | + | ||
273 | + c = optind; | ||
274 | + if (c == argc) | ||
275 | + return validate_arguments (); | ||
276 | + if (warn_percent == 0 && is_intnonneg (argv[c])) | ||
277 | + warn_percent = atoi (argv[c++]); | ||
278 | + | ||
279 | + if (c == argc) | ||
280 | + return validate_arguments (); | ||
281 | + if (crit_percent == 0 && is_intnonneg (argv[c])) | ||
282 | + crit_percent = atoi (argv[c++]); | ||
283 | + | ||
284 | + if (c == argc) | ||
285 | + return validate_arguments (); | ||
286 | + if (warn_size_bytes == 0 && is_intnonneg (argv[c])) | ||
287 | + warn_size_bytes = (float) atoi (argv[c++]); | ||
288 | + | ||
289 | + if (c == argc) | ||
290 | + return validate_arguments (); | ||
291 | + if (crit_size_bytes == 0 && is_intnonneg (argv[c])) | ||
292 | + crit_size_bytes = (float) atoi (argv[c++]); | ||
293 | + | ||
294 | + return validate_arguments (); | ||
295 | +} | ||
296 | + | ||
297 | +int validate_arguments (void) | ||
298 | +{ | ||
299 | + if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0 | ||
300 | + && crit_size_bytes == 0) { | ||
301 | + return ERROR; | ||
302 | + } | ||
303 | + else if (warn_percent < crit_percent) { | ||
304 | + usage4 | ||
305 | + (_("Warning percentage should be more than critical percentage")); | ||
306 | + } | ||
307 | + else if (warn_size_bytes < crit_size_bytes) { | ||
308 | + usage4 | ||
309 | + (_("Warning free space should be more than critical free space")); | ||
310 | + } | ||
311 | + return OK; | ||
312 | +} | ||
313 | + | ||
314 | +void print_help (void) | ||
315 | +{ | ||
316 | + print_revision (progname, revision); | ||
317 | + printf (_(COPYRIGHT), copyright, email); | ||
318 | + printf ("%s\n", _("Check RAM on local machine.")); | ||
319 | + printf ("\n\n"); | ||
320 | + print_usage (); | ||
321 | + printf (_(UT_HELP_VRSN)); | ||
322 | + printf (" %s\n", "-w, --warning=INTEGER"); | ||
323 | + printf (" %s\n", _("Exit with WARNING status if less than INTEGER bytes of RAM are free")); | ||
324 | + printf (" %s\n", "-w, --warning=PERCENT%%"); | ||
325 | + printf (" %s\n", _("Exit with WARNING status if less than PERCENT of RAM are free")); | ||
326 | + printf (" %s\n", "-c, --critical=INTEGER"); | ||
327 | + printf (" %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of RAM are free")); | ||
328 | + printf (" %s\n", "-c, --critical=PERCENT%%"); | ||
329 | + printf (" %s\n", _("Exit with CRITCAL status if less than PERCENT of RAM are free")); | ||
330 | + printf (" %s\n", "-v, --verbose"); | ||
331 | + printf (" %s\n", _("Verbose output. Up to 3 levels")); | ||
332 | + printf ("\n"); | ||
333 | + printf (_(UT_SUPPORT)); | ||
334 | +} | ||
335 | + | ||
336 | +void print_usage (void) | ||
337 | +{ | ||
338 | + printf (_("Usage:")); | ||
339 | + printf ("%s [-v] -w <percent_free>%% -c <percent_free>%%\n",progname); | ||
340 | + printf ("%s [-v] -w <bytes_free> -c <bytes_free>\n", progname); | ||
341 | +} | ||
342 | --- /dev/null | ||
343 | +++ nagios-plugins-1.4.5/plugins/t/check_ram.t | ||
344 | @@ -0,0 +1,32 @@ | ||
345 | +#! /usr/bin/perl -w -I .. | ||
346 | +# | ||
347 | +# Ram Space Tests via check_ram | ||
348 | +# | ||
349 | +# $Id: check_raam.t $ | ||
350 | +# | ||
351 | + | ||
352 | +use strict; | ||
353 | +use Test::More tests => 8; | ||
354 | +use NPTest; | ||
355 | + | ||
356 | +my $successOutput = '/^RAM OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | ||
357 | +my $failureOutput = '/^RAM CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | ||
358 | +my $warnOutput = '/^RAM WARNING - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/'; | ||
359 | + | ||
360 | +my $result; | ||
361 | + | ||
362 | +$result = NPTest->testCmd( "./check_ram -w 1048576 -c 1048576" ); # 1 MB free | ||
363 | +cmp_ok( $result->return_code, "==", 0, "At least 1MB free" ); | ||
364 | +like( $result->output, $successOutput, "Right output" ); | ||
365 | + | ||
366 | +$result = NPTest->testCmd( "./check_ram -w 1% -c 1%" ); # 1% free | ||
367 | +cmp_ok( $result->return_code, "==", 0, 'At least 1% free' ); | ||
368 | +like( $result->output, $successOutput, "Right output" ); | ||
369 | + | ||
370 | +$result = NPTest->testCmd( "./check_ram -w 100% -c 100%" ); # 100% (always critical) | ||
371 | +cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' ); | ||
372 | +like( $result->output, $failureOutput, "Right output" ); | ||
373 | + | ||
374 | +$result = NPTest->testCmd( "./check_ram -w 100% -c 1%" ); # 100% (always warn) | ||
375 | +cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' ); | ||
376 | +like( $result->output, $warnOutput, "Right output" ); | ||