Code

fix a variety of compiler warnings about qualifier discards and other pedantic stuff
[nagiosplug.git] / plugins / check_overcr.c
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 ******************************************************************************/
19 const char *progname = "check_overcr";
20 const char *revision = "$Revision$";
21 const char *copyright = "2000-2003";
22 const char *email = "nagiosplug-devel@lists.sourceforge.net";
24 #include "common.h"
25 #include "netutils.h"
26 #include "utils.h"
28 enum checkvar {
29         NONE,
30         LOAD1,
31         LOAD5,
32         LOAD15,
33         DPU,
34         PROCS,
35         NETSTAT,
36         UPTIME
37 };
39 enum {
40         PORT = 2000
41 };
43 char *server_address = NULL;
44 int server_port = PORT;
45 double warning_value = 0L;
46 double critical_value = 0L;
47 int check_warning_value = FALSE;
48 int check_critical_value = FALSE;
49 enum checkvar vars_to_check = NONE;
50 int cmd_timeout = 1;
52 int netstat_port = 0;
53 char *disk_name = NULL;
54 char *process_name = NULL;
55         char send_buffer[MAX_INPUT_BUFFER];
57 int process_arguments (int, char **);
58 void print_usage (void);
59 void print_help (void);
61 int
62 main (int argc, char **argv)
63 {
64         int result;
65         char recv_buffer[MAX_INPUT_BUFFER];
66         char temp_buffer[MAX_INPUT_BUFFER];
67         char *temp_ptr = NULL;
68         int found_disk = FALSE;
69         unsigned long percent_used_disk_space = 100;
70         double load;
71         double load_1min;
72         double load_5min;
73         double load_15min;
74         int port_connections = 0;
75         int processes = 0;
76         double uptime_raw_hours;
77         int uptime_raw_minutes = 0;
78         int uptime_days = 0;
79         int uptime_hours = 0;
80         int uptime_minutes = 0;
82         if (process_arguments (argc, argv) == ERROR)
83                 usage ("Could not parse arguments\n");
85         /* initialize alarm signal handling */
86         signal (SIGALRM, socket_timeout_alarm_handler);
88         /* set socket timeout */
89         alarm (socket_timeout);
91         result = process_tcp_request2 (server_address,
92                                        server_port,
93                                        send_buffer,
94                                        recv_buffer,
95                                        sizeof (recv_buffer));
97         switch (vars_to_check) {
99         case LOAD1:
100         case LOAD5:
101         case LOAD15:
102         
103                 if (result != STATE_OK)
104                         die (result, _("Unknown error fetching load data\n"));
106                 temp_ptr = (char *) strtok (recv_buffer, "\r\n");
107                 if (temp_ptr == NULL)
108                         die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
109                 load_1min = strtod (temp_ptr, NULL);
111                 temp_ptr = (char *) strtok (NULL, "\r\n");
112                 if (temp_ptr == NULL)
113                         die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
114                 load_5min = strtod (temp_ptr, NULL);
116                 temp_ptr = (char *) strtok (NULL, "\r\n");
117                 if (temp_ptr == NULL)
118                         die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
119                 load_15min = strtod (temp_ptr, NULL);
121                 switch (vars_to_check) {
122                 case LOAD1:
123                         strcpy (temp_buffer, "1");
124                         load = load_1min;
125                         break;
126                 case LOAD5:
127                         strcpy (temp_buffer, "5");
128                         load = load_5min;
129                         break;
130                 default:
131                         strcpy (temp_buffer, "15");
132                         load = load_15min;
133                         break;
134                 }
136                 if (check_critical_value == TRUE && (load >= critical_value))
137                         result = STATE_CRITICAL;
138                 else if (check_warning_value == TRUE && (load >= warning_value))
139                         result = STATE_WARNING;
141                 die (result,
142                           _("Load %s - %s-min load average = %0.2f"),
143                                                          state_text(result),
144                           temp_buffer,
145                           load);
147                         break;
149         case DPU:
151                 if (result != STATE_OK)
152                         die (result, _("Unknown error fetching disk data\n"));
154                 for (temp_ptr = (char *) strtok (recv_buffer, " ");
155                      temp_ptr != NULL;
156                      temp_ptr = (char *) strtok (NULL, " ")) {
158                         if (!strcmp (temp_ptr, disk_name)) {
159                                 found_disk = TRUE;
160                                 temp_ptr = (char *) strtok (NULL, "%");
161                                 if (temp_ptr == NULL)
162                                         die (STATE_CRITICAL, _("Invalid response from server\n"));
163                                 percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
164                                 break;
165                         }
167                         temp_ptr = (char *) strtok (NULL, "\r\n");
168                 }
170                 /* error if we couldn't find the info for the disk */
171                 if (found_disk == FALSE)
172                         die (STATE_CRITICAL,
173                                    "Error: Disk '%s' non-existent or not mounted",
174                                    disk_name);
176                 if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
177                         result = STATE_CRITICAL;
178                 else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
179                         result = STATE_WARNING;
181                 die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
183                 break;
185         case NETSTAT:
187                 if (result != STATE_OK)
188                         die (result, _("Unknown error fetching network status\n"));
190                 port_connections = strtod (recv_buffer, NULL);
192                 if (check_critical_value == TRUE && (port_connections >= critical_value))
193                         result = STATE_CRITICAL;
194                 else if (check_warning_value == TRUE && (port_connections >= warning_value))
195                         result = STATE_WARNING;
197                 die (result,
198                            _("Net %s - %d connection%s on port %d"),
199                            state_text(result),
200                            port_connections,
201                            (port_connections == 1) ? "" : "s",
202                            netstat_port);
204                 break;
206         case PROCS:
208                 if (result != STATE_OK)
209                         die (result, _("Unknown error fetching process status\n"));
211                 temp_ptr = (char *) strtok (recv_buffer, "(");
212                 if (temp_ptr == NULL)
213                         die (STATE_CRITICAL, _("Invalid response from server\n"));
215                 temp_ptr = (char *) strtok (NULL, ")");
216                 if (temp_ptr == NULL)
217                         die (STATE_CRITICAL, _("Invalid response from server\n"));
219                 processes = strtod (temp_ptr, NULL);
221                 if (check_critical_value == TRUE && (processes >= critical_value))
222                         result = STATE_CRITICAL;
223                 else if (check_warning_value == TRUE && (processes >= warning_value))
224                         result = STATE_WARNING;
226                 die (result,
227                            _("Process %s - %d instance%s of %s running"),
228                            state_text(result),
229                            processes,
230                            (processes == 1) ? "" : "s",
231                            process_name);
232                 break;
234         case UPTIME:
236                 if (result != STATE_OK)
237                         return result;
239                 uptime_raw_hours = strtod (recv_buffer, NULL);
240                 uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
242                 if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
243                         result = STATE_CRITICAL;
244                 else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
245                         result = STATE_WARNING;
247                 uptime_days = uptime_raw_minutes / 1440;
248                 uptime_raw_minutes %= 1440;
249                 uptime_hours = uptime_raw_minutes / 60;
250                 uptime_raw_minutes %= 60;
251                 uptime_minutes = uptime_raw_minutes;
253                 die (result,
254                            _("Uptime %s - Up %d days %d hours %d minutes"),
255                            state_text(result),
256                            uptime_days,
257                            uptime_hours,
258                            uptime_minutes);
259                 break;
261         default:
262                 die (STATE_UNKNOWN, _("Nothing to check!\n"));
263                 break;
264         }
266         /* reset timeout */
267         alarm (0);
269         printf (_("Reached end of program with no data returned\n"));
271         return result;
278 /* process command-line arguments */
279 int
280 process_arguments (int argc, char **argv)
282         int c;
284         int option_index = 0;
285         static struct option long_options[] = {
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:", long_options,
320                                                                          &option_index);
322                 if (c == -1 || c == EOF || c == 1)
323                         break;
325                 switch (c) {
326                 case '?':                                                                       /* print short usage statement if args not parsable */
327                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
328                         print_usage ();
329                         exit (STATE_UNKNOWN);
330                 case 'h':                                                                       /* help */
331                         print_help ();
332                         exit (STATE_OK);
333                 case 'V':                                                                       /* version */
334                         print_revision (progname, "$Revision$");
335                         exit (STATE_OK);
336                 case 'H':                                                                       /* hostname */
337                         server_address = optarg;
338                         break;
339                 case 'p':                                                                       /* port */
340                         if (is_intnonneg (optarg))
341                                 server_port = atoi (optarg);
342                         else
343                                 die (STATE_UNKNOWN,
344                                                                          _("Server port an integer (seconds)\nType '%s -h' for additional help\n"),
345                                                                          progname);
346                         break;
347                 case 'v':                                                                       /* variable */
348                         if (strcmp (optarg, "LOAD") == 0) {
349                                 strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
350                                 if (strcmp (optarg, "LOAD1") == 0)
351                                         vars_to_check = LOAD1;
352                                 else if (strcmp (optarg, "LOAD5") == 0)
353                                         vars_to_check = LOAD5;
354                                 else if (strcmp (optarg, "LOAD15") == 0)
355                                         vars_to_check = LOAD15;
356                         }
357                         else if (strcmp (optarg, "UPTIME") == 0) {
358                                 vars_to_check = UPTIME;
359                                 strcpy (send_buffer, "UPTIME\r\n");
360                         }
361                         else if (strstr (optarg, "PROC") == optarg) {
362                                 vars_to_check = PROCS;
363                                 process_name = strscpy (process_name, optarg + 4);
364                                 sprintf (send_buffer, "PROCESS %s\r\n", process_name);
365                         }
366                         else if (strstr (optarg, "NET") == optarg) {
367                                 vars_to_check = NETSTAT;
368                                 netstat_port = atoi (optarg + 3);
369                                 sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
370                         }
371                         else if (strstr (optarg, "DPU") == optarg) {
372                                 vars_to_check = DPU;
373                                 strcpy (send_buffer, "DISKSPACE\r\n");
374                                 disk_name = strscpy (disk_name, optarg + 3);
375                         }
376                         else
377                                 return ERROR;
378                         break;
379                 case 'w':                                                                       /* warning threshold */
380                         warning_value = strtoul (optarg, NULL, 10);
381                         check_warning_value = TRUE;
382                         break;
383                 case 'c':                                                                       /* critical threshold */
384                         critical_value = strtoul (optarg, NULL, 10);
385                         check_critical_value = TRUE;
386                         break;
387                 case 't':                                                                       /* timeout */
388                         socket_timeout = atoi (optarg);
389                         if (socket_timeout <= 0)
390                                 return ERROR;
391                 }
393         }
394         return OK;
396 \f
397 void
398 print_usage (void)
400         printf (_("\
401 Usage: %s -H host [-p port] [-v variable] [-w warning] [-c critical]\n\
402   [-t timeout]\n"),
403                 progname);
404         printf (_(UT_HLP_VRS), progname, progname);
407 void
408 print_help (void)
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));