Back to index

nagios-plugins  1.4.16
check_time.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_time plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 1999-2007 Nagios Plugins Development Team
00007 * 
00008 * Description:
00009 * 
00010 * This file contains the check_time plugin
00011 * 
00012 * This plugin will check the time difference with the specified host.
00013 * 
00014 * 
00015 * This program is free software: you can redistribute it and/or modify
00016 * it under the terms of the GNU General Public License as published by
00017 * the Free Software Foundation, either version 3 of the License, or
00018 * (at your option) any later version.
00019 * 
00020 * This program is distributed in the hope that it will be useful,
00021 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00022 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00023 * GNU General Public License for more details.
00024 * 
00025 * You should have received a copy of the GNU General Public License
00026 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00027 * 
00028 * 
00029 *****************************************************************************/
00030 
00031 const char *progname = "check_time";
00032 const char *copyright = "1999-2007";
00033 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00034 
00035 #include "common.h"
00036 #include "netutils.h"
00037 #include "utils.h"
00038 
00039 enum {
00040        TIME_PORT = 37
00041 };
00042 
00043 #define       UNIX_EPOCH 2208988800UL
00044 
00045 uint32_t raw_server_time;
00046 unsigned long server_time, diff_time;
00047 int warning_time = 0;
00048 int check_warning_time = FALSE;
00049 int critical_time = 0;
00050 int check_critical_time = FALSE;
00051 unsigned long warning_diff = 0;
00052 int check_warning_diff = FALSE;
00053 unsigned long critical_diff = 0;
00054 int check_critical_diff = FALSE;
00055 int server_port = TIME_PORT;
00056 char *server_address = NULL;
00057 int use_udp = FALSE;
00058 
00059 int process_arguments (int, char **);
00060 void print_help (void);
00061 void print_usage (void);
00062 
00063 int
00064 main (int argc, char **argv)
00065 {
00066        int sd;
00067        int result = STATE_UNKNOWN;
00068        time_t conntime;
00069 
00070        setlocale (LC_ALL, "");
00071        bindtextdomain (PACKAGE, LOCALEDIR);
00072        textdomain (PACKAGE);
00073 
00074        /* Parse extra opts if any */
00075        argv=np_extra_opts (&argc, argv, progname);
00076 
00077        if (process_arguments (argc, argv) == ERROR)
00078               usage4 (_("Could not parse arguments"));
00079 
00080        /* initialize alarm signal handling */
00081        signal (SIGALRM, socket_timeout_alarm_handler);
00082 
00083        /* set socket timeout */
00084        alarm (socket_timeout);
00085        time (&start_time);
00086 
00087        /* try to connect to the host at the given port number */
00088        if (use_udp) {
00089               result = my_udp_connect (server_address, server_port, &sd);
00090        } else {
00091               result = my_tcp_connect (server_address, server_port, &sd);
00092        }
00093 
00094        if (result != STATE_OK) {
00095               if (check_critical_time == TRUE)
00096                      result = STATE_CRITICAL;
00097               else if (check_warning_time == TRUE)
00098                      result = STATE_WARNING;
00099               else
00100                      result = STATE_UNKNOWN;
00101               die (result,
00102                          _("TIME UNKNOWN - could not connect to server %s, port %d\n"),
00103                          server_address, server_port);
00104        }
00105 
00106        if (use_udp) {
00107               if (send (sd, "", 0, 0) < 0) {
00108                      if (check_critical_time == TRUE)
00109                             result = STATE_CRITICAL;
00110                      else if (check_warning_time == TRUE)
00111                             result = STATE_WARNING;
00112                      else
00113                             result = STATE_UNKNOWN;
00114                      die (result,
00115                        _("TIME UNKNOWN - could not send UDP request to server %s, port %d\n"),
00116                        server_address, server_port);
00117               }
00118        }
00119 
00120        /* watch for the connection string */
00121        result = recv (sd, (void *)&raw_server_time, sizeof (raw_server_time), 0);
00122 
00123        /* close the connection */
00124        close (sd);
00125 
00126        /* reset the alarm */
00127        time (&end_time);
00128        alarm (0);
00129 
00130        /* return a WARNING status if we couldn't read any data */
00131        if (result <= 0) {
00132               if (check_critical_time == TRUE)
00133                      result = STATE_CRITICAL;
00134               else if (check_warning_time == TRUE)
00135                      result = STATE_WARNING;
00136               else
00137                      result = STATE_UNKNOWN;
00138               die (result,
00139                                                   _("TIME UNKNOWN - no data received from server %s, port %d\n"),
00140                                                   server_address, server_port);
00141        }
00142 
00143        result = STATE_OK;
00144 
00145        conntime = (end_time - start_time);
00146        if (check_critical_time == TRUE && conntime > critical_time)
00147               result = STATE_CRITICAL;
00148        else if (check_warning_time == TRUE && conntime > warning_time)
00149               result = STATE_WARNING;
00150 
00151        if (result != STATE_OK)
00152               die (result, _("TIME %s - %d second response time|%s\n"),
00153                    state_text (result), (int)conntime,
00154                    perfdata ("time", (long)conntime, "s",
00155                              check_warning_time, (long)warning_time,
00156                              check_critical_time, (long)critical_time,
00157                              TRUE, 0, FALSE, 0));
00158 
00159        server_time = ntohl (raw_server_time) - UNIX_EPOCH;
00160        if (server_time > (unsigned long)end_time)
00161               diff_time = server_time - (unsigned long)end_time;
00162        else
00163               diff_time = (unsigned long)end_time - server_time;
00164 
00165        if (check_critical_diff == TRUE && diff_time > critical_diff)
00166               result = STATE_CRITICAL;
00167        else if (check_warning_diff == TRUE && diff_time > warning_diff)
00168               result = STATE_WARNING;
00169 
00170        printf (_("TIME %s - %lu second time difference|%s %s\n"),
00171                state_text (result), diff_time,
00172                perfdata ("time", (long)conntime, "s",
00173                          check_warning_time, (long)warning_time,
00174                          check_critical_time, (long)critical_time,
00175                          TRUE, 0, FALSE, 0),
00176                perfdata ("offset", diff_time, "s",
00177                          check_warning_diff, warning_diff,
00178                          check_critical_diff, critical_diff,
00179                          TRUE, 0, FALSE, 0));
00180        return result;
00181 }
00182 
00183 
00184 
00185 /* process command-line arguments */
00186 int
00187 process_arguments (int argc, char **argv)
00188 {
00189        int c;
00190 
00191        int option = 0;
00192        static struct option longopts[] = {
00193               {"hostname", required_argument, 0, 'H'},
00194               {"warning-variance", required_argument, 0, 'w'},
00195               {"critical-variance", required_argument, 0, 'c'},
00196               {"warning-connect", required_argument, 0, 'W'},
00197               {"critical-connect", required_argument, 0, 'C'},
00198               {"port", required_argument, 0, 'p'},
00199               {"udp", no_argument, 0, 'u'},
00200               {"timeout", required_argument, 0, 't'},
00201               {"version", no_argument, 0, 'V'},
00202               {"help", no_argument, 0, 'h'},
00203               {0, 0, 0, 0}
00204        };
00205 
00206        if (argc < 2)
00207               usage ("\n");
00208 
00209        for (c = 1; c < argc; c++) {
00210               if (strcmp ("-to", argv[c]) == 0)
00211                      strcpy (argv[c], "-t");
00212               else if (strcmp ("-wd", argv[c]) == 0)
00213                      strcpy (argv[c], "-w");
00214               else if (strcmp ("-cd", argv[c]) == 0)
00215                      strcpy (argv[c], "-c");
00216               else if (strcmp ("-wt", argv[c]) == 0)
00217                      strcpy (argv[c], "-W");
00218               else if (strcmp ("-ct", argv[c]) == 0)
00219                      strcpy (argv[c], "-C");
00220        }
00221 
00222        while (1) {
00223               c = getopt_long (argc, argv, "hVH:w:c:W:C:p:t:u", longopts,
00224                                                                 &option);
00225 
00226               if (c == -1 || c == EOF)
00227                      break;
00228 
00229               switch (c) {
00230               case '?':                                                             /* print short usage statement if args not parsable */
00231                      usage5 ();
00232               case 'h':                                                             /* help */
00233                      print_help ();
00234                      exit (STATE_OK);
00235               case 'V':                                                             /* version */
00236                      print_revision (progname, NP_VERSION);
00237                      exit (STATE_OK);
00238               case 'H':                                                             /* hostname */
00239                      if (is_host (optarg) == FALSE)
00240                             usage2 (_("Invalid hostname/address"), optarg);
00241                      server_address = optarg;
00242                      break;
00243               case 'w':                                                             /* warning-variance */
00244                      if (is_intnonneg (optarg)) {
00245                             warning_diff = strtoul (optarg, NULL, 10);
00246                             check_warning_diff = TRUE;
00247                      }
00248                      else if (strspn (optarg, "0123456789:,") > 0) {
00249                             if (sscanf (optarg, "%lu%*[:,]%d", &warning_diff, &warning_time) == 2) {
00250                                    check_warning_diff = TRUE;
00251                                    check_warning_time = TRUE;
00252                             }
00253                             else {
00254                                    usage4 (_("Warning thresholds must be a positive integer"));
00255                             }
00256                      }
00257                      else {
00258                             usage4 (_("Warning threshold must be a positive integer"));
00259                      }
00260                      break;
00261               case 'c':                                                             /* critical-variance */
00262                      if (is_intnonneg (optarg)) {
00263                             critical_diff = strtoul (optarg, NULL, 10);
00264                             check_critical_diff = TRUE;
00265                      }
00266                      else if (strspn (optarg, "0123456789:,") > 0) {
00267                             if (sscanf (optarg, "%lu%*[:,]%d", &critical_diff, &critical_time) ==
00268                                           2) {
00269                                    check_critical_diff = TRUE;
00270                                    check_critical_time = TRUE;
00271                             }
00272                             else {
00273                                    usage4 (_("Critical thresholds must be a positive integer"));
00274                             }
00275                      }
00276                      else {
00277                             usage4 (_("Critical threshold must be a positive integer"));
00278                      }
00279                      break;
00280               case 'W':                                                             /* warning-connect */
00281                      if (!is_intnonneg (optarg))
00282                             usage4 (_("Warning threshold must be a positive integer"));
00283                      else
00284                             warning_time = atoi (optarg);
00285                      check_warning_time = TRUE;
00286                      break;
00287               case 'C':                                                             /* critical-connect */
00288                      if (!is_intnonneg (optarg))
00289                             usage4 (_("Critical threshold must be a positive integer"));
00290                      else
00291                             critical_time = atoi (optarg);
00292                      check_critical_time = TRUE;
00293                      break;
00294               case 'p':                                                             /* port */
00295                      if (!is_intnonneg (optarg))
00296                             usage4 (_("Port must be a positive integer"));
00297                      else
00298                             server_port = atoi (optarg);
00299                      break;
00300               case 't':                                                             /* timeout */
00301                      if (!is_intnonneg (optarg))
00302                             usage2 (_("Timeout interval must be a positive integer"), optarg);
00303                      else
00304                             socket_timeout = atoi (optarg);
00305                      break;
00306               case 'u':                                                             /* udp */
00307                      use_udp = TRUE;
00308               }
00309        }
00310 
00311        c = optind;
00312        if (server_address == NULL) {
00313               if (argc > c) {
00314                      if (is_host (argv[c]) == FALSE)
00315                             usage2 (_("Invalid hostname/address"), optarg);
00316                      server_address = argv[c];
00317               }
00318               else {
00319                      usage4 (_("Hostname was not supplied"));
00320               }
00321        }
00322 
00323        return OK;
00324 }
00325 
00326 
00327 
00328 void
00329 print_help (void)
00330 {
00331        char *myport;
00332        asprintf (&myport, "%d", TIME_PORT);
00333 
00334        print_revision (progname, NP_VERSION);
00335 
00336        printf ("Copyright (c) 1999 Ethan Galstad\n");
00337        printf (COPYRIGHT, copyright, email);
00338 
00339        printf ("%s\n", _("This plugin will check the time on the specified host."));
00340 
00341   printf ("\n\n");
00342 
00343        print_usage ();
00344 
00345        printf (UT_HELP_VRSN);
00346        printf (UT_EXTRA_OPTS);
00347 
00348        printf (UT_HOST_PORT, 'p', myport);
00349 
00350        printf (" %s\n", "-u, --udp");
00351   printf ("   %s\n", _("Use UDP to connect, not TCP"));
00352   printf (" %s\n", "-w, --warning-variance=INTEGER");
00353   printf ("   %s\n", _("Time difference (sec.) necessary to result in a warning status"));
00354   printf (" %s\n", "-c, --critical-variance=INTEGER");
00355   printf ("   %s\n", _("Time difference (sec.) necessary to result in a critical status"));
00356   printf (" %s\n", "-W, --warning-connect=INTEGER");
00357   printf ("   %s\n", _("Response time (sec.) necessary to result in warning status"));
00358   printf (" %s\n", "-C, --critical-connect=INTEGER");
00359   printf ("   %s\n", _("Response time (sec.) necessary to result in critical status"));
00360 
00361        printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
00362 
00363        printf (UT_SUPPORT);
00364 }
00365 
00366 
00367 
00368 void
00369 print_usage (void)
00370 {
00371   printf ("%s\n", _("Usage:"));
00372        printf ("%s -H <host_address> [-p port] [-u] [-w variance] [-c variance]\n",progname);
00373   printf (" [-W connect_time] [-C connect_time] [-t timeout]\n");
00374 }