Back to index

nagios-plugins  1.4.16
check_ntp_peer.c
Go to the documentation of this file.
00001 /*****************************************************************************
00002 * 
00003 * Nagios check_ntp_peer plugin
00004 * 
00005 * License: GPL
00006 * Copyright (c) 2006 Sean Finney <seanius@seanius.net>
00007 * Copyright (c) 2006-2008 Nagios Plugins Development Team
00008 * 
00009 * Description:
00010 * 
00011 * This file contains the check_ntp_peer plugin
00012 * 
00013 * This plugin checks an NTP server independent of any commandline
00014 * programs or external libraries.
00015 * 
00016 * Use this plugin to check the health of an NTP server. It supports
00017 * checking the offset with the sync peer, the jitter and stratum. This
00018 * plugin will not check the clock offset between the local host and NTP
00019 * server; please use check_ntp_time for that purpose.
00020 * 
00021 * 
00022 * This program is free software: you can redistribute it and/or modify
00023 * it under the terms of the GNU General Public License as published by
00024 * the Free Software Foundation, either version 3 of the License, or
00025 * (at your option) any later version.
00026 * 
00027 * This program is distributed in the hope that it will be useful,
00028 * but WITHOUT ANY WARRANTY; without even the implied warranty of
00029 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00030 * GNU General Public License for more details.
00031 * 
00032 * You should have received a copy of the GNU General Public License
00033 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00034 * 
00035 * 
00036 *****************************************************************************/
00037 
00038 const char *progname = "check_ntp_peer";
00039 const char *copyright = "2006-2008";
00040 const char *email = "nagiosplug-devel@lists.sourceforge.net";
00041 
00042 #include "common.h"
00043 #include "netutils.h"
00044 #include "utils.h"
00045 
00046 static char *server_address=NULL;
00047 static int port=123;
00048 static int verbose=0;
00049 static int quiet=0;
00050 static short do_offset=0;
00051 static char *owarn="60";
00052 static char *ocrit="120";
00053 static short do_stratum=0;
00054 static char *swarn="-1:16";
00055 static char *scrit="-1:16";
00056 static short do_jitter=0;
00057 static char *jwarn="-1:5000";
00058 static char *jcrit="-1:10000";
00059 static short do_truechimers=0;
00060 static char *twarn="0:";
00061 static char *tcrit="0:";
00062 static int syncsource_found=0;
00063 static int li_alarm=0;
00064 
00065 int process_arguments (int, char **);
00066 thresholds *offset_thresholds = NULL;
00067 thresholds *jitter_thresholds = NULL;
00068 thresholds *stratum_thresholds = NULL;
00069 thresholds *truechimer_thresholds = NULL;
00070 void print_help (void);
00071 void print_usage (void);
00072 
00073 /* max size of control message data */
00074 #define MAX_CM_SIZE 468
00075 
00076 /* this structure holds everything in an ntp control message as per rfc1305 */
00077 typedef struct {
00078        uint8_t flags;       /* byte with leapindicator,vers,mode. see macros */
00079        uint8_t op;          /* R,E,M bits and Opcode */
00080        uint16_t seq;        /* Packet sequence */
00081        uint16_t status;     /* Clock status */
00082        uint16_t assoc;      /* Association */
00083        uint16_t offset;     /* Similar to TCP sequence # */
00084        uint16_t count;      /* # bytes of data */
00085        char data[MAX_CM_SIZE]; /* ASCII data of the request */
00086                                /* NB: not necessarily NULL terminated! */
00087 } ntp_control_message;
00088 
00089 /* this is an association/status-word pair found in control packet reponses */
00090 typedef struct {
00091        uint16_t assoc;
00092        uint16_t status;
00093 } ntp_assoc_status_pair;
00094 
00095 /* bits 1,2 are the leap indicator */
00096 #define LI_MASK 0xc0
00097 #define LI(x) ((x&LI_MASK)>>6)
00098 #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
00099 /* and these are the values of the leap indicator */
00100 #define LI_NOWARNING 0x00
00101 #define LI_EXTRASEC 0x01
00102 #define LI_MISSINGSEC 0x02
00103 #define LI_ALARM 0x03
00104 /* bits 3,4,5 are the ntp version */
00105 #define VN_MASK 0x38
00106 #define VN(x) ((x&VN_MASK)>>3)
00107 #define VN_SET(x,y)  do{ x |= ((y<<3)&VN_MASK); }while(0)
00108 #define VN_RESERVED 0x02
00109 /* bits 6,7,8 are the ntp mode */
00110 #define MODE_MASK 0x07
00111 #define MODE(x) (x&MODE_MASK)
00112 #define MODE_SET(x,y)       do{ x |= (y&MODE_MASK); }while(0)
00113 /* here are some values */
00114 #define MODE_CLIENT 0x03
00115 #define MODE_CONTROLMSG 0x06
00116 /* In control message, bits 8-10 are R,E,M bits */
00117 #define REM_MASK 0xe0
00118 #define REM_RESP 0x80
00119 #define REM_ERROR 0x40
00120 #define REM_MORE 0x20
00121 /* In control message, bits 11 - 15 are opcode */
00122 #define OP_MASK 0x1f
00123 #define OP_SET(x,y)   do{ x |= (y&OP_MASK); }while(0)
00124 #define OP_READSTAT 0x01
00125 #define OP_READVAR  0x02
00126 /* In peer status bytes, bits 6,7,8 determine clock selection status */
00127 #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
00128 #define PEER_TRUECHIMER 0x02
00129 #define PEER_INCLUDED 0x04
00130 #define PEER_SYNCSOURCE 0x06
00131 
00132 /* NTP control message header is 12 bytes, plus any data in the data
00133  * field, plus null padding to the nearest 32-bit boundary per rfc.
00134  */
00135 #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0))
00136 
00137 /* finally, a little helper or two for debugging: */
00138 #define DBG(x) do{if(verbose>1){ x; }}while(0);
00139 #define PRINTSOCKADDR(x) \
00140        do{ \
00141               printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
00142        }while(0);
00143 
00144 void print_ntp_control_message(const ntp_control_message *p){
00145        int i=0, numpeers=0;
00146        const ntp_assoc_status_pair *peer=NULL;
00147 
00148        printf("control packet contents:\n");
00149        printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
00150        printf("\t  li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
00151        printf("\t  vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
00152        printf("\t  mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
00153        printf("\t  response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP);
00154        printf("\t  more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE);
00155        printf("\t  error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR);
00156        printf("\t  op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK);
00157        printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
00158        printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
00159        printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
00160        printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
00161        printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
00162        numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair));
00163        if(p->op&REM_RESP && p->op&OP_READSTAT){
00164               peer=(ntp_assoc_status_pair*)p->data;
00165               for(i=0;i<numpeers;i++){
00166                      printf("\tpeer id %.2x status %.2x",
00167                             ntohs(peer[i].assoc), ntohs(peer[i].status));
00168                      if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
00169                             printf(" <-- current sync source");
00170                      } else if(PEER_SEL(peer[i].status) >= PEER_INCLUDED){
00171                             printf(" <-- current sync candidate");
00172                      } else if(PEER_SEL(peer[i].status) >= PEER_TRUECHIMER){
00173                             printf(" <-- outlyer, but truechimer");
00174                      }
00175                      printf("\n");
00176               }
00177        }
00178 }
00179 
00180 void
00181 setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
00182        memset(p, 0, sizeof(ntp_control_message));
00183        LI_SET(p->flags, LI_NOWARNING);
00184        VN_SET(p->flags, VN_RESERVED);
00185        MODE_SET(p->flags, MODE_CONTROLMSG);
00186        OP_SET(p->op, opcode);
00187        p->seq = htons(seq);
00188        /* Remaining fields are zero for requests */
00189 }
00190 
00191 /* This function does all the actual work; roughly here's what it does
00192  * beside setting the offest, jitter and stratum passed as argument:
00193  *  - offset can be negative, so if it cannot get the offset, offset_result
00194  *    is set to UNKNOWN, otherwise OK.
00195  *  - jitter and stratum are set to -1 if they cannot be retrieved so any
00196  *    positive value means a success retrieving the value.
00197  *  - status is set to WARNING if there's no sync.peer (otherwise OK) and is
00198  *    the return value of the function.
00199  *  status is pretty much useless as syncsource_found is a global variable
00200  *  used later in main to check is the server was synchronized. It works
00201  *  so I left it alone */
00202 int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers){
00203        int conn=-1, i, npeers=0, num_candidates=0;
00204        double tmp_offset = 0;
00205        int min_peer_sel=PEER_INCLUDED;
00206        int peers_size=0, peer_offset=0;
00207        int status;
00208        ntp_assoc_status_pair *peers=NULL;
00209        ntp_control_message req;
00210        const char *getvar = "stratum,offset,jitter";
00211        char *data, *value, *nptr;
00212        void *tmp;
00213 
00214        status = STATE_OK;
00215        *offset_result = STATE_UNKNOWN;
00216        *jitter = *stratum = -1;
00217        *num_truechimers = 0;
00218 
00219        /* Long-winded explanation:
00220         * Getting the sync peer offset, jitter and stratum requires a number of
00221         * steps:
00222         * 1) Send a READSTAT request.
00223         * 2) Interpret the READSTAT reply
00224         *  a) The data section contains a list of peer identifiers (16 bits)
00225         *     and associated status words (16 bits)
00226         *  b) We want the value of 0x06 in the SEL (peer selection) value,
00227         *     which means "current synchronizatin source".  If that's missing,
00228         *     we take anything better than 0x04 (see the rfc for details) but
00229         *     set a minimum of warning.
00230         * 3) Send a READVAR request for information on each peer identified
00231         *    in 2b greater than the minimum selection value.
00232         * 4) Extract the offset, jitter and stratum value from the data[]
00233         *    (it's ASCII)
00234         */
00235        my_udp_connect(server_address, port, &conn);
00236 
00237        /* keep sending requests until the server stops setting the
00238         * REM_MORE bit, though usually this is only 1 packet. */
00239        do{
00240               setup_control_request(&req, OP_READSTAT, 1);
00241               DBG(printf("sending READSTAT request"));
00242               write(conn, &req, SIZEOF_NTPCM(req));
00243               DBG(print_ntp_control_message(&req));
00244               /* Attempt to read the largest size packet possible */
00245               req.count=htons(MAX_CM_SIZE);
00246               DBG(printf("recieving READSTAT response"))
00247               if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
00248                      die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
00249               DBG(print_ntp_control_message(&req));
00250               /* discard obviously invalid packets */
00251               if (ntohs(req.count) > MAX_CM_SIZE)
00252                      die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
00253               if (LI(req.flags) == LI_ALARM) li_alarm = 1;
00254               /* Each peer identifier is 4 bytes in the data section, which
00255                * we represent as a ntp_assoc_status_pair datatype.
00256                */
00257               peers_size+=ntohs(req.count);
00258               if((tmp=realloc(peers, peers_size)) == NULL)
00259                      free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
00260               peers=tmp;
00261               memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
00262               npeers=peers_size/sizeof(ntp_assoc_status_pair);
00263               peer_offset+=ntohs(req.count);
00264        } while(req.op&REM_MORE);
00265 
00266        /* first, let's find out if we have a sync source, or if there are
00267         * at least some candidates. In the latter case we'll issue
00268         * a warning but go ahead with the check on them. */
00269        for (i = 0; i < npeers; i++){
00270               if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){
00271                      (*num_truechimers)++;
00272                      if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){
00273                             num_candidates++;
00274                             if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
00275                                    syncsource_found=1;
00276                                    min_peer_sel=PEER_SYNCSOURCE;
00277                             }
00278                      }
00279               }
00280        }
00281        if(verbose) printf("%d candidate peers available\n", num_candidates);
00282        if(verbose && syncsource_found) printf("synchronization source found\n");
00283        if(! syncsource_found){
00284               status = STATE_WARNING;
00285               if(verbose) printf("warning: no synchronization source found\n");
00286        }
00287        if(li_alarm){
00288               status = STATE_WARNING;
00289               if(verbose) printf("warning: LI_ALARM bit is set\n");
00290        }
00291 
00292 
00293        for (i = 0; i < npeers; i++){
00294               /* Only query this server if it is the current sync source */
00295               /* If there's no sync.peer, query all candidates and use the best one */
00296               if (PEER_SEL(peers[i].status) >= min_peer_sel){
00297                      if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
00298                      asprintf(&data, "");
00299                      do{
00300                             setup_control_request(&req, OP_READVAR, 2);
00301                             req.assoc = peers[i].assoc;
00302                             /* Putting the wanted variable names in the request
00303                              * cause the server to provide _only_ the requested values.
00304                              * thus reducing net traffic, guaranteeing us only a single
00305                              * datagram in reply, and making intepretation much simpler
00306                              */
00307                             /* Older servers doesn't know what jitter is, so if we get an
00308                              * error on the first pass we redo it with "dispersion" */
00309                             strncpy(req.data, getvar, MAX_CM_SIZE-1);
00310                             req.count = htons(strlen(getvar));
00311                             DBG(printf("sending READVAR request...\n"));
00312                             write(conn, &req, SIZEOF_NTPCM(req));
00313                             DBG(print_ntp_control_message(&req));
00314 
00315                             req.count = htons(MAX_CM_SIZE);
00316                             DBG(printf("receiving READVAR response...\n"));
00317                             read(conn, &req, SIZEOF_NTPCM(req));
00318                             DBG(print_ntp_control_message(&req));
00319 
00320                             if(!(req.op&REM_ERROR))
00321                                    asprintf(&data, "%s%s", data, req.data);
00322                      } while(req.op&REM_MORE);
00323 
00324                      if(req.op&REM_ERROR) {
00325                             if(strstr(getvar, "jitter")) {
00326                                    if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
00327                                    getvar = "stratum,offset,dispersion";
00328                                    i--;
00329                                    continue;
00330                             } else if(strlen(getvar)) {
00331                                    if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
00332                                    getvar = "";
00333                                    i--;
00334                                    continue;
00335                             }
00336                      }
00337 
00338                      if(verbose > 1)
00339                             printf("Server responded: >>>%s<<<\n", data);
00340 
00341                      /* get the offset */
00342                      if(verbose)
00343                             printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
00344 
00345                      value = np_extract_ntpvar(data, "offset");
00346                      nptr=NULL;
00347                      /* Convert the value if we have one */
00348                      if(value != NULL)
00349                             tmp_offset = strtod(value, &nptr) / 1000;
00350                      /* If value is null or no conversion was performed */
00351                      if(value == NULL || value==nptr) {
00352                             if(verbose) printf("error: unable to read server offset response.\n");
00353                      } else {
00354                             if(verbose) printf("%.10g\n", tmp_offset);
00355                             if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
00356                                    *offset = tmp_offset;
00357                                    *offset_result = STATE_OK;
00358                             } else {
00359                                    /* Skip this one; move to the next */
00360                                    continue;
00361                             }
00362                      }
00363 
00364                      if(do_jitter) {
00365                             /* get the jitter */
00366                             if(verbose) {
00367                                    printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
00368                             }
00369                             value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
00370                             nptr=NULL;
00371                             /* Convert the value if we have one */
00372                             if(value != NULL)
00373                                    *jitter = strtod(value, &nptr);
00374                             /* If value is null or no conversion was performed */
00375                             if(value == NULL || value==nptr) {
00376                                    if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
00377                                    *jitter = -1;
00378                             } else if(verbose) {
00379                                    printf("%.10g\n", *jitter);
00380                             }
00381                      }
00382 
00383                      if(do_stratum) {
00384                             /* get the stratum */
00385                             if(verbose) {
00386                                    printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
00387                             }
00388                             value = np_extract_ntpvar(data, "stratum");
00389                             nptr=NULL;
00390                             /* Convert the value if we have one */
00391                             if(value != NULL)
00392                                    *stratum = strtol(value, &nptr, 10);
00393                             if(value == NULL || value==nptr) {
00394                                    if(verbose) printf("error: unable to read server stratum response.\n");
00395                                    *stratum = -1;
00396                             } else {
00397                                    if(verbose) printf("%i\n", *stratum);
00398                             }
00399                      }
00400               } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
00401        } /* for (i = 0; i < npeers; i++) */
00402 
00403        close(conn);
00404        if(peers!=NULL) free(peers);
00405 
00406        return status;
00407 }
00408 
00409 int process_arguments(int argc, char **argv){
00410        int c;
00411        int option=0;
00412        static struct option longopts[] = {
00413               {"version", no_argument, 0, 'V'},
00414               {"help", no_argument, 0, 'h'},
00415               {"verbose", no_argument, 0, 'v'},
00416               {"use-ipv4", no_argument, 0, '4'},
00417               {"use-ipv6", no_argument, 0, '6'},
00418               {"quiet", no_argument, 0, 'q'},
00419               {"warning", required_argument, 0, 'w'},
00420               {"critical", required_argument, 0, 'c'},
00421               {"swarn", required_argument, 0, 'W'},
00422               {"scrit", required_argument, 0, 'C'},
00423               {"jwarn", required_argument, 0, 'j'},
00424               {"jcrit", required_argument, 0, 'k'},
00425               {"twarn", required_argument, 0, 'm'},
00426               {"tcrit", required_argument, 0, 'n'},
00427               {"timeout", required_argument, 0, 't'},
00428               {"hostname", required_argument, 0, 'H'},
00429               {"port", required_argument, 0, 'p'},
00430               {0, 0, 0, 0}
00431        };
00432 
00433 
00434        if (argc < 2)
00435               usage ("\n");
00436 
00437        while (1) {
00438               c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option);
00439               if (c == -1 || c == EOF || c == 1)
00440                      break;
00441 
00442               switch (c) {
00443               case 'h':
00444                      print_help();
00445                      exit(STATE_OK);
00446                      break;
00447               case 'V':
00448                      print_revision(progname, NP_VERSION);
00449                      exit(STATE_OK);
00450                      break;
00451               case 'v':
00452                      verbose++;
00453                      break;
00454               case 'q':
00455                      quiet = 1;
00456                      break;
00457               case 'w':
00458                      do_offset=1;
00459                      owarn = optarg;
00460                      break;
00461               case 'c':
00462                      do_offset=1;
00463                      ocrit = optarg;
00464                      break;
00465               case 'W':
00466                      do_stratum=1;
00467                      swarn = optarg;
00468                      break;
00469               case 'C':
00470                      do_stratum=1;
00471                      scrit = optarg;
00472                      break;
00473               case 'j':
00474                      do_jitter=1;
00475                      jwarn = optarg;
00476                      break;
00477               case 'k':
00478                      do_jitter=1;
00479                      jcrit = optarg;
00480                      break;
00481               case 'm':
00482                      do_truechimers=1;
00483                      twarn = optarg;
00484                      break;
00485               case 'n':
00486                      do_truechimers=1;
00487                      tcrit = optarg;
00488                      break;
00489               case 'H':
00490                      if(is_host(optarg) == FALSE)
00491                             usage2(_("Invalid hostname/address"), optarg);
00492                      server_address = strdup(optarg);
00493                      break;
00494               case 'p':
00495                      port=atoi(optarg);
00496                      break;
00497               case 't':
00498                      socket_timeout=atoi(optarg);
00499                      break;
00500               case '4':
00501                      address_family = AF_INET;
00502                      break;
00503               case '6':
00504 #ifdef USE_IPV6
00505                      address_family = AF_INET6;
00506 #else
00507                      usage4 (_("IPv6 support not available"));
00508 #endif
00509                      break;
00510               case '?':
00511                      /* print short usage statement if args not parsable */
00512                      usage5 ();
00513                      break;
00514               }
00515        }
00516 
00517        if(server_address == NULL){
00518               usage4(_("Hostname was not supplied"));
00519        }
00520 
00521        return 0;
00522 }
00523 
00524 char *perfd_offset (double offset)
00525 {
00526        return fperfdata ("offset", offset, "s",
00527               TRUE, offset_thresholds->warning->end,
00528               TRUE, offset_thresholds->critical->end,
00529               FALSE, 0, FALSE, 0);
00530 }
00531 
00532 char *perfd_jitter (double jitter)
00533 {
00534        return fperfdata ("jitter", jitter, "",
00535               do_jitter, jitter_thresholds->warning->end,
00536               do_jitter, jitter_thresholds->critical->end,
00537               TRUE, 0, FALSE, 0);
00538 }
00539 
00540 char *perfd_stratum (int stratum)
00541 {
00542        return perfdata ("stratum", stratum, "",
00543               do_stratum, (int)stratum_thresholds->warning->end,
00544               do_stratum, (int)stratum_thresholds->critical->end,
00545               TRUE, 0, TRUE, 16);
00546 }
00547 
00548 char *perfd_truechimers (int num_truechimers)
00549 {
00550        return perfdata ("truechimers", num_truechimers, "",
00551               do_truechimers, (int)truechimer_thresholds->warning->end,
00552               do_truechimers, (int)truechimer_thresholds->critical->end,
00553               TRUE, 0, FALSE, 0);
00554 }
00555 
00556 int main(int argc, char *argv[]){
00557        int result, offset_result, stratum, num_truechimers;
00558        double offset=0, jitter=0;
00559        char *result_line, *perfdata_line;
00560 
00561        setlocale (LC_ALL, "");
00562        bindtextdomain (PACKAGE, LOCALEDIR);
00563        textdomain (PACKAGE);
00564 
00565        /* Parse extra opts if any */
00566        argv=np_extra_opts (&argc, argv, progname);
00567 
00568        if (process_arguments (argc, argv) == ERROR)
00569               usage4 (_("Could not parse arguments"));
00570 
00571        set_thresholds(&offset_thresholds, owarn, ocrit);
00572        set_thresholds(&jitter_thresholds, jwarn, jcrit);
00573        set_thresholds(&stratum_thresholds, swarn, scrit);
00574        set_thresholds(&truechimer_thresholds, twarn, tcrit);
00575 
00576        /* initialize alarm signal handling */
00577        signal (SIGALRM, socket_timeout_alarm_handler);
00578 
00579        /* set socket timeout */
00580        alarm (socket_timeout);
00581 
00582        /* This returns either OK or WARNING (See comment preceeding ntp_request) */
00583        result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum, &num_truechimers);
00584 
00585        if(offset_result == STATE_UNKNOWN) {
00586               /* if there's no sync peer (this overrides ntp_request output): */
00587               result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
00588        } else {
00589               /* Be quiet if there's no candidates either */
00590               if (quiet == 1 && result == STATE_WARNING)
00591                      result = STATE_UNKNOWN;
00592               result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
00593        }
00594 
00595        if(do_truechimers)
00596               result = max_state_alt(result, get_status(num_truechimers, truechimer_thresholds));
00597 
00598        if(do_stratum)
00599               result = max_state_alt(result, get_status(stratum, stratum_thresholds));
00600 
00601        if(do_jitter)
00602               result = max_state_alt(result, get_status(jitter, jitter_thresholds));
00603 
00604        switch (result) {
00605               case STATE_CRITICAL :
00606                      asprintf(&result_line, _("NTP CRITICAL:"));
00607                      break;
00608               case STATE_WARNING :
00609                      asprintf(&result_line, _("NTP WARNING:"));
00610                      break;
00611               case STATE_OK :
00612                      asprintf(&result_line, _("NTP OK:"));
00613                      break;
00614               default :
00615                      asprintf(&result_line, _("NTP UNKNOWN:"));
00616                      break;
00617        }
00618        if(!syncsource_found)
00619               asprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
00620        else if(li_alarm)
00621               asprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
00622 
00623        if(offset_result == STATE_UNKNOWN){
00624               asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
00625               asprintf(&perfdata_line, "");
00626        } else {
00627               asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
00628               asprintf(&perfdata_line, "%s", perfd_offset(offset));
00629        }
00630        if (do_jitter) {
00631               asprintf(&result_line, "%s, jitter=%f", result_line, jitter);
00632               asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
00633        }
00634        if (do_stratum) {
00635               asprintf(&result_line, "%s, stratum=%i", result_line, stratum);
00636               asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
00637        }
00638        if (do_truechimers) {
00639               asprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
00640               asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
00641        }
00642        printf("%s|%s\n", result_line, perfdata_line);
00643 
00644        if(server_address!=NULL) free(server_address);
00645        return result;
00646 }
00647 
00648 
00649 
00650 void print_help(void){
00651        print_revision(progname, NP_VERSION);
00652 
00653        printf ("Copyright (c) 2006 Sean Finney\n");
00654        printf (COPYRIGHT, copyright, email);
00655 
00656        printf ("%s\n", _("This plugin checks the selected ntp server"));
00657 
00658        printf ("\n\n");
00659 
00660        print_usage();
00661        printf (UT_HELP_VRSN);
00662        printf (UT_EXTRA_OPTS);
00663        printf (UT_HOST_PORT, 'p', "123");
00664        printf (" %s\n", "-q, --quiet");
00665        printf ("    %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
00666        printf (" %s\n", "-w, --warning=THRESHOLD");
00667        printf ("    %s\n", _("Offset to result in warning status (seconds)"));
00668        printf (" %s\n", "-c, --critical=THRESHOLD");
00669        printf ("    %s\n", _("Offset to result in critical status (seconds)"));
00670        printf (" %s\n", "-W, --swarn=THRESHOLD");
00671        printf ("    %s\n", _("Warning threshold for stratum"));
00672        printf (" %s\n", "-C, --scrit=THRESHOLD");
00673        printf ("    %s\n", _("Critical threshold for stratum"));
00674        printf (" %s\n", "-j, --jwarn=THRESHOLD");
00675        printf ("    %s\n", _("Warning threshold for jitter"));
00676        printf (" %s\n", "-k, --jcrit=THRESHOLD");
00677        printf ("    %s\n", _("Critical threshold for jitter"));
00678        printf (" %s\n", "-m, --twarn=THRESHOLD");
00679        printf ("    %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
00680        printf (" %s\n", "-n, --tcrit=THRESHOLD");
00681        printf ("    %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
00682        printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
00683        printf (UT_VERBOSE);
00684 
00685        printf("\n");
00686        printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
00687        printf("%s\n\n", _("programs or external libraries."));
00688 
00689        printf("%s\n", _("Notes:"));
00690        printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
00691        printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
00692        printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
00693        printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
00694        printf("\n");
00695        printf(UT_THRESHOLDS_NOTES);
00696 
00697        printf("\n");
00698        printf("%s\n", _("Examples:"));
00699        printf(" %s\n", _("Simple NTP server check:"));
00700        printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
00701        printf("\n");
00702        printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
00703        printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
00704        printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
00705        printf("\n");
00706        printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):"));
00707        printf("  %s\n", ("./check_ntp_peer -H ntpserv -m :5 -n :3"));
00708        printf("\n");
00709        printf(" %s\n", _("Check only stratum:"));
00710        printf("  %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
00711 
00712        printf (UT_SUPPORT);
00713 }
00714 
00715 void
00716 print_usage(void)
00717 {
00718        printf ("%s\n", _("Usage:"));
00719        printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
00720        printf("       [-j <warn>] [-k <crit>] [-v verbose]\n");
00721 }