src/ntpd.c: Add IncludeUnitID option for backwards compatibility
[collectd.git] / src / ntpd.c
index 9d716ad..9841e31 100644 (file)
@@ -52,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;
@@ -278,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);
@@ -786,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);
        }
@@ -822,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);
        }
@@ -853,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;