oconfig: fix oconfig_free to free all elements
[collectd.git] / src / powerdns.c
index 3e7ebb3..81e253b 100644 (file)
 #define FUNC_ERROR(func) do { char errbuf[1024]; ERROR ("powerdns plugin: %s failed: %s", func, sstrerror (errno, errbuf, sizeof (errbuf))); } while (0)
 
 #define SERVER_SOCKET  LOCALSTATEDIR"/run/pdns.controlsocket"
-#define SERVER_COMMAND "SHOW *"
+#define SERVER_COMMAND "SHOW * \n"
 
 #define RECURSOR_SOCKET  LOCALSTATEDIR"/run/pdns_recursor.controlsocket"
 #define RECURSOR_COMMAND "get noerror-answers nxdomain-answers " \
   "servfail-answers sys-msec user-msec qa-latency cache-entries cache-hits " \
-  "cache-misses questions"
+  "cache-misses questions\n"
 
 struct list_item_s;
 typedef struct list_item_s list_item_t;
@@ -84,10 +84,10 @@ typedef struct statname_lookup_s statname_lookup_t;
 
 /* Description of statistics returned by the recursor: {{{
 all-outqueries      counts the number of outgoing UDP queries since starting
-answers0-1          counts the number of queries answered within 1 milisecond
+answers0-1          counts the number of queries answered within 1 millisecond
 answers100-1000     counts the number of queries answered within 1 second
-answers10-100       counts the number of queries answered within 100 miliseconds
-answers1-10         counts the number of queries answered within 10 miliseconds
+answers10-100       counts the number of queries answered within 100 milliseconds
+answers1-10         counts the number of queries answered within 10 milliseconds
 answers-slow        counts the number of queries answered after 1 second
 cache-entries       shows the number of entries in the cache
 cache-hits          counts the number of cache hits since starting
@@ -259,9 +259,6 @@ static void submit (const char *plugin_instance, /* {{{ */
     if (strcmp (lookup_table[i].name, pdns_type) == 0)
       break;
 
-  if (lookup_table[i].type == NULL)
-    return;
-
   if (i >= lookup_table_length)
   {
     INFO ("powerdns plugin: submit: Not found in lookup table: %s = %s;",
@@ -269,6 +266,9 @@ static void submit (const char *plugin_instance, /* {{{ */
     return;
   }
 
+  if (lookup_table[i].type == NULL)
+    return;
+
   type = lookup_table[i].type;
   type_instance = lookup_table[i].type_instance;
 
@@ -321,6 +321,9 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
 
   struct sockaddr_un sa_unix;
 
+  struct timeval stv_timeout;
+  cdtime_t cdt_timeout;
+
   sd = socket (PF_UNIX, item->socktype, 0);
   if (sd < 0)
   {
@@ -361,12 +364,13 @@ static int powerdns_get_data_dgram (list_item_t *item, /* {{{ */
       break;
     }
 
-    struct timeval timeout;
-    timeout.tv_sec=2;
-    if (timeout.tv_sec < interval_g * 3 / 4)
-      timeout.tv_sec = interval_g * 3 / 4;
-    timeout.tv_usec=0;
-    status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout));
+    cdt_timeout = plugin_get_interval () * 3 / 4;
+    if (cdt_timeout < TIME_T_TO_CDTIME_T (2))
+      cdt_timeout = TIME_T_TO_CDTIME_T (2);
+
+    CDTIME_T_TO_TIMEVAL (cdt_timeout, &stv_timeout);
+
+    status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &stv_timeout, sizeof (stv_timeout));
     if (status != 0)
     {
       FUNC_ERROR ("setsockopt");
@@ -439,6 +443,11 @@ static int powerdns_get_data_stream (list_item_t *item, /* {{{ */
     return (-1);
   }
 
+  struct timeval timeout;
+  timeout.tv_sec=5;
+  timeout.tv_usec=0;
+  status = setsockopt (sd, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof (timeout));
+
   status = connect (sd, (struct sockaddr *) &item->sockaddr,
       sizeof (item->sockaddr));
   if (status != 0)
@@ -531,7 +540,7 @@ static int powerdns_read_server (list_item_t *item) /* {{{ */
   int fields_num;
 
   if (item->command == NULL)
-    item->command = strdup ("SHOW *");
+    item->command = strdup (SERVER_COMMAND);
   if (item->command == NULL)
   {
     ERROR ("powerdns plugin: strdup failed.");
@@ -612,7 +621,7 @@ static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
   else
   {
     sstrncpy (buffer, "get ", sizeof (buffer));
-    status = strjoin (&buffer[4], sizeof (buffer) - strlen ("get "),
+    status = strjoin (&buffer[strlen("get ")], sizeof (buffer) - strlen ("get "),
        li->fields, li->fields_num,
        /* seperator = */ " ");
     if (status < 0)
@@ -620,6 +629,14 @@ static int powerdns_update_recursor_command (list_item_t *li) /* {{{ */
       ERROR ("powerdns plugin: strjoin failed.");
       return (-1);
     }
+    buffer[sizeof (buffer) - 1] = 0;
+    int i = strlen (buffer);
+    if (i < sizeof (buffer) - 2)
+    {
+      buffer[i++] = ' ';
+      buffer[i++] = '\n';
+      buffer[i++] = '\0';
+    }
   }
 
   buffer[sizeof (buffer) - 1] = 0;