summaryrefslogtreecommitdiffstats
path: root/plugins/check_ntp.c
diff options
context:
space:
mode:
authorThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-01-29 08:54:48 (GMT)
committerThomas Guyot-Sionnest <dermoth@users.sourceforge.net>2008-01-29 08:54:48 (GMT)
commit34cfb0c1034a6b3289e9aca567fd2393126aa80a (patch)
treec9e725021b2c6d7144203c8d71b590f873f20457 /plugins/check_ntp.c
parent08d81dc7bd9a0c292b5dd7494da4b85cacb1c210 (diff)
downloadmonitoring-plugins-34cfb0c1034a6b3289e9aca567fd2393126aa80a.tar.gz
Fix bugs and flaws in best offset server selection of check_ntp_time and (deprecated) check_ntp
git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1909 f882894a-f735-0410-b71e-b25c423dba1c
Diffstat (limited to 'plugins/check_ntp.c')
-rw-r--r--plugins/check_ntp.c68
1 files changed, 35 insertions, 33 deletions
diff --git a/plugins/check_ntp.c b/plugins/check_ntp.c
index b474d9a..2b3cc91 100644
--- a/plugins/check_ntp.c
+++ b/plugins/check_ntp.c
@@ -302,50 +302,52 @@ void setup_request(ntp_message *p){
302 * this is done by filtering servers based on stratum, dispersion, and 302 * this is done by filtering servers based on stratum, dispersion, and
303 * finally round-trip delay. */ 303 * finally round-trip delay. */
304int best_offset_server(const ntp_server_results *slist, int nservers){ 304int best_offset_server(const ntp_server_results *slist, int nservers){
305 int i=0, j=0, cserver=0, candidates[5], csize=0; 305 int i=0, cserver=0, best_server=-1;
306 306
307 /* for each server */ 307 /* for each server */
308 for(cserver=0; cserver<nservers; cserver++){ 308 for(cserver=0; cserver<nservers; cserver++){
309 /* sort out servers with error flags */ 309 /* We don't want any servers that fails these tests */
310 if ( LI(slist[cserver].flags) != LI_NOWARNING ){ 310 /* Sort out servers that didn't respond or responede with a 0 stratum;
311 if (verbose) printf("discarding peer id %d: flags=%d\n", cserver, LI(slist[cserver].flags)); 311 * stratum 0 is for reference clocks so no NTP server should ever report
312 break; 312 * a stratum 0 */
313 if ( slist[cserver].stratum == 0){
314 if (verbose) printf("discarding peer %d: stratum=%d\n", cserver, slist[cserver].stratum);
315 continue;
316 }
317 /* Sort out servers with error flags */
318 if ( LI(slist[cserver].flags) == LI_ALARM ){
319 if (verbose) printf("discarding peer %d: flags=%d\n", cserver, LI(slist[cserver].flags));
320 continue;
313 } 321 }
314 322
315 /* compare it to each of the servers already in the candidate list */ 323 /* If we don't have a server yet, use the first one */
316 for(i=0; i<csize; i++){ 324 if (best_server == -1) {
317 /* does it have an equal or better stratum? */ 325 best_server = cserver;
318 if(slist[cserver].stratum <= slist[i].stratum){ 326 DBG(printf("using peer %d as our first candidate\n", best_server));
319 /* does it have an equal or better dispersion? */ 327 continue;
320 if(slist[cserver].rtdisp <= slist[i].rtdisp){
321 /* does it have a better rtdelay? */
322 if(slist[cserver].rtdelay < slist[i].rtdelay){
323 break;
324 }
325 }
326 }
327 } 328 }
328 329
329 /* if we haven't reached the current list's end, move everyone 330 /* compare the server to the best one we've seen so far */
330 * over one to the right, and insert the new candidate */ 331 /* does it have an equal or better stratum? */
331 if(i<csize){ 332 DBG(printf("comparing peer %d with peer %d\n", cserver, best_server));
332 for(j=4; j>i; j--){ 333 if(slist[cserver].stratum <= slist[best_server].stratum){
333 candidates[j]=candidates[j-1]; 334 DBG(printf("stratum for peer %d <= peer %d\n", cserver, best_server));
335 /* does it have an equal or better dispersion? */
336 if(slist[cserver].rtdisp <= slist[best_server].rtdisp){
337 DBG(printf("dispersion for peer %d <= peer %d\n", cserver, best_server));
338 /* does it have a better rtdelay? */
339 if(slist[cserver].rtdelay < slist[best_server].rtdelay){
340 DBG(printf("rtdelay for peer %d < peer %d\n", cserver, best_server));
341 best_server = cserver;
342 DBG(printf("peer %d is now our best candidate\n", best_server));
343 }
334 } 344 }
335 } 345 }
336 /* regardless, if they should be on the list... */
337 if(i<5) {
338 candidates[i]=cserver;
339 if(csize<5) csize++;
340 /* otherwise discard the server */
341 } else {
342 DBG(printf("discarding peer id %d\n", cserver));
343 }
344 } 346 }
345 347
346 if(csize>0) { 348 if(best_server >= 0) {
347 DBG(printf("best server selected: peer %d\n", candidates[0])); 349 DBG(printf("best server selected: peer %d\n", best_server));
348 return candidates[0]; 350 return best_server;
349 } else { 351 } else {
350 DBG(printf("no peers meeting synchronization criteria :(\n")); 352 DBG(printf("no peers meeting synchronization criteria :(\n"));
351 return -1; 353 return -1;