Merge branch 'collectd-5.4' into collectd-5.5
authorFlorian Forster <octo@collectd.org>
Sat, 6 Aug 2016 18:33:54 +0000 (20:33 +0200)
committerFlorian Forster <octo@collectd.org>
Sat, 6 Aug 2016 18:33:54 +0000 (20:33 +0200)
1  2 
src/libcollectdclient/client.c
src/ping.c
src/utils_db_query.c

@@@ -36,6 -36,7 +36,7 @@@
  
  #include <stdlib.h>
  #include <stdio.h>
+ #include <stdarg.h>
  #include <unistd.h>
  #include <sys/types.h>
  #include <sys/socket.h>
      _b[sizeof (_b) - 1] = 0; \
      SSTRCAT ((d), _b); \
    } while (0)
 -    
 +
  
  #define LCC_SET_ERRSTR(c, ...) do { \
    snprintf ((c)->errbuf, sizeof ((c)->errbuf), __VA_ARGS__); \
    (c)->errbuf[sizeof ((c)->errbuf) - 1] = 0; \
  } while (0)
  
- #if COLLECT_DEBUG
- # define LCC_DEBUG(...) printf (__VA_ARGS__)
- #else
- # define LCC_DEBUG(...) /**/
- #endif
  /*
   * Types
   */
@@@ -118,6 -113,22 +113,22 @@@ typedef struct lcc_response_s lcc_respo
  /*
   * Private functions
   */
+ static int lcc_tracef(char const *format, ...)
+ {
+   va_list ap;
+   int status;
+   char const *trace = getenv (LCC_TRACE_ENV);
+   if (!trace || (strcmp ("", trace) == 0) || (strcmp ("0", trace) == 0))
+     return 0;
+   va_start (ap, format);
+   status = vprintf (format, ap);
+   va_end (ap);
+   return status;
+ }
  /* Even though Posix requires "strerror_r" to return an "int",
   * some systems (e.g. the GNU libc) return a "char *" _and_
   * ignore the second argument ... -tokkee */
@@@ -250,7 -261,7 +261,7 @@@ static int lcc_send (lcc_connection_t *
  {
    int status;
  
-   LCC_DEBUG ("send:    --> %s\n", command);
+   lcc_tracef ("send:    --> %s\n", command);
  
    status = fprintf (c->fh, "%s\r\n", command);
    if (status < 0)
@@@ -281,7 -292,7 +292,7 @@@ static int lcc_receive (lcc_connection_
      return (-1);
    }
    lcc_chomp (buffer);
-   LCC_DEBUG ("receive: <-- %s\n", buffer);
+   lcc_tracef ("receive: <-- %s\n", buffer);
  
    /* Convert the leading status to an integer and make `ptr' to point to the
     * beginning of the message. */
        break;
      }
      lcc_chomp (buffer);
-     LCC_DEBUG ("receive: <-- %s\n", buffer);
+     lcc_tracef ("receive: <-- %s\n", buffer);
  
      res.lines[i] = strdup (buffer);
      if (res.lines[i] == NULL)
diff --combined src/ping.c
@@@ -2,23 -2,18 +2,23 @@@
   * collectd - src/ping.c
   * Copyright (C) 2005-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 collectd.org>
@@@ -379,7 -374,7 +379,7 @@@ static int start_thread (void) /* {{{ *
    if (ping_thread_loop != 0)
    {
      pthread_mutex_unlock (&ping_lock);
-     return (-1);
+     return (0);
    }
  
    ping_thread_loop = 1;
      pthread_mutex_unlock (&ping_lock);
      return (-1);
    }
-     
    pthread_mutex_unlock (&ping_lock);
    return (0);
  } /* }}} int start_thread */
@@@ -444,10 -439,7 +444,7 @@@ static int ping_init (void) /* {{{ *
          "Will use a timeout of %gs.", ping_timeout);
    }
  
-   if (start_thread () != 0)
-     return (-1);
-   return (0);
+   return (start_thread ());
  } /* }}} int ping_init */
  
  static int config_set_string (const char *name, /* {{{ */
diff --combined src/utils_db_query.c
@@@ -2,26 -2,21 +2,26 @@@
   * collectd - src/utils_db_query.c
   * Copyright (C) 2008,2009  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>
   **/
  
  #include "collectd.h"
@@@ -43,8 -38,6 +43,8 @@@ struct udb_result_
    size_t   instances_num;
    char   **values;
    size_t   values_num;
 +  char   **metadata;
 +  size_t   metadata_num;
  
    udb_result_t *next;
  }; /* }}} */
@@@ -66,10 -59,8 +66,10 @@@ struct udb_result_preparation_area_s /
    const   data_set_t *ds;
    size_t *instances_pos;
    size_t *values_pos;
 +  size_t *metadata_pos;
    char  **instances_buffer;
    char  **values_buffer;
 +  char  **metadata_buffer;
  
    struct udb_result_preparation_area_s *next;
  }; /* }}} */
