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 $Id$
19 ******************************************************************************/
21 const char *progname = "check_overcr";
22 const char *revision = "$Revision$";
23 const char *copyright = "2000-2004";
24 const char *email = "nagiosplug-devel@lists.sourceforge.net";
26 #include "common.h"
27 #include "netutils.h"
28 #include "utils.h"
30 enum checkvar {
31 NONE,
32 LOAD1,
33 LOAD5,
34 LOAD15,
35 DPU,
36 PROCS,
37 NETSTAT,
38 UPTIME
39 };
41 enum {
42 PORT = 2000
43 };
45 char *server_address = NULL;
46 int server_port = PORT;
47 double warning_value = 0L;
48 double critical_value = 0L;
49 int check_warning_value = FALSE;
50 int check_critical_value = FALSE;
51 enum checkvar vars_to_check = NONE;
52 int cmd_timeout = 1;
54 int netstat_port = 0;
55 char *disk_name = NULL;
56 char *process_name = NULL;
57 char send_buffer[MAX_INPUT_BUFFER];
59 int process_arguments (int, char **);
60 void print_usage (void);
61 void print_help (void);
63 int
64 main (int argc, char **argv)
65 {
66 int result = STATE_UNKNOWN;
67 char recv_buffer[MAX_INPUT_BUFFER];
68 char temp_buffer[MAX_INPUT_BUFFER];
69 char *temp_ptr = NULL;
70 int found_disk = FALSE;
71 unsigned long percent_used_disk_space = 100;
72 double load;
73 double load_1min;
74 double load_5min;
75 double load_15min;
76 int port_connections = 0;
77 int processes = 0;
78 double uptime_raw_hours;
79 int uptime_raw_minutes = 0;
80 int uptime_days = 0;
81 int uptime_hours = 0;
82 int uptime_minutes = 0;
84 setlocale (LC_ALL, "");
85 bindtextdomain (PACKAGE, LOCALEDIR);
86 textdomain (PACKAGE);
88 if (process_arguments (argc, argv) == ERROR)
89 usage4 (_("Could not parse arguments"));
91 /* initialize alarm signal handling */
92 signal (SIGALRM, socket_timeout_alarm_handler);
94 /* set socket timeout */
95 alarm (socket_timeout);
97 result = process_tcp_request2 (server_address,
98 server_port,
99 send_buffer,
100 recv_buffer,
101 sizeof (recv_buffer));
103 switch (vars_to_check) {
105 case LOAD1:
106 case LOAD5:
107 case LOAD15:
109 if (result != STATE_OK)
110 die (result, _("Unknown error fetching load data\n"));
112 temp_ptr = (char *) strtok (recv_buffer, "\r\n");
113 if (temp_ptr == NULL)
114 die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
115 else
116 load_1min = strtod (temp_ptr, NULL);
118 temp_ptr = (char *) strtok (NULL, "\r\n");
119 if (temp_ptr == NULL)
120 die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
121 else
122 load_5min = strtod (temp_ptr, NULL);
124 temp_ptr = (char *) strtok (NULL, "\r\n");
125 if (temp_ptr == NULL)
126 die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
127 else
128 load_15min = strtod (temp_ptr, NULL);
130 switch (vars_to_check) {
131 case LOAD1:
132 strcpy (temp_buffer, "1");
133 load = load_1min;
134 break;
135 case LOAD5:
136 strcpy (temp_buffer, "5");
137 load = load_5min;
138 break;
139 default:
140 strcpy (temp_buffer, "15");
141 load = load_15min;
142 break;
143 }
145 if (check_critical_value == TRUE && (load >= critical_value))
146 result = STATE_CRITICAL;
147 else if (check_warning_value == TRUE && (load >= warning_value))
148 result = STATE_WARNING;
150 die (result,
151 _("Load %s - %s-min load average = %0.2f"),
152 state_text(result),
153 temp_buffer,
154 load);
156 break;
158 case DPU:
160 if (result != STATE_OK)
161 die (result, _("Unknown error fetching disk data\n"));
163 for (temp_ptr = (char *) strtok (recv_buffer, " ");
164 temp_ptr != NULL;
165 temp_ptr = (char *) strtok (NULL, " ")) {
167 if (!strcmp (temp_ptr, disk_name)) {
168 found_disk = TRUE;
169 temp_ptr = (char *) strtok (NULL, "%");
170 if (temp_ptr == NULL)
171 die (STATE_CRITICAL, _("Invalid response from server\n"));
172 else
173 percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
174 break;
175 }
177 temp_ptr = (char *) strtok (NULL, "\r\n");
178 }
180 /* error if we couldn't find the info for the disk */
181 if (found_disk == FALSE)
182 die (STATE_CRITICAL,
183 "CRITICAL - Disk '%s' non-existent or not mounted",
184 disk_name);
186 if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
187 result = STATE_CRITICAL;
188 else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
189 result = STATE_WARNING;
191 die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
193 break;
195 case NETSTAT:
197 if (result != STATE_OK)
198 die (result, _("Unknown error fetching network status\n"));
199 else
200 port_connections = strtod (recv_buffer, NULL);
202 if (check_critical_value == TRUE && (port_connections >= critical_value))
203 result = STATE_CRITICAL;
204 else if (check_warning_value == TRUE && (port_connections >= warning_value))
205 result = STATE_WARNING;
207 die (result,
208 _("Net %s - %d connection%s on port %d"),
209 state_text(result),
210 port_connections,
211 (port_connections == 1) ? "" : "s",
212 netstat_port);
214 break;
216 case PROCS:
218 if (result != STATE_OK)
219 die (result, _("Unknown error fetching process status\n"));
221 temp_ptr = (char *) strtok (recv_buffer, "(");
222 if (temp_ptr == NULL)
223 die (STATE_CRITICAL, _("Invalid response from server\n"));
225 temp_ptr = (char *) strtok (NULL, ")");
226 if (temp_ptr == NULL)
227 die (STATE_CRITICAL, _("Invalid response from server\n"));
228 else
229 processes = strtod (temp_ptr, NULL);
231 if (check_critical_value == TRUE && (processes >= critical_value))
232 result = STATE_CRITICAL;
233 else if (check_warning_value == TRUE && (processes >= warning_value))
234 result = STATE_WARNING;
236 die (result,
237 _("Process %s - %d instance%s of %s running"),
238 state_text(result),
239 processes,
240 (processes == 1) ? "" : "s",
241 process_name);
242 break;
244 case UPTIME:
246 if (result != STATE_OK)
247 return result;
249 uptime_raw_hours = strtod (recv_buffer, NULL);
250 uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
252 if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
253 result = STATE_CRITICAL;
254 else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
255 result = STATE_WARNING;
257 uptime_days = uptime_raw_minutes / 1440;
258 uptime_raw_minutes %= 1440;
259 uptime_hours = uptime_raw_minutes / 60;
260 uptime_raw_minutes %= 60;
261 uptime_minutes = uptime_raw_minutes;
263 die (result,
264 _("Uptime %s - Up %d days %d hours %d minutes"),
265 state_text(result),
266 uptime_days,
267 uptime_hours,
268 uptime_minutes);
269 break;
271 default:
272 die (STATE_UNKNOWN, _("Nothing to check!\n"));
273 break;
274 }
276 /* reset timeout */
277 /* alarm (0); */
279 /* printf (_("Reached end of program with no data returned\n")); */
281 /* return result; */
282 }
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 }
407 void
408 print_help (void)
409 {
410 char *myport;
411 asprintf (&myport, "%d", PORT);
413 print_revision (progname, revision);
415 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
416 printf (COPYRIGHT, copyright, email);
418 printf (_("\
419 This plugin attempts to contact the Over-CR collector daemon running on the\n\
420 remote UNIX server in order to gather the requested system information.\n\n"));
422 print_usage ();
424 printf (_(UT_HELP_VRSN));
426 printf (_(UT_HOST_PORT), 'p', myport);
428 printf (_("\
429 -v, --variable=STRING\n\
430 Variable to check. Valid variables include:\n\
431 LOAD1 = 1 minute average CPU load\n\
432 LOAD5 = 5 minute average CPU load\n\
433 LOAD15 = 15 minute average CPU load\n\
434 DPU<filesys> = percent used disk space on filesystem <filesys>\n\
435 PROC<process> = number of running processes with name <process>\n\
436 NET<port> = number of active connections on TCP port <port>\n\
437 UPTIME = system uptime in seconds\n"));
439 printf (_("\
440 -w, --warning=INTEGER\n\
441 Threshold which will result in a warning status\n\
442 -c, --critical=INTEGER\n\
443 Threshold which will result in a critical status\n"));
445 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
447 printf (_("\
448 Notes:\n\
449 - For the available options, the critical threshold value should always be\n\
450 higher than the warning threshold value, EXCEPT with the uptime variable\n\n"));
452 printf (_("\
453 - This plugin requres that Eric Molitors' Over-CR collector daemon be\n\
454 running on the remote server. Over-CR can be downloaded from\n\
455 http://www.molitor.org/overcr (This plugin was tested with version\n\
456 0.99.53 of the Over-CR collector)\n\n"));
458 printf (_(UT_SUPPORT));
459 }
463 void
464 print_usage (void)
465 {
466 printf ("\
467 Usage: %s -H host [-p port] [-v variable] [-w warning] [-c critical]\n\
468 [-t timeout]\n", progname);
469 }