X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fntpd.c;h=9841e3155a95c077ac7b84d8d975890e25d34caa;hb=b6052ac362c1d289aec7e3a0397b18b99ca071a3;hp=44964bb5a8dbd936d23ef2e828e4d05b2561c543;hpb=597adb008e5cb9b39bc925046d2aedc084ec2b9e;p=collectd.git diff --git a/src/ntpd.c b/src/ntpd.c index 44964bb5..9841e315 100644 --- a/src/ntpd.c +++ b/src/ntpd.c @@ -19,6 +19,8 @@ * Florian octo Forster **/ +#define _BSD_SOURCE /* For NI_MAXHOST */ + #include "collectd.h" #include "common.h" #include "plugin.h" @@ -50,12 +52,18 @@ static const char *config_keys[] = { "Host", "Port", - "ReverseLookups" + "ReverseLookups", + "IncludeUnitID" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); static int do_reverse_lookups = 1; +/* This option only exists for backward compatibility. If it is false and two + * ntpd peers use the same refclock driver, the plugin will try to write + * simultaneous measurements from both to the same type instance. */ +static int include_unit_id = 0; + # define NTPD_DEFAULT_HOST "localhost" # define NTPD_DEFAULT_PORT "123" static int sock_descr = -1; @@ -276,13 +284,18 @@ static int ntpd_config (const char *key, const char *value) } else if (strcasecmp (key, "ReverseLookups") == 0) { - if ((strcasecmp (value, "True") == 0) - || (strcasecmp (value, "Yes") == 0) - || (strcasecmp (value, "On") == 0)) + if (IS_TRUE (value)) do_reverse_lookups = 1; else do_reverse_lookups = 0; } + else if (strcasecmp (key, "IncludeUnitID") == 0) + { + if (IS_TRUE (value)) + include_unit_id = 1; + else + include_unit_id = 0; + } else { return (-1); @@ -300,7 +313,6 @@ static void ntpd_submit (char *type, char *type_inst, double value) vl.values = values; vl.values_len = 1; - vl.time = time (NULL); sstrncpy (vl.host, hostname_g, sizeof (vl.host)); sstrncpy (vl.plugin, "ntpd", sizeof (vl.plugin)); sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance)); @@ -310,31 +322,6 @@ static void ntpd_submit (char *type, char *type_inst, double value) plugin_dispatch_values (&vl); } -/* returns `tv0 - tv1' in milliseconds or 0 if `tv1 > tv0' */ -static int timeval_sub (const struct timeval *tv0, const struct timeval *tv1) -{ - int sec; - int usec; - - if ((tv0->tv_sec < tv1->tv_sec) - || ((tv0->tv_sec == tv1->tv_sec) && (tv0->tv_usec < tv1->tv_usec))) - return (0); - - sec = tv0->tv_sec - tv1->tv_sec; - usec = tv0->tv_usec - tv1->tv_usec; - - while (usec < 0) - { - usec += 1000000; - sec -= 1; - } - - if (sec < 0) - return (0); - - return ((sec * 1000) + ((usec + 500) / 1000)); -} - static int ntpd_connect (void) { char *host; @@ -459,6 +446,8 @@ static int ntpd_receive_response (int *res_items, int *res_size, done = 0; while (done == 0) { + struct timeval time_left; + if (gettimeofday (&time_now, NULL) < 0) { char errbuf[1024]; @@ -467,8 +456,14 @@ static int ntpd_receive_response (int *res_items, int *res_size, return (-1); } + if (timeval_cmp (time_end, time_now, &time_left) <= 0) + timeout = 0; + else + timeout = 1000 * time_left.tv_sec + + ((time_left.tv_usec + 500) / 1000); + /* timeout reached */ - if ((timeout = timeval_sub (&time_end, &time_now)) == 0) + if (timeout <= 0) break; poll_s.fd = sd; @@ -802,15 +797,15 @@ static int ntpd_read (void) 0, 0, NULL, /* request data */ &ik_num, &ik_size, (char **) ((void *) &ik), /* response data */ sizeof (struct info_kernel)); - if (status != 0) { - DEBUG ("ntpd_do_query failed with status %i", status); - return (-1); + ERROR ("ntpd plugin: ntpd_do_query (REQ_GET_KERNEL) failed with status %i", status); + return (status); } - if ((ik == NULL) || (ik_num == 0) || (ik_size == 0)) + else if ((ik == NULL) || (ik_num == 0) || (ik_size == 0)) { - DEBUG ("ntpd_do_query returned: ik = %p; ik_num = %i; ik_size = %i;", + ERROR ("ntpd plugin: ntpd_do_query returned unexpected data. " + "(ik = %p; ik_num = %i; ik_size = %i)", (void *) ik, ik_num, ik_size); return (-1); } @@ -838,12 +833,13 @@ static int ntpd_read (void) sizeof (struct info_peer_summary)); if (status != 0) { - DEBUG ("ntpd_do_query failed with status %i", status); - return (-1); + ERROR ("ntpd plugin: ntpd_do_query (REQ_PEER_LIST_SUM) failed with status %i", status); + return (status); } - if ((ps == NULL) || (ps_num == 0) || (ps_size == 0)) + else if ((ps == NULL) || (ps_num == 0) || (ps_size == 0)) { - DEBUG ("ntpd_do_query returned: ps = %p; ps_num = %i; ps_size = %i;", + ERROR ("ntpd plugin: ntpd_do_query returned unexpected data. " + "(ps = %p; ps_num = %i; ps_size = %i)", (void *) ps, ps_num, ps_size); return (-1); } @@ -869,15 +865,26 @@ static int ntpd_read (void) { struct in_addr addr_obj; char *addr_str; + int name_refclock; refclock_id = (ntohl (ptr->srcadr) >> 8) & 0x000000FF; - if (refclock_id < refclock_names_num) + name_refclock = refclock_id < refclock_names_num; + + if (name_refclock && include_unit_id) + { + /* The unit number is in the lowest byte. */ + ssnprintf (peername, sizeof (peername), + "%s-%u", + refclock_names[refclock_id], + ntohl (ptr->srcadr) & 0xFF); + } + else if (name_refclock && !include_unit_id) { sstrncpy (peername, refclock_names[refclock_id], sizeof (peername)); } - else + else /* !name_refclock */ { memset ((void *) &addr_obj, '\0', sizeof (addr_obj)); addr_obj.s_addr = ptr->srcadr; @@ -896,25 +903,33 @@ static int ntpd_read (void) if (ptr->v6_flag) { - struct sockaddr_in6 *sa_ptr; - sa_ptr = (struct sockaddr_in6 *) &sa; + struct sockaddr_in6 sa6; + + assert (sizeof (sa) >= sizeof (sa6)); - sa_ptr->sin6_family = AF_INET6; - sa_ptr->sin6_port = htons (123); - memcpy (&sa_ptr->sin6_addr, &ptr->srcadr6, + memset (&sa6, 0, sizeof (sa6)); + sa6.sin6_family = AF_INET6; + sa6.sin6_port = htons (123); + memcpy (&sa6.sin6_addr, &ptr->srcadr6, sizeof (struct in6_addr)); - sa_len = sizeof (struct sockaddr_in6); + sa_len = sizeof (sa6); + + memcpy (&sa, &sa6, sizeof (sa6)); } else { - struct sockaddr_in *sa_ptr; - sa_ptr = (struct sockaddr_in *) &sa; + struct sockaddr_in sa4; + + assert (sizeof (sa) >= sizeof (sa4)); - sa_ptr->sin_family = AF_INET; - sa_ptr->sin_port = htons (123); - memcpy (&sa_ptr->sin_addr, &ptr->srcadr, + memset (&sa4, 0, sizeof (sa4)); + sa4.sin_family = AF_INET; + sa4.sin_port = htons (123); + memcpy (&sa4.sin_addr, &ptr->srcadr, sizeof (struct in_addr)); - sa_len = sizeof (struct sockaddr_in); + sa_len = sizeof (sa4); + + memcpy (&sa, &sa4, sizeof (sa4)); } if (do_reverse_lookups == 0)