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-2006";
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 }
275 }
278 /* process command-line arguments */
279 int
280 process_arguments (int argc, char **argv)
281 {
282 int c;
284 int option = 0;
285 static struct option longopts[] = {
286 {"port", required_argument, 0, 'p'},
287 {"timeout", required_argument, 0, 't'},
288 {"critical", required_argument, 0, 'c'},
289 {"warning", required_argument, 0, 'w'},
290 {"variable", required_argument, 0, 'v'},
291 {"hostname", required_argument, 0, 'H'},
292 {"version", no_argument, 0, 'V'},
293 {"help", no_argument, 0, 'h'},
294 {0, 0, 0, 0}
295 };
297 /* no options were supplied */
298 if (argc < 2)
299 return ERROR;
301 /* backwards compatibility */
302 if (!is_option (argv[1])) {
303 server_address = argv[1];
304 argv[1] = argv[0];
305 argv = &argv[1];
306 argc--;
307 }
309 for (c = 1; c < argc; c++) {
310 if (strcmp ("-to", argv[c]) == 0)
311 strcpy (argv[c], "-t");
312 else if (strcmp ("-wv", argv[c]) == 0)
313 strcpy (argv[c], "-w");
314 else if (strcmp ("-cv", argv[c]) == 0)
315 strcpy (argv[c], "-c");
316 }
318 while (1) {
319 c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
320 &option);
322 if (c == -1 || c == EOF || c == 1)
323 break;
325 switch (c) {
326 case '?': /* print short usage statement if args not parsable */
327 usage2 (_("Unknown argument"), optarg);
328 case 'h': /* help */
329 print_help ();
330 exit (STATE_OK);
331 case 'V': /* version */
332 print_revision (progname, revision);
333 exit (STATE_OK);
334 case 'H': /* hostname */
335 server_address = optarg;
336 break;
337 case 'p': /* port */
338 if (is_intnonneg (optarg))
339 server_port = atoi (optarg);
340 else
341 die (STATE_UNKNOWN,
342 _("Server port an integer\n"));
343 break;
344 case 'v': /* variable */
345 if (strcmp (optarg, "LOAD") == 0) {
346 strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
347 if (strcmp (optarg, "LOAD1") == 0)
348 vars_to_check = LOAD1;
349 else if (strcmp (optarg, "LOAD5") == 0)
350 vars_to_check = LOAD5;
351 else if (strcmp (optarg, "LOAD15") == 0)
352 vars_to_check = LOAD15;
353 }
354 else if (strcmp (optarg, "UPTIME") == 0) {
355 vars_to_check = UPTIME;
356 strcpy (send_buffer, "UPTIME\r\n");
357 }
358 else if (strstr (optarg, "PROC") == optarg) {
359 vars_to_check = PROCS;
360 process_name = strscpy (process_name, optarg + 4);
361 sprintf (send_buffer, "PROCESS %s\r\n", process_name);
362 }
363 else if (strstr (optarg, "NET") == optarg) {
364 vars_to_check = NETSTAT;
365 netstat_port = atoi (optarg + 3);
366 sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
367 }
368 else if (strstr (optarg, "DPU") == optarg) {
369 vars_to_check = DPU;
370 strcpy (send_buffer, "DISKSPACE\r\n");
371 disk_name = strscpy (disk_name, optarg + 3);
372 }
373 else
374 return ERROR;
375 break;
376 case 'w': /* warning threshold */
377 warning_value = strtoul (optarg, NULL, 10);
378 check_warning_value = TRUE;
379 break;
380 case 'c': /* critical threshold */
381 critical_value = strtoul (optarg, NULL, 10);
382 check_critical_value = TRUE;
383 break;
384 case 't': /* timeout */
385 socket_timeout = atoi (optarg);
386 if (socket_timeout <= 0)
387 return ERROR;
388 }
390 }
391 return OK;
392 }
395 void
396 print_help (void)
397 {
398 char *myport;
399 asprintf (&myport, "%d", PORT);
401 print_revision (progname, revision);
403 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
404 printf (COPYRIGHT, copyright, email);
406 printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
407 printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
409 printf ("\n\n");
411 print_usage ();
413 printf (_(UT_HELP_VRSN));
415 printf (_(UT_HOST_PORT), 'p', myport);
417 printf (" %s\n", "-w, --warning=INTEGER");
418 printf (" %s\n", _("Threshold which will result in a warning status"));
419 printf (" %s\n", "-c, --critical=INTEGER");
420 printf (" %s\n", _("Threshold which will result in a critical status"));
421 printf (" %s\n", "-v, --variable=STRING");
422 printf (" %s\n", _("Variable to check. Valid variables include:"));
423 printf (" %s\n", _("LOAD1 = 1 minute average CPU load"));
424 printf (" %s\n", _("LOAD5 = 5 minute average CPU load"));
425 printf (" %s\n", _("LOAD15 = 15 minute average CPU load"));
426 printf (" %s\n", _("DPU<filesys> = percent used disk space on filesystem <filesys>"));
427 printf (" %s\n", _("PROC<process> = number of running processes with name <process>"));
428 printf (" %s\n", _("NET<port> = number of active connections on TCP port <port>"));
429 printf (" %s\n", _("UPTIME = system uptime in seconds"));
431 printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
433 printf (_(UT_VERBOSE));
434 printf ("\n");
435 printf ("%s\n", _("Notes:"));
437 printf ("%s\n", _("For the available options, the critical threshold value should always be"));
438 printf ("%s\n\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
440 printf ("%s\n", _("This plugin requres that Eric Molitors' Over-CR collector daemon be"));
441 printf ("%s\n", _("running on the remote server. Over-CR can be downloaded from"));
442 printf ("%s\n", _("http://www.molitor.org/overcr (This plugin was tested with version 0.99.53"));
443 printf ("%s\n", _("of the Over-CR collector)"));
445 printf (_(UT_SUPPORT));
446 }
449 void
450 print_usage (void)
451 {
452 printf (_("Usage:"));
453 printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);
454 }