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