summaryrefslogtreecommitdiffstats
path: root/web/attachments/257980-check_ram.patch
blob: 09e7a0aad06e5cc0f2d61af5a7808557ff52bf9c (plain)
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
--- nagios-plugins-1.4.5.orig/configure.in
+++ nagios-plugins-1.4.5/configure.in
@@ -1585,7 +1585,7 @@ fi
 if test -n "$ac_cv_proc_meminfo"; then
 	AC_DEFINE(HAVE_PROC_MEMINFO,1,[Define if we have /proc/meminfo])
 	AC_DEFINE_UNQUOTED(PROC_MEMINFO,"$ac_cv_proc_meminfo",[path to /proc/meminfo if name changes])
-	EXTRAS="$EXTRAS check_swap"
+	EXTRAS="$EXTRAS check_swap check_ram"
 fi
 
 AC_PATH_PROG(PATH_TO_DIG,dig)
--- nagios-plugins-1.4.5.orig/plugins/Makefile.am
+++ nagios-plugins-1.4.5/plugins/Makefile.am
@@ -23,7 +23,7 @@ check_tcp_programs = check_ftp check_ima
 	check_udp check_clamd @check_tcp_ssl@
 
 EXTRA_PROGRAMS = check_mysql check_radius check_pgsql check_snmp check_hpjd \
-	check_swap check_fping check_ldap check_game check_dig \
+	check_swap check_fping check_ldap check_game check_dig check_ram \
 	check_nagios check_by_ssh check_dns check_nt check_ide_smart	\
 	check_procs check_mysql_query check_apt
 
@@ -76,6 +76,7 @@ check_pgsql_LDADD = $(NETLIBS) $(PGLIBS)
 check_ping_LDADD = $(NETLIBS) popen.o
 check_procs_LDADD = $(BASEOBJS) popen.o
 check_radius_LDADD = $(NETLIBS) $(RADIUSLIBS)
+check_ram_LDADD = $(MATHLIBS) $(BASEOBJS) popen.o
 check_real_LDADD = $(NETLIBS)
 check_snmp_LDADD = $(BASEOBJS) popen.o
 check_smtp_LDADD = $(SSLOBJS) $(NETLIBS) 
@@ -115,6 +116,7 @@ check_pgsql_DEPENDENCIES = check_pgsql.c
 check_ping_DEPENDENCIES = check_ping.c $(NETOBJS) popen.o $(DEPLIBS)
 check_procs_DEPENDENCIES = check_procs.c $(BASEOBJS) popen.o $(DEPLIBS)
 check_radius_DEPENDENCIES = check_radius.c $(NETOBJS)  $(DEPLIBS)
+check_ram_DEPENDENCIES = check_ram.c $(BASEOBJS) popen.o $(DEPLIBS)
 check_real_DEPENDENCIES = check_real.c $(NETOBJS) $(DEPLIBS)
 check_snmp_DEPENDENCIES = check_snmp.c $(BASEOBJS) popen.o $(DEPLIBS)
 check_smtp_DEPENDENCIES = check_smtp.c $(SSLOBJS) $(NETOBJS) $(DEPLIBS)
