1 /*****************************************************************************
3 * Nagios check_overcr plugin
6 * Copyright (c) 2000-2007 Nagios Plugins Development Team
10 * This file contains the check_overcr plugin
12 * This plugin attempts to contact the Over-CR collector daemon running on the
13 * remote UNIX server in order to gather the requested system information.
16 * This program is free software: you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation, either version 3 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program. If not, see <http://www.gnu.org/licenses/>.
30 *****************************************************************************/
32 const char *progname
= "check_overcr";
33 const char *copyright
= "2000-2007";
34 const char *email
= "nagiosplug-devel@lists.sourceforge.net";
55 char *server_address
= NULL
;
56 int server_port
= PORT
;
57 double warning_value
= 0L;
58 double critical_value
= 0L;
59 int check_warning_value
= FALSE
;
60 int check_critical_value
= FALSE
;
61 enum checkvar vars_to_check
= NONE
;
65 char *disk_name
= NULL
;
66 char *process_name
= NULL
;
67 char send_buffer
[MAX_INPUT_BUFFER
];
69 int process_arguments (int, char **);
70 void print_usage (void);
71 void print_help (void);
74 main (int argc
, char **argv
)
76 int result
= STATE_UNKNOWN
;
77 char recv_buffer
[MAX_INPUT_BUFFER
];
78 char temp_buffer
[MAX_INPUT_BUFFER
];
79 char *temp_ptr
= NULL
;
80 int found_disk
= FALSE
;
81 unsigned long percent_used_disk_space
= 100;
86 int port_connections
= 0;
88 double uptime_raw_hours
;
89 int uptime_raw_minutes
= 0;
92 int uptime_minutes
= 0;
94 setlocale (LC_ALL
, "");
95 bindtextdomain (PACKAGE
, LOCALEDIR
);
98 /* Parse extra opts if any */
99 argv
=np_extra_opts (&argc
, argv
, progname
);
101 if (process_arguments (argc
, argv
) == ERROR
)
102 usage4 (_("Could not parse arguments"));
104 /* initialize alarm signal handling */
105 signal (SIGALRM
, socket_timeout_alarm_handler
);
107 /* set socket timeout */
108 alarm (socket_timeout
);
110 result
= process_tcp_request2 (server_address
,
114 sizeof (recv_buffer
));
116 switch (vars_to_check
) {
122 if (result
!= STATE_OK
)
123 die (result
, _("Unknown error fetching load data\n"));
125 temp_ptr
= (char *) strtok (recv_buffer
, "\r\n");
126 if (temp_ptr
== NULL
)
127 die (STATE_CRITICAL
, _("Invalid response from server - no load information\n"));
129 load_1min
= strtod (temp_ptr
, NULL
);
131 temp_ptr
= (char *) strtok (NULL
, "\r\n");
132 if (temp_ptr
== NULL
)
133 die (STATE_CRITICAL
, _("Invalid response from server after load 1\n"));
135 load_5min
= strtod (temp_ptr
, NULL
);
137 temp_ptr
= (char *) strtok (NULL
, "\r\n");
138 if (temp_ptr
== NULL
)
139 die (STATE_CRITICAL
, _("Invalid response from server after load 5\n"));
141 load_15min
= strtod (temp_ptr
, NULL
);
143 switch (vars_to_check
) {
145 strcpy (temp_buffer
, "1");
149 strcpy (temp_buffer
, "5");
153 strcpy (temp_buffer
, "15");
158 if (check_critical_value
== TRUE
&& (load
>= critical_value
))
159 result
= STATE_CRITICAL
;
160 else if (check_warning_value
== TRUE
&& (load
>= warning_value
))
161 result
= STATE_WARNING
;
164 _("Load %s - %s-min load average = %0.2f"),
173 if (result
!= STATE_OK
)
174 die (result
, _("Unknown error fetching disk data\n"));
176 for (temp_ptr
= (char *) strtok (recv_buffer
, " ");
178 temp_ptr
= (char *) strtok (NULL
, " ")) {
180 if (!strcmp (temp_ptr
, disk_name
)) {
182 temp_ptr
= (char *) strtok (NULL
, "%");
183 if (temp_ptr
== NULL
)
184 die (STATE_CRITICAL
, _("Invalid response from server\n"));
186 percent_used_disk_space
= strtoul (temp_ptr
, NULL
, 10);
190 temp_ptr
= (char *) strtok (NULL
, "\r\n");
193 /* error if we couldn't find the info for the disk */
194 if (found_disk
== FALSE
)
196 "CRITICAL - Disk '%s' non-existent or not mounted",
199 if (check_critical_value
== TRUE
&& (percent_used_disk_space
>= critical_value
))
200 result
= STATE_CRITICAL
;
201 else if (check_warning_value
== TRUE
&& (percent_used_disk_space
>= warning_value
))
202 result
= STATE_WARNING
;
204 die (result
, "Disk %s - %lu%% used on %s", state_text(result
), percent_used_disk_space
, disk_name
);
210 if (result
!= STATE_OK
)
211 die (result
, _("Unknown error fetching network status\n"));
213 port_connections
= strtod (recv_buffer
, NULL
);
215 if (check_critical_value
== TRUE
&& (port_connections
>= critical_value
))
216 result
= STATE_CRITICAL
;
217 else if (check_warning_value
== TRUE
&& (port_connections
>= warning_value
))
218 result
= STATE_WARNING
;
221 _("Net %s - %d connection%s on port %d"),
224 (port_connections
== 1) ? "" : "s",
231 if (result
!= STATE_OK
)
232 die (result
, _("Unknown error fetching process status\n"));
234 temp_ptr
= (char *) strtok (recv_buffer
, "(");
235 if (temp_ptr
== NULL
)
236 die (STATE_CRITICAL
, _("Invalid response from server\n"));
238 temp_ptr
= (char *) strtok (NULL
, ")");
239 if (temp_ptr
== NULL
)
240 die (STATE_CRITICAL
, _("Invalid response from server\n"));
242 processes
= strtod (temp_ptr
, NULL
);
244 if (check_critical_value
== TRUE
&& (processes
>= critical_value
))
245 result
= STATE_CRITICAL
;
246 else if (check_warning_value
== TRUE
&& (processes
>= warning_value
))
247 result
= STATE_WARNING
;
250 _("Process %s - %d instance%s of %s running"),
253 (processes
== 1) ? "" : "s",
259 if (result
!= STATE_OK
)
262 uptime_raw_hours
= strtod (recv_buffer
, NULL
);
263 uptime_raw_minutes
= (unsigned long) (uptime_raw_hours
* 60.0);
265 if (check_critical_value
== TRUE
&& (uptime_raw_minutes
<= critical_value
))
266 result
= STATE_CRITICAL
;
267 else if (check_warning_value
== TRUE
&& (uptime_raw_minutes
<= warning_value
))
268 result
= STATE_WARNING
;
270 uptime_days
= uptime_raw_minutes
/ 1440;
271 uptime_raw_minutes
%= 1440;
272 uptime_hours
= uptime_raw_minutes
/ 60;
273 uptime_raw_minutes
%= 60;
274 uptime_minutes
= uptime_raw_minutes
;
277 _("Uptime %s - Up %d days %d hours %d minutes"),
285 die (STATE_UNKNOWN
, _("Nothing to check!\n"));
291 /* process command-line arguments */
293 process_arguments (int argc
, char **argv
)
298 static struct option longopts
[] = {
299 {"port", required_argument
, 0, 'p'},
300 {"timeout", required_argument
, 0, 't'},
301 {"critical", required_argument
, 0, 'c'},
302 {"warning", required_argument
, 0, 'w'},
303 {"variable", required_argument
, 0, 'v'},
304 {"hostname", required_argument
, 0, 'H'},
305 {"version", no_argument
, 0, 'V'},
306 {"help", no_argument
, 0, 'h'},
310 /* no options were supplied */
314 /* backwards compatibility */
315 if (!is_option (argv
[1])) {
316 server_address
= argv
[1];
322 for (c
= 1; c
< argc
; c
++) {
323 if (strcmp ("-to", argv
[c
]) == 0)
324 strcpy (argv
[c
], "-t");
325 else if (strcmp ("-wv", argv
[c
]) == 0)
326 strcpy (argv
[c
], "-w");
327 else if (strcmp ("-cv", argv
[c
]) == 0)
328 strcpy (argv
[c
], "-c");
332 c
= getopt_long (argc
, argv
, "+hVH:t:c:w:p:v:", longopts
,
335 if (c
== -1 || c
== EOF
|| c
== 1)
339 case '?': /* print short usage statement if args not parsable */
344 case 'V': /* version */
345 print_revision (progname
, NP_VERSION
);
347 case 'H': /* hostname */
348 server_address
= optarg
;
351 if (is_intnonneg (optarg
))
352 server_port
= atoi (optarg
);
355 _("Server port an integer\n"));
357 case 'v': /* variable */
358 if (strcmp (optarg
, "LOAD") == 0) {
359 strcpy (send_buffer
, "LOAD\r\nQUIT\r\n");
360 if (strcmp (optarg
, "LOAD1") == 0)
361 vars_to_check
= LOAD1
;
362 else if (strcmp (optarg
, "LOAD5") == 0)
363 vars_to_check
= LOAD5
;
364 else if (strcmp (optarg
, "LOAD15") == 0)
365 vars_to_check
= LOAD15
;
367 else if (strcmp (optarg
, "UPTIME") == 0) {
368 vars_to_check
= UPTIME
;
369 strcpy (send_buffer
, "UPTIME\r\n");
371 else if (strstr (optarg
, "PROC") == optarg
) {
372 vars_to_check
= PROCS
;
373 process_name
= strscpy (process_name
, optarg
+ 4);
374 sprintf (send_buffer
, "PROCESS %s\r\n", process_name
);
376 else if (strstr (optarg
, "NET") == optarg
) {
377 vars_to_check
= NETSTAT
;
378 netstat_port
= atoi (optarg
+ 3);
379 sprintf (send_buffer
, "NETSTAT %d\r\n", netstat_port
);
381 else if (strstr (optarg
, "DPU") == optarg
) {
383 strcpy (send_buffer
, "DISKSPACE\r\n");
384 disk_name
= strscpy (disk_name
, optarg
+ 3);
389 case 'w': /* warning threshold */
390 warning_value
= strtoul (optarg
, NULL
, 10);
391 check_warning_value
= TRUE
;
393 case 'c': /* critical threshold */
394 critical_value
= strtoul (optarg
, NULL
, 10);
395 check_critical_value
= TRUE
;
397 case 't': /* timeout */
398 socket_timeout
= atoi (optarg
);
399 if (socket_timeout
<= 0)
412 asprintf (&myport
, "%d", PORT
);
414 print_revision (progname
, NP_VERSION
);
416 printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
417 printf (COPYRIGHT
, copyright
, email
);
419 printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
420 printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
426 printf (_(UT_HELP_VRSN
));
427 printf (_(UT_EXTRA_OPTS
));
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
));
450 printf ("%s\n", _("This plugin requires that Eric Molitors' Over-CR collector daemon be"));
451 printf ("%s\n", _("running on the remote server."));
452 printf ("%s\n", _("Over-CR can be downloaded from http://www.molitor.org/overcr"));
453 printf ("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector"));
456 printf ("%s\n", _("Notes:"));
457 printf (" %s\n", _("For the available options, the critical threshold value should always be"));
458 printf (" %s\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
461 printf (_(UT_EXTRA_OPTS_NOTES
));
464 printf (_(UT_SUPPORT
));
471 printf (_("Usage:"));
472 printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname
);