ntpd plugin: Report failures of "ntpd_do_query" as *errors*, not debug messages.
[collectd.git] / src / ntpd.c
index 64e1bce..8bbf74d 100644 (file)
@@ -19,6 +19,8 @@
  *   Florian octo Forster <octo at verplant.org>
  **/
 
+#define _BSD_SOURCE /* For NI_MAXHOST */
+
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
@@ -276,9 +278,7 @@ 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;
@@ -784,15 +784,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);
        }
@@ -820,12 +820,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);
        }
@@ -878,25 +879,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;
 
-                               sa_ptr->sin_family = AF_INET;
-                               sa_ptr->sin_port = htons (123);
-                               memcpy (&sa_ptr->sin_addr, &ptr->srcadr,
+                               assert (sizeof (sa) >= sizeof (sa4));
+
+                               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)