summaryrefslogtreecommitdiffstats
path: root/plugins/check_ntp_peer.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ntp_peer.c')
-rw-r--r--plugins/check_ntp_peer.c1027
1 files changed, 579 insertions, 448 deletions
diff --git a/plugins/check_ntp_peer.c b/plugins/check_ntp_peer.c
index 464a9e10..26f74286 100644
--- a/plugins/check_ntp_peer.c
+++ b/plugins/check_ntp_peer.c
@@ -1,88 +1,78 @@
1/***************************************************************************** 1/*****************************************************************************
2* 2 *
3* Monitoring check_ntp_peer plugin 3 * Monitoring check_ntp_peer plugin
4* 4 *
5* License: GPL 5 * License: GPL
6* Copyright (c) 2006 Sean Finney <seanius@seanius.net> 6 * Copyright (c) 2006 Sean Finney <seanius@seanius.net>
7* Copyright (c) 2006-2008 Monitoring Plugins Development Team 7 * Copyright (c) 2006-2024 Monitoring Plugins Development Team
8* 8 *
9* Description: 9 * Description:
10* 10 *
11* This file contains the check_ntp_peer plugin 11 * This file contains the check_ntp_peer plugin
12* 12 *
13* This plugin checks an NTP server independent of any commandline 13 * This plugin checks an NTP server independent of any commandline
14* programs or external libraries. 14 * programs or external libraries.
15* 15 *
16* Use this plugin to check the health of an NTP server. It supports 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 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 18 * plugin will not check the clock offset between the local host and NTP
19* server; please use check_ntp_time for that purpose. 19 * server; please use check_ntp_time for that purpose.
20* 20 *
21* 21 *
22* This program is free software: you can redistribute it and/or modify 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 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 24 * the Free Software Foundation, either version 3 of the License, or
25* (at your option) any later version. 25 * (at your option) any later version.
26* 26 *
27* This program is distributed in the hope that it will be useful, 27 * This program is distributed in the hope that it will be useful,
28* but WITHOUT ANY WARRANTY; without even the implied warranty of 28 * but WITHOUT ANY WARRANTY; without even the implied warranty of
29* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 29 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
30* GNU General Public License for more details. 30 * GNU General Public License for more details.
31* 31 *
32* You should have received a copy of the GNU General Public License 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/>. 33 * along with this program. If not, see <http://www.gnu.org/licenses/>.
34* 34 *
35* 35 *
36*****************************************************************************/ 36 *****************************************************************************/
37 37
38const char *progname = "check_ntp_peer"; 38const char *progname = "check_ntp_peer";
39const char *copyright = "2006-2008"; 39const char *copyright = "2006-2024";
40const char *email = "devel@monitoring-plugins.org"; 40const char *email = "devel@monitoring-plugins.org";
41 41
42#include "output.h"
43#include "perfdata.h"
44#include <openssl/x509.h>
45#include "thresholds.h"
42#include "common.h" 46#include "common.h"
43#include "netutils.h" 47#include "netutils.h"
44#include "utils.h" 48#include "utils.h"
49#include "../lib/states.h"
50#include "check_ntp_peer.d/config.h"
45 51
46static char *server_address=NULL; 52static int verbose = 0;
47static int port=123; 53
48static int verbose=0; 54typedef struct {
49static bool quiet = false; 55 int errorcode;
50static char *owarn="60"; 56 check_ntp_peer_config config;
51static char *ocrit="120"; 57} check_ntp_peer_config_wrapper;
52static bool do_stratum = false; 58static check_ntp_peer_config_wrapper process_arguments(int /*argc*/, char ** /*argv*/);
53static char *swarn="-1:16"; 59static void print_help(void);
54static char *scrit="-1:16"; 60void print_usage(void);
55static bool do_jitter = false;
56static char *jwarn="-1:5000";
57static char *jcrit="-1:10000";
58static bool do_truechimers = false;
59static char *twarn="0:";
60static char *tcrit="0:";
61static bool syncsource_found = false;
62static bool li_alarm = false;
63
64int process_arguments (int, char **);
65thresholds *offset_thresholds = NULL;
66thresholds *jitter_thresholds = NULL;
67thresholds *stratum_thresholds = NULL;
68thresholds *truechimer_thresholds = NULL;
69void print_help (void);
70void print_usage (void);
71 61
72/* max size of control message data */ 62/* max size of control message data */
73#define MAX_CM_SIZE 468 63#define MAX_CM_SIZE 468
74 64
75/* this structure holds everything in an ntp control message as per rfc1305 */ 65/* this structure holds everything in an ntp control message as per rfc1305 */
76typedef struct { 66typedef struct {
77 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */ 67 uint8_t flags; /* byte with leapindicator,vers,mode. see macros */
78 uint8_t op; /* R,E,M bits and Opcode */ 68 uint8_t op; /* R,E,M bits and Opcode */
79 uint16_t seq; /* Packet sequence */ 69 uint16_t seq; /* Packet sequence */
80 uint16_t status; /* Clock status */ 70 uint16_t status; /* Clock status */
81 uint16_t assoc; /* Association */ 71 uint16_t assoc; /* Association */
82 uint16_t offset; /* Similar to TCP sequence # */ 72 uint16_t offset; /* Similar to TCP sequence # */
83 uint16_t count; /* # bytes of data */ 73 uint16_t count; /* # bytes of data */
84 char data[MAX_CM_SIZE]; /* ASCII data of the request */ 74 char data[MAX_CM_SIZE]; /* ASCII data of the request */
85 /* NB: not necessarily NULL terminated! */ 75 /* NB: not necessarily NULL terminated! */
86} ntp_control_message; 76} ntp_control_message;
87 77
88/* this is an association/status-word pair found in control packet responses */ 78/* this is an association/status-word pair found in control packet responses */
@@ -93,82 +83,97 @@ typedef struct {
93 83
94/* bits 1,2 are the leap indicator */ 84/* bits 1,2 are the leap indicator */
95#define LI_MASK 0xc0 85#define LI_MASK 0xc0
96#define LI(x) ((x&LI_MASK)>>6) 86#define LI(x) ((x & LI_MASK) >> 6)
97#define LI_SET(x,y) do{ x |= ((y<<6)&LI_MASK); }while(0) 87#define LI_SET(x, y) \
88 do { \
89 x |= ((y << 6) & LI_MASK); \
90 } while (0)
98/* and these are the values of the leap indicator */ 91/* and these are the values of the leap indicator */
99#define LI_NOWARNING 0x00 92#define LI_NOWARNING 0x00
100#define LI_EXTRASEC 0x01 93#define LI_EXTRASEC 0x01
101#define LI_MISSINGSEC 0x02 94#define LI_MISSINGSEC 0x02
102#define LI_ALARM 0x03 95#define LI_ALARM 0x03
103/* bits 3,4,5 are the ntp version */ 96/* bits 3,4,5 are the ntp version */
104#define VN_MASK 0x38 97#define VN_MASK 0x38
105#define VN(x) ((x&VN_MASK)>>3) 98#define VN(x) ((x & VN_MASK) >> 3)
106#define VN_SET(x,y) do{ x |= ((y<<3)&VN_MASK); }while(0) 99#define VN_SET(x, y) \
100 do { \
101 x |= ((y << 3) & VN_MASK); \
102 } while (0)
107#define VN_RESERVED 0x02 103#define VN_RESERVED 0x02
108/* bits 6,7,8 are the ntp mode */ 104/* bits 6,7,8 are the ntp mode */
109#define MODE_MASK 0x07 105#define MODE_MASK 0x07
110#define MODE(x) (x&MODE_MASK) 106#define MODE(x) (x & MODE_MASK)
111#define MODE_SET(x,y) do{ x |= (y&MODE_MASK); }while(0) 107#define MODE_SET(x, y) \
108 do { \
109 x |= (y & MODE_MASK); \
110 } while (0)
112/* here are some values */ 111/* here are some values */
113#define MODE_CLIENT 0x03 112#define MODE_CLIENT 0x03
114#define MODE_CONTROLMSG 0x06 113#define MODE_CONTROLMSG 0x06
115/* In control message, bits 8-10 are R,E,M bits */ 114/* In control message, bits 8-10 are R,E,M bits */
116#define REM_MASK 0xe0 115#define REM_MASK 0xe0
117#define REM_RESP 0x80 116#define REM_RESP 0x80
118#define REM_ERROR 0x40 117#define REM_ERROR 0x40
119#define REM_MORE 0x20 118#define REM_MORE 0x20
120/* In control message, bits 11 - 15 are opcode */ 119/* In control message, bits 11 - 15 are opcode */
121#define OP_MASK 0x1f 120#define OP_MASK 0x1f
122#define OP_SET(x,y) do{ x |= (y&OP_MASK); }while(0) 121#define OP_SET(x, y) \
122 do { \
123 x |= (y & OP_MASK); \
124 } while (0)
123#define OP_READSTAT 0x01 125#define OP_READSTAT 0x01
124#define OP_READVAR 0x02 126#define OP_READVAR 0x02
125/* In peer status bytes, bits 6,7,8 determine clock selection status */ 127/* In peer status bytes, bits 6,7,8 determine clock selection status */
126#define PEER_SEL(x) ((ntohs(x)>>8)&0x07) 128#define PEER_SEL(x) ((ntohs(x) >> 8) & 0x07)
127#define PEER_TRUECHIMER 0x02 129#define PEER_TRUECHIMER 0x02
128#define PEER_INCLUDED 0x04 130#define PEER_INCLUDED 0x04
129#define PEER_SYNCSOURCE 0x06 131#define PEER_SYNCSOURCE 0x06
130 132
131/* NTP control message header is 12 bytes, plus any data in the data 133/* NTP control message header is 12 bytes, plus any data in the data
132 * field, plus null padding to the nearest 32-bit boundary per rfc. 134 * field, plus null padding to the nearest 32-bit boundary per rfc.
133 */ 135 */
134#define SIZEOF_NTPCM(m) (12+ntohs(m.count)+((ntohs(m.count)%4)?4-(ntohs(m.count)%4):0)) 136#define SIZEOF_NTPCM(m) \
137 (12 + ntohs(m.count) + ((ntohs(m.count) % 4) ? 4 - (ntohs(m.count) % 4) : 0))
135 138
136/* finally, a little helper or two for debugging: */ 139/* finally, a little helper or two for debugging: */
137#define DBG(x) do{if(verbose>1){ x; }}while(0); 140#define DBG(x) \
138#define PRINTSOCKADDR(x) \ 141 do { \
139 do{ \ 142 if (verbose > 1) { \
140 printf("%u.%u.%u.%u", (x>>24)&0xff, (x>>16)&0xff, (x>>8)&0xff, x&0xff);\ 143 x; \
141 }while(0); 144 } \
142 145 } while (0);
143void print_ntp_control_message(const ntp_control_message *p){ 146#define PRINTSOCKADDR(x) \
144 int i=0, numpeers=0; 147 do { \
145 const ntp_assoc_status_pair *peer=NULL; 148 printf("%u.%u.%u.%u", (x >> 24) & 0xff, (x >> 16) & 0xff, (x >> 8) & 0xff, x & 0xff); \
146 149 } while (0);
150
151void print_ntp_control_message(const ntp_control_message *message) {
147 printf("control packet contents:\n"); 152 printf("control packet contents:\n");
148 printf("\tflags: 0x%.2x , 0x%.2x\n", p->flags, p->op); 153 printf("\tflags: 0x%.2x , 0x%.2x\n", message->flags, message->op);
149 printf("\t li=%d (0x%.2x)\n", LI(p->flags), p->flags&LI_MASK); 154 printf("\t li=%d (0x%.2x)\n", LI(message->flags), message->flags & LI_MASK);
150 printf("\t vn=%d (0x%.2x)\n", VN(p->flags), p->flags&VN_MASK); 155 printf("\t vn=%d (0x%.2x)\n", VN(message->flags), message->flags & VN_MASK);
151 printf("\t mode=%d (0x%.2x)\n", MODE(p->flags), p->flags&MODE_MASK); 156 printf("\t mode=%d (0x%.2x)\n", MODE(message->flags), message->flags & MODE_MASK);
152 printf("\t response=%d (0x%.2x)\n", (p->op&REM_RESP)>0, p->op&REM_RESP); 157 printf("\t response=%d (0x%.2x)\n", (message->op & REM_RESP) > 0, message->op & REM_RESP);
153 printf("\t more=%d (0x%.2x)\n", (p->op&REM_MORE)>0, p->op&REM_MORE); 158 printf("\t more=%d (0x%.2x)\n", (message->op & REM_MORE) > 0, message->op & REM_MORE);
154 printf("\t error=%d (0x%.2x)\n", (p->op&REM_ERROR)>0, p->op&REM_ERROR); 159 printf("\t error=%d (0x%.2x)\n", (message->op & REM_ERROR) > 0, message->op & REM_ERROR);
155 printf("\t op=%d (0x%.2x)\n", p->op&OP_MASK, p->op&OP_MASK); 160 printf("\t op=%d (0x%.2x)\n", message->op & OP_MASK, message->op & OP_MASK);
156 printf("\tsequence: %d (0x%.2x)\n", ntohs(p->seq), ntohs(p->seq)); 161 printf("\tsequence: %d (0x%.2x)\n", ntohs(message->seq), ntohs(message->seq));
157 printf("\tstatus: %d (0x%.2x)\n", ntohs(p->status), ntohs(p->status)); 162 printf("\tstatus: %d (0x%.2x)\n", ntohs(message->status), ntohs(message->status));
158 printf("\tassoc: %d (0x%.2x)\n", ntohs(p->assoc), ntohs(p->assoc)); 163 printf("\tassoc: %d (0x%.2x)\n", ntohs(message->assoc), ntohs(message->assoc));
159 printf("\toffset: %d (0x%.2x)\n", ntohs(p->offset), ntohs(p->offset)); 164 printf("\toffset: %d (0x%.2x)\n", ntohs(message->offset), ntohs(message->offset));
160 printf("\tcount: %d (0x%.2x)\n", ntohs(p->count), ntohs(p->count)); 165 printf("\tcount: %d (0x%.2x)\n", ntohs(message->count), ntohs(message->count));
161 numpeers=ntohs(p->count)/(sizeof(ntp_assoc_status_pair)); 166
162 if(p->op&REM_RESP && p->op&OP_READSTAT){ 167 int numpeers = ntohs(message->count) / (sizeof(ntp_assoc_status_pair));
163 peer=(ntp_assoc_status_pair*)p->data; 168 if (message->op & REM_RESP && message->op & OP_READSTAT) {
164 for(i=0;i<numpeers;i++){ 169 const ntp_assoc_status_pair *peer = (ntp_assoc_status_pair *)message->data;
165 printf("\tpeer id %.2x status %.2x", 170 for (int i = 0; i < numpeers; i++) {
166 ntohs(peer[i].assoc), ntohs(peer[i].status)); 171 printf("\tpeer id %.2x status %.2x", ntohs(peer[i].assoc), ntohs(peer[i].status));
167 if(PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE){ 172 if (PEER_SEL(peer[i].status) >= PEER_SYNCSOURCE) {
168 printf(" <-- current sync source"); 173 printf(" <-- current sync source");
169 } else if(PEER_SEL(peer[i].status) >= PEER_INCLUDED){ 174 } else if (PEER_SEL(peer[i].status) >= PEER_INCLUDED) {
170 printf(" <-- current sync candidate"); 175 printf(" <-- current sync candidate");
171 } else if(PEER_SEL(peer[i].status) >= PEER_TRUECHIMER){ 176 } else if (PEER_SEL(peer[i].status) >= PEER_TRUECHIMER) {
172 printf(" <-- outlyer, but truechimer"); 177 printf(" <-- outlyer, but truechimer");
173 } 178 }
174 printf("\n"); 179 printf("\n");
@@ -176,14 +181,13 @@ void print_ntp_control_message(const ntp_control_message *p){
176 } 181 }
177} 182}
178 183
179void 184void setup_control_request(ntp_control_message *message, uint8_t opcode, uint16_t seq) {
180setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){ 185 memset(message, 0, sizeof(ntp_control_message));
181 memset(p, 0, sizeof(ntp_control_message)); 186 LI_SET(message->flags, LI_NOWARNING);
182 LI_SET(p->flags, LI_NOWARNING); 187 VN_SET(message->flags, VN_RESERVED);
183 VN_SET(p->flags, VN_RESERVED); 188 MODE_SET(message->flags, MODE_CONTROLMSG);
184 MODE_SET(p->flags, MODE_CONTROLMSG); 189 OP_SET(message->op, opcode);
185 OP_SET(p->op, opcode); 190 message->seq = htons(seq);
186 p->seq = htons(seq);
187 /* Remaining fields are zero for requests */ 191 /* Remaining fields are zero for requests */
188} 192}
189 193
@@ -195,25 +199,28 @@ setup_control_request(ntp_control_message *p, uint8_t opcode, uint16_t seq){
195 * positive value means a success retrieving the value. 199 * positive value means a success retrieving the value.
196 * - status is set to WARNING if there's no sync.peer (otherwise OK) and is 200 * - status is set to WARNING if there's no sync.peer (otherwise OK) and is
197 * the return value of the function. 201 * the return value of the function.
198 * status is pretty much useless as syncsource_found is a global variable 202 */
199 * used later in main to check is the server was synchronized. It works 203typedef struct {
200 * so I left it alone */ 204 mp_state_enum state;
201int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum, int *num_truechimers){ 205 mp_state_enum offset_result;
202 int conn=-1, i, npeers=0, num_candidates=0; 206 double offset;
203 double tmp_offset = 0; 207 double jitter;
204 int min_peer_sel=PEER_INCLUDED; 208 long stratum;
205 int peers_size=0, peer_offset=0; 209 int num_truechimers;
206 int status; 210 bool syncsource_found;
207 ntp_assoc_status_pair *peers=NULL; 211 bool li_alarm;
208 ntp_control_message req; 212} ntp_request_result;
209 const char *getvar = "stratum,offset,jitter"; 213ntp_request_result ntp_request(const check_ntp_peer_config config) {
210 char *data, *value, *nptr; 214
211 void *tmp; 215 ntp_request_result result = {
212 216 .state = STATE_OK,
213 status = STATE_OK; 217 .offset_result = STATE_UNKNOWN,
214 *offset_result = STATE_UNKNOWN; 218 .jitter = -1,
215 *jitter = *stratum = -1; 219 .stratum = -1,
216 *num_truechimers = 0; 220 .num_truechimers = 0,
221 .syncsource_found = false,
222 .li_alarm = false,
223 };
217 224
218 /* Long-winded explanation: 225 /* Long-winded explanation:
219 * Getting the sync peer offset, jitter and stratum requires a number of 226 * Getting the sync peer offset, jitter and stratum requires a number of
@@ -231,11 +238,17 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
231 * 4) Extract the offset, jitter and stratum value from the data[] 238 * 4) Extract the offset, jitter and stratum value from the data[]
232 * (it's ASCII) 239 * (it's ASCII)
233 */ 240 */
234 my_udp_connect(server_address, port, &conn); 241 int conn = -1;
242 my_udp_connect(config.server_address, config.port, &conn);
235 243
236 /* keep sending requests until the server stops setting the 244 /* keep sending requests until the server stops setting the
237 * REM_MORE bit, though usually this is only 1 packet. */ 245 * REM_MORE bit, though usually this is only 1 packet. */
238 do{ 246 ntp_control_message req;
247 ntp_assoc_status_pair *peers = NULL;
248 int peer_offset = 0;
249 size_t peers_size = 0;
250 size_t npeers = 0;
251 do {
239 setup_control_request(&req, OP_READSTAT, 1); 252 setup_control_request(&req, OP_READSTAT, 1);
240 DBG(printf("sending READSTAT request")); 253 DBG(printf("sending READSTAT request"));
241 write(conn, &req, SIZEOF_NTPCM(req)); 254 write(conn, &req, SIZEOF_NTPCM(req));
@@ -243,63 +256,84 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
243 256
244 do { 257 do {
245 /* Attempt to read the largest size packet possible */ 258 /* Attempt to read the largest size packet possible */
246 req.count=htons(MAX_CM_SIZE); 259 req.count = htons(MAX_CM_SIZE);
247 DBG(printf("receiving READSTAT response")) 260 DBG(printf("receiving READSTAT response"))
248 if(read(conn, &req, SIZEOF_NTPCM(req)) == -1) 261 if (read(conn, &req, SIZEOF_NTPCM(req)) == -1) {
249 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n"); 262 die(STATE_CRITICAL, "NTP CRITICAL: No response from NTP server\n");
263 }
250 DBG(print_ntp_control_message(&req)); 264 DBG(print_ntp_control_message(&req));
251 /* discard obviously invalid packets */ 265 /* discard obviously invalid packets */
252 if (ntohs(req.count) > MAX_CM_SIZE) 266 if (ntohs(req.count) > MAX_CM_SIZE) {
253 die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n"); 267 die(STATE_CRITICAL, "NTP CRITICAL: Invalid packet received from NTP server\n");
254 } while (!(req.op&OP_READSTAT && ntohs(req.seq) == 1)); 268 }
269 } while (!(req.op & OP_READSTAT && ntohs(req.seq) == 1));
255 270
256 if (LI(req.flags) == LI_ALARM) li_alarm = true; 271 if (LI(req.flags) == LI_ALARM) {
272 result.li_alarm = true;
273 }
257 /* Each peer identifier is 4 bytes in the data section, which 274 /* Each peer identifier is 4 bytes in the data section, which
258 * we represent as a ntp_assoc_status_pair datatype. 275 * we represent as a ntp_assoc_status_pair datatype.
259 */ 276 */
260 peers_size+=ntohs(req.count); 277 peers_size += ntohs(req.count);
261 if((tmp=realloc(peers, peers_size)) == NULL) 278 void *tmp;
279 if ((tmp = realloc(peers, peers_size)) == NULL) {
262 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n"); 280 free(peers), die(STATE_UNKNOWN, "can not (re)allocate 'peers' buffer\n");
263 peers=tmp; 281 }
264 memcpy((void*)((ptrdiff_t)peers+peer_offset), (void*)req.data, ntohs(req.count)); 282 peers = tmp;
265 npeers=peers_size/sizeof(ntp_assoc_status_pair); 283 memcpy((peers + peer_offset), (void *)req.data, ntohs(req.count));
266 peer_offset+=ntohs(req.count); 284 npeers = peers_size / sizeof(ntp_assoc_status_pair);
267 } while(req.op&REM_MORE); 285 peer_offset += ntohs(req.count);
286 } while (req.op & REM_MORE);
268 287
269 /* first, let's find out if we have a sync source, or if there are 288 /* first, let's find out if we have a sync source, or if there are
270 * at least some candidates. In the latter case we'll issue 289 * at least some candidates. In the latter case we'll issue
271 * a warning but go ahead with the check on them. */ 290 * a warning but go ahead with the check on them. */
272 for (i = 0; i < npeers; i++){ 291 int min_peer_sel = PEER_INCLUDED;
273 if(PEER_SEL(peers[i].status) >= PEER_TRUECHIMER){ 292 int num_candidates = 0;
274 (*num_truechimers)++; 293 for (size_t i = 0; i < npeers; i++) {
275 if(PEER_SEL(peers[i].status) >= PEER_INCLUDED){ 294 if (PEER_SEL(peers[i].status) >= PEER_TRUECHIMER) {
295 result.num_truechimers++;
296 if (PEER_SEL(peers[i].status) >= PEER_INCLUDED) {
276 num_candidates++; 297 num_candidates++;
277 if(PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE){ 298 if (PEER_SEL(peers[i].status) >= PEER_SYNCSOURCE) {
278 syncsource_found = true; 299 result.syncsource_found = true;
279 min_peer_sel=PEER_SYNCSOURCE; 300 min_peer_sel = PEER_SYNCSOURCE;
280 } 301 }
281 } 302 }
282 } 303 }
283 } 304 }
284 if(verbose) printf("%d candidate peers available\n", num_candidates); 305
285 if(verbose && syncsource_found) printf("synchronization source found\n"); 306 if (verbose) {
286 if(! syncsource_found){ 307 printf("%d candidate peers available\n", num_candidates);
287 status = STATE_WARNING; 308 if (result.syncsource_found) {
288 if(verbose) printf("warning: no synchronization source found\n"); 309 printf("synchronization source found\n");
289 } 310 }
290 if(li_alarm){
291 status = STATE_WARNING;
292 if(verbose) printf("warning: LI_ALARM bit is set\n");
293 } 311 }
294 312
313 if (!result.syncsource_found) {
314 result.state = STATE_WARNING;
315 if (verbose) {
316 printf("warning: no synchronization source found\n");
317 }
318 }
319 if (result.li_alarm) {
320 result.state = STATE_WARNING;
321 if (verbose) {
322 printf("warning: LI_ALARM bit is set\n");
323 }
324 }
295 325
296 for (i = 0; i < npeers; i++){ 326 const char *getvar = "stratum,offset,jitter";
327 char *data;
328 for (size_t i = 0; i < npeers; i++) {
297 /* Only query this server if it is the current sync source */ 329 /* Only query this server if it is the current sync source */
298 /* If there's no sync.peer, query all candidates and use the best one */ 330 /* If there's no sync.peer, query all candidates and use the best one */
299 if (PEER_SEL(peers[i].status) >= min_peer_sel){ 331 if (PEER_SEL(peers[i].status) >= min_peer_sel) {
300 if(verbose) printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc)); 332 if (verbose) {
301 xasprintf(&data, ""); 333 printf("Getting offset, jitter and stratum for peer %.2x\n", ntohs(peers[i].assoc));
302 do{ 334 }
335 data = strdup("");
336 do {
303 setup_control_request(&req, OP_READVAR, 2); 337 setup_control_request(&req, OP_READVAR, 2);
304 req.assoc = peers[i].assoc; 338 req.assoc = peers[i].assoc;
305 /* Putting the wanted variable names in the request 339 /* Putting the wanted variable names in the request
@@ -309,7 +343,7 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
309 */ 343 */
310 /* Older servers doesn't know what jitter is, so if we get an 344 /* Older servers doesn't know what jitter is, so if we get an
311 * error on the first pass we redo it with "dispersion" */ 345 * error on the first pass we redo it with "dispersion" */
312 strncpy(req.data, getvar, MAX_CM_SIZE-1); 346 strncpy(req.data, getvar, MAX_CM_SIZE - 1);
313 req.count = htons(strlen(getvar)); 347 req.count = htons(strlen(getvar));
314 DBG(printf("sending READVAR request...\n")); 348 DBG(printf("sending READVAR request...\n"));
315 write(conn, &req, SIZEOF_NTPCM(req)); 349 write(conn, &req, SIZEOF_NTPCM(req));
@@ -320,131 +354,184 @@ int ntp_request(double *offset, int *offset_result, double *jitter, int *stratum
320 DBG(printf("receiving READVAR response...\n")); 354 DBG(printf("receiving READVAR response...\n"));
321 read(conn, &req, SIZEOF_NTPCM(req)); 355 read(conn, &req, SIZEOF_NTPCM(req));
322 DBG(print_ntp_control_message(&req)); 356 DBG(print_ntp_control_message(&req));
323 } while (!(req.op&OP_READVAR && ntohs(req.seq) == 2)); 357 } while (!(req.op & OP_READVAR && ntohs(req.seq) == 2));
324 358
325 if(!(req.op&REM_ERROR)) 359 if (!(req.op & REM_ERROR)) {
326 xasprintf(&data, "%s%s", data, req.data); 360 xasprintf(&data, "%s%s", data, req.data);
327 } while(req.op&REM_MORE); 361 }
328 362 } while (req.op & REM_MORE);
329 if(req.op&REM_ERROR) { 363
330 if(strstr(getvar, "jitter")) { 364 if (req.op & REM_ERROR) {
331 if(verbose) printf("The command failed. This is usually caused by servers refusing the 'jitter'\nvariable. Restarting with 'dispersion'...\n"); 365 if (strstr(getvar, "jitter")) {
366 if (verbose) {
367 printf("The command failed. This is usually caused by servers refusing the "
368 "'jitter'\nvariable. Restarting with "
369 "'dispersion'...\n");
370 }
332 getvar = "stratum,offset,dispersion"; 371 getvar = "stratum,offset,dispersion";
333 i--; 372 i--;
334 continue; 373 continue;
335 } else if(strlen(getvar)) { 374 }
336 if(verbose) printf("Server didn't like dispersion either; will retrieve everything\n"); 375 if (strlen(getvar)) {
376 if (verbose) {
377 printf("Server didn't like dispersion either; will retrieve everything\n");
378 }
337 getvar = ""; 379 getvar = "";
338 i--; 380 i--;
339 continue; 381 continue;
340 } 382 }
341 } 383 }
342 384
343 if(verbose > 1) 385 if (verbose > 1) {
344 printf("Server responded: >>>%s<<<\n", data); 386 printf("Server responded: >>>%s<<<\n", data);
387 }
345 388
389 double tmp_offset = 0;
390 char *value;
391 char *nptr;
346 /* get the offset */ 392 /* get the offset */
347 if(verbose) 393 if (verbose) {
348 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc)); 394 printf("parsing offset from peer %.2x: ", ntohs(peers[i].assoc));
395 }
349 396
350 value = np_extract_ntpvar(data, "offset"); 397 value = np_extract_ntpvar(data, "offset");
351 nptr=NULL; 398 nptr = NULL;
352 /* Convert the value if we have one */ 399 /* Convert the value if we have one */
353 if(value != NULL) 400 if (value != NULL) {
354 tmp_offset = strtod(value, &nptr) / 1000; 401 tmp_offset = strtod(value, &nptr) / 1000;
402 }
355 /* If value is null or no conversion was performed */ 403 /* If value is null or no conversion was performed */
356 if(value == NULL || value==nptr) { 404 if (value == NULL || value == nptr) {
357 if(verbose) printf("error: unable to read server offset response.\n"); 405 if (verbose) {
406 printf("error: unable to read server offset response.\n");
407 }
358 } else { 408 } else {
359 if(verbose) printf("%.10g\n", tmp_offset); 409 if (verbose) {
360 if(*offset_result == STATE_UNKNOWN || fabs(tmp_offset) < fabs(*offset)) { 410 printf("%.10g\n", tmp_offset);
361 *offset = tmp_offset; 411 }
362 *offset_result = STATE_OK; 412 if (result.offset_result == STATE_UNKNOWN ||
413 fabs(tmp_offset) < fabs(result.offset)) {
414 result.offset = tmp_offset;
415 result.offset_result = STATE_OK;
363 } else { 416 } else {
364 /* Skip this one; move to the next */ 417 /* Skip this one; move to the next */
365 continue; 418 continue;
366 } 419 }
367 } 420 }
368 421
369 if(do_jitter) { 422 if (config.do_jitter) {
370 /* get the jitter */ 423 /* get the jitter */
371 if(verbose) { 424 if (verbose) {
372 printf("parsing %s from peer %.2x: ", strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter", ntohs(peers[i].assoc)); 425 printf("parsing %s from peer %.2x: ",
426 strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter",
427 ntohs(peers[i].assoc));
373 } 428 }
374 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion" : "jitter"); 429 value = np_extract_ntpvar(data, strstr(getvar, "dispersion") != NULL ? "dispersion"
375 nptr=NULL; 430 : "jitter");
431 nptr = NULL;
376 /* Convert the value if we have one */ 432 /* Convert the value if we have one */
377 if(value != NULL) 433 if (value != NULL) {
378 *jitter = strtod(value, &nptr); 434 result.jitter = strtod(value, &nptr);
435 }
379 /* If value is null or no conversion was performed */ 436 /* If value is null or no conversion was performed */
380 if(value == NULL || value==nptr) { 437 if (value == NULL || value == nptr) {
381 if(verbose) printf("error: unable to read server jitter/dispersion response.\n"); 438 if (verbose) {
382 *jitter = -1; 439 printf("error: unable to read server jitter/dispersion response.\n");
383 } else if(verbose) { 440 }
384 printf("%.10g\n", *jitter); 441 result.jitter = -1;
442 } else if (verbose) {
443 printf("%.10g\n", result.jitter);
385 } 444 }
386 } 445 }
387 446
388 if(do_stratum) { 447 if (config.do_stratum) {
389 /* get the stratum */ 448 /* get the stratum */
390 if(verbose) { 449 if (verbose) {
391 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc)); 450 printf("parsing stratum from peer %.2x: ", ntohs(peers[i].assoc));
392 } 451 }
393 value = np_extract_ntpvar(data, "stratum"); 452 value = np_extract_ntpvar(data, "stratum");
394 nptr=NULL; 453 nptr = NULL;
395 /* Convert the value if we have one */ 454 /* Convert the value if we have one */
396 if(value != NULL) 455 if (value != NULL) {
397 *stratum = strtol(value, &nptr, 10); 456 result.stratum = strtol(value, &nptr, 10);
398 if(value == NULL || value==nptr) { 457 }
399 if(verbose) printf("error: unable to read server stratum response.\n"); 458 if (value == NULL || value == nptr) {
400 *stratum = -1; 459 if (verbose) {
460 printf("error: unable to read server stratum response.\n");
461 }
462 result.stratum = -1;
401 } else { 463 } else {
402 if(verbose) printf("%i\n", *stratum); 464 if (verbose) {
465 printf("%li\n", result.stratum);
466 }
403 } 467 }
404 } 468 }
405 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */ 469 } /* if (PEER_SEL(peers[i].status) >= min_peer_sel) */
406 } /* for (i = 0; i < npeers; i++) */ 470 } /* for (i = 0; i < npeers; i++) */
407 471
408 close(conn); 472 close(conn);
409 if(peers!=NULL) free(peers); 473 if (peers != NULL) {
474 free(peers);
475 }
410 476
411 return status; 477 return result;
412} 478}
413 479
414int process_arguments(int argc, char **argv){ 480check_ntp_peer_config_wrapper process_arguments(int argc, char **argv) {
415 int c; 481
416 int option=0; 482 enum {
417 static struct option longopts[] = { 483 output_format_index = CHAR_MAX + 1,
418 {"version", no_argument, 0, 'V'},
419 {"help", no_argument, 0, 'h'},
420 {"verbose", no_argument, 0, 'v'},
421 {"use-ipv4", no_argument, 0, '4'},
422 {"use-ipv6", no_argument, 0, '6'},
423 {"quiet", no_argument, 0, 'q'},
424 {"warning", required_argument, 0, 'w'},
425 {"critical", required_argument, 0, 'c'},
426 {"swarn", required_argument, 0, 'W'},
427 {"scrit", required_argument, 0, 'C'},
428 {"jwarn", required_argument, 0, 'j'},
429 {"jcrit", required_argument, 0, 'k'},
430 {"twarn", required_argument, 0, 'm'},
431 {"tcrit", required_argument, 0, 'n'},
432 {"timeout", required_argument, 0, 't'},
433 {"hostname", required_argument, 0, 'H'},
434 {"port", required_argument, 0, 'p'},
435 {0, 0, 0, 0}
436 }; 484 };
437 485
486 static struct option longopts[] = {{"version", no_argument, 0, 'V'},
487 {"help", no_argument, 0, 'h'},
488 {"verbose", no_argument, 0, 'v'},
489 {"use-ipv4", no_argument, 0, '4'},
490 {"use-ipv6", no_argument, 0, '6'},
491 {"quiet", no_argument, 0, 'q'},
492 {"warning", required_argument, 0, 'w'},
493 {"critical", required_argument, 0, 'c'},
494 {"swarn", required_argument, 0, 'W'},
495 {"scrit", required_argument, 0, 'C'},
496 {"jwarn", required_argument, 0, 'j'},
497 {"jcrit", required_argument, 0, 'k'},
498 {"twarn", required_argument, 0, 'm'},
499 {"tcrit", required_argument, 0, 'n'},
500 {"timeout", required_argument, 0, 't'},
501 {"hostname", required_argument, 0, 'H'},
502 {"port", required_argument, 0, 'p'},
503 {"output-format", required_argument, 0, output_format_index},
504 {0, 0, 0, 0}};
505
506 if (argc < 2) {
507 usage("\n");
508 }
438 509
439 if (argc < 2) 510 check_ntp_peer_config_wrapper result = {
440 usage ("\n"); 511 .errorcode = OK,
512 .config = check_ntp_peer_config_init(),
513 };
441 514
442 while (true) { 515 while (true) {
443 c = getopt_long (argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option); 516 int option = 0;
444 if (c == -1 || c == EOF || c == 1) 517 int option_char =
518 getopt_long(argc, argv, "Vhv46qw:c:W:C:j:k:m:n:t:H:p:", longopts, &option);
519 if (option_char == -1 || option_char == EOF || option_char == 1) {
445 break; 520 break;
521 }
522
523 switch (option_char) {
524 case output_format_index: {
525 parsed_output_format parser = mp_parse_output_format(optarg);
526 if (!parser.parsing_success) {
527 printf("Invalid output format: %s\n", optarg);
528 exit(STATE_UNKNOWN);
529 }
446 530
447 switch (c) { 531 result.config.output_format_is_set = true;
532 result.config.output_format = parser.output_format;
533 break;
534 }
448 case 'h': 535 case 'h':
449 print_help(); 536 print_help();
450 exit(STATE_UNKNOWN); 537 exit(STATE_UNKNOWN);
@@ -457,48 +544,97 @@ int process_arguments(int argc, char **argv){
457 verbose++; 544 verbose++;
458 break; 545 break;
459 case 'q': 546 case 'q':
460 quiet = true; 547 result.config.quiet = true;
461 break;
462 case 'w':
463 owarn = optarg;
464 break;
465 case 'c':
466 ocrit = optarg;
467 break;
468 case 'W':
469 do_stratum = true;
470 swarn = optarg;
471 break;
472 case 'C':
473 do_stratum = true;
474 scrit = optarg;
475 break;
476 case 'j':
477 do_jitter = true;
478 jwarn = optarg;
479 break;
480 case 'k':
481 do_jitter = true;
482 jcrit = optarg;
483 break;
484 case 'm':
485 do_truechimers = true;
486 twarn = optarg;
487 break;
488 case 'n':
489 do_truechimers = true;
490 tcrit = optarg;
491 break; 548 break;
549 case 'w': {
550 mp_range_parsed tmp = mp_parse_range_string(optarg);
551 if (tmp.error != MP_PARSING_SUCCES) {
552 die(STATE_UNKNOWN, "failed to parse warning offset threshold");
553 }
554
555 result.config.offset_thresholds =
556 mp_thresholds_set_warn(result.config.offset_thresholds, tmp.range);
557 } break;
558 case 'c': {
559 mp_range_parsed tmp = mp_parse_range_string(optarg);
560 if (tmp.error != MP_PARSING_SUCCES) {
561 die(STATE_UNKNOWN, "failed to parse critical offset threshold");
562 }
563
564 result.config.offset_thresholds =
565 mp_thresholds_set_crit(result.config.offset_thresholds, tmp.range);
566 } break;
567 case 'W': {
568 result.config.do_stratum = true;
569 mp_range_parsed tmp = mp_parse_range_string(optarg);
570 if (tmp.error != MP_PARSING_SUCCES) {
571 die(STATE_UNKNOWN, "failed to parse warning stratum threshold");
572 }
573
574 result.config.stratum_thresholds =
575 mp_thresholds_set_warn(result.config.stratum_thresholds, tmp.range);
576 } break;
577 case 'C': {
578 result.config.do_stratum = true;
579 mp_range_parsed tmp = mp_parse_range_string(optarg);
580 if (tmp.error != MP_PARSING_SUCCES) {
581 die(STATE_UNKNOWN, "failed to parse critical stratum threshold");
582 }
583
584 result.config.stratum_thresholds =
585 mp_thresholds_set_crit(result.config.stratum_thresholds, tmp.range);
586 } break;
587 case 'j': {
588 result.config.do_jitter = true;
589 mp_range_parsed tmp = mp_parse_range_string(optarg);
590 if (tmp.error != MP_PARSING_SUCCES) {
591 die(STATE_UNKNOWN, "failed to parse warning jitter threshold");
592 }
593
594 result.config.jitter_thresholds =
595 mp_thresholds_set_warn(result.config.jitter_thresholds, tmp.range);
596 } break;
597 case 'k': {
598 result.config.do_jitter = true;
599 mp_range_parsed tmp = mp_parse_range_string(optarg);
600 if (tmp.error != MP_PARSING_SUCCES) {
601 die(STATE_UNKNOWN, "failed to parse critical jitter threshold");
602 }
603
604 result.config.jitter_thresholds =
605 mp_thresholds_set_crit(result.config.jitter_thresholds, tmp.range);
606 } break;
607 case 'm': {
608 result.config.do_truechimers = true;
609 mp_range_parsed tmp = mp_parse_range_string(optarg);
610 if (tmp.error != MP_PARSING_SUCCES) {
611 die(STATE_UNKNOWN, "failed to parse warning truechimer threshold");
612 }
613
614 result.config.truechimer_thresholds =
615 mp_thresholds_set_warn(result.config.truechimer_thresholds, tmp.range);
616 } break;
617 case 'n': {
618 result.config.do_truechimers = true;
619 mp_range_parsed tmp = mp_parse_range_string(optarg);
620 if (tmp.error != MP_PARSING_SUCCES) {
621 die(STATE_UNKNOWN, "failed to parse critical truechimer threshold");
622 }
623
624 result.config.truechimer_thresholds =
625 mp_thresholds_set_crit(result.config.truechimer_thresholds, tmp.range);
626 } break;
492 case 'H': 627 case 'H':
493 if(!is_host(optarg)) 628 if (!is_host(optarg) && (optarg[0] != '/')) {
494 usage2(_("Invalid hostname/address"), optarg); 629 usage2(_("Invalid hostname/address"), optarg);
495 server_address = strdup(optarg); 630 }
631 result.config.server_address = strdup(optarg);
496 break; 632 break;
497 case 'p': 633 case 'p':
498 port=atoi(optarg); 634 result.config.port = atoi(optarg);
499 break; 635 break;
500 case 't': 636 case 't':
501 socket_timeout=atoi(optarg); 637 socket_timeout = atoi(optarg);
502 break; 638 break;
503 case '4': 639 case '4':
504 address_family = AF_INET; 640 address_family = AF_INET;
@@ -507,222 +643,218 @@ int process_arguments(int argc, char **argv){
507#ifdef USE_IPV6 643#ifdef USE_IPV6
508 address_family = AF_INET6; 644 address_family = AF_INET6;
509#else 645#else
510 usage4 (_("IPv6 support not available")); 646 usage4(_("IPv6 support not available"));
511#endif 647#endif
512 break; 648 break;
513 case '?': 649 case '?':
514 /* print short usage statement if args not parsable */ 650 /* print short usage statement if args not parsable */
515 usage5 (); 651 usage5();
516 break; 652 break;
517 } 653 }
518 } 654 }
519 655
520 if(server_address == NULL){ 656 if (result.config.server_address == NULL) {
521 usage4(_("Hostname was not supplied")); 657 usage4(_("Hostname was not supplied"));
522 } 658 }
523 659
524 return 0; 660 return result;
525} 661}
526 662
527char *perfd_offset (double offset) 663char *perfd_offset(double offset, thresholds *offset_thresholds) {
528{ 664 return fperfdata("offset", offset, "s", true, offset_thresholds->warning->end, true,
529 return fperfdata ("offset", offset, "s", 665 offset_thresholds->critical->end, false, 0, false, 0);
530 true, offset_thresholds->warning->end,
531 true, offset_thresholds->critical->end,
532 false, 0, false, 0);
533} 666}
534 667
535char *perfd_jitter (double jitter) 668char *perfd_jitter(double jitter, bool do_jitter, thresholds *jitter_thresholds) {
536{ 669 return fperfdata("jitter", jitter, "", do_jitter, jitter_thresholds->warning->end, do_jitter,
537 return fperfdata ("jitter", jitter, "", 670 jitter_thresholds->critical->end, true, 0, false, 0);
538 do_jitter, jitter_thresholds->warning->end,
539 do_jitter, jitter_thresholds->critical->end,
540 true, 0, false, 0);
541} 671}
542 672
543char *perfd_stratum (int stratum) 673char *perfd_stratum(int stratum, bool do_stratum, thresholds *stratum_thresholds) {
544{ 674 return perfdata("stratum", stratum, "", do_stratum, (int)stratum_thresholds->warning->end,
545 return perfdata ("stratum", stratum, "", 675 do_stratum, (int)stratum_thresholds->critical->end, true, 0, true, 16);
546 do_stratum, (int)stratum_thresholds->warning->end,
547 do_stratum, (int)stratum_thresholds->critical->end,
548 true, 0, true, 16);
549} 676}
550 677
551char *perfd_truechimers (int num_truechimers) 678char *perfd_truechimers(int num_truechimers, const bool do_truechimers,
552{ 679 thresholds *truechimer_thresholds) {
553 return perfdata ("truechimers", num_truechimers, "", 680 return perfdata("truechimers", num_truechimers, "", do_truechimers,
554 do_truechimers, (int)truechimer_thresholds->warning->end, 681 (int)truechimer_thresholds->warning->end, do_truechimers,
555 do_truechimers, (int)truechimer_thresholds->critical->end, 682 (int)truechimer_thresholds->critical->end, true, 0, false, 0);
556 true, 0, false, 0);
557} 683}
558 684
559int main(int argc, char *argv[]){ 685int main(int argc, char *argv[]) {
560 int result, offset_result, stratum, num_truechimers; 686 setlocale(LC_ALL, "");
561 double offset=0, jitter=0; 687 bindtextdomain(PACKAGE, LOCALEDIR);
562 char *result_line, *perfdata_line; 688 textdomain(PACKAGE);
563
564 setlocale (LC_ALL, "");
565 bindtextdomain (PACKAGE, LOCALEDIR);
566 textdomain (PACKAGE);
567 689
568 /* Parse extra opts if any */ 690 /* Parse extra opts if any */
569 argv=np_extra_opts (&argc, argv, progname); 691 argv = np_extra_opts(&argc, argv, progname);
692
693 check_ntp_peer_config_wrapper tmp_config = process_arguments(argc, argv);
570 694
571 if (process_arguments (argc, argv) == ERROR) 695 if (tmp_config.errorcode == ERROR) {
572 usage4 (_("Could not parse arguments")); 696 usage4(_("Could not parse arguments"));
697 }
698
699 const check_ntp_peer_config config = tmp_config.config;
573 700
574 set_thresholds(&offset_thresholds, owarn, ocrit); 701 if (config.output_format_is_set) {
575 set_thresholds(&jitter_thresholds, jwarn, jcrit); 702 mp_set_format(config.output_format);
576 set_thresholds(&stratum_thresholds, swarn, scrit); 703 }
577 set_thresholds(&truechimer_thresholds, twarn, tcrit);
578 704
579 /* initialize alarm signal handling */ 705 /* initialize alarm signal handling */
580 signal (SIGALRM, socket_timeout_alarm_handler); 706 signal(SIGALRM, socket_timeout_alarm_handler);
581 707
582 /* set socket timeout */ 708 /* set socket timeout */
583 alarm (socket_timeout); 709 alarm(socket_timeout);
584 710
585 /* This returns either OK or WARNING (See comment preceding ntp_request) */ 711 /* This returns either OK or WARNING (See comment preceding ntp_request) */
586 result = ntp_request(&offset, &offset_result, &jitter, &stratum, &num_truechimers); 712 const ntp_request_result ntp_res = ntp_request(config);
713 mp_check overall = mp_check_init();
587 714
588 if(offset_result == STATE_UNKNOWN) { 715 mp_subcheck sc_offset = mp_subcheck_init();
716 xasprintf(&sc_offset.output, "offset");
717 if (ntp_res.offset_result == STATE_UNKNOWN) {
589 /* if there's no sync peer (this overrides ntp_request output): */ 718 /* if there's no sync peer (this overrides ntp_request output): */
590 result = (quiet ? STATE_UNKNOWN : STATE_CRITICAL); 719 sc_offset =
720 mp_set_subcheck_state(sc_offset, (config.quiet ? STATE_UNKNOWN : STATE_CRITICAL));
721 xasprintf(&sc_offset.output, "%s unknown", sc_offset.output);
591 } else { 722 } else {
592 /* Be quiet if there's no candidates either */ 723 /* Be quiet if there's no candidates either */
593 if (quiet && result == STATE_WARNING) 724 mp_state_enum tmp = STATE_OK;
594 result = STATE_UNKNOWN; 725 if (config.quiet && ntp_res.state == STATE_WARNING) {
595 result = max_state_alt(result, get_status(fabs(offset), offset_thresholds)); 726 tmp = STATE_UNKNOWN;
596 } 727 }
597 728
598 int oresult = result; 729 xasprintf(&sc_offset.output, "%s: %.6fs", sc_offset.output, ntp_res.offset);
599 730
731 mp_perfdata pd_offset = perfdata_init();
732 pd_offset.value = mp_create_pd_value(fabs(ntp_res.offset));
733 pd_offset = mp_pd_set_thresholds(pd_offset, config.offset_thresholds);
734 pd_offset.label = "offset";
735 pd_offset.uom = "s";
736 mp_add_perfdata_to_subcheck(&sc_offset, pd_offset);
600 737
601 int tresult = STATE_UNKNOWN; 738 tmp = max_state_alt(tmp, mp_get_pd_status(pd_offset));
602 739 sc_offset = mp_set_subcheck_state(sc_offset, tmp);
603 if(do_truechimers) {
604 tresult = get_status(num_truechimers, truechimer_thresholds);
605 result = max_state_alt(result, tresult);
606 } 740 }
607 741
742 mp_add_subcheck_to_check(&overall, sc_offset);
743
744 // truechimers
745 if (config.do_truechimers) {
746 mp_subcheck sc_truechimers = mp_subcheck_init();
747 xasprintf(&sc_truechimers.output, "truechimers: %i", ntp_res.num_truechimers);
608 748
609 int sresult = STATE_UNKNOWN; 749 mp_perfdata pd_truechimers = perfdata_init();
750 pd_truechimers.value = mp_create_pd_value(ntp_res.num_truechimers);
751 pd_truechimers.label = "truechimers";
752 pd_truechimers = mp_pd_set_thresholds(pd_truechimers, config.truechimer_thresholds);
610 753
611 if(do_stratum) { 754 mp_add_perfdata_to_subcheck(&sc_truechimers, pd_truechimers);
612 sresult = get_status(stratum, stratum_thresholds); 755
613 result = max_state_alt(result, sresult); 756 sc_truechimers = mp_set_subcheck_state(sc_truechimers, mp_get_pd_status(pd_truechimers));
757
758 mp_add_subcheck_to_check(&overall, sc_truechimers);
614 } 759 }
615 760
761 if (config.do_stratum) {
762 mp_subcheck sc_stratum = mp_subcheck_init();
763 xasprintf(&sc_stratum.output, "stratum: %li", ntp_res.stratum);
764
765 mp_perfdata pd_stratum = perfdata_init();
766 pd_stratum.value = mp_create_pd_value(ntp_res.stratum);
767 pd_stratum = mp_pd_set_thresholds(pd_stratum, config.stratum_thresholds);
768 pd_stratum.label = "stratum";
616 769
617 int jresult = STATE_UNKNOWN; 770 mp_add_perfdata_to_subcheck(&sc_stratum, pd_stratum);
618 771
619 if(do_jitter) { 772 sc_stratum = mp_set_subcheck_state(sc_stratum, mp_get_pd_status(pd_stratum));
620 jresult = get_status(jitter, jitter_thresholds); 773
621 result = max_state_alt(result, jresult); 774 mp_add_subcheck_to_check(&overall, sc_stratum);
622 } 775 }
623 776
624 switch (result) { 777 if (config.do_jitter) {
625 case STATE_CRITICAL : 778 mp_subcheck sc_jitter = mp_subcheck_init();
626 xasprintf(&result_line, _("NTP CRITICAL:")); 779 xasprintf(&sc_jitter.output, "jitter: %f", ntp_res.jitter);
627 break; 780
628 case STATE_WARNING : 781 mp_perfdata pd_jitter = perfdata_init();
629 xasprintf(&result_line, _("NTP WARNING:")); 782 pd_jitter.value = mp_create_pd_value(ntp_res.jitter);
630 break; 783 pd_jitter = mp_pd_set_thresholds(pd_jitter, config.jitter_thresholds);
631 case STATE_OK : 784 pd_jitter.label = "jitter";
632 xasprintf(&result_line, _("NTP OK:")); 785
633 break; 786 mp_add_perfdata_to_subcheck(&sc_jitter, pd_jitter);
634 default : 787
635 xasprintf(&result_line, _("NTP UNKNOWN:")); 788 sc_jitter = mp_set_subcheck_state(sc_jitter, mp_get_pd_status(pd_jitter));
636 break; 789 mp_add_subcheck_to_check(&overall, sc_jitter);
637 } 790 }
638 if(!syncsource_found) 791
639 xasprintf(&result_line, "%s %s,", result_line, _("Server not synchronized")); 792 mp_subcheck sc_other_info = mp_subcheck_init();
640 else if(li_alarm) 793 sc_other_info = mp_set_subcheck_default_state(sc_other_info, STATE_OK);
641 xasprintf(&result_line, "%s %s,", result_line, _("Server has the LI_ALARM bit set")); 794 if (!ntp_res.syncsource_found) {
642 795 xasprintf(&sc_other_info.output, "%s", _("Server not synchronized"));
643 if(offset_result == STATE_UNKNOWN){ 796 mp_add_subcheck_to_check(&overall, sc_other_info);
644 xasprintf(&result_line, "%s %s", result_line, _("Offset unknown")); 797 } else if (ntp_res.li_alarm) {
645 xasprintf(&perfdata_line, ""); 798 xasprintf(&sc_other_info.output, "%s", _("Server has the LI_ALARM bit set"));
646 } else if (oresult == STATE_WARNING) { 799 mp_add_subcheck_to_check(&overall, sc_other_info);
647 xasprintf(&result_line, "%s %s %.10g secs (WARNING)", result_line, _("Offset"), offset);
648 } else if (oresult == STATE_CRITICAL) {
649 xasprintf(&result_line, "%s %s %.10g secs (CRITICAL)", result_line, _("Offset"), offset);
650 } else {
651 xasprintf(&result_line, "%s %s %.10g secs", result_line, _("Offset"), offset);
652 }
653 xasprintf(&perfdata_line, "%s", perfd_offset(offset));
654
655 if (do_jitter) {
656 if (jresult == STATE_WARNING) {
657 xasprintf(&result_line, "%s, jitter=%f (WARNING)", result_line, jitter);
658 } else if (jresult == STATE_CRITICAL) {
659 xasprintf(&result_line, "%s, jitter=%f (CRITICAL)", result_line, jitter);
660 } else {
661 xasprintf(&result_line, "%s, jitter=%f", result_line, jitter);
662 }
663 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_jitter(jitter));
664 } 800 }
665 if (do_stratum) { 801
666 if (sresult == STATE_WARNING) { 802 {
667 xasprintf(&result_line, "%s, stratum=%i (WARNING)", result_line, stratum); 803 mp_subcheck sc_offset = mp_subcheck_init();
668 } else if (sresult == STATE_CRITICAL) { 804 sc_offset = mp_set_subcheck_default_state(sc_offset, STATE_OK);
669 xasprintf(&result_line, "%s, stratum=%i (CRITICAL)", result_line, stratum); 805 xasprintf(&sc_offset.output, "offset: %.10gs", ntp_res.offset);
670 } else { 806
671 xasprintf(&result_line, "%s, stratum=%i", result_line, stratum); 807 mp_perfdata pd_offset = perfdata_init();
672 } 808 pd_offset.value = mp_create_pd_value(ntp_res.offset);
673 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_stratum(stratum)); 809 pd_offset = mp_pd_set_thresholds(pd_offset, config.offset_thresholds);
810
811 sc_offset = mp_set_subcheck_state(sc_offset, ntp_res.offset_result);
674 } 812 }
675 if (do_truechimers) { 813
676 if (tresult == STATE_WARNING) { 814 if (config.server_address != NULL) {
677 xasprintf(&result_line, "%s, truechimers=%i (WARNING)", result_line, num_truechimers); 815 free(config.server_address);
678 } else if (tresult == STATE_CRITICAL) {
679 xasprintf(&result_line, "%s, truechimers=%i (CRITICAL)", result_line, num_truechimers);
680 } else {
681 xasprintf(&result_line, "%s, truechimers=%i", result_line, num_truechimers);
682 }
683 xasprintf(&perfdata_line, "%s %s", perfdata_line, perfd_truechimers(num_truechimers));
684 } 816 }
685 printf("%s|%s\n", result_line, perfdata_line);
686 817
687 if(server_address!=NULL) free(server_address); 818 mp_exit(overall);
688 return result;
689} 819}
690 820
691void print_help(void){ 821void print_help(void) {
692 print_revision(progname, NP_VERSION); 822 print_revision(progname, NP_VERSION);
693 823
694 printf ("Copyright (c) 2006 Sean Finney\n"); 824 printf("Copyright (c) 2006 Sean Finney\n");
695 printf (COPYRIGHT, copyright, email); 825 printf(COPYRIGHT, copyright, email);
696 826
697 printf ("%s\n", _("This plugin checks the selected ntp server")); 827 printf("%s\n", _("This plugin checks the selected ntp server"));
698 828
699 printf ("\n\n"); 829 printf("\n\n");
700 830
701 print_usage(); 831 print_usage();
702 printf (UT_HELP_VRSN); 832 printf(UT_HELP_VRSN);
703 printf (UT_EXTRA_OPTS); 833 printf(UT_EXTRA_OPTS);
704 printf (UT_IPv46); 834 printf(UT_IPv46);
705 printf (UT_HOST_PORT, 'p', "123"); 835 printf(UT_HOST_PORT, 'p', "123");
706 printf (" %s\n", "-q, --quiet"); 836 printf(" %s\n", "-q, --quiet");
707 printf (" %s\n", _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized")); 837 printf(" %s\n",
708 printf (" %s\n", "-w, --warning=THRESHOLD"); 838 _("Returns UNKNOWN instead of CRITICAL or WARNING if server isn't synchronized"));
709 printf (" %s\n", _("Offset to result in warning status (seconds)")); 839 printf(" %s\n", "-w, --warning=THRESHOLD");
710 printf (" %s\n", "-c, --critical=THRESHOLD"); 840 printf(" %s\n", _("Offset to result in warning status (seconds)"));
711 printf (" %s\n", _("Offset to result in critical status (seconds)")); 841 printf(" %s\n", "-c, --critical=THRESHOLD");
712 printf (" %s\n", "-W, --swarn=THRESHOLD"); 842 printf(" %s\n", _("Offset to result in critical status (seconds)"));
713 printf (" %s\n", _("Warning threshold for stratum of server's synchronization peer")); 843 printf(" %s\n", "-W, --swarn=THRESHOLD");
714 printf (" %s\n", "-C, --scrit=THRESHOLD"); 844 printf(" %s\n", _("Warning threshold for stratum of server's synchronization peer"));
715 printf (" %s\n", _("Critical threshold for stratum of server's synchronization peer")); 845 printf(" %s\n", "-C, --scrit=THRESHOLD");
716 printf (" %s\n", "-j, --jwarn=THRESHOLD"); 846 printf(" %s\n", _("Critical threshold for stratum of server's synchronization peer"));
717 printf (" %s\n", _("Warning threshold for jitter")); 847 printf(" %s\n", "-j, --jwarn=THRESHOLD");
718 printf (" %s\n", "-k, --jcrit=THRESHOLD"); 848 printf(" %s\n", _("Warning threshold for jitter"));
719 printf (" %s\n", _("Critical threshold for jitter")); 849 printf(" %s\n", "-k, --jcrit=THRESHOLD");
720 printf (" %s\n", "-m, --twarn=THRESHOLD"); 850 printf(" %s\n", _("Critical threshold for jitter"));
721 printf (" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")")); 851 printf(" %s\n", "-m, --twarn=THRESHOLD");
722 printf (" %s\n", "-n, --tcrit=THRESHOLD"); 852 printf(" %s\n", _("Warning threshold for number of usable time sources (\"truechimers\")"));
723 printf (" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")")); 853 printf(" %s\n", "-n, --tcrit=THRESHOLD");
724 printf (UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT); 854 printf(" %s\n", _("Critical threshold for number of usable time sources (\"truechimers\")"));
725 printf (UT_VERBOSE); 855 printf(UT_CONN_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
856 printf(UT_VERBOSE);
857 printf(UT_OUTPUT_FORMAT);
726 858
727 printf("\n"); 859 printf("\n");
728 printf("%s\n", _("This plugin checks an NTP server independent of any commandline")); 860 printf("%s\n", _("This plugin checks an NTP server independent of any commandline"));
@@ -741,7 +873,8 @@ void print_help(void){
741 printf(" %s\n", _("Simple NTP server check:")); 873 printf(" %s\n", _("Simple NTP server check:"));
742 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1")); 874 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1"));
743 printf("\n"); 875 printf("\n");
744 printf(" %s\n", _("Check jitter too, avoiding critical notifications if jitter isn't available")); 876 printf(" %s\n",
877 _("Check jitter too, avoiding critical notifications if jitter isn't available"));
745 printf(" %s\n", _("(See Notes above for more details on thresholds formats):")); 878 printf(" %s\n", _("(See Notes above for more details on thresholds formats):"));
746 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200")); 879 printf(" %s\n", ("./check_ntp_peer -H ntpserv -w 0.5 -c 1 -j -1:100 -k -1:200"));
747 printf("\n"); 880 printf("\n");
@@ -751,13 +884,11 @@ void print_help(void){
751 printf(" %s\n", _("Check only stratum:")); 884 printf(" %s\n", _("Check only stratum:"));
752 printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6")); 885 printf(" %s\n", ("./check_ntp_peer -H ntpserv -W 4 -C 6"));
753 886
754 printf (UT_SUPPORT); 887 printf(UT_SUPPORT);
755} 888}
756 889
757void 890void print_usage(void) {
758print_usage(void) 891 printf("%s\n", _("Usage:"));
759{
760 printf ("%s\n", _("Usage:"));
761 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname); 892 printf(" %s -H <host> [-4|-6] [-w <warn>] [-c <crit>] [-W <warn>] [-C <crit>]\n", progname);
762 printf(" [-j <warn>] [-k <crit>] [-v verbose]\n"); 893 printf(" [-j <warn>] [-k <crit>] [-v verbose]\n");
763} 894}