Code

- shuffling some code around to keep things tidy.
authorM. Sean Finney <seanius@users.sourceforge.net>
Mon, 20 Mar 2006 22:06:07 +0000 (22:06 +0000)
committerM. Sean Finney <seanius@users.sourceforge.net>
Mon, 20 Mar 2006 22:06:07 +0000 (22:06 +0000)
- now average the result of 4 queries just as ntpdate does
- put things in place for jitter calculation

git-svn-id: https://nagiosplug.svn.sourceforge.net/svnroot/nagiosplug/nagiosplug/trunk@1332 f882894a-f735-0410-b71e-b25c423dba1c

plugins/check_ntp.c

index 56e5a05d9f034957ce01dbe6e3be69bf15b8e27a..5037786a97d316b4ddfcd1c53f016c71b13d8b4b 100644 (file)
@@ -183,6 +183,32 @@ void setup_request(ntp_message *p){
        TVtoNTP64(t,p->txts);
 }
 
+double offset_request(const char *host){
+       int i=0, conn=-1;
+       ntp_message req;
+       double next_offset=0., avg_offset=0.;
+       struct timeval recv_time;
+
+       for(i=0; i<4; i++){
+               setup_request(&req);
+               my_udp_connect(server_address, 123, &conn);
+               write(conn, &req, sizeof(ntp_message));
+               read(conn, &req, sizeof(ntp_message));
+               gettimeofday(&recv_time, NULL);
+               /* if(verbose) print_packet(&req); */
+               close(conn);
+               next_offset=calc_offset(&req, &recv_time);
+               if(verbose) printf("offset: %g\n", next_offset);
+               avg_offset+=next_offset;
+       }
+       return avg_offset/4.;
+}
+
+/* not yet implemented yet */
+double jitter_request(const char *host){
+       return 0.;
+}
+
 int process_arguments(int argc, char **argv){
        int c;
        int option=0;
@@ -282,9 +308,6 @@ int process_arguments(int argc, char **argv){
 
 int main(int argc, char *argv[]){
        int result = STATE_UNKNOWN;
-       int conn;
-       ntp_message m;
-       struct timeval recv_time;
        double offset=0, jitter=0;
 
        if (process_arguments (argc, argv) == ERROR)
@@ -296,18 +319,7 @@ int main(int argc, char *argv[]){
        /* set socket timeout */
        alarm (socket_timeout);
 
-       setup_request(&m);
-       if(verbose) print_packet(&m);
-       my_udp_connect(server_address, 123, &conn);
-       write(conn, &m, sizeof(ntp_message));
-       read(conn, &m, sizeof(ntp_message));
-       gettimeofday(&recv_time, NULL);
-       if(verbose) print_packet(&m);
-       close(conn);
-
-       offset=calc_offset(&m, &recv_time);
-       printf("total offset: %g\n", offset);
-
+       offset = offset_request(server_address);
        if(offset > ocrit){
                printf("NTP CRITICAL: ");
                result = STATE_CRITICAL;
@@ -319,14 +331,15 @@ int main(int argc, char *argv[]){
                result = STATE_OK;
        }
 
-       /* not implemented yet:
-       jitter=calc_jitter(&m, &recv_time);
+       /* not implemented yet: */
+       jitter=jitter_request(server_address);
 
+       /* not implemented yet:
        if(do_jitter){
-               if(offset > ocrit){
+               if(jitter > jcrit){
                        printf("NTP CRITICAL: ");
                        result = STATE_CRITICAL;
-               } else if(offset > owarn) {
+               } else if(jitter > jwarn) {
                        printf("NTP WARNING: ");
                        result = STATE_WARNING;
                } else {