Code

Removing CVS/SVN tags and replacing with git-based versioning
[nagiosplug.git] / plugins / check_ntp_peer.c
1 /*****************************************************************************
2
3 * Nagios check_ntp_peer plugin
4
5 * License: GPL
6 * Copyright (c) 2006 Sean Finney <seanius@seanius.net>
7 * Copyright (c) 2006-2008 Nagios Plugins Development Team
8
9 * Description:
10
11 * This file contains the check_ntp_peer plugin
12
13 * This plugin checks an NTP server independent of any commandline
14 * programs or external libraries.
15
16 * Use this plugin to check the health of an NTP server. It supports
17 * checking the offset with the sync peer, the jitter and stratum. This
18 * plugin will not check the clock offset between the local host and NTP
19 * server; please use check_ntp_time for that purpose.
20
21
22 * This program is free software: you can redistribute it and/or modify
23 * it under the terms of the GNU General Public License as published by
24 * the Free Software Foundation, either version 3 of the License, or
25 * (at your option) any later version.
26
27 * This program is distributed in the hope that it will be useful,
28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
30 * GNU General Public License for more details.
31
32 * You should have received a copy of the GNU General Public License
33 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
34
35
36 *****************************************************************************/
38 const char *progname = "check_ntp_peer";
39 const char *copyright = "2006-2008";
40 const char *email = "nagiosplug-devel@lists.sourceforge.net";
42 #include "common.h"
43 #include "netutils.h"
44 #include "utils.h"
46 static char *server_address=NULL;
47 static int port=123;
48 static int verbose=0;
49 static int quiet=0;
50 static short do_offset=0;
51 static char *owarn="60";
52 static char *ocrit="120";
53 static short do_stratum=0;
54 static char *swarn="-1:16";
55 static char *scrit="-1:16";
56 static short do_jitter=0;
57 static char *jwarn="-1:5000";
58 static char *jcrit="-1:10000";
59 static int syncsource_found=0;
60 static int li_alarm=0;
62 int process_arguments (int, char **);
63 thresholds *offset_thresholds = NULL;
64 thresholds *jitter_thresholds = NULL;
65 thresholds *stratum_thresholds = NULL;
66 void print_help (void);
67 void print_usage (void);
69 /* max size of control message data */
70 #define MAX_CM_SIZE 468
72 /* this structure holds everything in an ntp control message as per rfc1305 */
73 typedef struct {
74         uint8_t flags;       /* byte with leapindicator,vers,mode. see macros */
75         uint8_t op;          /* R,E,M bits and Opcode */
76         uint16_t seq;        /* Packet sequence */
77         uint16_t status;     /* Clock status */
78         uint16_t assoc;      /* Association */
79         uint16_t offset;     /* Similar to TCP sequence # */
80         uint16_t count;      /* # bytes of data */
81         char data[MAX_CM_SIZE]; /* ASCII data of the request */
82                                 /* NB: not necessarily NULL terminated! */
83 } ntp_control_message;
85 /* this is an association/status-word pair found in control packet reponses */
86 typedef struct {
87         uint16_t assoc;
88         uint16_t status;
89 } ntp_assoc_status_pair;
91 /* bits 1,2 are the leap indicator */
92 #define LI_MASK 0xc0
93 #define LI(x) ((x&LI_MASK)>>6)
94 #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
95 /* and these are the values of the leap indicator */
96 #define LI_NOWARNING 0x00
97 #define LI_EXTRASEC 0x01
98 #define LI_MISSINGSEC 0x02
99 #define LI_ALARM 0x03
100 /* bits 3,4,5 are the ntp version */
101 #define VN_MASK 0x38
102 #define VN(x)   ((x&VN_MASK)>>3)
103 #define VN_SET(x,y)     do{ x |= ((y<<3)&VN_MASK); }while(0)
104 #define VN_RESERVED 0x02
105 /* bits 6,7,8 are the ntp mode */
106 #define MODE_MASK 0x07
107 #define MODE(x) (x&MODE_MASK)
108 #define MODE_SET(x,y)   do{ x |= (y&MODE_MASK); }while(0)
109 /* here are some values */
110 #define MODE_CLIENT 0x03
111 #define MODE_CONTROLMSG 0x06
112 /* In control message, bits 8-10 are R,E,M bits */
113 #define REM_MASK 0xe0
114 #define REM_RESP 0x80
115 #define REM_ERROR 0x40
116 #define REM_MORE 0x20
117 /* In control message, bits 11 - 15 are opcode */
118 #define OP_MASK 0x1f
119 #define OP_SET(x,y)   do{ x |= (y&OP_MASK); }while(0)
120 #define OP_READSTAT 0x01
121 #define OP_READVAR  0x02
122 /* In peer status bytes, bits 6,7,8 determine clock selection status */
123 #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
124 #define PEER_INCLUDED 0x04
125 #define PEER_SYNCSOURCE 0x06
127 /* NTP control message header is 12 bytes, plus any data in the data
128  * field, plus null padding to the nearest 32-bit boundary per rfc.
129  */
130 #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0))
132 /* finally, a little helper or two for debugging: */
133 #define DBG(x) do{if(verbose>1){ x; }}while(0);
134 #define PRINTSOCKADDR(x) \
135         do{ \
136                 printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
137         }while(0);
139 void print_ntp_control_message(const ntp_control_message *p){
140         int i=0, numpeers=0;
141         const ntp_assoc_status_pair *peer=NULL;
143         printf("control packet contents:\n");
144         printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
145         printf("\t  li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
146         printf("\t  vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
147         printf("\t  mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
148         printf("\t  response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP);
149         printf("\t  more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE);
150         printf("\t  error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR);
151         printf("\t  op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK);
152         printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
153         printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
154         printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
155         printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
156         printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
157         numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair));
158         if(p->op&REM_RESP && p->op&OP_READSTAT){
159                 peer=(ntp_assoc_status_pair*)p->data;
160                 for(i=0;i<numpeers;i++){
161                         printf("\tpeer id %.2x status %.2x",
162                                ntohs(peer[i].assoc), ntohs(peer[i].status));
163                         if (PEER_SEL(peer[i].status) >= PEER_INCLUDED){
164                                 if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
165                                         printf(" <-- current sync source");
166                                 } else {
167                                         printf(" <-- current sync candidate");
168                                 }
169                         }
170                         printf("\n");
171                 }
172         }
175 /*
176  * Extract the value from NTP key/value pairs, or return NULL.
177  * The value returned can be free()ed.
178  */
179 char *extract_value(const char *varlist, const char *name){
180         char *tmp=NULL, *value=NULL;
181         int i;
183         while (1) {
184                 /* Strip any leading space */
185                 for (varlist; isspace(varlist[0]); varlist++);
187                 if (strncmp(name, varlist, strlen(name)) == 0) {
188                         varlist += strlen(name);
189                         /* strip trailing spaces */
190                         for (varlist; isspace(varlist[0]); varlist++);
192                         if (varlist[0] == '=') {
193                                 /* We matched the key, go past the = sign */
194                                 varlist++;
195                                 /* strip leading spaces */
196                                 for (varlist; isspace(varlist[0]); varlist++);
198                                 if (tmp = index(varlist, ',')) {
199                                         /* Value is delimited by a comma */
200                                         if (tmp-varlist == 0) continue;
201                                         value = (char *)malloc(tmp-varlist+1);
202                                         strncpy(value, varlist, tmp-varlist);
203                                         value[tmp-varlist] = '\0';
204                                 } else {
205                                         /* Value is delimited by a \0 */
206                                         if (strlen(varlist) == 0) continue;
207                                         value = (char *)malloc(strlen(varlist) + 1);
208                                         strncpy(value, varlist, strlen(varlist));
209                                         value[strlen(varlist)] = '\0';
210                                 }
211                                 break;
212                         }
213                 }
214                 if (tmp = index(varlist, ',')) {
215                         /* More keys, keep going... */
216                         varlist = tmp + 1;
217                 } else {
218                         /* We're done */
219                         break;
220                 }
221         }
223         /* Clean-up trailing spaces/newlines */
224         if (value) for (i=strlen(value)-1; isspace(value[i]); i--) value[i] = '\0';
226         return value;
229 void
230 setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
231         memset(p, 0, sizeof(ntp_control_message));
232         LI_SET(p->flags, LI_NOWARNING);
233         VN_SET(p->flags, VN_RESERVED);
234         MODE_SET(p->flags, MODE_CONTROLMSG);
235         OP_SET(p->op, opcode);
236         p->seq = htons(seq);
237         /* Remaining fields are zero for requests */
240 /* This function does all the actual work; roughly here's what it does
241  * beside setting the offest, jitter and stratum passed as argument:
242  *  - offset can be negative, so if it cannot get the offset, offset_result
243  *    is set to UNKNOWN, otherwise OK.
244  *  - jitter and stratum are set to -1 if they cannot be retrieved so any
245  *    positive value means a success retrieving the value.
246  *  - status is set to WARNING if there's no sync.peer (otherwise OK) and is
247  *    the return value of the function.
248  *  status is pretty much useless as syncsource_found is a global variable
249  *  used later in main to check is the server was synchronized. It works
250  *  so I left it alone */
251 int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum){
252         int conn=-1, i, npeers=0, num_candidates=0;
253         double tmp_offset = 0;
254         int min_peer_sel=PEER_INCLUDED;
255         int peers_size=0, peer_offset=0;
256         int status;
257         ntp_assoc_status_pair *peers=NULL;
258         ntp_control_message req;
259         const char *getvar = "stratum,offset,jitter";
260         char *data, *value, *nptr;
261         void *tmp;
263         status = STATE_OK;
264         *offset_result = STATE_UNKNOWN;
265         *jitter = *stratum = -1;
267         /* Long-winded explanation:
268          * Getting the sync peer offset, jitter and stratum requires a number of
269          * steps:
270          * 1) Send a READSTAT request.
271          * 2) Interpret the READSTAT reply
272          *  a) The data section contains a list of peer identifiers (16 bits)
273          *     and associated status words (16 bits)
274          *  b) We want the value of 0x06 in the SEL (peer selection) value,
275          *     which means "current synchronizatin source".  If that's missing,
276          *     we take anything better than 0x04 (see the rfc for details) but
277          *     set a minimum of warning.
278          * 3) Send a READVAR request for information on each peer identified
279          *    in 2b greater than the minimum selection value.
280          * 4) Extract the offset, jitter and stratum value from the data[]
281          *    (it's ASCII)
282          */
283         my_udp_connect(server_address, port, &conn);
285         /* keep sending requests until the server stops setting the
286          * REM_MORE bit, though usually this is only 1 packet. */
287         do{
288                 setup_control_request(&req, OP_READSTAT, 1);
289                 DBG(printf("sending READSTAT request"));
290                 write(conn, &req, SIZEOF_NTPCM(req));
291                 DBG(print_ntp_control_message(&req));
292                 /* Attempt to read the largest size packet possible */
293                 req.count=htons(MAX_CM_SIZE);
294                 DBG(printf("recieving READSTAT response"))
295                 if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
296                         die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
297                 DBG(print_ntp_control_message(&req));
298                 /* discard obviously invalid packets */
299                 if (ntohs(req.count) > MAX_CM_SIZE)
300                         die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
301                 if (LI(req.flags) == LI_ALARM) li_alarm = 1;
302                 /* Each peer identifier is 4 bytes in the data section, which
303                  * we represent as a ntp_assoc_status_pair datatype.
304                  */
305                 peers_size+=ntohs(req.count);
306                 if((tmp=realloc(peers, peers_size)) == NULL)
307                         free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
308                 peers=tmp;
309                 memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
310                 npeers=peers_size/sizeof(ntp_assoc_status_pair);
311                 peer_offset+=ntohs(req.count);
312         } while(req.op&REM_MORE);
314         /* first, let's find out if we have a sync source, or if there are
315          * at least some candidates. In the latter case we'll issue
316          * a warning but go ahead with the check on them. */
317         for (i = 0; i < npeers; i++){
318                 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED){
319                         num_candidates++;
320                         if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
321                                 syncsource_found=1;
322                                 min_peer_sel=PEER_SYNCSOURCE;
323                         }
324                 }
325         }
326         if(verbose) printf("%d candiate peers available\n", num_candidates);
327         if(verbose && syncsource_found) printf("synchronization source found\n");
328         if(! syncsource_found){
329                 status = STATE_WARNING;
330                 if(verbose) printf("warning: no synchronization source found\n");
331         }
332         if(li_alarm){
333                 status = STATE_WARNING;
334                 if(verbose) printf("warning: LI_ALARM bit is set\n");
335         }
338         for (i = 0; i < npeers; i++){
339                 /* Only query this server if it is the current sync source */
340                 /* If there's no sync.peer, query all candidates and use the best one */
341                 if (PEER_SEL(peers[i].status) >= min_peer_sel){
342                         if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
343                         asprintf(&data, "");
344                         do{
345                                 setup_control_request(&req, OP_READVAR, 2);
346                                 req.assoc = peers[i].assoc;
347                                 /* Putting the wanted variable names in the request
348                                  * cause the server to provide _only_ the requested values.
349                                  * thus reducing net traffic, guaranteeing us only a single
350                                  * datagram in reply, and making intepretation much simpler
351                                  */
352                                 /* Older servers doesn't know what jitter is, so if we get an
353                                  * error on the first pass we redo it with "dispersion" */
354                                 strncpy(req.data, getvar, MAX_CM_SIZE-1);
355                                 req.count = htons(strlen(getvar));
356                                 DBG(printf("sending READVAR request...\n"));
357                                 write(conn, &req, SIZEOF_NTPCM(req));
358                                 DBG(print_ntp_control_message(&req));
360                                 req.count = htons(MAX_CM_SIZE);
361                                 DBG(printf("receiving READVAR response...\n"));
362                                 read(conn, &req, SIZEOF_NTPCM(req));
363                                 DBG(print_ntp_control_message(&req));
365                                 if(!(req.op&REM_ERROR))
366                                         asprintf(&data, "%s%s", data, req.data);
367                         } while(req.op&REM_MORE);
369                         if(req.op&REM_ERROR) {
370                                 if(strstr(getvar, "jitter")) {
371                                         if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
372                                         getvar = "stratum,offset,dispersion";
373                                         i--;
374                                         continue;
375                                 } else if(strlen(getvar)) {
376                                         if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
377                                         getvar = "";
378                                         i--;
379                                         continue;
380                                 }
381                         }
383                         if(verbose > 1)
384                                 printf("Server responded: >>>%s<<<\n", data);
386                         /* get the offset */
387                         if(verbose)
388                                 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
390                         value = extract_value(data, "offset");
391                         nptr=NULL;
392                         /* Convert the value if we have one */
393                         if(value != NULL)
394                                 tmp_offset = strtod(value, &nptr) / 1000;
395                         /* If value is null or no conversion was performed */
396                         if(value == NULL || value==nptr) {
397                                 if(verbose) printf("error: unable to read server offset response.\n");
398                         } else {
399                                 if(verbose) printf("%.10g\n", tmp_offset);
400                                 if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
401                                         *offset = tmp_offset;
402                                         *offset_result = STATE_OK;
403                                 } else {
404                                         /* Skip this one; move to the next */
405                                         continue;
406                                 }
407                         }
409                         if(do_jitter) {
410                                 /* get the jitter */
411                                 if(verbose) {
412                                         printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
413                                 }
414                                 value = extract_value(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
415                                 nptr=NULL;
416                                 /* Convert the value if we have one */
417                                 if(value != NULL)
418                                         *jitter = strtod(value, &nptr);
419                                 /* If value is null or no conversion was performed */
420                                 if(value == NULL || value==nptr) {
421                                         if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
422                                         *jitter = -1;
423                                 } else if(verbose) {
424                                         printf("%.10g\n", *jitter);
425                                 }
426                         }
428                         if(do_stratum) {
429                                 /* get the stratum */
430                                 if(verbose) {
431                                         printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
432                                 }
433                                 value = extract_value(data, "stratum");
434                                 nptr=NULL;
435                                 /* Convert the value if we have one */
436                                 if(value != NULL)
437                                         *stratum = strtol(value, &nptr, 10);
438                                 if(value == NULL || value==nptr) {
439                                         if(verbose) printf("error: unable to read server stratum response.\n");
440                                         *stratum = -1;
441                                 } else {
442                                         if(verbose) printf("%i\n", *stratum);
443                                 }
444                         }
445                 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
446         } /* for (i = 0; i < npeers; i++) */
448         close(conn);
449         if(peers!=NULL) free(peers);
451         return status;
454 int process_arguments(int argc, char **argv){
455         int c;
456         int option=0;
457         static struct option longopts[] = {
458                 {"version", no_argument, 0, 'V'},
459                 {"help", no_argument, 0, 'h'},
460                 {"verbose", no_argument, 0, 'v'},
461                 {"use-ipv4", no_argument, 0, '4'},
462                 {"use-ipv6", no_argument, 0, '6'},
463                 {"quiet", no_argument, 0, 'q'},
464                 {"warning", required_argument, 0, 'w'},
465                 {"critical", required_argument, 0, 'c'},
466                 {"swarn", required_argument, 0, 'W'},
467                 {"scrit", required_argument, 0, 'C'},
468                 {"jwarn", required_argument, 0, 'j'},
469                 {"jcrit", required_argument, 0, 'k'},
470                 {"timeout", required_argument, 0, 't'},
471                 {"hostname", required_argument, 0, 'H'},
472                 {"port", required_argument, 0, 'p'},
473                 {0, 0, 0, 0}
474         };
477         if (argc < 2)
478                 usage ("\n");
480         while (1) {
481                 c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:t:H:p:", longopts, &option);
482                 if (c == -1 || c == EOF || c == 1)
483                         break;
485                 switch (c) {
486                 case 'h':
487                         print_help();
488                         exit(STATE_OK);
489                         break;
490                 case 'V':
491                         print_revision(progname, NP_VERSION);
492                         exit(STATE_OK);
493                         break;
494                 case 'v':
495                         verbose++;
496                         break;
497                 case 'q':
498                         quiet = 1;
499                         break;
500                 case 'w':
501                         do_offset=1;
502                         owarn = optarg;
503                         break;
504                 case 'c':
505                         do_offset=1;
506                         ocrit = optarg;
507                         break;
508                 case 'W':
509                         do_stratum=1;
510                         swarn = optarg;
511                         break;
512                 case 'C':
513                         do_stratum=1;
514                         scrit = optarg;
515                         break;
516                 case 'j':
517                         do_jitter=1;
518                         jwarn = optarg;
519                         break;
520                 case 'k':
521                         do_jitter=1;
522                         jcrit = optarg;
523                         break;
524                 case 'H':
525                         if(is_host(optarg) == FALSE)
526                                 usage2(_("Invalid hostname/address"), optarg);
527                         server_address = strdup(optarg);
528                         break;
529                 case 'p':
530                         port=atoi(optarg);
531                         break;
532                 case 't':
533                         socket_timeout=atoi(optarg);
534                         break;
535                 case '4':
536                         address_family = AF_INET;
537                         break;
538                 case '6':
539 #ifdef USE_IPV6
540                         address_family = AF_INET6;
541 #else
542                         usage4 (_("IPv6 support not available"));
543 #endif
544                         break;
545                 case '?':
546                         /* print short usage statement if args not parsable */
547                         usage5 ();
548                         break;
549                 }
550         }
552         if(server_address == NULL){
553                 usage4(_("Hostname was not supplied"));
554         }
556         return 0;
559 char *perfd_offset (double offset)
561         return fperfdata ("offset", offset, "s",
562                 TRUE, offset_thresholds->warning->end,
563                 TRUE, offset_thresholds->critical->end,
564                 FALSE, 0, FALSE, 0);
567 char *perfd_jitter (double jitter)
569         return fperfdata ("jitter", jitter, "",
570                 do_jitter, jitter_thresholds->warning->end,
571                 do_jitter, jitter_thresholds->critical->end,
572                 TRUE, 0, FALSE, 0);
575 char *perfd_stratum (int stratum)
577         return perfdata ("stratum", stratum, "",
578                 do_stratum, (int)stratum_thresholds->warning->end,
579                 do_stratum, (int)stratum_thresholds->critical->end,
580                 TRUE, 0, TRUE, 16);
583 int main(int argc, char *argv[]){
584         int result, offset_result, stratum;
585         double offset=0, jitter=0;
586         char *result_line, *perfdata_line;
588         setlocale (LC_ALL, "");
589         bindtextdomain (PACKAGE, LOCALEDIR);
590         textdomain (PACKAGE);
592         /* Parse extra opts if any */
593         argv=np_extra_opts (&argc, argv, progname);
595         if (process_arguments (argc, argv) == ERROR)
596                 usage4 (_("Could not parse arguments"));
598         set_thresholds(&offset_thresholds, owarn, ocrit);
599         set_thresholds(&jitter_thresholds, jwarn, jcrit);
600         set_thresholds(&stratum_thresholds, swarn, scrit);
602         /* initialize alarm signal handling */
603         signal (SIGALRM, socket_timeout_alarm_handler);
605         /* set socket timeout */
606         alarm (socket_timeout);
608         /* This returns either OK or WARNING (See comment preceeding ntp_request) */
609         result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum);
611         if(offset_result == STATE_UNKNOWN) {
612                 /* if there's no sync peer (this overrides ntp_request output): */
613                 result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
614         } else {
615                 /* Be quiet if there's no candidates either */
616                 if (quiet == 1 && result == STATE_WARNING)
617                         result = STATE_UNKNOWN;
618                 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
619         }
621         if(do_stratum)
622                 result = max_state_alt(result, get_status(stratum, stratum_thresholds));
624         if(do_jitter)
625                 result = max_state_alt(result, get_status(jitter, jitter_thresholds));
627         switch (result) {
628                 case STATE_CRITICAL :
629                         asprintf(&result_line, _("NTP CRITICAL:"));
630                         break;
631                 case STATE_WARNING :
632                         asprintf(&result_line, _("NTP WARNING:"));
633                         break;
634                 case STATE_OK :
635                         asprintf(&result_line, _("NTP OK:"));
636                         break;
637                 default :
638                         asprintf(&result_line, _("NTP UNKNOWN:"));
639                         break;
640         }
641         if(!syncsource_found)
642                 asprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
643         else if(li_alarm)
644                 asprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
646         if(offset_result == STATE_UNKNOWN){
647                 asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
648                 asprintf(&perfdata_line, "");
649         } else {
650                 asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
651                 asprintf(&perfdata_line, "%s", perfd_offset(offset));
652         }
653         if (do_jitter) {
654                 asprintf(&result_line, "%s, jitter=%f", result_line, jitter);
655                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
656         }
657         if (do_stratum) {
658                 asprintf(&result_line, "%s, stratum=%i", result_line, stratum);
659                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
660         }
661         printf("%s|%s\n", result_line, perfdata_line);
663         if(server_address!=NULL) free(server_address);
664         return result;
669 void print_help(void){
670         print_revision(progname, NP_VERSION);
672         printf ("Copyright (c) 2006 Sean Finney\n");
673         printf (COPYRIGHT, copyright, email);
675         printf ("%s\n", _("This plugin checks the selected ntp server"));
677         printf ("\n\n");
679         print_usage();
680         printf (_(UT_HELP_VRSN));
681         printf (_(UT_EXTRA_OPTS));
682         printf (_(UT_HOST_PORT), 'p', "123");
683         printf (" %s\n", "-q, --quiet");
684         printf ("    %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
685         printf (" %s\n", "-w, --warning=THRESHOLD");
686         printf ("    %s\n", _("Offset to result in warning status (seconds)"));
687         printf (" %s\n", "-c, --critical=THRESHOLD");
688         printf ("    %s\n", _("Offset to result in critical status (seconds)"));
689         printf (" %s\n", "-W, --swarn=THRESHOLD");
690         printf ("    %s\n", _("Warning threshold for stratum"));
691         printf (" %s\n", "-C, --scrit=THRESHOLD");
692         printf ("    %s\n", _("Critical threshold for stratum"));
693         printf (" %s\n", "-j, --jwarn=THRESHOLD");
694         printf ("    %s\n", _("Warning threshold for jitter"));
695         printf (" %s\n", "-k, --jcrit=THRESHOLD");
696         printf ("    %s\n", _("Critical threshold for jitter"));
697         printf (_(UT_TIMEOUT), DEFAULT_SOCKET_TIMEOUT);
698         printf (_(UT_VERBOSE));
700         printf("\n");
701         printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
702         printf("%s\n\n", _("programs or external libraries."));
704         printf("%s\n", _("Notes:"));
705         printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
706         printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
707         printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
708         printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
709         printf("\n");
710         printf(_(UT_THRESHOLDS_NOTES));
711 #ifdef NP_EXTRA_OPTS
712         printf("\n");
713         printf(_(UT_EXTRA_OPTS_NOTES));
714 #endif
716         printf("\n");
717         printf("%s\n", _("Examples:"));
718         printf(" %s\n", _("Simple NTP server check:"));
719         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
720         printf("\n");
721         printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
722         printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
723         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
724         printf("\n");
725         printf(" %s\n", _("Check only stratum:"));
726         printf("  %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
728         printf (_(UT_SUPPORT));
731 void
732 print_usage(void)
734         printf (_("Usage:"));
735         printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
736         printf("       [-j <warn>] [-k <crit>] [-v verbose]\n");