write_kafka: Fix misleading indentation
[collectd.git] / src / rrdtool.c
index 71da547..7cfdae6 100644 (file)
@@ -198,7 +198,7 @@ static int srrd_update (char *filename, char *template,
 } /* int srrd_update */
 #endif /* !HAVE_THREADSAFE_LIBRRD */
 
-static int value_list_to_string (char *buffer, int buffer_len,
+static int value_list_to_string_multiple (char *buffer, int buffer_len,
                const data_set_t *ds, const value_list_t *vl)
 {
        int offset;
@@ -227,7 +227,7 @@ static int value_list_to_string (char *buffer, int buffer_len,
                                        ":%llu", vl->values[i].counter);
                else if (ds->ds[i].type == DS_TYPE_GAUGE)
                        status = ssnprintf (buffer + offset, buffer_len - offset,
-                                       ":%lf", vl->values[i].gauge);
+                                       ":"GAUGE_FORMAT, vl->values[i].gauge);
                else if (ds->ds[i].type == DS_TYPE_DERIVE)
                        status = ssnprintf (buffer + offset, buffer_len - offset,
                                        ":%"PRIi64, vl->values[i].derive);
@@ -242,6 +242,45 @@ static int value_list_to_string (char *buffer, int buffer_len,
        } /* for ds->ds_num */
 
        return (0);
+} /* int value_list_to_string_multiple */
+
+static int value_list_to_string (char *buffer, int buffer_len,
+               const data_set_t *ds, const value_list_t *vl)
+{
+       int status;
+       time_t tt;
+
+       if (ds->ds_num != 1)
+               return (value_list_to_string_multiple (buffer, buffer_len,
+                                       ds, vl));
+
+       tt = CDTIME_T_TO_TIME_T (vl->time);
+       switch (ds->ds[0].type)
+       {
+               case DS_TYPE_DERIVE:
+                       status = ssnprintf (buffer, buffer_len, "%u:%"PRIi64,
+                               (unsigned) tt, vl->values[0].derive);
+                       break;
+               case DS_TYPE_GAUGE:
+                       status = ssnprintf (buffer, buffer_len, "%u:"GAUGE_FORMAT,
+                               (unsigned) tt, vl->values[0].gauge);
+                       break;
+               case DS_TYPE_COUNTER:
+                       status = ssnprintf (buffer, buffer_len, "%u:%llu",
+                               (unsigned) tt, vl->values[0].counter);
+                       break;
+               case DS_TYPE_ABSOLUTE:
+                       status = ssnprintf (buffer, buffer_len, "%u:%"PRIu64,
+                               (unsigned) tt, vl->values[0].absolute);
+                       break;
+               default:
+                       return (EINVAL);
+       }
+
+       if ((status < 1) || (status >= buffer_len))
+               return (ENOMEM);
+
+       return (0);
 } /* int value_list_to_string */
 
 static int value_list_to_filename (char *buffer, size_t buffer_size,
@@ -694,7 +733,10 @@ static int rrd_cache_insert (const char *filename,
        {
                rc = malloc (sizeof (*rc));
                if (rc == NULL)
+               {
+                       pthread_mutex_unlock (&cache_lock);
                        return (-1);
+               }
                rc->values_num = 0;
                rc->values = NULL;
                rc->first_value = 0;
@@ -704,6 +746,7 @@ static int rrd_cache_insert (const char *filename,
                new_rc = 1;
        }
 
+       assert (value_time > 0); /* plugin_dispatch() ensures this. */
        if (rc->last_value >= value_time)
        {
                pthread_mutex_unlock (&cache_lock);
@@ -965,23 +1008,36 @@ static int rrd_config (const char *key, const char *value)
        }
        else if (strcasecmp ("DataDir", key) == 0)
        {
-               if (datadir != NULL)
-                       free (datadir);
-               datadir = strdup (value);
+               char *tmp;
+               size_t len;
+
+               tmp = strdup (value);
+               if (tmp == NULL)
+               {
+                       ERROR ("rrdtool plugin: strdup failed.");
+                       return (1);
+               }
+
+               len = strlen (tmp);
+               while ((len > 0) && (tmp[len - 1] == '/'))
+               {
+                       len--;
+                       tmp[len] = 0;
+               }
+
+               if (len == 0)
+               {
+                       ERROR ("rrdtool plugin: Invalid \"DataDir\" option.");
+                       sfree (tmp);
+                       return (1);
+               }
+
                if (datadir != NULL)
                {
-                       int len = strlen (datadir);
-                       while ((len > 0) && (datadir[len - 1] == '/'))
-                       {
-                               len--;
-                               datadir[len] = '\0';
-                       }
-                       if (len <= 0)
-                       {
-                               free (datadir);
-                               datadir = NULL;
-                       }
+                       sfree (datadir);
                }
+
+               datadir = tmp;
        }
        else if (strcasecmp ("StepSize", key) == 0)
        {
@@ -1164,6 +1220,7 @@ static int rrd_init (void)
        cache = c_avl_create ((int (*) (const void *, const void *)) strcmp);
        if (cache == NULL)
        {
+               pthread_mutex_unlock (&cache_lock);
                ERROR ("rrdtool plugin: c_avl_create failed.");
                return (-1);
        }