Code

Fix translations when extra-opts aren't enabled
[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 short do_truechimers=0;
60 static char *twarn="0:";
61 static char *tcrit="0:";
62 static int syncsource_found=0;
63 static int li_alarm=0;
65 int process_arguments (int, char **);
66 thresholds *offset_thresholds = NULL;
67 thresholds *jitter_thresholds = NULL;
68 thresholds *stratum_thresholds = NULL;
69 thresholds *truechimer_thresholds = NULL;
70 void print_help (void);
71 void print_usage (void);
73 /* max size of control message data */
74 #define MAX_CM_SIZE 468
76 /* this structure holds everything in an ntp control message as per rfc1305 */
77 typedef struct {
78         uint8_t flags;       /* byte with leapindicator,vers,mode. see macros */
79         uint8_t op;          /* R,E,M bits and Opcode */
80         uint16_t seq;        /* Packet sequence */
81         uint16_t status;     /* Clock status */
82         uint16_t assoc;      /* Association */
83         uint16_t offset;     /* Similar to TCP sequence # */
84         uint16_t count;      /* # bytes of data */
85         char data[MAX_CM_SIZE]; /* ASCII data of the request */
86                                 /* NB: not necessarily NULL terminated! */
87 } ntp_control_message;
89 /* this is an association/status-word pair found in control packet reponses */
90 typedef struct {
91         uint16_t assoc;
92         uint16_t status;
93 } ntp_assoc_status_pair;
95 /* bits 1,2 are the leap indicator */
96 #define LI_MASK 0xc0
97 #define LI(x) ((x&LI_MASK)>>6)
98 #define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0)
99 /* and these are the values of the leap indicator */
100 #define LI_NOWARNING 0x00
101 #define LI_EXTRASEC 0x01
102 #define LI_MISSINGSEC 0x02
103 #define LI_ALARM 0x03
104 /* bits 3,4,5 are the ntp version */
105 #define VN_MASK 0x38
106 #define VN(x)   ((x&VN_MASK)>>3)
107 #define VN_SET(x,y)     do{ x |= ((y<<3)&VN_MASK); }while(0)
108 #define VN_RESERVED 0x02
109 /* bits 6,7,8 are the ntp mode */
110 #define MODE_MASK 0x07
111 #define MODE(x) (x&MODE_MASK)
112 #define MODE_SET(x,y)   do{ x |= (y&MODE_MASK); }while(0)
113 /* here are some values */
114 #define MODE_CLIENT 0x03
115 #define MODE_CONTROLMSG 0x06
116 /* In control message, bits 8-10 are R,E,M bits */
117 #define REM_MASK 0xe0
118 #define REM_RESP 0x80
119 #define REM_ERROR 0x40
120 #define REM_MORE 0x20
121 /* In control message, bits 11 - 15 are opcode */
122 #define OP_MASK 0x1f
123 #define OP_SET(x,y)   do{ x |= (y&OP_MASK); }while(0)
124 #define OP_READSTAT 0x01
125 #define OP_READVAR  0x02
126 /* In peer status bytes, bits 6,7,8 determine clock selection status */
127 #define PEER_SEL(x) ((ntohs(x)>>8)&0x07)
128 #define PEER_TRUECHIMER 0x02
129 #define PEER_INCLUDED 0x04
130 #define PEER_SYNCSOURCE 0x06
132 /* NTP control message header is 12 bytes, plus any data in the data
133  * field, plus null padding to the nearest 32-bit boundary per rfc.
134  */
135 #define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0))
137 /* finally, a little helper or two for debugging: */
138 #define DBG(x) do{if(verbose>1){ x; }}while(0);
139 #define PRINTSOCKADDR(x) \
140         do{ \
141                 printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\
142         }while(0);
144 void print_ntp_control_message(const ntp_control_message *p){
145         int i=0, numpeers=0;
146         const ntp_assoc_status_pair *peer=NULL;
148         printf("control packet contents:\n");
149         printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op);
150         printf("\t  li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK);
151         printf("\t  vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK);
152         printf("\t  mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK);
153         printf("\t  response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP);
154         printf("\t  more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE);
155         printf("\t  error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR);
156         printf("\t  op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK);
157         printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq));
158         printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status));
159         printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc));
160         printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset));
161         printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count));
162         numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair));
163         if(p->op&REM_RESP && p->op&OP_READSTAT){
164                 peer=(ntp_assoc_status_pair*)p->data;
165                 for(i=0;i<numpeers;i++){
166                         printf("\tpeer id %.2x status %.2x",
167                                ntohs(peer[i].assoc), ntohs(peer[i].status));
168                         if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){
169                                 printf(" <-- current sync source");
170                         } else if(PEER_SEL(peer[i].status) >= PEER_INCLUDED){
171                                 printf(" <-- current sync candidate");
172                         } else if(PEER_SEL(peer[i].status) >= PEER_TRUECHIMER){
173                                 printf(" <-- outlyer, but truechimer");
174                         }
175                         printf("\n");
176                 }
177         }
180 void
181 setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
182         memset(p, 0, sizeof(ntp_control_message));
183         LI_SET(p->flags, LI_NOWARNING);
184         VN_SET(p->flags, VN_RESERVED);
185         MODE_SET(p->flags, MODE_CONTROLMSG);
186         OP_SET(p->op, opcode);
187         p->seq = htons(seq);
188         /* Remaining fields are zero for requests */
191 /* This function does all the actual work; roughly here's what it does
192  * beside setting the offest, jitter and stratum passed as argument:
193  *  - offset can be negative, so if it cannot get the offset, offset_result
194  *    is set to UNKNOWN, otherwise OK.
195  *  - jitter and stratum are set to -1 if they cannot be retrieved so any
196  *    positive value means a success retrieving the value.
197  *  - status is set to WARNING if there's no sync.peer (otherwise OK) and is
198  *    the return value of the function.
199  *  status is pretty much useless as syncsource_found is a global variable
200  *  used later in main to check is the server was synchronized. It works
201  *  so I left it alone */
202 int ntp_request(const char *host, double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers){
203         int conn=-1, i, npeers=0, num_candidates=0;
204         double tmp_offset = 0;
205         int min_peer_sel=PEER_INCLUDED;
206         int peers_size=0, peer_offset=0;
207         int status;
208         ntp_assoc_status_pair *peers=NULL;
209         ntp_control_message req;
210         const char *getvar = "stratum,offset,jitter";
211         char *data, *value, *nptr;
212         void *tmp;
214         status = STATE_OK;
215         *offset_result = STATE_UNKNOWN;
216         *jitter = *stratum = -1;
217         *num_truechimers = 0;
219         /* Long-winded explanation:
220          * Getting the sync peer offset, jitter and stratum requires a number of
221          * steps:
222          * 1) Send a READSTAT request.
223          * 2) Interpret the READSTAT reply
224          *  a) The data section contains a list of peer identifiers (16 bits)
225          *     and associated status words (16 bits)
226          *  b) We want the value of 0x06 in the SEL (peer selection) value,
227          *     which means "current synchronizatin source".  If that's missing,
228          *     we take anything better than 0x04 (see the rfc for details) but
229          *     set a minimum of warning.
230          * 3) Send a READVAR request for information on each peer identified
231          *    in 2b greater than the minimum selection value.
232          * 4) Extract the offset, jitter and stratum value from the data[]
233          *    (it's ASCII)
234          */
235         my_udp_connect(server_address, port, &conn);
237         /* keep sending requests until the server stops setting the
238          * REM_MORE bit, though usually this is only 1 packet. */
239         do{
240                 setup_control_request(&req, OP_READSTAT, 1);
241                 DBG(printf("sending READSTAT request"));
242                 write(conn, &req, SIZEOF_NTPCM(req));
243                 DBG(print_ntp_control_message(&req));
244                 /* Attempt to read the largest size packet possible */
245                 req.count=htons(MAX_CM_SIZE);
246                 DBG(printf("recieving READSTAT response"))
247                 if(read(conn, &req, SIZEOF_NTPCM(req)) == -1)
248                         die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
249                 DBG(print_ntp_control_message(&req));
250                 /* discard obviously invalid packets */
251                 if (ntohs(req.count) > MAX_CM_SIZE)
252                         die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
253                 if (LI(req.flags) == LI_ALARM) li_alarm = 1;
254                 /* Each peer identifier is 4 bytes in the data section, which
255                  * we represent as a ntp_assoc_status_pair datatype.
256                  */
257                 peers_size+=ntohs(req.count);
258                 if((tmp=realloc(peers, peers_size)) == NULL)
259                         free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
260                 peers=tmp;
261                 memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count));
262                 npeers=peers_size/sizeof(ntp_assoc_status_pair);
263                 peer_offset+=ntohs(req.count);
264         } while(req.op&REM_MORE);
266         /* first, let's find out if we have a sync source, or if there are
267          * at least some candidates. In the latter case we'll issue
268          * a warning but go ahead with the check on them. */
269         for (i = 0; i < npeers; i++){
270                 if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){
271                         (*num_truechimers)++;
272                         if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){
273                                 num_candidates++;
274                                 if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){
275                                         syncsource_found=1;
276                                         min_peer_sel=PEER_SYNCSOURCE;
277                                 }
278                         }
279                 }
280         }
281         if(verbose) printf("%d candidate peers available\n", num_candidates);
282         if(verbose && syncsource_found) printf("synchronization source found\n");
283         if(! syncsource_found){
284                 status = STATE_WARNING;
285                 if(verbose) printf("warning: no synchronization source found\n");
286         }
287         if(li_alarm){
288                 status = STATE_WARNING;
289                 if(verbose) printf("warning: LI_ALARM bit is set\n");
290         }
293         for (i = 0; i < npeers; i++){
294                 /* Only query this server if it is the current sync source */
295                 /* If there's no sync.peer, query all candidates and use the best one */
296                 if (PEER_SEL(peers[i].status) >= min_peer_sel){
297                         if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
298                         asprintf(&data, "");
299                         do{
300                                 setup_control_request(&req, OP_READVAR, 2);
301                                 req.assoc = peers[i].assoc;
302                                 /* Putting the wanted variable names in the request
303                                  * cause the server to provide _only_ the requested values.
304                                  * thus reducing net traffic, guaranteeing us only a single
305                                  * datagram in reply, and making intepretation much simpler
306                                  */
307                                 /* Older servers doesn't know what jitter is, so if we get an
308                                  * error on the first pass we redo it with "dispersion" */
309                                 strncpy(req.data, getvar, MAX_CM_SIZE-1);
310                                 req.count = htons(strlen(getvar));
311                                 DBG(printf("sending READVAR request...\n"));
312                                 write(conn, &req, SIZEOF_NTPCM(req));
313                                 DBG(print_ntp_control_message(&req));
315                                 req.count = htons(MAX_CM_SIZE);
316                                 DBG(printf("receiving READVAR response...\n"));
317                                 read(conn, &req, SIZEOF_NTPCM(req));
318                                 DBG(print_ntp_control_message(&req));
320                                 if(!(req.op&REM_ERROR))
321                                         asprintf(&data, "%s%s", data, req.data);
322                         } while(req.op&REM_MORE);
324                         if(req.op&REM_ERROR) {
325                                 if(strstr(getvar, "jitter")) {
326                                         if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n");
327                                         getvar = "stratum,offset,dispersion";
328                                         i--;
329                                         continue;
330                                 } else if(strlen(getvar)) {
331                                         if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n");
332                                         getvar = "";
333                                         i--;
334                                         continue;
335                                 }
336                         }
338                         if(verbose > 1)
339                                 printf("Server responded: >>>%s<<<\n", data);
341                         /* get the offset */
342                         if(verbose)
343                                 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
345                         value = np_extract_ntpvar(data, "offset");
346                         nptr=NULL;
347                         /* Convert the value if we have one */
348                         if(value != NULL)
349                                 tmp_offset = strtod(value, &nptr) / 1000;
350                         /* If value is null or no conversion was performed */
351                         if(value == NULL || value==nptr) {
352                                 if(verbose) printf("error: unable to read server offset response.\n");
353                         } else {
354                                 if(verbose) printf("%.10g\n", tmp_offset);
355                                 if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) {
356                                         *offset = tmp_offset;
357                                         *offset_result = STATE_OK;
358                                 } else {
359                                         /* Skip this one; move to the next */
360                                         continue;
361                                 }
362                         }
364                         if(do_jitter) {
365                                 /* get the jitter */
366                                 if(verbose) {
367                                         printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc));
368                                 }
369                                 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter");
370                                 nptr=NULL;
371                                 /* Convert the value if we have one */
372                                 if(value != NULL)
373                                         *jitter = strtod(value, &nptr);
374                                 /* If value is null or no conversion was performed */
375                                 if(value == NULL || value==nptr) {
376                                         if(verbose) printf("error: unable to read server jitter/dispersion response.\n");
377                                         *jitter = -1;
378                                 } else if(verbose) {
379                                         printf("%.10g\n", *jitter);
380                                 }
381                         }
383                         if(do_stratum) {
384                                 /* get the stratum */
385                                 if(verbose) {
386                                         printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
387                                 }
388                                 value = np_extract_ntpvar(data, "stratum");
389                                 nptr=NULL;
390                                 /* Convert the value if we have one */
391                                 if(value != NULL)
392                                         *stratum = strtol(value, &nptr, 10);
393                                 if(value == NULL || value==nptr) {
394                                         if(verbose) printf("error: unable to read server stratum response.\n");
395                                         *stratum = -1;
396                                 } else {
397                                         if(verbose) printf("%i\n", *stratum);
398                                 }
399                         }
400                 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
401         } /* for (i = 0; i < npeers; i++) */
403         close(conn);
404         if(peers!=NULL) free(peers);
406         return status;
409 int process_arguments(int argc, char **argv){
410         int c;
411         int option=0;
412         static struct option longopts[] = {
413                 {"version", no_argument, 0, 'V'},
414                 {"help", no_argument, 0, 'h'},
415                 {"verbose", no_argument, 0, 'v'},
416                 {"use-ipv4", no_argument, 0, '4'},
417                 {"use-ipv6", no_argument, 0, '6'},
418                 {"quiet", no_argument, 0, 'q'},
419                 {"warning", required_argument, 0, 'w'},
420                 {"critical", required_argument, 0, 'c'},
421                 {"swarn", required_argument, 0, 'W'},
422                 {"scrit", required_argument, 0, 'C'},
423                 {"jwarn", required_argument, 0, 'j'},
424                 {"jcrit", required_argument, 0, 'k'},
425                 {"twarn", required_argument, 0, 'm'},
426                 {"tcrit", required_argument, 0, 'n'},
427                 {"timeout", required_argument, 0, 't'},
428                 {"hostname", required_argument, 0, 'H'},
429                 {"port", required_argument, 0, 'p'},
430                 {0, 0, 0, 0}
431         };
434         if (argc < 2)
435                 usage ("\n");
437         while (1) {
438                 c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option);
439                 if (c == -1 || c == EOF || c == 1)
440                         break;
442                 switch (c) {
443                 case 'h':
444                         print_help();
445                         exit(STATE_OK);
446                         break;
447                 case 'V':
448                         print_revision(progname, NP_VERSION);
449                         exit(STATE_OK);
450                         break;
451                 case 'v':
452                         verbose++;
453                         break;
454                 case 'q':
455                         quiet = 1;
456                         break;
457                 case 'w':
458                         do_offset=1;
459                         owarn = optarg;
460                         break;
461                 case 'c':
462                         do_offset=1;
463                         ocrit = optarg;
464                         break;
465                 case 'W':
466                         do_stratum=1;
467                         swarn = optarg;
468                         break;
469                 case 'C':
470                         do_stratum=1;
471                         scrit = optarg;
472                         break;
473                 case 'j':
474                         do_jitter=1;
475                         jwarn = optarg;
476                         break;
477                 case 'k':
478                         do_jitter=1;
479                         jcrit = optarg;
480                         break;
481                 case 'm':
482                         do_truechimers=1;
483                         twarn = optarg;
484                         break;
485                 case 'n':
486                         do_truechimers=1;
487                         tcrit = optarg;
488                         break;
489                 case 'H':
490                         if(is_host(optarg) == FALSE)
491                                 usage2(_("Invalid hostname/address"), optarg);
492                         server_address = strdup(optarg);
493                         break;
494                 case 'p':
495                         port=atoi(optarg);
496                         break;
497                 case 't':
498                         socket_timeout=atoi(optarg);
499                         break;
500                 case '4':
501                         address_family = AF_INET;
502                         break;
503                 case '6':
504 #ifdef USE_IPV6
505                         address_family = AF_INET6;
506 #else
507                         usage4 (_("IPv6 support not available"));
508 #endif
509                         break;
510                 case '?':
511                         /* print short usage statement if args not parsable */
512                         usage5 ();
513                         break;
514                 }
515         }
517         if(server_address == NULL){
518                 usage4(_("Hostname was not supplied"));
519         }
521         return 0;
524 char *perfd_offset (double offset)
526         return fperfdata ("offset", offset, "s",
527                 TRUE, offset_thresholds->warning->end,
528                 TRUE, offset_thresholds->critical->end,
529                 FALSE, 0, FALSE, 0);
532 char *perfd_jitter (double jitter)
534         return fperfdata ("jitter", jitter, "",
535                 do_jitter, jitter_thresholds->warning->end,
536                 do_jitter, jitter_thresholds->critical->end,
537                 TRUE, 0, FALSE, 0);
540 char *perfd_stratum (int stratum)
542         return perfdata ("stratum", stratum, "",
543                 do_stratum, (int)stratum_thresholds->warning->end,
544                 do_stratum, (int)stratum_thresholds->critical->end,
545                 TRUE, 0, TRUE, 16);
548 char *perfd_truechimers (int num_truechimers)
550         return perfdata ("truechimers", num_truechimers, "",
551                 do_truechimers, (int)truechimer_thresholds->warning->end,
552                 do_truechimers, (int)truechimer_thresholds->critical->end,
553                 TRUE, 0, FALSE, 0);
556 int main(int argc, char *argv[]){
557         int result, offset_result, stratum, num_truechimers;
558         double offset=0, jitter=0;
559         char *result_line, *perfdata_line;
561         setlocale (LC_ALL, "");
562         bindtextdomain (PACKAGE, LOCALEDIR);
563         textdomain (PACKAGE);
565         /* Parse extra opts if any */
566         argv=np_extra_opts (&argc, argv, progname);
568         if (process_arguments (argc, argv) == ERROR)
569                 usage4 (_("Could not parse arguments"));
571         set_thresholds(&offset_thresholds, owarn, ocrit);
572         set_thresholds(&jitter_thresholds, jwarn, jcrit);
573         set_thresholds(&stratum_thresholds, swarn, scrit);
574         set_thresholds(&truechimer_thresholds, twarn, tcrit);
576         /* initialize alarm signal handling */
577         signal (SIGALRM, socket_timeout_alarm_handler);
579         /* set socket timeout */
580         alarm (socket_timeout);
582         /* This returns either OK or WARNING (See comment preceeding ntp_request) */
583         result = ntp_request(server_address, &offset, &offset_result, &jitter, &stratum, &num_truechimers);
585         if(offset_result == STATE_UNKNOWN) {
586                 /* if there's no sync peer (this overrides ntp_request output): */
587                 result = (quiet == 1 ? STATE_UNKNOWN : STATE_CRITICAL);
588         } else {
589                 /* Be quiet if there's no candidates either */
590                 if (quiet == 1 && result == STATE_WARNING)
591                         result = STATE_UNKNOWN;
592                 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds));
593         }
595         if(do_truechimers)
596                 result = max_state_alt(result, get_status(num_truechimers, truechimer_thresholds));
598         if(do_stratum)
599                 result = max_state_alt(result, get_status(stratum, stratum_thresholds));
601         if(do_jitter)
602                 result = max_state_alt(result, get_status(jitter, jitter_thresholds));
604         switch (result) {
605                 case STATE_CRITICAL :
606                         asprintf(&result_line, _("NTP CRITICAL:"));
607                         break;
608                 case STATE_WARNING :
609                         asprintf(&result_line, _("NTP WARNING:"));
610                         break;
611                 case STATE_OK :
612                         asprintf(&result_line, _("NTP OK:"));
613                         break;
614                 default :
615                         asprintf(&result_line, _("NTP UNKNOWN:"));
616                         break;
617         }
618         if(!syncsource_found)
619                 asprintf(&result_line, "%s %s,", result_line, _("Server not synchronized"));
620         else if(li_alarm)
621                 asprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set"));
623         if(offset_result == STATE_UNKNOWN){
624                 asprintf(&result_line, "%s %s", result_line, _("Offset unknown"));
625                 asprintf(&perfdata_line, "");
626         } else {
627                 asprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
628                 asprintf(&perfdata_line, "%s", perfd_offset(offset));
629         }
630         if (do_jitter) {
631                 asprintf(&result_line, "%s, jitter=%f", result_line, jitter);
632                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
633         }
634         if (do_stratum) {
635                 asprintf(&result_line, "%s, stratum=%i", result_line, stratum);
636                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum));
637         }
638         if (do_truechimers) {
639                 asprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
640                 asprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
641         }
642         printf("%s|%s\n", result_line, perfdata_line);
644         if(server_address!=NULL) free(server_address);
645         return result;
650 void print_help(void){
651         print_revision(progname, NP_VERSION);
653         printf ("Copyright (c) 2006 Sean Finney\n");
654         printf (COPYRIGHT, copyright, email);
656         printf ("%s\n", _("This plugin checks the selected ntp server"));
658         printf ("\n\n");
660         print_usage();
661         printf (UT_HELP_VRSN);
662         printf (UT_EXTRA_OPTS);
663         printf (UT_HOST_PORT, 'p', "123");
664         printf (" %s\n", "-q, --quiet");
665         printf ("    %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
666         printf (" %s\n", "-w, --warning=THRESHOLD");
667         printf ("    %s\n", _("Offset to result in warning status (seconds)"));
668         printf (" %s\n", "-c, --critical=THRESHOLD");
669         printf ("    %s\n", _("Offset to result in critical status (seconds)"));
670         printf (" %s\n", "-W, --swarn=THRESHOLD");
671         printf ("    %s\n", _("Warning threshold for stratum"));
672         printf (" %s\n", "-C, --scrit=THRESHOLD");
673         printf ("    %s\n", _("Critical threshold for stratum"));
674         printf (" %s\n", "-j, --jwarn=THRESHOLD");
675         printf ("    %s\n", _("Warning threshold for jitter"));
676         printf (" %s\n", "-k, --jcrit=THRESHOLD");
677         printf ("    %s\n", _("Critical threshold for jitter"));
678         printf (" %s\n", "-m, --twarn=THRESHOLD");
679         printf ("    %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
680         printf (" %s\n", "-n, --tcrit=THRESHOLD");
681         printf ("    %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
682         printf (UT_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
683         printf (UT_VERBOSE);
685         printf("\n");
686         printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
687         printf("%s\n\n", _("programs or external libraries."));
689         printf("%s\n", _("Notes:"));
690         printf(" %s\n", _("Use this plugin to check the health of an NTP server. It supports"));
691         printf(" %s\n", _("checking the offset with the sync peer, the jitter and stratum. This"));
692         printf(" %s\n", _("plugin will not check the clock offset between the local host and NTP"));
693         printf(" %s\n", _("server; please use check_ntp_time for that purpose."));
694         printf("\n");
695         printf(UT_THRESHOLDS_NOTES);
696 #ifdef NP_EXTRA_OPTS
697         printf("\n");
698         printf(UT_EXTRA_OPTS_NOTES);
699 #endif
701         printf("\n");
702         printf("%s\n", _("Examples:"));
703         printf(" %s\n", _("Simple NTP server check:"));
704         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
705         printf("\n");
706         printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available"));
707         printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
708         printf("  %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
709         printf("\n");
710         printf(" %s\n", _("Only check the number of usable time sources (\"truechimers\"):"));
711         printf("  %s\n", ("./check_ntp_peer -H ntpserv -m :5 -n :3"));
712         printf("\n");
713         printf(" %s\n", _("Check only stratum:"));
714         printf("  %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
716         printf (UT_SUPPORT);
719 void
720 print_usage(void)
722         printf (_("Usage:"));
723         printf(" %s -H <host> [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
724         printf("       [-j <warn>] [-k <crit>] [-v verbose]\n");