X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Frrdtool.c;h=4a3c345ef3ae34f198f8ce89f5b4806e2fe51b95;hb=02f8dbc0985310574ea2837cd5e3c06cccda203d;hp=71da54749fe1d1a6377bbe450551e04658e433bb;hpb=443afcad2cbacaca9bb1fd7d9ea790cbeb6ff015;p=collectd.git diff --git a/src/rrdtool.c b/src/rrdtool.c index 71da5474..094dd42a 100644 --- a/src/rrdtool.c +++ b/src/rrdtool.c @@ -24,6 +24,7 @@ **/ #include "collectd.h" + #include "plugin.h" #include "common.h" #include "utils_avltree.h" @@ -32,10 +33,6 @@ #include -#if HAVE_PTHREAD_H -# include -#endif - /* * Private types */ @@ -166,7 +163,7 @@ static int srrd_update (char *filename, char *template, assert (template == NULL); new_argc = 2 + argc; - new_argv = (char **) malloc ((new_argc + 1) * sizeof (char *)); + new_argv = malloc ((new_argc + 1) * sizeof (*new_argv)); if (new_argv == NULL) { ERROR ("rrdtool plugin: malloc failed."); @@ -198,13 +195,12 @@ 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; int status; time_t tt; - int i; memset (buffer, '\0', buffer_len); @@ -214,7 +210,7 @@ static int value_list_to_string (char *buffer, int buffer_len, return (-1); offset = status; - for (i = 0; i < ds->ds_num; i++) + for (size_t i = 0; i < ds->ds_num; i++) { if ((ds->ds[i].type != DS_TYPE_COUNTER) && (ds->ds[i].type != DS_TYPE_GAUGE) @@ -227,7 +223,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 +238,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, @@ -296,7 +331,6 @@ static void *rrd_queue_thread (void __attribute__((unused)) *data) char **values; int values_num; int status; - int i; values = NULL; values_num = 0; @@ -377,7 +411,7 @@ static void *rrd_queue_thread (void __attribute__((unused)) *data) pthread_mutex_unlock (&queue_lock); /* We now need the cache lock so the entry isn't updated while - * we make a copy of it's values */ + * we make a copy of its values */ pthread_mutex_lock (&cache_lock); status = c_avl_get (cache, queue_entry->filename, @@ -403,7 +437,7 @@ static void *rrd_queue_thread (void __attribute__((unused)) *data) } /* Update `tv_next_update' */ - if (write_rate > 0.0) + if (write_rate > 0.0) { gettimeofday (&tv_now, /* timezone = */ NULL); tv_next_update.tv_sec = tv_now.tv_sec; @@ -423,7 +457,7 @@ static void *rrd_queue_thread (void __attribute__((unused)) *data) values_num, (values_num == 1) ? "" : "s", queue_entry->filename); - for (i = 0; i < values_num; i++) + for (int i = 0; i < values_num; i++) { sfree (values[i]); } @@ -441,7 +475,7 @@ static int rrd_queue_enqueue (const char *filename, { rrd_queue_t *queue_entry; - queue_entry = (rrd_queue_t *) malloc (sizeof (rrd_queue_t)); + queue_entry = malloc (sizeof (*queue_entry)); if (queue_entry == NULL) return (-1); @@ -483,7 +517,7 @@ static int rrd_queue_dequeue (const char *filename, { if (strcmp (this->filename, filename) == 0) break; - + prev = this; this = this->next; } @@ -521,7 +555,6 @@ static void rrd_cache_flush (cdtime_t timeout) char *key; c_avl_iterator_t *iter; - int i; DEBUG ("rrdtool plugin: Flushing cache, timeout = %.3f", CDTIME_T_TO_DOUBLE (timeout)); @@ -549,7 +582,7 @@ static void rrd_cache_flush (cdtime_t timeout) } else /* ancient and no values -> waste of memory */ { - char **tmp = (char **) realloc ((void *) keys, + char **tmp = realloc (keys, (keys_num + 1) * sizeof (char *)); if (tmp == NULL) { @@ -568,8 +601,8 @@ static void rrd_cache_flush (cdtime_t timeout) } } /* while (c_avl_iterator_next) */ c_avl_iterator_destroy (iter); - - for (i = 0; i < keys_num; i++) + + for (int i = 0; i < keys_num; i++) { if (c_avl_remove (cache, keys[i], (void *) &key, (void *) &rc) != 0) { @@ -653,7 +686,7 @@ static int64_t rrd_get_random_variation (void) long min; long max; - if (random_timeout <= 0) + if (random_timeout == 0) return (0); /* Assure that "cache_timeout + random_variation" is never negative. */ @@ -694,7 +727,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 +740,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); @@ -713,7 +750,7 @@ static int rrd_cache_insert (const char *filename, return (-1); } - values_new = (char **) realloc ((void *) rc->values, + values_new = realloc ((void *) rc->values, (rc->values_num + 1) * sizeof (char *)); if (values_new == NULL) { @@ -817,7 +854,6 @@ static int rrd_cache_destroy (void) /* {{{ */ while (c_avl_pick (cache, &key, &value) == 0) { rrd_cache_t *rc; - int i; sfree (key); key = NULL; @@ -828,7 +864,7 @@ static int rrd_cache_destroy (void) /* {{{ */ if (rc->values_num > 0) non_empty++; - for (i = 0; i < rc->values_num; i++) + for (int i = 0; i < rc->values_num; i++) sfree (rc->values[i]); sfree (rc->values); sfree (rc); @@ -965,23 +1001,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) { @@ -1031,7 +1080,7 @@ static int rrd_config (const char *key, const char *value) while ((ptr = strtok_r (dummy, ", \t", &saveptr)) != NULL) { dummy = NULL; - + tmp_alloc = realloc (rrdcreate_config.timespans, sizeof (int) * (rrdcreate_config.timespans_num + 1)); if (tmp_alloc == NULL) @@ -1164,6 +1213,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); }