1 /*****************************************************************************
2 *
3 * Nagios check_overcr plugin
4 *
5 * License: GPL
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
7 *
8 * Last Modified: $Date$
9 *
10 * Description:
11 *
12 * This file contains the check_overcr plugin
13 *
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.
16 *
17 *
18 * This program is free software: you can redistribute it and/or modify
19 * it under the terms of the GNU General Public License as published by
20 * the Free Software Foundation, either version 3 of the License, or
21 * (at your option) any later version.
22 *
23 * This program is distributed in the hope that it will be useful,
24 * but WITHOUT ANY WARRANTY; without even the implied warranty of
25 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
26 * GNU General Public License for more details.
27 *
28 * You should have received a copy of the GNU General Public License
29 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *
31 * $Id$
32 *
33 *****************************************************************************/
35 const char *progname = "check_overcr";
36 const char *revision = "$Revision$";
37 const char *copyright = "2000-2007";
38 const char *email = "nagiosplug-devel@lists.sourceforge.net";
40 #include "common.h"
41 #include "netutils.h"
42 #include "utils.h"
44 enum checkvar {
45 NONE,
46 LOAD1,
47 LOAD5,
48 LOAD15,
49 DPU,
50 PROCS,
51 NETSTAT,
52 UPTIME
53 };
55 enum {
56 PORT = 2000
57 };
59 char *server_address = NULL;
60 int server_port = PORT;
61 double warning_value = 0L;
62 double critical_value = 0L;
63 int check_warning_value = FALSE;
64 int check_critical_value = FALSE;
65 enum checkvar vars_to_check = NONE;
66 int cmd_timeout = 1;
68 int netstat_port = 0;
69 char *disk_name = NULL;
70 char *process_name = NULL;
71 char send_buffer[MAX_INPUT_BUFFER];
73 int process_arguments (int, char **);
74 void print_usage (void);
75 void print_help (void);
77 int
78 main (int argc, char **argv)
79 {
80 int result = STATE_UNKNOWN;
81 char recv_buffer[MAX_INPUT_BUFFER];
82 char temp_buffer[MAX_INPUT_BUFFER];
83 char *temp_ptr = NULL;
84 int found_disk = FALSE;
85 unsigned long percent_used_disk_space = 100;
86 double load;
87 double load_1min;
88 double load_5min;
89 double load_15min;
90 int port_connections = 0;
91 int processes = 0;
92 double uptime_raw_hours;
93 int uptime_raw_minutes = 0;
94 int uptime_days = 0;
95 int uptime_hours = 0;
96 int uptime_minutes = 0;
98 setlocale (LC_ALL, "");
99 bindtextdomain (PACKAGE, LOCALEDIR);
100 textdomain (PACKAGE);
102 if (process_arguments (argc, argv) == ERROR)
103 usage4 (_("Could not parse arguments"));
105 /* initialize alarm signal handling */
106 signal (SIGALRM, socket_timeout_alarm_handler);
108 /* set socket timeout */
109 alarm (socket_timeout);
111 result = process_tcp_request2 (server_address,
112 server_port,
113 send_buffer,
114 recv_buffer,
115 sizeof (recv_buffer));
117 switch (vars_to_check) {
119 case LOAD1:
120 case LOAD5:
121 case LOAD15:
123 if (result != STATE_OK)
124 die (result, _("Unknown error fetching load data\n"));
126 temp_ptr = (char *) strtok (recv_buffer, "\r\n");
127 if (temp_ptr == NULL)
128 die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
129 else
130 load_1min = strtod (temp_ptr, NULL);
132 temp_ptr = (char *) strtok (NULL, "\r\n");
133 if (temp_ptr == NULL)
134 die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
135 else
136 load_5min = strtod (temp_ptr, NULL);
138 temp_ptr = (char *) strtok (NULL, "\r\n");
139 if (temp_ptr == NULL)
140 die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
141 else
142 load_15min = strtod (temp_ptr, NULL);
144 switch (vars_to_check) {
145 case LOAD1:
146 strcpy (temp_buffer, "1");
147 load = load_1min;
148 break;
149 case LOAD5:
150 strcpy (temp_buffer, "5");
151 load = load_5min;
152 break;
153 default:
154 strcpy (temp_buffer, "15");
155 load = load_15min;
156 break;
157 }
159 if (check_critical_value == TRUE && (load >= critical_value))
160 result = STATE_CRITICAL;
161 else if (check_warning_value == TRUE && (load >= warning_value))
162 result = STATE_WARNING;
164 die (result,
165 _("Load %s - %s-min load average = %0.2f"),
166 state_text(result),
167 temp_buffer,
168 load);
170 break;
172 case DPU:
174 if (result != STATE_OK)
175 die (result, _("Unknown error fetching disk data\n"));
177 for (temp_ptr = (char *) strtok (recv_buffer, " ");
178 temp_ptr != NULL;
179 temp_ptr = (char *) strtok (NULL, " ")) {
181 if (!strcmp (temp_ptr, disk_name)) {
182 found_disk = TRUE;
183 temp_ptr = (char *) strtok (NULL, "%");
184 if (temp_ptr == NULL)
185 die (STATE_CRITICAL, _("Invalid response from server\n"));
186 else
187 percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
188 break;
189 }
191 temp_ptr = (char *) strtok (NULL, "\r\n");
192 }
194 /* error if we couldn't find the info for the disk */
195 if (found_disk == FALSE)
196 die (STATE_CRITICAL,
197 "CRITICAL - Disk '%s' non-existent or not mounted",
198 disk_name);
200 if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
201 result = STATE_CRITICAL;
202 else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
203 result = STATE_WARNING;
205 die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
207 break;
209 case NETSTAT:
211 if (result != STATE_OK)
212 die (result, _("Unknown error fetching network status\n"));
213 else
214 port_connections = strtod (recv_buffer, NULL);
216 if (check_critical_value == TRUE && (port_connections >= critical_value))
217 result = STATE_CRITICAL;
218 else if (check_warning_value == TRUE && (port_connections >= warning_value))
219 result = STATE_WARNING;
221 die (result,
222 _("Net %s - %d connection%s on port %d"),
223 state_text(result),
224 port_connections,
225 (port_connections == 1) ? "" : "s",
226 netstat_port);
228 break;
230 case PROCS:
232 if (result != STATE_OK)
233 die (result, _("Unknown error fetching process status\n"));
235 temp_ptr = (char *) strtok (recv_buffer, "(");
236 if (temp_ptr == NULL)
237 die (STATE_CRITICAL, _("Invalid response from server\n"));
239 temp_ptr = (char *) strtok (NULL, ")");
240 if (temp_ptr == NULL)
241 die (STATE_CRITICAL, _("Invalid response from server\n"));
242 else
243 processes = strtod (temp_ptr, NULL);
245 if (check_critical_value == TRUE && (processes >= critical_value))
246 result = STATE_CRITICAL;
247 else if (check_warning_value == TRUE && (processes >= warning_value))
248 result = STATE_WARNING;
250 die (result,
251 _("Process %s - %d instance%s of %s running"),
252 state_text(result),
253 processes,
254 (processes == 1) ? "" : "s",
255 process_name);
256 break;
258 case UPTIME:
260 if (result != STATE_OK)
261 return result;
263 uptime_raw_hours = strtod (recv_buffer, NULL);
264 uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
266 if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
267 result = STATE_CRITICAL;
268 else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
269 result = STATE_WARNING;
271 uptime_days = uptime_raw_minutes / 1440;
272 uptime_raw_minutes %= 1440;
273 uptime_hours = uptime_raw_minutes / 60;
274 uptime_raw_minutes %= 60;
275 uptime_minutes = uptime_raw_minutes;
277 die (result,
278 _("Uptime %s - Up %d days %d hours %d minutes"),
279 state_text(result),
280 uptime_days,
281 uptime_hours,
282 uptime_minutes);
283 break;
285 default:
286 die (STATE_UNKNOWN, _("Nothing to check!\n"));
287 break;
288 }
289 }
292 /* process command-line arguments */
293 int
294 process_arguments (int argc, char **argv)
295 {
296 int c;
298 int option = 0;
299 static struct option longopts[] = {
300 {"port", required_argument, 0, 'p'},
301 {"timeout", required_argument, 0, 't'},
302 {"critical", required_argument, 0, 'c'},
303 {"warning", required_argument, 0, 'w'},
304 {"variable", required_argument, 0, 'v'},
305 {"hostname", required_argument, 0, 'H'},
306 {"version", no_argument, 0, 'V'},
307 {"help", no_argument, 0, 'h'},
308 {0, 0, 0, 0}
309 };
311 /* no options were supplied */
312 if (argc < 2)
313 return ERROR;
315 /* backwards compatibility */
316 if (!is_option (argv[1])) {
317 server_address = argv[1];
318 argv[1] = argv[0];
319 argv = &argv[1];
320 argc--;
321 }
323 for (c = 1; c < argc; c++) {
324 if (strcmp ("-to", argv[c]) == 0)
325 strcpy (argv[c], "-t");
326 else if (strcmp ("-wv", argv[c]) == 0)
327 strcpy (argv[c], "-w");
328 else if (strcmp ("-cv", argv[c]) == 0)
329 strcpy (argv[c], "-c");
330 }
332 while (1) {
333 c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
334 &option);
336 if (c == -1 || c == EOF || c == 1)
337 break;
339 switch (c) {
340 case '?': /* print short usage statement if args not parsable */
341 usage5 ();
342 case 'h': /* help */
343 print_help ();
344 exit (STATE_OK);
345 case 'V': /* version */
346 print_revision (progname, revision);
347 exit (STATE_OK);
348 case 'H': /* hostname */
349 server_address = optarg;
350 break;
351 case 'p': /* port */
352 if (is_intnonneg (optarg))
353 server_port = atoi (optarg);
354 else
355 die (STATE_UNKNOWN,
356 _("Server port an integer\n"));
357 break;
358 case 'v': /* variable */
359 if (strcmp (optarg, "LOAD") == 0) {
360 strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
361 if (strcmp (optarg, "LOAD1") == 0)
362 vars_to_check = LOAD1;
363 else if (strcmp (optarg, "LOAD5") == 0)
364 vars_to_check = LOAD5;
365 else if (strcmp (optarg, "LOAD15") == 0)
366 vars_to_check = LOAD15;
367 }
368 else if (strcmp (optarg, "UPTIME") == 0) {
369 vars_to_check = UPTIME;
370 strcpy (send_buffer, "UPTIME\r\n");
371 }
372 else if (strstr (optarg, "PROC") == optarg) {
373 vars_to_check = PROCS;
374 process_name = strscpy (process_name, optarg + 4);
375 sprintf (send_buffer, "PROCESS %s\r\n", process_name);
376 }
377 else if (strstr (optarg, "NET") == optarg) {
378 vars_to_check = NETSTAT;
379 netstat_port = atoi (optarg + 3);
380 sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
381 }
382 else if (strstr (optarg, "DPU") == optarg) {
383 vars_to_check = DPU;
384 strcpy (send_buffer, "DISKSPACE\r\n");
385 disk_name = strscpy (disk_name, optarg + 3);
386 }
387 else
388 return ERROR;
389 break;
390 case 'w': /* warning threshold */
391 warning_value = strtoul (optarg, NULL, 10);
392 check_warning_value = TRUE;
393 break;
394 case 'c': /* critical threshold */
395 critical_value = strtoul (optarg, NULL, 10);
396 check_critical_value = TRUE;
397 break;
398 case 't': /* timeout */
399 socket_timeout = atoi (optarg);
400 if (socket_timeout <= 0)
401 return ERROR;
402 }
404 }
405 return OK;
406 }
409 void
410 print_help (void)
411 {
412 char *myport;
413 asprintf (&myport, "%d", PORT);
415 print_revision (progname, revision);
417 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
418 printf (COPYRIGHT, copyright, email);
420 printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
421 printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
423 printf ("\n\n");
425 print_usage ();
427 printf (_(UT_HELP_VRSN));
429 printf (_(UT_HOST_PORT), 'p', myport);
431 printf (" %s\n", "-w, --warning=INTEGER");
432 printf (" %s\n", _("Threshold which will result in a warning status"));
433 printf (" %s\n", "-c, --critical=INTEGER");
434 printf (" %s\n", _("Threshold which will result in a critical status"));
435 printf (" %s\n", "-v, --variable=STRING");
436 printf (" %s\n", _("Variable to check. Valid variables include:"));
437 printf (" %s\n", _("LOAD1 = 1 minute average CPU load"));
438 printf (" %s\n", _("LOAD5 = 5 minute average CPU load"));
439 printf (" %s\n", _("LOAD15 = 15 minute average CPU load"));
440 printf (" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>"));
441 printf (" %s\n", _("PROC<process> = number of running processes with name <process>"));
442 printf (" %s\n", _("NET<port> = number of active connections on TCP port <port>"));
443 printf (" %s\n", _("UPTIME = system uptime in seconds"));
445 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
447 printf (_(UT_VERBOSE));
448 printf ("\n");
449 printf ("%s\n", _("Notes:"));
451 printf ("%s\n", _("For the available options, the critical threshold value should always be"));
452 printf ("%s\n\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
454 printf ("%s\n", _("This plugin requres that Eric Molitors' Over-CR collector daemon be"));
455 printf ("%s\n", _("running on the remote server."));
456 printf ("%s\n", " Over-CR can be downloaded from http://www.molitor.org/overcr");
457 printf ("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector"));
459 printf (_(UT_SUPPORT));
460 }
463 void
464 print_usage (void)
465 {
466 printf (_("Usage:"));
467 printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);
468 }