1 /******************************************************************************
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License as published by
5 the Free Software Foundation; either version 2 of the License, or
6 (at your option) any later version.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; if not, write to the Free Software
15 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 ******************************************************************************/
19 const char *progname = "check_overcr";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "utils.h"
28 enum checkvar {
29 NONE,
30 LOAD1,
31 LOAD5,
32 LOAD15,
33 DPU,
34 PROCS,
35 NETSTAT,
36 UPTIME
37 };
39 enum {
40 PORT = 2000
41 };
43 char *server_address = NULL;
44 int server_port = PORT;
45 double warning_value = 0L;
46 double critical_value = 0L;
47 int check_warning_value = FALSE;
48 int check_critical_value = FALSE;
49 enum checkvar vars_to_check = NONE;
50 int cmd_timeout = 1;
52 int netstat_port = 0;
53 char *disk_name = NULL;
54 char *process_name = NULL;
55 char send_buffer[MAX_INPUT_BUFFER];
57 int process_arguments (int, char **);
58 void print_usage (void);
59 void print_help (void);
61 int
62 main (int argc, char **argv)
63 {
64 int result;
65 char recv_buffer[MAX_INPUT_BUFFER];
66 char temp_buffer[MAX_INPUT_BUFFER];
67 char *temp_ptr = NULL;
68 int found_disk = FALSE;
69 unsigned long percent_used_disk_space = 100;
70 double load;
71 double load_1min;
72 double load_5min;
73 double load_15min;
74 int port_connections = 0;
75 int processes = 0;
76 double uptime_raw_hours;
77 int uptime_raw_minutes = 0;
78 int uptime_days = 0;
79 int uptime_hours = 0;
80 int uptime_minutes = 0;
82 setlocale (LC_ALL, "");
83 bindtextdomain (PACKAGE, LOCALEDIR);
84 textdomain (PACKAGE);
86 if (process_arguments (argc, argv) == ERROR)
87 usage ("Could not parse arguments\n");
89 /* initialize alarm signal handling */
90 signal (SIGALRM, socket_timeout_alarm_handler);
92 /* set socket timeout */
93 alarm (socket_timeout);
95 result = process_tcp_request2 (server_address,
96 server_port,
97 send_buffer,
98 recv_buffer,
99 sizeof (recv_buffer));
101 switch (vars_to_check) {
103 case LOAD1:
104 case LOAD5:
105 case LOAD15:
107 if (result != STATE_OK)
108 die (result, _("Unknown error fetching load data\n"));
110 temp_ptr = (char *) strtok (recv_buffer, "\r\n");
111 if (temp_ptr == NULL)
112 die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
113 else
114 load_1min = strtod (temp_ptr, NULL);
116 temp_ptr = (char *) strtok (NULL, "\r\n");
117 if (temp_ptr == NULL)
118 die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
119 else
120 load_5min = strtod (temp_ptr, NULL);
122 temp_ptr = (char *) strtok (NULL, "\r\n");
123 if (temp_ptr == NULL)
124 die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
125 else
126 load_15min = strtod (temp_ptr, NULL);
128 switch (vars_to_check) {
129 case LOAD1:
130 strcpy (temp_buffer, "1");
131 load = load_1min;
132 break;
133 case LOAD5:
134 strcpy (temp_buffer, "5");
135 load = load_5min;
136 break;
137 default:
138 strcpy (temp_buffer, "15");
139 load = load_15min;
140 break;
141 }
143 if (check_critical_value == TRUE && (load >= critical_value))
144 result = STATE_CRITICAL;
145 else if (check_warning_value == TRUE && (load >= warning_value))
146 result = STATE_WARNING;
148 die (result,
149 _("Load %s - %s-min load average = %0.2f"),
150 state_text(result),
151 temp_buffer,
152 load);
154 break;
156 case DPU:
158 if (result != STATE_OK)
159 die (result, _("Unknown error fetching disk data\n"));
161 for (temp_ptr = (char *) strtok (recv_buffer, " ");
162 temp_ptr != NULL;
163 temp_ptr = (char *) strtok (NULL, " ")) {
165 if (!strcmp (temp_ptr, disk_name)) {
166 found_disk = TRUE;
167 temp_ptr = (char *) strtok (NULL, "%");
168 if (temp_ptr == NULL)
169 die (STATE_CRITICAL, _("Invalid response from server\n"));
170 else
171 percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
172 break;
173 }
175 temp_ptr = (char *) strtok (NULL, "\r\n");
176 }
178 /* error if we couldn't find the info for the disk */
179 if (found_disk == FALSE)
180 die (STATE_CRITICAL,
181 "Error: Disk '%s' non-existent or not mounted",
182 disk_name);
184 if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
185 result = STATE_CRITICAL;
186 else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
187 result = STATE_WARNING;
189 die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
191 break;
193 case NETSTAT:
195 if (result != STATE_OK)
196 die (result, _("Unknown error fetching network status\n"));
197 else
198 port_connections = strtod (recv_buffer, NULL);
200 if (check_critical_value == TRUE && (port_connections >= critical_value))
201 result = STATE_CRITICAL;
202 else if (check_warning_value == TRUE && (port_connections >= warning_value))
203 result = STATE_WARNING;
205 die (result,
206 _("Net %s - %d connection%s on port %d"),
207 state_text(result),
208 port_connections,
209 (port_connections == 1) ? "" : "s",
210 netstat_port);
212 break;
214 case PROCS:
216 if (result != STATE_OK)
217 die (result, _("Unknown error fetching process status\n"));
219 temp_ptr = (char *) strtok (recv_buffer, "(");
220 if (temp_ptr == NULL)
221 die (STATE_CRITICAL, _("Invalid response from server\n"));
223 temp_ptr = (char *) strtok (NULL, ")");
224 if (temp_ptr == NULL)
225 die (STATE_CRITICAL, _("Invalid response from server\n"));
226 else
227 processes = strtod (temp_ptr, NULL);
229 if (check_critical_value == TRUE && (processes >= critical_value))
230 result = STATE_CRITICAL;
231 else if (check_warning_value == TRUE && (processes >= warning_value))
232 result = STATE_WARNING;
234 die (result,
235 _("Process %s - %d instance%s of %s running"),
236 state_text(result),
237 processes,
238 (processes == 1) ? "" : "s",
239 process_name);
240 break;
242 case UPTIME:
244 if (result != STATE_OK)
245 return result;
247 uptime_raw_hours = strtod (recv_buffer, NULL);
248 uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
250 if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
251 result = STATE_CRITICAL;
252 else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
253 result = STATE_WARNING;
255 uptime_days = uptime_raw_minutes / 1440;
256 uptime_raw_minutes %= 1440;
257 uptime_hours = uptime_raw_minutes / 60;
258 uptime_raw_minutes %= 60;
259 uptime_minutes = uptime_raw_minutes;
261 die (result,
262 _("Uptime %s - Up %d days %d hours %d minutes"),
263 state_text(result),
264 uptime_days,
265 uptime_hours,
266 uptime_minutes);
267 break;
269 default:
270 die (STATE_UNKNOWN, _("Nothing to check!\n"));
271 break;
272 }
274 /* reset timeout */
275 /* alarm (0); */
277 /* printf (_("Reached end of program with no data returned\n")); */
279 /* return result; */
280 }
286 /* process command-line arguments */
287 int
288 process_arguments (int argc, char **argv)
289 {
290 int c;
292 int option = 0;
293 static struct option longopts[] = {
294 {"port", required_argument, 0, 'p'},
295 {"timeout", required_argument, 0, 't'},
296 {"critical", required_argument, 0, 'c'},
297 {"warning", required_argument, 0, 'w'},
298 {"variable", required_argument, 0, 'v'},
299 {"hostname", required_argument, 0, 'H'},
300 {"version", no_argument, 0, 'V'},
301 {"help", no_argument, 0, 'h'},
302 {0, 0, 0, 0}
303 };
305 /* no options were supplied */
306 if (argc < 2)
307 return ERROR;
309 /* backwards compatibility */
310 if (!is_option (argv[1])) {
311 server_address = argv[1];
312 argv[1] = argv[0];
313 argv = &argv[1];
314 argc--;
315 }
317 for (c = 1; c < argc; c++) {
318 if (strcmp ("-to", argv[c]) == 0)
319 strcpy (argv[c], "-t");
320 else if (strcmp ("-wv", argv[c]) == 0)
321 strcpy (argv[c], "-w");
322 else if (strcmp ("-cv", argv[c]) == 0)
323 strcpy (argv[c], "-c");
324 }
326 while (1) {
327 c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
328 &option);
330 if (c == -1 || c == EOF || c == 1)
331 break;
333 switch (c) {
334 case '?': /* print short usage statement if args not parsable */
335 printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
336 print_usage ();
337 exit (STATE_UNKNOWN);
338 case 'h': /* help */
339 print_help ();
340 exit (STATE_OK);
341 case 'V': /* version */
342 print_revision (progname, "$Revision$");
343 exit (STATE_OK);
344 case 'H': /* hostname */
345 server_address = optarg;
346 break;
347 case 'p': /* port */
348 if (is_intnonneg (optarg))
349 server_port = atoi (optarg);
350 else
351 die (STATE_UNKNOWN,
352 _("Server port an integer (seconds)\nType '%s -h' for additional help\n"),
353 progname);
354 break;
355 case 'v': /* variable */
356 if (strcmp (optarg, "LOAD") == 0) {
357 strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
358 if (strcmp (optarg, "LOAD1") == 0)
359 vars_to_check = LOAD1;
360 else if (strcmp (optarg, "LOAD5") == 0)
361 vars_to_check = LOAD5;
362 else if (strcmp (optarg, "LOAD15") == 0)
363 vars_to_check = LOAD15;
364 }
365 else if (strcmp (optarg, "UPTIME") == 0) {
366 vars_to_check = UPTIME;
367 strcpy (send_buffer, "UPTIME\r\n");
368 }
369 else if (strstr (optarg, "PROC") == optarg) {
370 vars_to_check = PROCS;
371 process_name = strscpy (process_name, optarg + 4);
372 sprintf (send_buffer, "PROCESS %s\r\n", process_name);
373 }
374 else if (strstr (optarg, "NET") == optarg) {
375 vars_to_check = NETSTAT;
376 netstat_port = atoi (optarg + 3);
377 sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
378 }
379 else if (strstr (optarg, "DPU") == optarg) {
380 vars_to_check = DPU;
381 strcpy (send_buffer, "DISKSPACE\r\n");
382 disk_name = strscpy (disk_name, optarg + 3);
383 }
384 else
385 return ERROR;
386 break;
387 case 'w': /* warning threshold */
388 warning_value = strtoul (optarg, NULL, 10);
389 check_warning_value = TRUE;
390 break;
391 case 'c': /* critical threshold */
392 critical_value = strtoul (optarg, NULL, 10);
393 check_critical_value = TRUE;
394 break;
395 case 't': /* timeout */
396 socket_timeout = atoi (optarg);
397 if (socket_timeout <= 0)
398 return ERROR;
399 }
401 }
402 return OK;
403 }
404 \f
405 void
406 print_usage (void)
407 {
408 printf (_("\
409 Usage: %s -H host [-p port] [-v variable] [-w warning] [-c critical]\n\
410 [-t timeout]\n"),
411 progname);
412 printf (_(UT_HLP_VRS), progname, progname);
413 }
415 void
416 print_help (void)
417 {
418 char *myport;
419 asprintf (&myport, "%d", PORT);
421 print_revision (progname, revision);
423 printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
424 printf (_(COPYRIGHT), copyright, email);
426 printf (_("\
427 This plugin attempts to contact the Over-CR collector daemon running on the\n\
428 remote UNIX server in order to gather the requested system information.\n\n"));
430 print_usage ();
432 printf (_(UT_HELP_VRSN));
434 printf (_(UT_HOST_PORT), 'p', myport);
436 printf (_("\
437 -v, --variable=STRING\n\
438 Variable to check. Valid variables include:\n\
439 LOAD1 = 1 minute average CPU load\n\
440 LOAD5 = 5 minute average CPU load\n\
441 LOAD15 = 15 minute average CPU load\n\
442 DPU<filesys> = percent used disk space on filesystem <filesys>\n\
443 PROC<process> = number of running processes with name <process>\n\
444 NET<port> = number of active connections on TCP port <port>\n\
445 UPTIME = system uptime in seconds\n"));
447 printf (_("\
448 -w, --warning=INTEGER\n\
449 Threshold which will result in a warning status\n\
450 -c, --critical=INTEGER\n\
451 Threshold which will result in a critical status\n"));
453 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
455 printf (_("\
456 Notes:\n\
457 - For the available options, the critical threshold value should always be\n\
458 higher than the warning threshold value, EXCEPT with the uptime variable\n\n"));
460 printf (_("\
461 - This plugin requres that Eric Molitors' Over-CR collector daemon be\n\
462 running on the remote server. Over-CR can be downloaded from\n\
463 http://www.molitor.org/overcr (This plugin was tested with version\n\
464 0.99.53 of the Over-CR collector)\n\n"));
466 printf (_(UT_SUPPORT));
467 }