summaryrefslogtreecommitdiffstats
path: root/plugins/check_vsz.c
blob: f53eeae370062e8076a12a2ccb3ec4409d8af7b3 (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
/******************************************************************************
 *
 * CHECK_VSZ.C
 *
 * Program: Process plugin for Nagios
 * License: GPL
 * Copyright (c) 1999,2000 Karl DeBisschop <kdebiss@alum.mit.edu>
 *
 * Last Modified: $Date$
 *
 * Description:
 *
 * This plugin will check for processes whose total image size exceeds
 * the warning or critical thresholds given on the command line.   With
 * no command_name, everything that shows up on ps is evaluated.
 * Otherwise, only jobs with the command_name given are examined.
 * This program is particularly useful if you have to run a piece of
 * commercial software that has a memory leak.  With it you can shut
 * down and restart the processes whenever the program threatens to
 * take over your system.
 *
 * Modifications:
 *
 * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
 *            change to getopt, use print_help
 * 08-18-1999 Ethan Galstad (nagios@nagios.org)
 *            Changed code to use common include file
 *            Changed fclose() to pclose()
 * 09-09-1999 Ethan Galstad (nagios@nagios.org)
 *            Changed popen()/pclose() to spopen()/spclose()
 * 11-18-1999 Karl DeBisschop (kdebiss@alum.mit.edu)
 *            set STATE_WARNING of stderr written or nonzero status returned
 *
 *****************************************************************************/

#include "common.h"
#include "popen.h"
#include "utils.h"

int process_arguments (int argc, char **argv);
int call_getopt (int argc, char **argv);
void print_help (char *cmd);
void print_usage (char *cmd);

int warn = -1;
int crit = -1;
char *proc = NULL;

int
main (int argc, char **argv)
{
	int len;
	int result = STATE_OK;
	int line = 0;
	int proc_size = -1;
	char input_buffer[MAX_INPUT_BUFFER];
	char proc_name[MAX_INPUT_BUFFER];
	char *message = NULL;

	if (!process_arguments (argc, argv)) {
		printf ("%s: failure parsing arguments\n", my_basename (argv[0]));
		print_help (my_basename (argv[0]));
		return STATE_UNKNOWN;
	}

	/* run the command */
	child_process = spopen (VSZ_COMMAND);
	if (child_process == NULL) {
		printf ("Unable to open pipe: %s\n", VSZ_COMMAND);
		return STATE_UNKNOWN;
	}

	child_stderr = fdopen (child_stderr_array[fileno (child_process)], "r");
	if (child_stderr == NULL)
		printf ("Could not open stderr for %s\n", VSZ_COMMAND);

	message = malloc ((size_t) 1);
	message[0] = 0;
	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_process)) {

		line++;

		/* skip the first line */
		if (line == 1)
			continue;

		if (sscanf (input_buffer, VSZ_FORMAT, &proc_size, proc_name) == 2) {
			if (proc == NULL) {
				if (proc_size > warn) {
					len = strlen (message) + strlen (proc_name) + 23;
					message = realloc (message, len);
					if (message == NULL)
						terminate (STATE_UNKNOWN,
											 "check_vsz: could not malloc message (1)");
					sprintf (message, "%s %s(%d)", message, proc_name, proc_size);
					result = max_state (result, STATE_WARNING);
				}
				if (proc_size > crit) {
					result = STATE_CRITICAL;
				}
			}
			else if (strstr (proc_name, proc)) {
				len = strlen (message) + 21;
				message = realloc (message, len);
				if (message == NULL)
					terminate (STATE_UNKNOWN,
										 "check_vsz: could not malloc message (2)");
				sprintf (message, "%s %d", message, proc_size);
				if (proc_size > warn) {
					result = max_state (result, STATE_WARNING);
				}
				if (proc_size > crit) {
					result = STATE_CRITICAL;
				}
			}
		}
	}

	/* If we get anything on STDERR, at least set warning */
	while (fgets (input_buffer, MAX_INPUT_BUFFER - 1, child_stderr))
		result = max_state (result, STATE_WARNING);

	(void) fclose (child_stderr);

	/* close the pipe */
	if (spclose (child_process))
		result = max_state (result, STATE_WARNING);

	if (result == STATE_OK)
		printf ("ok (all VSZ<%d): %s\n", warn, message);
	else if (result == STATE_UNKNOWN)
		printf ("Unable to read output\n");
	else if (result == STATE_WARNING)
		printf ("WARNING (VSZ>%d):%s\n", warn, message);
	else
		printf ("CRITICAL (VSZ>%d):%s\n", crit, message);

	return result;
}




