X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Futils_cache.c;h=910e30b29cbec6d99974eae30bb0f9418bb80a16;hb=58a503a9659c95813d0eb09382e3478b242f2de8;hp=7e1dcf758af50785e0cb5551989a91e2c38373ec;hpb=af94242000c1f54c0f752bb66bcecfcced4080f0;p=collectd.git diff --git a/src/utils_cache.c b/src/utils_cache.c index 7e1dcf75..910e30b2 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -24,7 +24,6 @@ #include "plugin.h" #include "utils_avltree.h" #include "utils_cache.h" -#include "utils_threshold.h" #include "meta_data.h" #include @@ -120,81 +119,6 @@ static void cache_free (cache_entry_t *ce) sfree (ce); } /* void cache_free */ -__attribute__((unused)) -static int uc_send_notification (const char *name) -{ - cache_entry_t *ce = NULL; - int status; - - char *name_copy; - char *host; - char *plugin; - char *plugin_instance; - char *type; - char *type_instance; - - notification_t n; - - name_copy = strdup (name); - if (name_copy == NULL) - { - ERROR ("uc_send_notification: strdup failed."); - return (-1); - } - - status = parse_identifier (name_copy, &host, - &plugin, &plugin_instance, - &type, &type_instance); - if (status != 0) - { - ERROR ("uc_send_notification: Cannot parse name `%s'", name); - return (-1); - } - - /* Copy the associative members */ - notification_init (&n, NOTIF_FAILURE, /* host = */ NULL, - host, plugin, plugin_instance, type, type_instance); - - sfree (name_copy); - name_copy = host = plugin = plugin_instance = type = type_instance = NULL; - - pthread_mutex_lock (&cache_lock); - - /* - * Set the time _after_ getting the lock because we don't know how long - * acquiring the lock takes and we will use this time later to decide - * whether or not the state is OKAY. - */ - n.time = cdtime (); - - status = c_avl_get (cache_tree, name, (void *) &ce); - if (status != 0) - { - pthread_mutex_unlock (&cache_lock); - sfree (name_copy); - return (-1); - } - - /* Check if the entry has been updated in the meantime */ - if ((n.time - ce->last_update) < (timeout_g * ce->interval)) - { - ce->state = STATE_OKAY; - pthread_mutex_unlock (&cache_lock); - sfree (name_copy); - return (-1); - } - - ssnprintf (n.message, sizeof (n.message), - "%s has not been updated for %.3f seconds.", name, - CDTIME_T_TO_DOUBLE (n.time - ce->last_update)); - - pthread_mutex_unlock (&cache_lock); - - plugin_dispatch_notification (&n); - - return (0); -} /* int uc_send_notification */ - static void uc_check_range (const data_set_t *ds, cache_entry_t *ce) { int i; @@ -267,6 +191,7 @@ static int uc_insert (const data_set_t *ds, const value_list_t *vl, /* This shouldn't happen. */ ERROR ("uc_insert: Don't know how to handle data source type %i.", ds->ds[i].type); + sfree (key_copy); return (-1); } /* switch (ds->ds[i].type) */ } /* for (i) */ @@ -372,7 +297,13 @@ int uc_check_timeout (void) pthread_mutex_unlock (&cache_lock); if (keys_len == 0) + { + /* realloc() may have been called for these. */ + sfree (keys); + sfree (keys_time); + sfree (keys_interval); return (0); + } /* Call the "missing" callback for each value. Do this before removing the * value from the cache, so that callbacks can still access the data stored, @@ -391,7 +322,6 @@ int uc_check_timeout (void) if (status != 0) { ERROR ("uc_check_timeout: parse_identifier_vl (\"%s\") failed.", keys[i]); - cache_free (ce); continue; } @@ -436,9 +366,6 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) { char name[6 * DATA_MAX_NAME_LEN]; cache_entry_t *ce = NULL; - int send_okay_notification = 0; - cdtime_t update_delay = 0; - notification_t n; int status; int i; @@ -472,15 +399,6 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) return (-1); } - /* Send a notification (after the lock has been released) if we switch the - * state from something else to `okay'. */ - if (ce->state == STATE_MISSING) - { - send_okay_notification = 1; - ce->state = STATE_OKAY; - update_delay = cdtime () - ce->last_update; - } - for (i = 0; i < ds->ds_num; i++) { switch (ds->ds[i].type) @@ -567,28 +485,6 @@ int uc_update (const data_set_t *ds, const value_list_t *vl) pthread_mutex_unlock (&cache_lock); - if (send_okay_notification == 0) - return (0); - - /* Do not send okay notifications for uninteresting values, i. e. values for - * which no threshold is configured. */ - status = ut_check_interesting (name); - if (status <= 0) - return (0); - - /* Initialize the notification */ - memset (&n, '\0', sizeof (n)); - NOTIFICATION_INIT_VL (&n, vl, ds); - - n.severity = NOTIF_OKAY; - n.time = vl->time; - - ssnprintf (n.message, sizeof (n.message), - "Received a value for %s. It was missing for %u seconds.", - name, (unsigned int) update_delay); - - plugin_dispatch_notification (&n); - return (0); } /* int uc_update */ @@ -682,6 +578,7 @@ int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number) char **names = NULL; cdtime_t *times = NULL; size_t number = 0; + size_t size_arrays = 0; int status = 0; @@ -690,42 +587,47 @@ int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number) pthread_mutex_lock (&cache_lock); + size_arrays = (size_t) c_avl_size (cache_tree); + if (size_arrays < 1) + { + /* Handle the "no values" case here, to avoid the error message when + * calloc() returns NULL. */ + pthread_mutex_unlock (&cache_lock); + return (0); + } + + names = calloc (size_arrays, sizeof (*names)); + times = calloc (size_arrays, sizeof (*times)); + if ((names == NULL) || (times == NULL)) + { + ERROR ("uc_get_names: calloc failed."); + sfree (names); + sfree (times); + pthread_mutex_unlock (&cache_lock); + return (ENOMEM); + } + iter = c_avl_get_iterator (cache_tree); while (c_avl_iterator_next (iter, (void *) &key, (void *) &value) == 0) { - char **temp; - /* remove missing values when list values */ if (value->state == STATE_MISSING) continue; - if (ret_times != NULL) - { - cdtime_t *tmp_times; + /* c_avl_size does not return a number smaller than the number of elements + * returned by c_avl_iterator_next. */ + assert (number < size_arrays); - tmp_times = (cdtime_t *) realloc (times, sizeof (cdtime_t) * (number + 1)); - if (tmp_times == NULL) - { - status = -1; - break; - } - times = tmp_times; + if (ret_times != NULL) times[number] = value->last_time; - } - temp = (char **) realloc (names, sizeof (char *) * (number + 1)); - if (temp == NULL) - { - status = -1; - break; - } - names = temp; names[number] = strdup (key); if (names[number] == NULL) { status = -1; break; } + number++; } /* while (c_avl_iterator_next) */ @@ -735,12 +637,13 @@ int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number) if (status != 0) { size_t i; - + for (i = 0; i < number; i++) { sfree (names[i]); } sfree (names); + sfree (times); return (-1); } @@ -748,6 +651,8 @@ int uc_get_names (char ***ret_names, cdtime_t **ret_times, size_t *ret_number) *ret_names = names; if (ret_times != NULL) *ret_times = times; + else + sfree (times); *ret_number = number; return (0);