{GPL, other}: Relicense to MIT license.
[collectd.git] / src / ntpd.c
index 9e09f81..6bed82c 100644 (file)
@@ -1,24 +1,31 @@
 /**
  * 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 <octo at verplant.org>
+ *   Florian octo Forster <octo at collectd.org>
  **/
 
+#define _BSD_SOURCE /* For NI_MAXHOST */
+
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
@@ -50,9 +57,17 @@ static const char *config_keys[] =
 {
        "Host",
        "Port",
-       NULL
+       "ReverseLookups",
+       "IncludeUnitID"
 };
-static int config_keys_num = 2;
+static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
+
+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"
@@ -66,7 +81,7 @@ static char  ntpd_port[16];
 #define MAXFILENAME 128
 #define MAXSEQ  127
 #define MODE_PRIVATE 7
-#define NTP_OLDVERSION ((u_char) 1) /* oldest credible version */
+#define NTP_OLDVERSION ((uint8_t) 1) /* oldest credible version */
 #define IMPL_XNTPD 3
 #define FP_FRAC 65536.0
 
@@ -115,27 +130,27 @@ struct resp_pkt
 
 #define        ISRESPONSE(rm_vn_mode)  (((rm_vn_mode)&RESP_BIT)!=0)
 #define        ISMORE(rm_vn_mode)      (((rm_vn_mode)&MORE_BIT)!=0)
-#define INFO_VERSION(rm_vn_mode) ((u_char)(((rm_vn_mode)>>3)&0x7))
+#define INFO_VERSION(rm_vn_mode) ((uint8_t)(((rm_vn_mode)>>3)&0x7))
 #define        INFO_MODE(rm_vn_mode)   ((rm_vn_mode)&0x7)
 
 #define        RM_VN_MODE(resp, more, version)         \
-                               ((u_char)(((resp)?RESP_BIT:0)\
+                               ((uint8_t)(((resp)?RESP_BIT:0)\
                                |((more)?MORE_BIT:0)\
                                |((version?version:(NTP_OLDVERSION+1))<<3)\
                                |(MODE_PRIVATE)))
 
 #define        INFO_IS_AUTH(auth_seq)  (((auth_seq) & 0x80) != 0)
 #define        INFO_SEQ(auth_seq)      ((auth_seq)&0x7f)
-#define        AUTH_SEQ(auth, seq)     ((u_char)((((auth)!=0)?0x80:0)|((seq)&0x7f)))
+#define        AUTH_SEQ(auth, seq)     ((uint8_t)((((auth)!=0)?0x80:0)|((seq)&0x7f)))
 
-#define        INFO_ERR(err_nitems)    ((u_short)((ntohs(err_nitems)>>12)&0xf))
-#define        INFO_NITEMS(err_nitems) ((u_short)(ntohs(err_nitems)&0xfff))
-#define        ERR_NITEMS(err, nitems) (htons((u_short)((((u_short)(err)<<12)&0xf000)\
-                               |((u_short)(nitems)&0xfff))))
+#define        INFO_ERR(err_nitems)    ((uint16_t)((ntohs(err_nitems)>>12)&0xf))
+#define        INFO_NITEMS(err_nitems) ((uint16_t)(ntohs(err_nitems)&0xfff))
+#define        ERR_NITEMS(err, nitems) (htons((uint16_t)((((uint16_t)(err)<<12)&0xf000)\
+                               |((uint16_t)(nitems)&0xfff))))
 
 #define        INFO_MBZ(mbz_itemsize)  ((ntohs(mbz_itemsize)>>12)&0xf)
-#define        INFO_ITEMSIZE(mbz_itemsize)     ((u_short)(ntohs(mbz_itemsize)&0xfff))
-#define        MBZ_ITEMSIZE(itemsize)  (htons((u_short)(itemsize)))
+#define        INFO_ITEMSIZE(mbz_itemsize)     ((uint16_t)(ntohs(mbz_itemsize)&0xfff))
+#define        MBZ_ITEMSIZE(itemsize)  (htons((uint16_t)(itemsize)))
 
 /* negate a long float type */
 #define M_NEG(v_i, v_f) \
@@ -247,9 +262,9 @@ static char *refclock_names[] =
        "CHRONOLOG",  "DUMBCLOCK",    "ULINK_M320", "PCF",         /* 32-35 */
        "WWV_AUDIO",  "GPS_FG",       "HOPF_S",     "HOPF_P",      /* 36-39 */
        "JJY",        "TT_IRIG",      "GPS_ZYFER",  "GPS_RIPENCC", /* 40-43 */
-       "NEOCLK4X",   NULL                                         /* 44    */
+       "NEOCLK4X"                                                 /* 44    */
 };
