1 /******************************************************************************
3 * Nagios check_overcr plugin
6 * Copyright (c) 2000-2006 nagios-plugins team
8 * Last Modified: $Date$
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.
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";
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
;
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);
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;
93 int port_connections
= 0;
95 double uptime_raw_hours
;
96 int uptime_raw_minutes
= 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
,
118 sizeof (recv_buffer
));
120 switch (vars_to_check
) {
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"));
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"));
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"));
145 load_15min
= strtod (temp_ptr
, NULL
);
147 switch (vars_to_check
) {
149 strcpy (temp_buffer
, "1");
153 strcpy (temp_buffer
, "5");
157 strcpy (temp_buffer
, "15");
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
;
168 _("Load %s - %s-min load average = %0.2f"),
177 if (result
!= STATE_OK
)
178 die (result
, _("Unknown error fetching disk data\n"));
180 for (temp_ptr
= (char *) strtok (recv_buffer
, " ");
182 temp_ptr
= (char *) strtok (NULL
, " ")) {
184 if (!strcmp (temp_ptr
, disk_name
)) {
186 temp_ptr
= (char *) strtok (NULL
, "%");
187 if (temp_ptr
== NULL
)
188 die (STATE_CRITICAL
, _("Invalid response from server\n"));
190 percent_used_disk_space
= strtoul (temp_ptr
, NULL
, 10);
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
)
200 "CRITICAL - Disk '%s' non-existent or not mounted",
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
);
214 if (result
!= STATE_OK
)
215 die (result
, _("Unknown error fetching network status\n"));
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
;
225 _("Net %s - %d connection%s on port %d"),
228 (port_connections
== 1) ? "" : "s",
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"));
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
;
254 _("Process %s - %d instance%s of %s running"),
257 (processes
== 1) ? "" : "s",
263 if (result
!= STATE_OK
)
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
;
281 _("Uptime %s - Up %d days %d hours %d minutes"),
289 die (STATE_UNKNOWN
, _("Nothing to check!\n"));
295 /* process command-line arguments */
297 process_arguments (int argc
, char **argv
)
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'},
314 /* no options were supplied */
318 /* backwards compatibility */
319 if (!is_option (argv
[1])) {
320 server_address
= argv
[1];
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");
336 c
= getopt_long (argc
, argv
, "+hVH:t:c:w:p:v:", longopts
,
339 if (c
== -1 || c
== EOF
|| c
== 1)
343 case '?': /* print short usage statement if args not parsable */
348 case 'V': /* version */
349 print_revision (progname
, revision
);
351 case 'H': /* hostname */
352 server_address
= optarg
;
355 if (is_intnonneg (optarg
))
356 server_port
= atoi (optarg
);
359 _("Server port an integer\n"));
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
) {
387 strcpy (send_buffer
, "DISKSPACE\r\n");
388 disk_name
= strscpy (disk_name
, optarg
+ 3);
393 case 'w': /* warning threshold */
394 warning_value
= strtoul (optarg
, NULL
, 10);
395 check_warning_value
= TRUE
;
397 case 'c': /* critical threshold */
398 critical_value
= strtoul (optarg
, NULL
, 10);
399 check_critical_value
= TRUE
;
401 case 't': /* timeout */
402 socket_timeout
= atoi (optarg
);
403 if (socket_timeout
<= 0)
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."));
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
));
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
));
469 printf (_("Usage:"));
470 printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname
);