X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fntpd.c;h=4a31e455eece36ad595f8bbc41ac7ee6985c35e7;hb=9c1acd10074995f6ec95eca6664c4bce4b7d7c1b;hp=15e60a5d0317735a00f75b6a1ae1391c827ac0aa;hpb=f2696c0e21e9226d3911457250ef64138900aa2b;p=collectd.git diff --git a/src/ntpd.c b/src/ntpd.c index 15e60a5d..4a31e455 100644 --- a/src/ntpd.c +++ b/src/ntpd.c @@ -1,24 +1,30 @@ /** * collectd - src/ntpd.c - * Copyright (C) 2006-2007 Florian octo Forster + * Copyright (C) 2006-2012 Florian octo Forster * - * This program is free software; you can redistribute it and/or modify it - * under the terms of the GNU General Public License as published by the - * Free Software Foundation; only version 2 of the License is applicable. + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * General Public License for more details. + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. * - * You should have received a copy of the GNU General Public License along - * with this program; if not, write to the Free Software Foundation, Inc., - * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER + * DEALINGS IN THE SOFTWARE. * * Authors: - * Florian octo Forster + * Florian octo Forster **/ +#define _DEFAULT_SOURCE #define _BSD_SOURCE /* For NI_MAXHOST */ #include "collectd.h" @@ -32,9 +38,6 @@ #if HAVE_NETDB_H # include #endif -#if HAVE_SYS_SOCKET_H -# include -#endif #if HAVE_NETINET_IN_H # include #endif @@ -52,11 +55,17 @@ 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; +static _Bool 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 _Bool include_unit_id = 0; # define NTPD_DEFAULT_HOST "localhost" # define NTPD_DEFAULT_PORT "123" @@ -154,16 +163,16 @@ struct resp_pkt /* l_fp to double */ #define M_LFPTOD(r_i, r_uf, d) \ do { \ - register int32_t i; \ - register uint32_t f; \ + register int32_t ri; \ + register uint32_t rf; \ \ - i = (r_i); \ - f = (r_uf); \ - if (i < 0) { \ - M_NEG(i, f); \ - (d) = -((double) i + ((double) f) / 4294967296.0); \ + ri = (r_i); \ + rf = (r_uf); \ + if (ri < 0) { \ + M_NEG(ri, rf); \ + (d) = -((double) ri + ((double) rf) / 4294967296.0); \ } else { \ - (d) = (double) i + ((double) f) / 4294967296.0; \ + (d) = (double) ri + ((double) rf) / 4294967296.0; \ } \ } while (0) @@ -238,7 +247,7 @@ struct info_kernel }; /* List of reference clock names */ -static char *refclock_names[] = +static const char *refclock_names[] = { "UNKNOWN", "LOCAL", "GPS_TRAK", "WWV_PST", /* 0- 3 */ "SPECTRACOM", "TRUETIME", "IRIG_AUDIO", "CHU_AUDIO", /* 4- 7 */ @@ -253,7 +262,7 @@ static char *refclock_names[] = "JJY", "TT_IRIG", "GPS_ZYFER", "GPS_RIPENCC", /* 40-43 */ "NEOCLK4X" /* 44 */ }; -static int refclock_names_num = STATIC_ARRAY_SIZE (refclock_names); +static size_t refclock_names_num = STATIC_ARRAY_SIZE (refclock_names); /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * End of the copied stuff.. * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ @@ -283,6 +292,13 @@ static int ntpd_config (const char *key, const char *value) 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); @@ -291,7 +307,7 @@ static int ntpd_config (const char *key, const char *value) return (0); } -static void ntpd_submit (char *type, char *type_inst, double value) +static void ntpd_submit (const char *type, const char *type_inst, gauge_t value) { value_t values[1]; value_list_t vl = VALUE_LIST_INIT; @@ -309,10 +325,22 @@ static void ntpd_submit (char *type, char *type_inst, double value) plugin_dispatch_values (&vl); } +/* Each time a peer is polled, ntpd shifts the reach register to the left and + * sets the LSB based on whether the peer was reachable. If the LSB is zero, + * the values are out of date. */ +static void ntpd_submit_reach (const char *type, const char *type_inst, + uint8_t reach, gauge_t value) +{ + if (!(reach & 1)) + value = NAN; + + ntpd_submit (type, type_inst, value); +} + static int ntpd_connect (void) { - char *host; - char *port; + const char *host; + const char *port; struct addrinfo ai_hints; struct addrinfo *ai_list; @@ -456,7 +484,7 @@ static int ntpd_receive_response (int *res_items, int *res_size, poll_s.fd = sd; poll_s.events = POLLIN | POLLPRI; poll_s.revents = 0; - + DEBUG ("Polling for %ims", timeout); status = poll (&poll_s, 1, timeout); @@ -496,7 +524,7 @@ static int ntpd_receive_response (int *res_items, int *res_size, DEBUG ("recv'd %i bytes", status); - /* + /* * Do some sanity checks first */ if (status < RESP_HEADER_SIZE) @@ -639,7 +667,6 @@ static int ntpd_receive_response (int *res_items, int *res_size, (items_num + pkt_item_num) * res_item_size); if (items == NULL) { - items = *res_data; ERROR ("ntpd plugin: realloc failed."); continue; } @@ -706,7 +733,7 @@ static int ntpd_send_request (int req_code, int req_items, int req_size, char *r req.err_nitems = ERR_NITEMS (0, req_items); req.mbz_itemsize = MBZ_ITEMSIZE (req_size); - + if (req_data != NULL) memcpy ((void *) req.data, (const void *) req_data, req_data_len); @@ -763,6 +790,108 @@ static double ntpd_read_fp (int32_t val_int) return (val_double); } +static uint32_t ntpd_get_refclock_id (struct info_peer_summary const *peer_info) +{ + uint32_t addr = ntohl (peer_info->srcadr); + uint32_t refclock_id = (addr >> 8) & 0x00FF; + + return (refclock_id); +} + +static int ntpd_get_name_from_address (char *buffer, size_t buffer_size, + struct info_peer_summary const *peer_info, _Bool do_reverse_lookup) +{ + struct sockaddr_storage sa; + socklen_t sa_len; + int flags = 0; + int status; + + memset (&sa, 0, sizeof (sa)); + + if (peer_info->v6_flag) + { + struct sockaddr_in6 sa6; + + assert (sizeof (sa) >= sizeof (sa6)); + + memset (&sa6, 0, sizeof (sa6)); + sa6.sin6_family = AF_INET6; + sa6.sin6_port = htons (123); + memcpy (&sa6.sin6_addr, &peer_info->srcadr6, + sizeof (struct in6_addr)); + sa_len = sizeof (sa6); + + memcpy (&sa, &sa6, sizeof (sa6)); + } + else + { + struct sockaddr_in sa4; + + assert (sizeof (sa) >= sizeof (sa4)); + + memset (&sa4, 0, sizeof (sa4)); + sa4.sin_family = AF_INET; + sa4.sin_port = htons (123); + memcpy (&sa4.sin_addr, &peer_info->srcadr, + sizeof (struct in_addr)); + sa_len = sizeof (sa4); + + memcpy (&sa, &sa4, sizeof (sa4)); + } + + if (!do_reverse_lookup) + flags |= NI_NUMERICHOST; + + status = getnameinfo ((struct sockaddr const *) &sa, sa_len, + buffer, buffer_size, + NULL, 0, /* No port name */ + flags); + if (status != 0) + { + char errbuf[1024]; + ERROR ("ntpd plugin: getnameinfo failed: %s", + (status == EAI_SYSTEM) + ? sstrerror (errno, errbuf, sizeof (errbuf)) + : gai_strerror (status)); + return (-1); + } + + return (0); +} /* ntpd_get_name_from_address */ + +static int ntpd_get_name_refclock (char *buffer, size_t buffer_size, + struct info_peer_summary const *peer_info) +{ + uint32_t refclock_id = ntpd_get_refclock_id (peer_info); + uint32_t unit_id = ntohl (peer_info->srcadr) & 0x00FF; + + if (((size_t) refclock_id) >= refclock_names_num) + return (ntpd_get_name_from_address (buffer, buffer_size, + peer_info, + /* do_reverse_lookup = */ 0)); + + if (include_unit_id) + ssnprintf (buffer, buffer_size, "%s-%"PRIu32, + refclock_names[refclock_id], unit_id); + else + sstrncpy (buffer, refclock_names[refclock_id], buffer_size); + + return (0); +} /* int ntpd_get_name_refclock */ + +static int ntpd_get_name (char *buffer, size_t buffer_size, + struct info_peer_summary const *peer_info) +{ + uint32_t addr = ntohl (peer_info->srcadr); + + if (!peer_info->v6_flag && ((addr & REFCLOCK_MASK) == REFCLOCK_ADDR)) + return (ntpd_get_name_refclock (buffer, buffer_size, + peer_info)); + else + return (ntpd_get_name_from_address (buffer, buffer_size, + peer_info, do_reverse_lookups)); +} /* int ntpd_addr_to_name */ + static int ntpd_read (void) { struct info_kernel *ik; @@ -773,9 +902,19 @@ static int ntpd_read (void) int ps_num; int ps_size; + gauge_t offset_loop; + gauge_t freq_loop; + gauge_t offset_error; + int status; int i; + /* On Linux, if the STA_NANO bit is set in ik->status, then ik->offset + * is is nanoseconds, otherwise it's microseconds. + * TODO(octo): STA_NANO is defined in the Linux specific header. */ + double scale_loop = 1e-6; + double scale_error = 1e-6; + ik = NULL; ik_num = 0; ik_size = 0; @@ -798,18 +937,19 @@ static int ntpd_read (void) } /* kerninfo -> estimated error */ + offset_loop = scale_loop * ((gauge_t) ntohl (ik->offset)); + freq_loop = ntpd_read_fp (ik->freq); + offset_error = scale_error * ((gauge_t) ntohl (ik->esterror)); DEBUG ("info_kernel:\n" - " pll offset = %.8f\n" - " pll frequency = %.8f\n" /* drift compensation */ - " est error = %.8f\n", - ntpd_read_fp (ik->offset), - ntpd_read_fp (ik->freq), - ntpd_read_fp (ik->esterror)); + " pll offset = %.8g\n" + " pll frequency = %.8g\n" /* drift compensation */ + " est error = %.8g\n", + offset_loop, freq_loop, offset_error); - ntpd_submit ("frequency_offset", "loop", ntpd_read_fp (ik->freq)); - ntpd_submit ("time_offset", "loop", ntpd_read_fp (ik->offset)); - ntpd_submit ("time_offset", "error", ntpd_read_fp (ik->esterror)); + ntpd_submit ("frequency_offset", "loop", freq_loop); + ntpd_submit ("time_offset", "loop", offset_loop); + ntpd_submit ("time_offset", "error", offset_error); free (ik); ik = NULL; @@ -837,102 +977,26 @@ static int ntpd_read (void) double offset; char peername[NI_MAXHOST]; - int refclock_id; - - ptr = ps + i; - refclock_id = 0; + uint32_t refclock_id; - /* Convert the `long floating point' offset value to double */ - M_LFPTOD (ntohl (ptr->offset_int), ntohl (ptr->offset_frc), offset); + ptr = ps + i; - /* Special IP addresses for hardware clocks and stuff.. */ - if (!ptr->v6_flag - && ((ntohl (ptr->srcadr) & REFCLOCK_MASK) - == REFCLOCK_ADDR)) + status = ntpd_get_name (peername, sizeof (peername), ptr); + if (status != 0) { - struct in_addr addr_obj; - char *addr_str; - - refclock_id = (ntohl (ptr->srcadr) >> 8) & 0x000000FF; - - if (refclock_id < refclock_names_num) - { - /* The unit number is in the lowest byte. */ - ssnprintf (peername, sizeof (peername), - "%s%i", - refclock_names[refclock_id], - ntohl (ptr->srcadr) & 0xFF); - } - else - { - memset ((void *) &addr_obj, '\0', sizeof (addr_obj)); - addr_obj.s_addr = ptr->srcadr; - addr_str = inet_ntoa (addr_obj); - - sstrncpy (peername, addr_str, sizeof (peername)); - } + ERROR ("ntpd plugin: Determining name of peer failed."); + continue; } - else /* Normal network host. */ - { - struct sockaddr_storage sa; - socklen_t sa_len; - int flags = 0; - - memset (&sa, '\0', sizeof (sa)); - - if (ptr->v6_flag) - { - struct sockaddr_in6 sa6; - - assert (sizeof (sa) >= sizeof (sa6)); - - 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 (sa6); - - memcpy (&sa, &sa6, sizeof (sa6)); - } - else - { - struct sockaddr_in sa4; - assert (sizeof (sa) >= sizeof (sa4)); + refclock_id = ntpd_get_refclock_id (ptr); - 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 (sa4); - - memcpy (&sa, &sa4, sizeof (sa4)); - } - - if (do_reverse_lookups == 0) - flags |= NI_NUMERICHOST; - - status = getnameinfo ((const struct sockaddr *) &sa, - sa_len, - peername, sizeof (peername), - NULL, 0, /* No port name */ - flags); - if (status != 0) - { - char errbuf[1024]; - ERROR ("ntpd plugin: getnameinfo failed: %s", - (status == EAI_SYSTEM) - ? sstrerror (errno, errbuf, sizeof (errbuf)) - : gai_strerror (status)); - continue; - } - } + /* Convert the `long floating point' offset value to double */ + M_LFPTOD (ntohl (ptr->offset_int), ntohl (ptr->offset_frc), offset); DEBUG ("peer %i:\n" " peername = %s\n" " srcadr = 0x%08x\n" + " reach = 0%03o\n" " delay = %f\n" " offset_int = %i\n" " offset_frc = %i\n" @@ -941,6 +1005,7 @@ static int ntpd_read (void) i, peername, ntohl (ptr->srcadr), + ptr->reach, ntpd_read_fp (ptr->delay), ntohl (ptr->offset_int), ntohl (ptr->offset_frc), @@ -948,10 +1013,13 @@ static int ntpd_read (void) ntpd_read_fp (ptr->dispersion)); if (refclock_id != 1) /* not the system clock (offset will always be zero.. */ - ntpd_submit ("time_offset", peername, offset); - ntpd_submit ("time_dispersion", peername, ntpd_read_fp (ptr->dispersion)); + ntpd_submit_reach ("time_offset", peername, ptr->reach, + offset); + ntpd_submit_reach ("time_dispersion", peername, ptr->reach, + ntpd_read_fp (ptr->dispersion)); if (refclock_id == 0) /* not a reference clock */ - ntpd_submit ("delay", peername, ntpd_read_fp (ptr->delay)); + ntpd_submit_reach ("delay", peername, ptr->reach, + ntpd_read_fp (ptr->delay)); } free (ps);