int
process_arguments (int argc, char **argv)
{
	int c;

	if (argc < 2)
		return ERROR;

	c = 0;
	while (c += (call_getopt (argc - c, &argv[c]))) {
		if (argc <= c)
			break;
		if (warn == -1) {
			if (!is_intnonneg (argv[c])) {
				printf ("%s: critical threshold must be an integer: %s\n",
								my_basename (argv[0]), argv[c]);
				print_usage (my_basename (argv[0]));
				exit (STATE_UNKNOWN);
			}
			warn = atoi (argv[c]);
		}
		else if (crit == -1) {
			if (!is_intnonneg (argv[c])) {
				printf ("%s: critical threshold must be an integer: %s\n",
								my_basename (argv[0]), argv[c]);
				print_usage (my_basename (argv[0]));
				exit (STATE_UNKNOWN);
			}
			crit = atoi (argv[c]);
		}
		else if (proc == NULL) {
			proc = malloc (strlen (argv[c]) + 1);
			if (proc == NULL)
				terminate (STATE_UNKNOWN,
									 "check_vsz: failed malloc of proc in process_arguments");
			strcpy (proc, argv[c]);
		}
	}
	return c;
}

int
call_getopt (int argc, char **argv)
{
	int c, i = 1;

#ifdef HAVE_GETOPT_H
	int option_index = 0;
	static struct option long_options[] = {
		{"help", no_argument, 0, 'h'},
		{"version", no_argument, 0, 'V'},
		{"critical", required_argument, 0, 'c'},
		{"warning", required_argument, 0, 'w'},
		{"command", required_argument, 0, 'C'},
		{0, 0, 0, 0}
	};
#endif

	while (1) {
#ifdef HAVE_GETOPT_H
		c = getopt_long (argc, argv, "+hVc:w:C:", long_options, &option_index);
#else
		c = getopt (argc, argv, "+hVc:w:C:");
#endif
		if (c == EOF)
			break;

		i++;
		switch (c) {
		case 'c':
		case 'w':
		case 'C':
			i++;
		}

		switch (c) {
		case '?':									/* help */
			printf ("%s: Unknown argument: %s\n\n", my_basename (argv[0]), optarg);
			print_usage (my_basename (argv[0]));
			exit (STATE_UNKNOWN);
		case 'h':									/* help */
			print_help (my_basename (argv[0]));
			exit (STATE_OK);
		case 'V':									/* version */
			print_revision (my_basename (argv[0]), "$Revision$");
			exit (STATE_OK);
		case 'c':									/* critical threshold */
			if (!is_intnonneg (optarg)) {
				printf ("%s: critical threshold must be an integer: %s\n",
								my_basename (argv[0]), optarg);
				print_usage (my_basename (argv[0]));
				exit (STATE_UNKNOWN);
			}
			crit = atoi (optarg);
			break;
		case 'w':									/* warning threshold */
			if (!is_intnonneg (optarg)) {
				printf ("%s: warning threshold must be an integer: %s\n",
								my_basename (argv[0]), optarg);
				print_usage (my_basename (argv[0]));
				exit (STATE_UNKNOWN);
			}
			warn = atoi (optarg);
			break;
		case 'C':									/* command name */
			proc = malloc (strlen (optarg) + 1);
			if (proc == NULL)
				terminate (STATE_UNKNOWN,
									 "check_vsz: failed malloc of proc in process_arguments");
			strcpy (proc, optarg);
			break;
		}
	}
	return i;
}

void
print_usage (char *cmd)
{
	printf ("Usage: %s -w <wsize> -c <csize> [-C command]\n"
					"       %s --help\n" "       %s --version\n", cmd, cmd, cmd);
}

void
print_help (char *cmd)
{
	print_revision ("check_vsz", "$Revision$");
	printf
		("Copyright (c) 2000 Karl DeBisschop <kdebiss@alum.mit.edu>\n\n"
		 "This plugin checks the image size of a running program and returns an\n"
		 "error if the number is above either of the thresholds given.\n\n");
	print_usage (cmd);
	printf
		("\nOptions:\n"
		 " -h, --help\n"
		 "    Print detailed help\n"
		 " -V, --version\n"
		 "    Print version numbers and license information\n"
		 " -w, --warning=INTEGER\n"
		 "    Program image size necessary to cause a WARNING state\n"
		 " -c, --critical=INTEGER\n"
		 "    Program image size necessary to cause a CRITICAL state\n"
		 " -C, --command=STRING\n" "    Program to search for [optional]\n");
}