-static int refclock_names_num = 45;
+static int refclock_names_num = STATIC_ARRAY_SIZE (refclock_names);
 /* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
  * End of the copied stuff..                                         *
  * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
@@ -267,11 +282,24 @@ static int ntpd_config (const char *key, const char *value)
        {
                int port = (int) (atof (value));
                if ((port > 0) && (port <= 65535))
-                       snprintf (ntpd_port, sizeof (ntpd_port),
+                       ssnprintf (ntpd_port, sizeof (ntpd_port),
                                        "%i", port);
                else
-                       strncpy (ntpd_port, value, sizeof (ntpd_port));
-               ntpd_port[sizeof (ntpd_port) - 1] = '\0';
+                       sstrncpy (ntpd_port, value, sizeof (ntpd_port));
+       }
+       else if (strcasecmp (key, "ReverseLookups") == 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
        {
@@ -290,38 +318,25 @@ static void ntpd_submit (char *type, char *type_inst, double value)
 
        vl.values = values;
        vl.values_len = 1;
-       vl.time = time (NULL);
-       strcpy (vl.host, hostname_g);
-       strcpy (vl.plugin, "ntpd");
-       strcpy (vl.plugin_instance, "");
-       strncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "ntpd", sizeof (vl.plugin));
+       sstrncpy (vl.plugin_instance, "", sizeof (vl.plugin_instance));
+       sstrncpy (vl.type, type, sizeof (vl.type));
+       sstrncpy (vl.type_instance, type_inst, sizeof (vl.type_instance));
 
-       plugin_dispatch_values (type, &vl);
+       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)
+/* 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 (char *type, char *type_inst, uint8_t reach,
+               double value)
 {
-       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);
+       if (!(reach & 1))
+               value = NAN;
 
-       return ((sec * 1000) + ((usec + 500) / 1000));
+       ntpd_submit (type, type_inst, value);
 }
 
 static int ntpd_connect (void)
@@ -397,7 +412,7 @@ static int ntpd_connect (void)
 }
 
 /* For a description of the arguments see `ntpd_do_query' below. */
-static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
+static int ntpd_receive_response (int *res_items, int *res_size,
                char **res_data, int res_item_size)
 {
        int              sd;
@@ -448,6 +463,8 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
        done = 0;
        while (done == 0)
        {
+               struct timeval time_left;
+
                if (gettimeofday (&time_now, NULL) < 0)
                {
                        char errbuf[1024];
@@ -456,8 +473,14 @@ static int ntpd_receive_response (int req_code, 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;
@@ -601,7 +624,7 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
                pkt_padding = 0;
                if (pkt_item_len < res_item_size)
                        pkt_padding = res_item_size - pkt_item_len;
-               DEBUG ("res_item_size = %i; pkt_padding = %i;",
+               DEBUG ("res_item_size = %i; pkt_padding = %zi;",
                                res_item_size, pkt_padding);
 
                /* Extract the sequence number */
@@ -640,7 +663,7 @@ static int ntpd_receive_response (int req_code, int *res_items, int *res_size,
                 * Enough with the checks. Copy the data now.
                 * We start by allocating some more memory.
                 */
-               DEBUG ("realloc (%p, %i)", (void *) *res_data,
+               DEBUG ("realloc (%p, %zu)", (void *) *res_data,
                                (items_num + pkt_item_num) * res_item_size);
                items = realloc ((void *) *res_data,
                                (items_num + pkt_item_num) * res_item_size);
@@ -755,7 +778,7 @@ static int ntpd_do_query (int req_code, int req_items, int req_size, char *req_d
        if (status != 0)
                return (status);
 
-       status = ntpd_receive_response (req_code, res_items, res_size, res_data,
+       status = ntpd_receive_response (res_items, res_size, res_data,
                        res_item_size);
        return (status);
 }
@@ -770,6 +793,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 (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;
@@ -791,15 +916,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);
        }
@@ -827,12 +952,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);
        }
@@ -843,88 +969,26 @@ static int ntpd_read (void)
                double offset;
 
                char peername[NI_MAXHOST];
-               int refclock_id;
-               
-               ptr = ps + i;
-               refclock_id = 0;
-
-               /*
-               if (((ntohl (ptr->dstadr) & 0xFFFFFF00) == 0x7F000000) || (ptr->dstadr == 0))
-                       continue;
-                       */
+               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;
 
-               if (ptr->v6_flag)
-               {
-                       struct sockaddr_in6 sa;
-
-                       memset (&sa, 0, sizeof (sa));
-                       sa.sin6_family = AF_INET6;
-                       sa.sin6_port = htons (123);
-                       memcpy (&sa.sin6_addr, &ptr->srcadr6, sizeof (struct in6_addr));
-
-                       status = getnameinfo ((const struct sockaddr *) &sa,
-                                       sizeof (sa),
-                                       peername, sizeof (peername),
-                                       NULL, 0, 0 /* no 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;
-                       }
-               }
-               else if ((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)
-                       {
-                               strncpy (peername, refclock_names[refclock_id],
-                                               sizeof (peername));
-                       }
-                       else
-                       {
-                               memset ((void *) &addr_obj, '\0', sizeof (addr_obj));
-                               addr_obj.s_addr = ptr->srcadr;
-                               addr_str = inet_ntoa (addr_obj);
-
-                               strncpy (peername, addr_str, sizeof (peername));
-                       }
+                       ERROR ("ntpd plugin: Determining name of peer failed.");
+                       continue;
                }
-               else /* IPv4 */
-               {
-                       struct in_addr  addr_obj;
-                       struct hostent *addr_he;
-                       char           *addr_str;
 
-                       memset ((void *) &addr_obj, '\0', sizeof (addr_obj));
-                       addr_obj.s_addr = ptr->srcadr;
-                       addr_str = inet_ntoa (addr_obj);
+               refclock_id = ntpd_get_refclock_id (ptr);
 
-                       addr_he = gethostbyaddr ((const void *) &addr_obj,
-                                       sizeof (addr_obj), AF_INET);
-                       if (addr_he != NULL)
-                       {
-                               strncpy (peername, addr_he->h_name, sizeof (peername));
-                       }
-                       else
-                       {
-                               strncpy (peername, addr_str, sizeof (peername));
-                       }
-               }
+               /* 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"
@@ -933,6 +997,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),
@@ -940,10 +1005,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);