summaryrefslogtreecommitdiffstats
path: root/plugins/check_ntp_time.c
diff options
context:
space:
mode:
Diffstat (limited to 'plugins/check_ntp_time.c')
-rw-r--r--plugins/check_ntp_time.c68
1 files changed, 35 insertions, 33 deletions
diff --git a/plugins/check_ntp_time.c b/plugins/check_ntp_time.c
index 16e4c3c..11a0ece 100644
--- a/plugins/check_ntp_time.c
+++ b/plugins/check_ntp_time.c
@@ -247,50 +247,52 @@ void setup_request(ntp_message *p){
247 * this is done by filtering servers based on stratum, dispersion, and 247 * this is done by filtering servers based on stratum, dispersion, and
248 * finally round-trip delay. */ 248 * finally round-trip delay. */
249int best_offset_server(const ntp_server_results *slist, int nservers){ 249int best_offset_server(const ntp_server_results *slist, int nservers){
250 int i=0, j=0, cserver=0, candidates[5], csize=0; 250 int i=0, cserver=0, best_server=-1;
251 251
252 /* for each server */ 252 /* for each server */
253 for(cserver=0; cserver<nservers; cserver++){ 253 for(cserver=0; cserver<nservers; cserver++){
254 /* sort out servers with error flags */ 254 /* We don't want any servers that fails these tests */
255 if ( LI(slist[cserver].flags) != LI_NOWARNING ){ 255 /* Sort out servers that didn't respond or responede with a 0 stratum;
256 if (verbose) printf("discarding peer id %d: flags=%d\n", cserver, LI(slist[cserver].flags)); 256 * stratum 0 is for reference clocks so no NTP server should ever report
257 break; 257 * a stratum 0 */
258 if ( slist[cserver].stratum == 0){
259 if (verbose) printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum);
260 continue;
261 }
262 /* Sort out servers with error flags */
263 if ( LI(slist[cserver].flags) == LI_ALARM ){
264 if (verbose) printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags));
265 continue;
258 } 266 }
259 267
260 /* compare it to each of the servers already in the candidate list */ 268 /* If we don't have a server yet, use the first one */
261 for(i=0; i<csize; i++){ 269 if (best_server == -1) {
262 /* does it have an equal or better stratum? */ 270 best_server = cserver;
263 if(slist[cserver].stratum <= slist[i].stratum){ 271 DBG(printf("using peer %d as our first candidate\n", best_server));
264 /* does it have an equal or better dispersion? */ 272 continue;
265 if(slist[cserver].rtdisp <= slist[i].rtdisp){
266 /* does it have a better rtdelay? */
267 if(slist[cserver].rtdelay < slist[i].rtdelay){
268 break;
269 }
270 }
271 }
272 } 273 }
273 274
274 /* if we haven't reached the current list's end, move everyone 275 /* compare the server to the best one we've seen so far */
275 * over one to the right, and insert the new candidate */ 276 /* does it have an equal or better stratum? */
276 if(i<csize){ 277 DBG(printf("comparing peer %d with peer %d\n", cserver, best_server));
277 for(j=4; j>i; j--){ 278 if(slist[cserver].stratum <= slist[best_server].stratum){
278 candidates[j]=candidates[j-1]; 279 DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server));
280 /* does it have an equal or better dispersion? */
281 if(slist[cserver].rtdisp <= slist[best_server].rtdisp){
282 DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server));
283 /* does it have a better rtdelay? */
284 if(slist[cserver].rtdelay < slist[best_server].rtdelay){
285 DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server));
286 best_server = cserver;
287 DBG(printf("peer %d is now our best candidate\n", best_server));
288 }
279 } 289 }
280 } 290 }
281 /* regardless, if they should be on the list... */
282 if(i<5) {
283 candidates[i]=cserver;
284 if(csize<5) csize++;
285 /* otherwise discard the server */
286 } else {
287 DBG(printf("discarding peer id %d\n", cserver));
288 }
289 } 291 }
290 292
291 if(csize>0) { 293 if(best_server >= 0) {
292 DBG(printf("best server selected: peer %d\n", candidates[0])); 294 DBG(printf("best server selected: peer %d\n", best_server));
293 return candidates[0]; 295 return best_server;
294 } else { 296 } else {
295 DBG(printf("no peers meeting synchronization criteria :(\n")); 297 DBG(printf("no peers meeting synchronization criteria :(\n"));
296 return -1; 298 return -1;