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