Code

the last round of pedantic compiler warnings
[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                 else
110                         load_1min = strtod (temp_ptr, NULL);
112                 temp_ptr = (char *) strtok (NULL, "\r\n");
113                 if (temp_ptr == NULL)
114                         die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
115                 else
116                         load_5min = 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 5\n"));
121                 else
122                         load_15min = strtod (temp_ptr, NULL);
124                 switch (vars_to_check) {
125                 case LOAD1:
126                         strcpy (temp_buffer, "1");
127                         load = load_1min;
128                         break;
129                 case LOAD5:
130                         strcpy (temp_buffer, "5");
131                         load = load_5min;
132                         break;
133                 default:
134                         strcpy (temp_buffer, "15");
135                         load = load_15min;
136                         break;
137                 }
139                 if (check_critical_value == TRUE && (load >= critical_value))
140                         result = STATE_CRITICAL;
141                 else if (check_warning_value == TRUE && (load >= warning_value))
142                         result = STATE_WARNING;
144                 die (result,
145                           _("Load %s - %s-min load average = %0.2f"),
146                                                          state_text(result),
147                           temp_buffer,
148                           load);
150                         break;
152         case DPU:
154                 if (result != STATE_OK)
155                         die (result, _("Unknown error fetching disk data\n"));
157                 for (temp_ptr = (char *) strtok (recv_buffer, " ");
158                      temp_ptr != NULL;
159                      temp_ptr = (char *) strtok (NULL, " ")) {
161                         if (!strcmp (temp_ptr, disk_name)) {
162                                 found_disk = TRUE;
163                                 temp_ptr = (char *) strtok (NULL, "%");
164                                 if (temp_ptr == NULL)
165                                         die (STATE_CRITICAL, _("Invalid response from server\n"));
166                                 else
167                                         percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
168                                 break;
169                         }
171                         temp_ptr = (char *) strtok (NULL, "\r\n");
172                 }
174                 /* error if we couldn't find the info for the disk */
175                 if (found_disk == FALSE)
176                         die (STATE_CRITICAL,
177                                    "Error: Disk '%s' non-existent or not mounted",
178                                    disk_name);
180                 if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
181                         result = STATE_CRITICAL;
182                 else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
183                         result = STATE_WARNING;
185                 die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
187                 break;
189         case NETSTAT:
191                 if (result != STATE_OK)
192                         die (result, _("Unknown error fetching network status\n"));
193                 else
194                         port_connections = strtod (recv_buffer, NULL);
196                 if (check_critical_value == TRUE && (port_connections >= critical_value))
197                         result = STATE_CRITICAL;
198                 else if (check_warning_value == TRUE && (port_connections >= warning_value))
199                         result = STATE_WARNING;
201                 die (result,
202                            _("Net %s - %d connection%s on port %d"),
203                            state_text(result),
204                            port_connections,
205                            (port_connections == 1) ? "" : "s",
206                            netstat_port);
208                 break;
210         case PROCS:
212                 if (result != STATE_OK)
213                         die (result, _("Unknown error fetching process status\n"));
215                 temp_ptr = (char *) strtok (recv_buffer, "(");
216                 if (temp_ptr == NULL)
217                         die (STATE_CRITICAL, _("Invalid response from server\n"));
219                 temp_ptr = (char *) strtok (NULL, ")");
220                 if (temp_ptr == NULL)
221                         die (STATE_CRITICAL, _("Invalid response from server\n"));
222                 else
223                         processes = strtod (temp_ptr, NULL);
225                 if (check_critical_value == TRUE && (processes >= critical_value))
226                         result = STATE_CRITICAL;
227                 else if (check_warning_value == TRUE && (processes >= warning_value))
228                         result = STATE_WARNING;
230                 die (result,
231                            _("Process %s - %d instance%s of %s running"),
232                            state_text(result),
233                            processes,
234                            (processes == 1) ? "" : "s",
235                            process_name);
236                 break;
238         case UPTIME:
240                 if (result != STATE_OK)
241                         return result;
243                 uptime_raw_hours = strtod (recv_buffer, NULL);
244                 uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
246                 if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
247                         result = STATE_CRITICAL;
248                 else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
249                         result = STATE_WARNING;
251                 uptime_days = uptime_raw_minutes / 1440;
252                 uptime_raw_minutes %= 1440;
253                 uptime_hours = uptime_raw_minutes / 60;
254                 uptime_raw_minutes %= 60;
255                 uptime_minutes = uptime_raw_minutes;
257                 die (result,
258                            _("Uptime %s - Up %d days %d hours %d minutes"),
259                            state_text(result),
260                            uptime_days,
261                            uptime_hours,
262                            uptime_minutes);
263                 break;
265         default:
266                 die (STATE_UNKNOWN, _("Nothing to check!\n"));
267                 break;
268         }
270         /* reset timeout */
271 /*      alarm (0); */
273 /*      printf (_("Reached end of program with no data returned\n")); */
275 /*      return result; */
282 /* process command-line arguments */
283 int
284 process_arguments (int argc, char **argv)
286         int c;
288         int option = 0;
289         static struct option longopts[] = {
290                 {"port", required_argument, 0, 'p'},
291                 {"timeout", required_argument, 0, 't'},
292                 {"critical", required_argument, 0, 'c'},
293                 {"warning", required_argument, 0, 'w'},
294                 {"variable", required_argument, 0, 'v'},
295                 {"hostname", required_argument, 0, 'H'},
296                 {"version", no_argument, 0, 'V'},
297                 {"help", no_argument, 0, 'h'},
298                 {0, 0, 0, 0}
299         };
301         /* no options were supplied */
302         if (argc < 2)
303                 return ERROR;
305         /* backwards compatibility */
306         if (!is_option (argv[1])) {
307                 server_address = argv[1];
308                 argv[1] = argv[0];
309                 argv = &argv[1];
310                 argc--;
311         }
313         for (c = 1; c < argc; c++) {
314                 if (strcmp ("-to", argv[c]) == 0)
315                         strcpy (argv[c], "-t");
316                 else if (strcmp ("-wv", argv[c]) == 0)
317                         strcpy (argv[c], "-w");
318                 else if (strcmp ("-cv", argv[c]) == 0)
319                         strcpy (argv[c], "-c");
320         }
322         while (1) {
323                 c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
324                                                                          &option);
326                 if (c == -1 || c == EOF || c == 1)
327                         break;
329                 switch (c) {
330                 case '?':                                                                       /* print short usage statement if args not parsable */
331                         printf (_("%s: Unknown argument: %s\n\n"), progname, optarg);
332                         print_usage ();
333                         exit (STATE_UNKNOWN);
334                 case 'h':                                                                       /* help */
335                         print_help ();
336                         exit (STATE_OK);
337                 case 'V':                                                                       /* version */
338                         print_revision (progname, "$Revision$");
339                         exit (STATE_OK);
340                 case 'H':                                                                       /* hostname */
341                         server_address = optarg;
342                         break;
343                 case 'p':                                                                       /* port */
344                         if (is_intnonneg (optarg))
345                                 server_port = atoi (optarg);
346                         else
347                                 die (STATE_UNKNOWN,
348                                                                          _("Server port an integer (seconds)\nType '%s -h' for additional help\n"),
349                                                                          progname);
350                         break;
351                 case 'v':                                                                       /* variable */
352                         if (strcmp (optarg, "LOAD") == 0) {
353                                 strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
354                                 if (strcmp (optarg, "LOAD1") == 0)
355                                         vars_to_check = LOAD1;
356                                 else if (strcmp (optarg, "LOAD5") == 0)
357                                         vars_to_check = LOAD5;
358                                 else if (strcmp (optarg, "LOAD15") == 0)
359                                         vars_to_check = LOAD15;
360                         }
361                         else if (strcmp (optarg, "UPTIME") == 0) {
362                                 vars_to_check = UPTIME;
363                                 strcpy (send_buffer, "UPTIME\r\n");
364                         }
365                         else if (strstr (optarg, "PROC") == optarg) {
366                                 vars_to_check = PROCS;
367                                 process_name = strscpy (process_name, optarg + 4);
368                                 sprintf (send_buffer, "PROCESS %s\r\n", process_name);
369                         }
370                         else if (strstr (optarg, "NET") == optarg) {
371                                 vars_to_check = NETSTAT;
372                                 netstat_port = atoi (optarg + 3);
373                                 sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
374                         }
375                         else if (strstr (optarg, "DPU") == optarg) {
376                                 vars_to_check = DPU;
377                                 strcpy (send_buffer, "DISKSPACE\r\n");
378                                 disk_name = strscpy (disk_name, optarg + 3);
379                         }
380                         else
381                                 return ERROR;
382                         break;
383                 case 'w':                                                                       /* warning threshold */
384                         warning_value = strtoul (optarg, NULL, 10);
385                         check_warning_value = TRUE;
386                         break;
387                 case 'c':                                                                       /* critical threshold */
388                         critical_value = strtoul (optarg, NULL, 10);
389                         check_critical_value = TRUE;
390                         break;
391                 case 't':                                                                       /* timeout */
392                         socket_timeout = atoi (optarg);
393                         if (socket_timeout <= 0)
394                                 return ERROR;
395                 }
397         }
398         return OK;
400 \f
401 void
402 print_usage (void)
404         printf (_("\
405 Usage: %s -H host [-p port] [-v variable] [-w warning] [-c critical]\n\
406   [-t timeout]\n"),
407                 progname);
408         printf (_(UT_HLP_VRS), progname, progname);
411 void
412 print_help (void)
414         char *myport;
415         asprintf (&myport, "%d", PORT);
417         print_revision (progname, revision);
419         printf (_("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n"));
420         printf (_(COPYRIGHT), copyright, email);
422         printf (_("\
423 This plugin attempts to contact the Over-CR collector daemon running on the\n\
424 remote UNIX server in order to gather the requested system information.\n\n"));
426         print_usage ();
428         printf (_(UT_HELP_VRSN));
430         printf (_(UT_HOST_PORT), 'p', myport);
432         printf (_("\
433 -v, --variable=STRING\n\
434    Variable to check.  Valid variables include:\n\
435      LOAD1         = 1 minute average CPU load\n\
436      LOAD5         = 5 minute average CPU load\n\
437      LOAD15        = 15 minute average CPU load\n\
438      DPU<filesys>  = percent used disk space on filesystem <filesys>\n\
439      PROC<process> = number of running processes with name <process>\n\
440      NET<port>     = number of active connections on TCP port <port>\n\
441      UPTIME        = system uptime in seconds\n"));
443         printf (_("\
444  -w, --warning=INTEGER\n\
445    Threshold which will result in a warning status\n\
446  -c, --critical=INTEGER\n\
447    Threshold which will result in a critical status\n"));
449         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
451         printf (_("\
452 Notes:\n\
453  - For the available options, the critical threshold value should always be\n\
454    higher than the warning threshold value, EXCEPT with the uptime variable\n\n"));
456         printf (_("\
457  - This plugin requres that Eric Molitors' Over-CR collector daemon be\n\
458    running on the remote server. Over-CR can be downloaded from\n\
459    http://www.molitor.org/overcr (This plugin was tested with version\n\
460    0.99.53 of the Over-CR collector)\n\n"));
462         printf (_(UT_SUPPORT));