@@@ -197,7 -188,6 +197,7 @@@ static int udb_result_submit (udb_resul
  {
    value_list_t vl = VALUE_LIST_INIT;
    size_t i;
 +  int status;
  
    assert (r != NULL);
    assert (r_area->ds != NULL);
    {
      if (r->instance_prefix == NULL)
      {
-       strjoin (vl.type_instance, sizeof (vl.type_instance),
+       int status = strjoin (vl.type_instance, sizeof (vl.type_instance),
            r_area->instances_buffer, r->instances_num, "-");
+       if (status != 0)
+       {
+         ERROR ("udb_result_submit: creating type_instance failed with status %d.",
+             status);
+         return (status);
+       }
      }
      else
      {
        char tmp[DATA_MAX_NAME_LEN];
  
-       strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
+       int status = strjoin (tmp, sizeof (tmp), r_area->instances_buffer,
            r->instances_num, "-");
+       if (status != 0)
+       {
+         ERROR ("udb_result_submit: creating type_instance failed with status %d.",
+             status);
+         return (status);
+       }
        tmp[sizeof (tmp) - 1] = 0;
  
        snprintf (vl.type_instance, sizeof (vl.type_instance), "%s-%s",
    vl.type_instance[sizeof (vl.type_instance) - 1] = 0;
    /* }}} */
  
 +  /* Annotate meta data. {{{ */
 +  if (r->metadata_num > 0)
 +  {
 +    vl.meta = meta_data_create ();
 +    if (vl.meta == NULL)
 +    {
 +      ERROR ("db query utils:: meta_data_create failed.");
 +      return (-ENOMEM);
 +    }
 +
 +    for (i = 0; i < r->metadata_num; i++)
 +    {
 +      status = meta_data_add_string (vl.meta, r->metadata[i],
 +          r_area->metadata_buffer[i]);
 +      if (status != 0)
 +      {
 +        ERROR ("db query utils:: meta_data_add_string failed.");
 +        meta_data_destroy (vl.meta);
 +        vl.meta = NULL;
 +        return (status);
 +      }
 +    }
 +  }
 +  /* }}} */
 +
    plugin_dispatch_values (&vl);
  
 +  if (r->metadata_num > 0)
 +  {
 +    meta_data_destroy (vl.meta);
 +    vl.meta = NULL;
 +  }
    sfree (vl.values);
    return (0);
  } /* }}} void udb_result_submit */
@@@ -309,10 -281,8 +321,10 @@@ static void udb_result_finish_result (u
    prep_area->ds = NULL;
    sfree (prep_area->instances_pos);
    sfree (prep_area->values_pos);
 +  sfree (prep_area->metadata_pos);
    sfree (prep_area->instances_buffer);
    sfree (prep_area->values_buffer);
 +  sfree (prep_area->metadata_buffer);
  } /* }}} void udb_result_finish_result */
  
  static int udb_result_handle_result (udb_result_t *r, /* {{{ */
    for (i = 0; i < r->values_num; i++)
      r_area->values_buffer[i] = column_values[r_area->values_pos[i]];
  
 +  for (i = 0; i < r->metadata_num; i++)
 +    r_area->metadata_buffer[i] = column_values[r_area->metadata_pos[i]];
 +
    return udb_result_submit (r, r_area, q, q_area);
  } /* }}} int udb_result_handle_result */
  
@@@ -349,17 -316,14 +361,17 @@@ static int udb_result_prepare_result (u
    prep_area->ds = NULL; \
    sfree (prep_area->instances_pos); \
    sfree (prep_area->values_pos); \
 +  sfree (prep_area->metadata_pos); \
    sfree (prep_area->instances_buffer); \
    sfree (prep_area->values_buffer); \
 +  sfree (prep_area->metadata_buffer); \
    return (status)
  
    /* Make sure previous preparations are cleaned up. */
    udb_result_finish_result (r, prep_area);
    prep_area->instances_pos = NULL;
    prep_area->values_pos = NULL;
 +  prep_area->metadata_pos = NULL;
  
    /* Read `ds' and check number of values {{{ */
    prep_area->ds = plugin_get_ds (r->type);
    }
    /* }}} */
  
 -  /* Allocate r->instances_pos, r->values_pos, r->instances_buffer, and
 -   * r->values_buffer {{{ */
 +  /* Allocate r->instances_pos, r->values_pos, r->metadata_post,
 +   * r->instances_buffer, r->values_buffer, and r->metadata_buffer {{{ */
    if (r->instances_num > 0)
    {
      prep_area->instances_pos
      ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
      BAIL_OUT (-ENOMEM);
    }
 +
 +  prep_area->metadata_pos
 +    = (size_t *) calloc (r->metadata_num, sizeof (size_t));
 +  if (prep_area->metadata_pos == NULL)
 +  {
 +    ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
 +    BAIL_OUT (-ENOMEM);
 +  }
 +
 +  prep_area->metadata_buffer
 +    = (char **) calloc (r->metadata_num, sizeof (char *));
 +  if (prep_area->metadata_buffer == NULL)
 +  {
 +    ERROR ("db query utils: udb_result_prepare_result: malloc failed.");
 +    BAIL_OUT (-ENOMEM);
 +  }
 +
    /* }}} */
  
    /* Determine the position of the instance columns {{{ */
      }
    } /* }}} for (i = 0; i < r->values_num; i++) */
  
 +  /* Determine the position of the metadata columns {{{ */
 +  for (i = 0; i < r->metadata_num; i++)
 +  {
 +    size_t j;
 +
 +    for (j = 0; j < column_num; j++)
 +    {
 +      if (strcasecmp (r->metadata[i], column_names[j]) == 0)
 +      {
 +        prep_area->metadata_pos[i] = j;
 +        break;
 +      }
 +    }
 +
 +    if (j >= column_num)
 +    {
 +      ERROR ("db query utils: udb_result_prepare_result: "
 +          "Metadata column `%s' could not be found.",
 +          r->values[i]);
 +      BAIL_OUT (-ENOENT);
 +    }
 +  } /* }}} for (i = 0; i < r->metadata_num; i++) */
 +
  #undef BAIL_OUT
    return (0);
  } /* }}} int udb_result_prepare_result */
@@@ -527,10 -451,6 +539,10 @@@ static void udb_result_free (udb_result
      sfree (r->values[i]);
    sfree (r->values);
  
 +  for (i = 0; i < r->metadata_num; i++)
 +    sfree (r->metadata[i]);
 +  sfree (r->metadata);
 +
    udb_result_free (r->next);
  
    sfree (r);
@@@ -561,7 -481,6 +573,7 @@@ static int udb_result_create (const cha
    r->instance_prefix = NULL;
    r->instances = NULL;
    r->values = NULL;
 +  r->metadata = NULL;
    r->next = NULL;
  
    /* Fill the `udb_result_t' structure.. */
        status = udb_config_add_string (&r->instances, &r->instances_num, child);
      else if (strcasecmp ("ValuesFrom", child->key) == 0)
        status = udb_config_add_string (&r->values, &r->values_num, child);
 +    else if (strcasecmp ("MetadataFrom", child->key) == 0)
 +      status = udb_config_add_string (&r->metadata, &r->metadata_num, child);
      else
      {
        WARNING ("db query utils: Query `%s': Option `%s' not allowed here.",