--- /dev/null
+++ nagios-plugins-1.4.5/plugins/check_ram.c
@@ -0,0 +1,300 @@
+/******************************************************************************
+*
+* Nagios check_ram plugin
+*
+* License: GPL
+* Copyright (c) 2000 Karl DeBisschop (kdebisschop@users.sourceforge.net)
+* Copyright (c) 2000-2006 nagios-plugins team
+* Copyright (c) 2007 Christian Heim (christian.heim@uni-greifswald.de)
+*
+* Last Modified: $Date: $
+*
+* Description:
+*
+* This file contains the check_ram plugin
+*
+* License Information:
+*
+* 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; either version 2 of the License, or
+* (at your option) any later version.
+*
+* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
+*
+* $Id: check_ram.c $
+*
+*****************************************************************************/
+
+const char *progname = "check_ram";
+const char *revision = "$Revision: $";
+const char *copyright = "2000-2007";
+const char *email = "nagiosplug-devel@lists.sourceforge.net";
+
+#include "common.h"
+#include "popen.h"
+#include "utils.h"
+
+int check_ram (int usp, float free_ram_mb);
+int process_arguments (int argc, char **argv);
+int validate_arguments (void);
+void print_usage (void);
+void print_help (void);
+
+int warn_percent = 0;
+int crit_percent = 0;
+float warn_size_bytes = 0;
+float crit_size_bytes= 0;
+int verbose;
+
+int main (int argc, char **argv)
+{
+	int percent_used, percent;
+	float total_ram_mb = 0, used_ram_mb = 0, free_ram_mb = 0;
+	float dsktotal_mb = 0, dskused_mb = 0, dskfree_mb = 0, tmp_mb = 0;
+	int result = STATE_UNKNOWN;
+	char input_buffer[MAX_INPUT_BUFFER];
+#ifdef HAVE_PROC_MEMINFO
+	FILE *fp;
+#endif
+	char str[32];
+	char *status;
+
+	setlocale (LC_ALL, "");
+	bindtextdomain (PACKAGE, LOCALEDIR);
+	textdomain (PACKAGE);
+
+	status = strdup ("");
+
+	if (process_arguments (argc, argv) == ERROR)
+		usage4 (_("Could not parse arguments"));
+
+#ifdef HAVE_PROC_MEMINFO
+	if (verbose >= 3) {
+		printf("Reading PROC_MEMINFO at %s\n", PROC_MEMINFO);
+	}
+	fp = fopen (PROC_MEMINFO, "r");
+	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, fp)) {
+		if (sscanf (input_buffer, "%*[M]%*[e]%*[m]%*[:] %f %f %f", &dsktotal_mb, &dskused_mb, &dskfree_mb) == 3) {
+			dsktotal_mb = dsktotal_mb / 1048576;	/* Apply conversion */
+			dskused_mb = dskused_mb / 1048576;
+			dskfree_mb = dskfree_mb / 1048576;
+			total_ram_mb += dsktotal_mb;
+			used_ram_mb += dskused_mb;
+			free_ram_mb += dskfree_mb;
+
+			percent = 100 * (((double) dskused_mb) / ((double) dsktotal_mb));
+			result = max_state (result, check_ram (percent, dskfree_mb));
+
+			if (verbose)
+				asprintf (&status, "%s [%.0f (%d%%)]", status, dskfree_mb, 100 - percent);
+		}
+		else if (sscanf (input_buffer, "%*[M]%*[e]%*[m]%[TotalFre]%*[:] %f %*[k]%*[B]", str, &tmp_mb)) {
+			if (verbose >= 3) {
+				printf("Got %s with %f\n", str, tmp_mb);
+			}
+			/* I think this part is always in Kb, so convert to mb */
+			if (strcmp ("Total", str) == 0) {
+				dsktotal_mb = tmp_mb / 1024;
+			}
+			else if (strcmp ("Free", str) == 0) {
+				dskfree_mb = tmp_mb / 1024;
+			}
+		}
+	}
+	fclose(fp);
+	dskused_mb = dsktotal_mb - dskfree_mb;
+	total_ram_mb = dsktotal_mb;
+	used_ram_mb = dskused_mb;
+	free_ram_mb = dskfree_mb;
+#endif /* HAVE_PROC_MEMINFO */
+
+	/* if total_ram_mb == 0, let's not divide by 0 */
+	if(total_ram_mb) {
+		percent_used = 100 * ((double) used_ram_mb) / ((double) total_ram_mb);
+	} else {
+		percent_used = 0;
+	}
+
+	result = max_state (result, check_ram (percent_used, free_ram_mb));
+	printf (_("RAM %s - %d%% free (%d MB out of %d MB) %s|"),
+			state_text (result),
+			(100 - percent_used), (int) free_ram_mb, (int) total_ram_mb, status);
+
+	puts (perfdata ("ram", (long) free_ram_mb, "MB",
+	                TRUE, (long) max (warn_size_bytes/(1024 * 1024), warn_percent/100.0*total_ram_mb),
+	                TRUE, (long) max (crit_size_bytes/(1024 * 1024), crit_percent/100.0*total_ram_mb),
+	                TRUE, 0,
+	                TRUE, (long) total_ram_mb));
+
+	return result;
+}
+
+int check_ram (int usp, float free_ram_mb)
+{
+	int result = STATE_UNKNOWN;
+	float free_ram = free_ram_mb * (1024 * 1024);		/* Convert back to bytes as warn and crit specified in bytes */
+	if (usp >= 0 && crit_percent != 0 && usp >= (100.0 - crit_percent))
+		result = STATE_CRITICAL;
+	else if (crit_size_bytes > 0 && free_ram <= crit_size_bytes)
+		result = STATE_CRITICAL;
+	else if (usp >= 0 && warn_percent != 0 && usp >= (100.0 - warn_percent))
+		result = STATE_WARNING;
+	else if (warn_size_bytes > 0 && free_ram <= warn_size_bytes)
+		result = STATE_WARNING;
+	else if (usp >= 0.0)
+		result = STATE_OK;
+	return result;
+}
+
+/* process command-line arguments */
+int process_arguments (int argc, char **argv)
+{
+	int c = 0;  /* option character */
+
+	int option = 0;
+	static struct option longopts[] = {
+		{"warning", required_argument, 0, 'w'},
+		{"critical", required_argument, 0, 'c'},
+		{"verbose", no_argument, 0, 'v'},
+		{"version", no_argument, 0, 'V'},
+		{"help", no_argument, 0, 'h'},
+		{0, 0, 0, 0}
+	};
+
+	if (argc < 2)
+		return ERROR;
+
+	while (1) {
+		c = getopt_long (argc, argv, "+?Vvhc:w:", longopts, &option);
+
+		if (c == -1 || c == EOF)
+			break;
+
+		switch (c) {
+		case 'w':									/* warning size threshold */
+			if (is_intnonneg (optarg)) {
+				warn_size_bytes = (float) atoi (optarg);
+				break;
+			}
+			else if (strstr (optarg, ",") &&
+							 strstr (optarg, "%") &&
+							 sscanf (optarg, "%f,%d%%", &warn_size_bytes, &warn_percent) == 2) {
+				warn_size_bytes = floorf(warn_size_bytes);
+				break;
+			}
+			else if (strstr (optarg, "%") &&
+							 sscanf (optarg, "%d%%", &warn_percent) == 1) {
+				break;
+			}
+			else {
+				usage4 (_("Warning threshold must be integer or percentage!"));
+			}
+		case 'c':									/* critical size threshold */
+			if (is_intnonneg (optarg)) {
+				crit_size_bytes = (float) atoi (optarg);
+				break;
+			}
+			else if (strstr (optarg, ",") &&
+							 strstr (optarg, "%") &&
+							 sscanf (optarg, "%f,%d%%", &crit_size_bytes, &crit_percent) == 2) {
+				crit_size_bytes = floorf(crit_size_bytes);
+				break;
+			}
+			else if (strstr (optarg, "%") &&
+							 sscanf (optarg, "%d%%", &crit_percent) == 1) {
+				break;
+			}
+			else {
+				usage4 (_("Critical threshold must be integer or percentage!"));
+			}
+		case 'v':									/* verbose */
+			verbose++;
+			break;
+		case 'V':									/* version */
+			print_revision (progname, revision);
+			exit (STATE_OK);
+		case 'h':									/* help */
+			print_help ();
+			exit (STATE_OK);
+		case '?':									/* error */
+			usage2 (_("Unknown argument"), optarg);
+		}
+	}
+
+	c = optind;
+	if (c == argc)
+		return validate_arguments ();
+	if (warn_percent == 0 && is_intnonneg (argv[c]))
+		warn_percent = atoi (argv[c++]);
+
+	if (c == argc)
+		return validate_arguments ();
+	if (crit_percent == 0 && is_intnonneg (argv[c]))
+		crit_percent = atoi (argv[c++]);
+
+	if (c == argc)
+		return validate_arguments ();
+	if (warn_size_bytes == 0 && is_intnonneg (argv[c]))
+		warn_size_bytes = (float) atoi (argv[c++]);
+
+	if (c == argc)
+		return validate_arguments ();
+	if (crit_size_bytes == 0 && is_intnonneg (argv[c]))
+		crit_size_bytes = (float) atoi (argv[c++]);
+
+	return validate_arguments ();
+}
+
+int validate_arguments (void)
+{
+	if (warn_percent == 0 && crit_percent == 0 && warn_size_bytes == 0
+			&& crit_size_bytes == 0) {
+		return ERROR;
+	}
+	else if (warn_percent < crit_percent) {
+		usage4
+			(_("Warning percentage should be more than critical percentage"));
+	}
+	else if (warn_size_bytes < crit_size_bytes) {
+		usage4
+			(_("Warning free space should be more than critical free space"));
+	}
+	return OK;
+}
+
+void print_help (void)
+{
+	print_revision (progname, revision);
+	printf (_(COPYRIGHT), copyright, email);
+	printf ("%s\n", _("Check RAM on local machine."));
+	printf ("\n\n");
+  	print_usage ();
+	printf (_(UT_HELP_VRSN));
+	printf (" %s\n", "-w, --warning=INTEGER");
+	printf ("    %s\n", _("Exit with WARNING status if less than INTEGER bytes of RAM are free"));
+	printf (" %s\n", "-w, --warning=PERCENT%%");
+	printf ("    %s\n", _("Exit with WARNING status if less than PERCENT of RAM are free"));
+	printf (" %s\n", "-c, --critical=INTEGER");
+	printf ("    %s\n", _("Exit with CRITICAL status if less than INTEGER bytes of RAM are free"));
+	printf (" %s\n", "-c, --critical=PERCENT%%");
+	printf ("    %s\n", _("Exit with CRITCAL status if less than PERCENT of RAM are free"));
+	printf (" %s\n", "-v, --verbose");
+	printf ("    %s\n", _("Verbose output. Up to 3 levels"));
+	printf ("\n");
+	printf (_(UT_SUPPORT));
+}
+
+void print_usage (void)
+{
+	printf (_("Usage:"));
+	printf ("%s [-v] -w <percent_free>%% -c <percent_free>%%\n",progname);
+	printf ("%s [-v] -w <bytes_free> -c <bytes_free>\n", progname);
+}
--- /dev/null
+++ nagios-plugins-1.4.5/plugins/t/check_ram.t
@@ -0,0 +1,32 @@
+#! /usr/bin/perl -w -I ..
+#
+# Ram Space Tests via check_ram
+#
+# $Id: check_raam.t $
+#
+
+use strict;
+use Test::More tests => 8;
+use NPTest;
+
+my $successOutput = '/^RAM OK - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/';
+my $failureOutput = '/^RAM CRITICAL - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/';
+my $warnOutput    = '/^RAM WARNING - [0-9]+\% free \([0-9]+ MB out of [0-9]+ MB\)/';
+
+my $result;
+
+$result = NPTest->testCmd( "./check_ram -w 1048576 -c 1048576" );		# 1 MB free
+cmp_ok( $result->return_code, "==", 0, "At least 1MB free" );
+like( $result->output, $successOutput, "Right output" );
+
+$result = NPTest->testCmd( "./check_ram -w 1% -c 1%" );			# 1% free
+cmp_ok( $result->return_code, "==", 0, 'At least 1% free' );
+like( $result->output, $successOutput, "Right output" );
+
+$result = NPTest->testCmd( "./check_ram -w 100% -c 100%" );			# 100% (always critical)
+cmp_ok( $result->return_code, "==", 2, 'Get critical because not 100% free' );
+like( $result->output, $failureOutput, "Right output" );
+
+$result = NPTest->testCmd( "./check_ram -w 100% -c 1%" );			# 100% (always warn)
+cmp_ok( $result->return_code, "==", 1, 'Get warning because not 100% free' );
+like( $result->output, $warnOutput, "Right output" );