Back to index

nagios-plugins  1.4.16
check_overcr.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_overcr plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2000-2007 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the check_overcr plugin
00011 * 
00012 * This plugin attempts to contact the Over-CR collector daemon running on the
00013 * remote UNIX server in order to gather the requested system information.
00014 * 
00015 * 
00016 * This program is free software: you can redistribute it and/or modify
00017 * it under the terms of the GNU General Public License as published by
00018 * the Free Software Foundation, either version 3 of the License, or
00019 * (at your option) any later version.
00020 * 
00021 * This program is distributed in the hope that it will be useful,
00022 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00024 * GNU General Public License for more details.
00025 * 
00026 * You should have received a copy of the GNU General Public License
00027 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00028 * 
00029 * 
00030 *****************************************************************************/
00031 
00032 const char *progname = "check_overcr";
00033 const char *copyright = "2000-2007";
00034 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00035 
00036 #include "common.h"
00037 #include "netutils.h"
00038 #include "utils.h"
00039 
00040 enum checkvar {
00041        NONE,
00042        LOAD1,
00043        LOAD5,
00044        LOAD15,
00045        DPU,
00046        PROCS,
00047        NETSTAT,
00048        UPTIME
00049 };
00050 
00051 enum {
00052        PORT = 2000
00053 };
00054 
00055 char *server_address = NULL;
00056 int server_port = PORT;
00057 double warning_value = 0L;
00058 double critical_value = 0L;
00059 int check_warning_value = FALSE;
00060 int check_critical_value = FALSE;
00061 enum checkvar vars_to_check = NONE;
00062 int cmd_timeout = 1;
00063 
00064 int netstat_port = 0;
00065 char *disk_name = NULL;
00066 char *process_name = NULL;
00067        char send_buffer[MAX_INPUT_BUFFER];
00068 
00069 int process_arguments (int, char **);
00070 void print_usage (void);
00071 void print_help (void);
00072 
00073 int
00074 main (int argc, char **argv)
00075 {
00076        int result = STATE_UNKNOWN;
00077        char recv_buffer[MAX_INPUT_BUFFER];
00078        char temp_buffer[MAX_INPUT_BUFFER];
00079        char *temp_ptr = NULL;
00080        int found_disk = FALSE;
00081        unsigned long percent_used_disk_space = 100;
00082        double load;
00083        double load_1min;
00084        double load_5min;
00085        double load_15min;
00086        int port_connections = 0;
00087        int processes = 0;
00088        double uptime_raw_hours;
00089        int uptime_raw_minutes = 0;
00090        int uptime_days = 0;
00091        int uptime_hours = 0;
00092        int uptime_minutes = 0;
00093 
00094        setlocale (LC_ALL, "");
00095        bindtextdomain (PACKAGE, LOCALEDIR);
00096        textdomain (PACKAGE);
00097 
00098        /* Parse extra opts if any */
00099        argv=np_extra_opts (&argc, argv, progname);
00100 
00101        if (process_arguments (argc, argv) == ERROR)
00102               usage4 (_("Could not parse arguments"));
00103 
00104        /* initialize alarm signal handling */
00105        signal (SIGALRM, socket_timeout_alarm_handler);
00106 
00107        /* set socket timeout */
00108        alarm (socket_timeout);
00109 
00110        result = process_tcp_request2 (server_address,
00111                                       server_port,
00112                                       send_buffer,
00113                                       recv_buffer,
00114                                       sizeof (recv_buffer));
00115 
00116        switch (vars_to_check) {
00117 
00118        case LOAD1:
00119        case LOAD5:
00120        case LOAD15:
00121        
00122               if (result != STATE_OK)
00123                      die (result, _("Unknown error fetching load data\n"));
00124 
00125               temp_ptr = (char *) strtok (recv_buffer, "\r\n");
00126               if (temp_ptr == NULL)
00127                      die (STATE_CRITICAL, _("Invalid response from server - no load information\n"));
00128               else
00129                      load_1min = strtod (temp_ptr, NULL);
00130 
00131               temp_ptr = (char *) strtok (NULL, "\r\n");
00132               if (temp_ptr == NULL)
00133                      die (STATE_CRITICAL, _("Invalid response from server after load 1\n"));
00134               else
00135                      load_5min = strtod (temp_ptr, NULL);
00136 
00137               temp_ptr = (char *) strtok (NULL, "\r\n");
00138               if (temp_ptr == NULL)
00139                      die (STATE_CRITICAL, _("Invalid response from server after load 5\n"));
00140               else
00141                      load_15min = strtod (temp_ptr, NULL);
00142 
00143               switch (vars_to_check) {
00144               case LOAD1:
00145                      strcpy (temp_buffer, "1");
00146                      load = load_1min;
00147                      break;
00148               case LOAD5:
00149                      strcpy (temp_buffer, "5");
00150                      load = load_5min;
00151                      break;
00152               default:
00153                      strcpy (temp_buffer, "15");
00154                      load = load_15min;
00155                      break;
00156               }
00157 
00158               if (check_critical_value == TRUE && (load >= critical_value))
00159                      result = STATE_CRITICAL;
00160               else if (check_warning_value == TRUE && (load >= warning_value))
00161                      result = STATE_WARNING;
00162 
00163               die (result,
00164                         _("Load %s - %s-min load average = %0.2f"),
00165                                                   state_text(result),
00166                         temp_buffer,
00167                         load);
00168 
00169                      break;
00170 
00171        case DPU:
00172 
00173               if (result != STATE_OK)
00174                      die (result, _("Unknown error fetching disk data\n"));
00175 
00176               for (temp_ptr = (char *) strtok (recv_buffer, " ");
00177                    temp_ptr != NULL;
00178                    temp_ptr = (char *) strtok (NULL, " ")) {
00179 
00180                      if (!strcmp (temp_ptr, disk_name)) {
00181                             found_disk = TRUE;
00182                             temp_ptr = (char *) strtok (NULL, "%");
00183                             if (temp_ptr == NULL)
00184                                    die (STATE_CRITICAL, _("Invalid response from server\n"));
00185                             else
00186                                    percent_used_disk_space = strtoul (temp_ptr, NULL, 10);
00187                             break;
00188                      }
00189 
00190                      temp_ptr = (char *) strtok (NULL, "\r\n");
00191               }
00192 
00193               /* error if we couldn't find the info for the disk */
00194               if (found_disk == FALSE)
00195                      die (STATE_CRITICAL,
00196                                 "CRITICAL - Disk '%s' non-existent or not mounted",
00197                                 disk_name);
00198 
00199               if (check_critical_value == TRUE && (percent_used_disk_space >= critical_value))
00200                      result = STATE_CRITICAL;
00201               else if (check_warning_value == TRUE && (percent_used_disk_space >= warning_value))
00202                      result = STATE_WARNING;
00203 
00204               die (result, "Disk %s - %lu%% used on %s", state_text(result), percent_used_disk_space, disk_name);
00205 
00206               break;
00207 
00208        case NETSTAT:
00209 
00210               if (result != STATE_OK)
00211                      die (result, _("Unknown error fetching network status\n"));
00212               else
00213                      port_connections = strtod (recv_buffer, NULL);
00214 
00215               if (check_critical_value == TRUE && (port_connections >= critical_value))
00216                      result = STATE_CRITICAL;
00217               else if (check_warning_value == TRUE && (port_connections >= warning_value))
00218                      result = STATE_WARNING;
00219 
00220               die (result,
00221                          _("Net %s - %d connection%s on port %d"),
00222                          state_text(result),
00223                          port_connections,
00224                          (port_connections == 1) ? "" : "s",
00225                          netstat_port);
00226 
00227               break;
00228 
00229        case PROCS:
00230 
00231               if (result != STATE_OK)
00232                      die (result, _("Unknown error fetching process status\n"));
00233 
00234               temp_ptr = (char *) strtok (recv_buffer, "(");
00235               if (temp_ptr == NULL)
00236                      die (STATE_CRITICAL, _("Invalid response from server\n"));
00237 
00238               temp_ptr = (char *) strtok (NULL, ")");
00239               if (temp_ptr == NULL)
00240                      die (STATE_CRITICAL, _("Invalid response from server\n"));
00241               else
00242                      processes = strtod (temp_ptr, NULL);
00243 
00244               if (check_critical_value == TRUE && (processes >= critical_value))
00245                      result = STATE_CRITICAL;
00246               else if (check_warning_value == TRUE && (processes >= warning_value))
00247                      result = STATE_WARNING;
00248 
00249               die (result,
00250                          _("Process %s - %d instance%s of %s running"),
00251                          state_text(result),
00252                          processes,
00253                          (processes == 1) ? "" : "s",
00254                          process_name);
00255               break;
00256 
00257        case UPTIME:
00258 
00259               if (result != STATE_OK)
00260                      return result;
00261 
00262               uptime_raw_hours = strtod (recv_buffer, NULL);
00263               uptime_raw_minutes = (unsigned long) (uptime_raw_hours * 60.0);
00264 
00265               if (check_critical_value == TRUE && (uptime_raw_minutes <= critical_value))
00266                      result = STATE_CRITICAL;
00267               else if (check_warning_value == TRUE && (uptime_raw_minutes <= warning_value))
00268                      result = STATE_WARNING;
00269 
00270               uptime_days = uptime_raw_minutes / 1440;
00271               uptime_raw_minutes %= 1440;
00272               uptime_hours = uptime_raw_minutes / 60;
00273               uptime_raw_minutes %= 60;
00274               uptime_minutes = uptime_raw_minutes;
00275 
00276               die (result,
00277                          _("Uptime %s - Up %d days %d hours %d minutes"),
00278                          state_text(result),
00279                          uptime_days,
00280                          uptime_hours,
00281                          uptime_minutes);
00282               break;
00283 
00284        default:
00285               die (STATE_UNKNOWN, _("Nothing to check!\n"));
00286               break;
00287        }
00288 }
00289 
00290 
00291 /* process command-line arguments */
00292 int
00293 process_arguments (int argc, char **argv)
00294 {
00295        int c;
00296 
00297        int option = 0;
00298        static struct option longopts[] = {
00299               {"port", required_argument, 0, 'p'},
00300               {"timeout", required_argument, 0, 't'},
00301               {"critical", required_argument, 0, 'c'},
00302               {"warning", required_argument, 0, 'w'},
00303               {"variable", required_argument, 0, 'v'},
00304               {"hostname", required_argument, 0, 'H'},
00305               {"version", no_argument, 0, 'V'},
00306               {"help", no_argument, 0, 'h'},
00307               {0, 0, 0, 0}
00308        };
00309 
00310        /* no options were supplied */
00311        if (argc < 2)
00312               return ERROR;
00313 
00314        /* backwards compatibility */
00315        if (!is_option (argv[1])) {
00316               server_address = argv[1];
00317               argv[1] = argv[0];
00318               argv = &argv[1];
00319               argc--;
00320        }
00321 
00322        for (c = 1; c < argc; c++) {
00323               if (strcmp ("-to", argv[c]) == 0)
00324                      strcpy (argv[c], "-t");
00325               else if (strcmp ("-wv", argv[c]) == 0)
00326                      strcpy (argv[c], "-w");
00327               else if (strcmp ("-cv", argv[c]) == 0)
00328                      strcpy (argv[c], "-c");
00329        }
00330 
00331        while (1) {
00332               c = getopt_long (argc, argv, "+hVH:t:c:w:p:v:", longopts,
00333                                                                 &option);
00334 
00335               if (c == -1 || c == EOF || c == 1)
00336                      break;
00337 
00338               switch (c) {
00339               case '?':                                                             /* print short usage statement if args not parsable */
00340                      usage5 ();
00341               case 'h':                                                             /* help */
00342                      print_help ();
00343                      exit (STATE_OK);
00344               case 'V':                                                             /* version */
00345                      print_revision (progname, NP_VERSION);
00346                      exit (STATE_OK);
00347               case 'H':                                                             /* hostname */
00348                      server_address = optarg;
00349                      break;
00350               case 'p':                                                             /* port */
00351                      if (is_intnonneg (optarg))
00352                             server_port = atoi (optarg);
00353                      else
00354                             die (STATE_UNKNOWN,
00355                                                                 _("Server port an integer\n"));
00356                      break;
00357               case 'v':                                                             /* variable */
00358                      if (strcmp (optarg, "LOAD") == 0) {
00359                             strcpy (send_buffer, "LOAD\r\nQUIT\r\n");
00360                             if (strcmp (optarg, "LOAD1") == 0)
00361                                    vars_to_check = LOAD1;
00362                             else if (strcmp (optarg, "LOAD5") == 0)
00363                                    vars_to_check = LOAD5;
00364                             else if (strcmp (optarg, "LOAD15") == 0)
00365                                    vars_to_check = LOAD15;
00366                      }
00367                      else if (strcmp (optarg, "UPTIME") == 0) {
00368                             vars_to_check = UPTIME;
00369                             strcpy (send_buffer, "UPTIME\r\n");
00370                      }
00371                      else if (strstr (optarg, "PROC") == optarg) {
00372                             vars_to_check = PROCS;
00373                             process_name = strscpy (process_name, optarg + 4);
00374                             sprintf (send_buffer, "PROCESS %s\r\n", process_name);
00375                      }
00376                      else if (strstr (optarg, "NET") == optarg) {
00377                             vars_to_check = NETSTAT;
00378                             netstat_port = atoi (optarg + 3);
00379                             sprintf (send_buffer, "NETSTAT %d\r\n", netstat_port);
00380                      }
00381                      else if (strstr (optarg, "DPU") == optarg) {
00382                             vars_to_check = DPU;
00383                             strcpy (send_buffer, "DISKSPACE\r\n");
00384                             disk_name = strscpy (disk_name, optarg + 3);
00385                      }
00386                      else
00387                             return ERROR;
00388                      break;
00389               case 'w':                                                             /* warning threshold */
00390                      warning_value = strtoul (optarg, NULL, 10);
00391                      check_warning_value = TRUE;
00392                      break;
00393               case 'c':                                                             /* critical threshold */
00394                      critical_value = strtoul (optarg, NULL, 10);
00395                      check_critical_value = TRUE;
00396                      break;
00397               case 't':                                                             /* timeout */
00398                      socket_timeout = atoi (optarg);
00399                      if (socket_timeout <= 0)
00400                             return ERROR;
00401               }
00402 
00403        }
00404        return OK;
00405 }
00406 
00407 
00408 void
00409 print_help (void)
00410 {
00411        char *myport;
00412        asprintf (&myport, "%d", PORT);
00413 
00414        print_revision (progname, NP_VERSION);
00415 
00416        printf ("Copyright (c) 1999 Ethan Galstad <nagios@nagios.org>\n");
00417        printf (COPYRIGHT, copyright, email);
00418 
00419        printf ("%s\n", _("This plugin attempts to contact the Over-CR collector daemon running on the"));
00420   printf ("%s\n", _("remote UNIX server in order to gather the requested system information."));
00421 
00422   printf ("\n\n");
00423 
00424        print_usage ();
00425 
00426        printf (UT_HELP_VRSN);
00427        printf (UT_EXTRA_OPTS);
00428 
00429        printf (UT_HOST_PORT, 'p', myport);
00430 
00431   printf (" %s\n", "-w, --warning=INTEGER");
00432   printf ("    %s\n", _("Threshold which will result in a warning status"));
00433   printf (" %s\n", "-c, --critical=INTEGER");
00434   printf ("    %s\n", _("Threshold which will result in a critical status"));
00435   printf (" %s\n", "-v, --variable=STRING");
00436   printf ("    %s\n", _("Variable to check.  Valid variables include:"));
00437   printf ("    %s\n", _("LOAD1         = 1 minute average CPU load"));
00438   printf ("    %s\n", _("LOAD5         = 5 minute average CPU load"));
00439   printf ("    %s\n", _("LOAD15        = 15 minute average CPU load"));
00440   printf ("    %s\n", _("DPU<filesys>  = percent used disk space on filesystem <filesys>"));
00441   printf ("    %s\n", _("PROC<process> = number of running processes with name <process>"));
00442   printf ("    %s\n", _("NET<port>     = number of active connections on TCP port <port>"));
00443   printf ("    %s\n", _("UPTIME        = system uptime in seconds"));
00444 
00445        printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
00446 
00447   printf (UT_VERBOSE);
00448 
00449   printf ("\n");
00450   printf ("%s\n", _("This plugin requires that Eric Molitors' Over-CR collector daemon be"));
00451   printf ("%s\n", _("running on the remote server."));
00452   printf ("%s\n", _("Over-CR can be downloaded from http://www.molitor.org/overcr"));
00453   printf ("%s\n", _("This plugin was tested with version 0.99.53 of the Over-CR collector"));
00454 
00455   printf ("\n");
00456   printf ("%s\n", _("Notes:"));
00457   printf (" %s\n", _("For the available options, the critical threshold value should always be"));
00458   printf (" %s\n", _("higher than the warning threshold value, EXCEPT with the uptime variable"));
00459 
00460   printf (UT_SUPPORT);
00461 }
00462 
00463 
00464 void
00465 print_usage (void)
00466 {
00467   printf ("%s\n", _("Usage:"));
00468        printf ("%s -H host [-p port] [-v variable] [-w warning] [-c critical] [-t timeout]\n", progname);
00469 }