Fix INADDR_NONE value (for systems which don't define it).
[monitoring-plugins.git] / plugins / check_overcr.c
blob5bcf931d47df2fe9de619db9b7ea14e5901e970c
1 /******************************************************************************
3 * Nagios check_overcr plugin
5 * License: GPL
6 * Copyright (c) 2000-2006 nagios-plugins team
8 * Last Modified: $Date$
10 * Description:
12 * This file contains the check_overcr plugin
14 * This plugin attempts to contact the Over-CR collector daemon running on the
15 * remote UNIX server in order to gather the requested system information.
18 * License Information:
20 * This program is free software; you can redistribute it and/or modify
21 * it under the terms of the GNU General Public License as published by
22 * the Free Software Foundation; either version 2 of the License, or
23 * (at your option) any later version.
25 * This program is distributed in the hope that it will be useful,
26 * but WITHOUT ANY WARRANTY; without even the implied warranty of
27 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
28 * GNU General Public License for more details.
30 * You should have received a copy of the GNU General Public License
31 * along with this program; if not, write to the Free Software
32 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
34 $Id$
36 ******************************************************************************/
38 const char *progname = "check_overcr";
39 const char *revision = "$Revision$";
40 const char *copyright = "2000-2006";
41 const char *email = "nagiosplug-devel@lists.sourceforge.net";
43 #include "common.h"
44 #include "netutils.h"
45 #include "utils.h"
47 enum checkvar {
48 NONE,
49 LOAD1,
50 LOAD5,
51 LOAD15,
52 DPU,
53 PROCS,
54 NETSTAT,
55 UPTIME
58 enum {
59 PORT = 2000
62 char *server_address = NULL;
63 int server_port = PORT;
64 double warning_value = 0L;
65 double critical_value = 0L;
66 int check_warning_value = FALSE;
67 int check_critical_value = FALSE;
68 enum checkvar vars_to_check = NONE;
69 int cmd_timeout = 1;
71 int netstat_port = 0;
72 char *disk_name = NULL;
73 char *process_name = NULL;
74 char send_buffer[MAX_INPUT_BUFFER];
76 int process_arguments (int, char **);
77 void print_usage (void);
78 void print_help (void);
80 int
81 main (int argc, char **argv)
83 int result = STATE_UNKNOWN;
84 char recv_buffer[MAX_INPUT_BUFFER];
85 char temp_buffer[MAX_INPUT_BUFFER];
86 char *temp_ptr = NULL;
87 int found_disk = FALSE;
88 unsigned long percent_used_disk_space = 100;
89 double load;
90 double load_1min;
91 double load_5min;
92 double load_15min;
93 int port_connections = 0;
94 int processes = 0;
95 double uptime_raw_hours;
96 int uptime_raw_minutes = 0;
97 int uptime_days = 0;
98 int uptime_hours = 0;
99 int uptime_minutes = 0;
101 setlocale (LC_ALL, "");
102 bindtextdomain (PACKAGE, LOCALEDIR);
103 textdomain (PACKAGE);
105 if (process_arguments (argc, argv) == ERROR)
106 usage4 (_("Could not parse arguments"));
108 /* initialize alarm signal handling */
109 signal (SIGALRM, socket_timeout_alarm_handler);
111 /* set socket timeout */
112 alarm (socket_timeout);
114 result = process_tcp_request2 (server_address,
115 server_port,
116 send_buffer,
117 recv_buffer,
118 sizeof (recv_buffer));
120 switch (vars_to_check) {
122 case LOAD1:
123 case LOAD5:
124 case LOAD15:
126 if (result != STATE_OK)
127 die (result, _("Unknown error fetching load data\n"));
129 temp_ptr = (char *) strtok (recv_buffer, "\r\n");
130 if (temp_ptr == NULL)
131 die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
132 else
133 load_1min = strtod (temp_ptr, NULL);
135 temp_ptr = (char *) strtok (NULL, "\r\n");
136 if (temp_ptr == NULL)
137 die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
138 else
139 load_5min = strtod (temp_ptr, NULL);
141 temp_ptr = (char *) strtok (NULL, "\r\n");
142 if (temp_ptr == NULL)
143 die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
144 else
145 load_15min = strtod (temp_ptr, NULL);
147 switch (vars_to_check) {
148 case LOAD1:
149 strcpy (temp_buffer, "1");
150 load = load_1min;
151 break;
152 case LOAD5:
153 strcpy (temp_buffer, "5");
154 load = load_5min;
155 break;
156 default:
157 strcpy (temp_buffer, "15");
158 load = load_15min;
159 break;
162 if (check_critical_value == TRUE && (load >= critical_value))
163 result = STATE_CRITICAL;
164 else if (check_warning_value == TRUE && (load >= warning_value))
165 result = STATE_WARNING;
167 die (result,
168 _("Load %s - %s-min load average = %0.2f"),
169 state_text(result),
170 temp_buffer,
171 load);
173 break;
175 case DPU:
177 if (result != STATE_OK)
178 die (result, _("Unknown error fetching disk data\n"));
180 for (temp_ptr = (char *) strtok (recv_buffer, " ");
181 temp_ptr != NULL;
182 temp_ptr = (char *) strtok (NULL, " ")) {
184 if (!strcmp (temp_ptr, disk_name)) {
185 found_disk = TRUE;
186 temp_ptr = (char *) strtok (NULL, "%");
187 if (temp_ptr == NULL)
188 die (STATE_CRITICAL, _("Invalid response from server\n"));
189 else
190 percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
191 break;
194 temp_ptr = (char *) strtok (NULL, "\r\n");
197 /* error if we couldn't find the info for the disk */
198 if (found_disk == FALSE)
199 die (STATE_CRITICAL,
200 "CRITICAL - Disk '%s' non-existent or not mounted",
201 disk_name);
203 if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
204 result = STATE_CRITICAL;
205 else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
206 result = STATE_WARNING;
208 die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
210 break;
212 case NETSTAT:
214 if (result != STATE_OK)
215 die (result, _("Unknown error fetching network status\n"));
216 else
217 port_connections = strtod (recv_buffer, NULL);
219 if (check_critical_value == TRUE && (port_connections >= critical_value))
220 result = STATE_CRITICAL;
221 else if (check_warning_value == TRUE && (port_connections >= warning_value))
222 result = STATE_WARNING;
224 die (result,
225 _("Net %s - %d connection%s on port %d"),
226 state_text(result),
227 port_connections,
228 (port_connections == 1) ? "" : "s",
229 netstat_port);
231 break;
233 case PROCS:
235 if (result != STATE_OK)
236 die (result, _("Unknown error fetching process status\n"));
238 temp_ptr = (char *) strtok (recv_buffer, "(");
239 if (temp_ptr == NULL)
240 die (STATE_CRITICAL, _("Invalid response from server\n"));
242 temp_ptr = (char *) strtok (NULL, ")");
243 if (temp_ptr == NULL)
244 die (STATE_CRITICAL, _("Invalid response from server\n"));
245 else
246 processes = strtod (temp_ptr, NULL);
248 if (check_critical_value == TRUE && (processes >= critical_value))
249 result = STATE_CRITICAL;
250 else if (check_warning_value == TRUE && (processes >= warning_value))
251 result = STATE_WARNING;
253 die (result,
254 _("Process %s - %d instance%s of %s running"),
255 state_text(result),
256 processes,
257 (processes == 1) ? "" : "s",
258 process_name);
259 break;
261 case UPTIME:
263 if (result != STATE_OK)
264 return result;
266 uptime_raw_hours = strtod (recv_buffer, NULL);
267 uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
269 if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
270 result = STATE_CRITICAL;
271 else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
272 result = STATE_WARNING;
274 uptime_days = uptime_raw_minutes / 1440;
275 uptime_raw_minutes %= 1440;
276 uptime_hours = uptime_raw_minutes / 60;
277 uptime_raw_minutes %= 60;
278 uptime_minutes = uptime_raw_minutes;
280 die (result,
281 _("Uptime %s - Up %d days %d hours %d minutes"),
282 state_text(result),
283 uptime_days,
284 uptime_hours,
285 uptime_minutes);
286 break;
288 default:
289 die (STATE_UNKNOWN, _("Nothing to check!\n"));
290 break;
295 /* process command-line arguments */
297 process_arguments (int argc, char **argv)
299 int c;
301 int option = 0;
302 static struct option longopts[] = {
303 {"port", required_argument, 0, 'p'},
304 {"timeout", required_argument, 0, 't'},
305 {"critical", required_argument, 0, 'c'},
306 {"warning", required_argument, 0, 'w'},
307 {"variable", required_argument, 0, 'v'},
308 {"hostname", required_argument, 0, 'H'},
309 {"version", no_argument, 0, 'V'},
310 {"help", no_argument, 0, 'h'},
311 {0, 0, 0, 0}
314 /* no options were supplied */
315 if (argc < 2)
316 return ERROR;
318 /* backwards compatibility */
319 if (!is_option (argv[1])) {
320 server_address = argv[1];
321 argv[1] = argv[0];
322 argv = &argv[1];
323 argc--;
326 for (c = 1; c < argc; c++) {
327 if (strcmp ("-to", argv[c]) == 0)
328 strcpy (argv[c], "-t");
329 else if (strcmp ("-wv", argv[c]) == 0)
330 strcpy (argv[c], "-w");
331 else if (strcmp ("-cv", argv[c]) == 0)
332 strcpy (argv[c], "-c");
335 while (1) {
336 c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
337 &option);
339 if (c == -1 || c == EOF || c == 1)
340 break;
342 switch (c) {
343 case '?': /* print short usage statement if args not parsable */
344 usage5 ();
345 case 'h': /* help */
346 print_help ();
347 exit (STATE_OK);
348 case 'V': /* version */
349 print_revision (progname, revision);
350 exit (STATE_OK);
351 case 'H': /* hostname */
352 server_address = optarg;
353 break;
354 case 'p': /* port */
355 if (is_intnonneg (optarg))
356 server_port = atoi (optarg);
357 else
358 die (STATE_UNKNOWN,
359 _("Server port an integer\n"));
360 break;
361 case 'v': /* variable */
362 if (strcmp (optarg, "LOAD") == 0) {
363 strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
364 if (strcmp (optarg, "LOAD1") == 0)
365 vars_to_check = LOAD1;
366 else if (strcmp (optarg, "LOAD5") == 0)
367 vars_to_check = LOAD5;
368 else if (strcmp (optarg, "LOAD15") == 0)
369 vars_to_check = LOAD15;
371 else if (strcmp (optarg, "UPTIME") == 0) {
372 vars_to_check = UPTIME;
373 strcpy (send_buffer, "UPTIME\r\n");
375 else if (strstr (optarg, "PROC") == optarg) {
376 vars_to_check = PROCS;
377 process_name = strscpy (process_name, optarg + 4);
378 sprintf (send_buffer, "PROCESS %s\r\n", process_name);
380 else if (strstr (optarg, "NET") == optarg) {
381 vars_to_check = NETSTAT;
382 netstat_port = atoi (optarg + 3);
383 sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
385 else if (strstr (optarg, "DPU") == optarg) {
386 vars_to_check = DPU;
387 strcpy (send_buffer, "DISKSPACE\r\n");
388 disk_name = strscpy (disk_name, optarg + 3);
390 else
391 return ERROR;
392 break;
393 case 'w': /* warning threshold */
394 warning_value = strtoul (optarg, NULL, 10);
395 check_warning_value = TRUE;
396 break;
397 case 'c': /* critical threshold */
398 critical_value = strtoul (optarg, NULL, 10);
399 check_critical_value = TRUE;
400 break;
401 case 't': /* timeout */
402 socket_timeout = atoi (optarg);
403 if (socket_timeout <= 0)
404 return ERROR;
408 return OK;
412 void
413 print_help (void)
415 char *myport;
416 asprintf (&myport, "%d", PORT);
418 print_revision (progname, revision);
420 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
421 printf (COPYRIGHT, copyright, email);
423 printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
424 printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
426 printf ("\n\n");
428 print_usage ();
430 printf (_(UT_HELP_VRSN));
432 printf (_(UT_HOST_PORT), 'p', myport);
434 printf (" %s\n", "-w, --warning=INTEGER");
435 printf (" %s\n", _("Threshold which will result in a warning status"));
436 printf (" %s\n", "-c, --critical=INTEGER");
437 printf (" %s\n", _("Threshold which will result in a critical status"));
438 printf (" %s\n", "-v, --variable=STRING");
439 printf (" %s\n", _("Variable to check. Valid variables include:"));
440 printf (" %s\n", _("LOAD1 = 1 minute average CPU load"));
441 printf (" %s\n", _("LOAD5 = 5 minute average CPU load"));
442 printf (" %s\n", _("LOAD15 = 15 minute average CPU load"));
443 printf (" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>"));
444 printf (" %s\n", _("PROC<process> = number of running processes with name <process>"));
445 printf (" %s\n", _("NET<port> = number of active connections on TCP port <port>"));
446 printf (" %s\n", _("UPTIME = system uptime in seconds"));
448 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
450 printf (_(UT_VERBOSE));
451 printf ("\n");
452 printf ("%s\n", _("Notes:"));
454 printf ("%s\n", _("For the available options, the critical threshold value should always be"));
455 printf ("%s\n\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
457 printf ("%s\n", _("This plugin requres that Eric Molitors' Over-CR collector daemon be"));
458 printf ("%s\n", _("running on the remote server."));
459 printf ("%s\n", " Over-CR can be downloaded from http://www.molitor.org/overcr");
460 printf ("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector"));
462 printf (_(UT_SUPPORT));
466 void
467 print_usage (void)
469 printf (_("Usage:"));
470 printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);