From 9bf72b2afd1e7151cf02def4a5512ebc8626a0cf Mon Sep 17 00:00:00 2001 From: =?utf8?q?Andr=C3=A9s=20J=2E=20D=C3=ADaz?= Date: Mon, 31 Aug 2009 18:06:15 +0200 Subject: [PATCH] =?utf8?q?src/utils=5Fcache.c:=20Implement=20=E2=80=9Cuc?= =?utf8?q?=5F{get,set,inc}=5Fhits=E2=80=9D.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Hi I've attached a patch to add hit counter to thresholds, that is, each time when threhsold raised, then an internal hit counter is incremented, when the value of the counter raise a specific value setted in configuration, then the notification is generated and counter is reset. Here are an example of threshold configuration with hit conter: WarninMax 1 Hits 3 In this example the notification will be generated when load value is greater than 1 for 3 intervals. Here are two patches, the first one adds hit feature to the collectd cached and the second one use hits in thresholds, Of course comments and suggestions are welcome :) Enjoy! Regards, Andres Signed-off-by: Florian Forster --- src/utils_cache.c | 78 +++++++++++++++++++++++++++++++++++++++++++++++ src/utils_cache.h | 3 ++ 2 files changed, 81 insertions(+) diff --git a/src/utils_cache.c b/src/utils_cache.c index 956f8269..9e7bc067 100644 --- a/src/utils_cache.c +++ b/src/utils_cache.c @@ -46,6 +46,7 @@ typedef struct cache_entry_s * (for purding old entries) */ int interval; int state; + int hits; /* * +-----+-----+-----+-----+-----+-----+-----+-----+-----+---- @@ -895,6 +896,83 @@ int uc_get_history (const data_set_t *ds, const value_list_t *vl, return (uc_get_history_by_name (name, ret_history, num_steps, num_ds)); } /* int uc_get_history */ +int uc_get_hits (const data_set_t *ds, const value_list_t *vl) +{ + char name[6 * DATA_MAX_NAME_LEN]; + cache_entry_t *ce = NULL; + int ret = STATE_ERROR; + + if (FORMAT_VL (name, sizeof (name), vl) != 0) + { + ERROR ("uc_get_state: FORMAT_VL failed."); + return (STATE_ERROR); + } + + pthread_mutex_lock (&cache_lock); + + if (c_avl_get (cache_tree, name, (void *) &ce) == 0) + { + assert (ce != NULL); + ret = ce->hits; + } + + pthread_mutex_unlock (&cache_lock); + + return (ret); +} /* int uc_get_hits */ + +int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits) +{ + char name[6 * DATA_MAX_NAME_LEN]; + cache_entry_t *ce = NULL; + int ret = -1; + + if (FORMAT_VL (name, sizeof (name), vl) != 0) + { + ERROR ("uc_get_state: FORMAT_VL failed."); + return (STATE_ERROR); + } + + pthread_mutex_lock (&cache_lock); + + if (c_avl_get (cache_tree, name, (void *) &ce) == 0) + { + assert (ce != NULL); + ret = ce->hits; + ce->hits = hits; + } + + pthread_mutex_unlock (&cache_lock); + + return (ret); +} /* int uc_set_hits */ + +int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step) +{ + char name[6 * DATA_MAX_NAME_LEN]; + cache_entry_t *ce = NULL; + int ret = -1; + + if (FORMAT_VL (name, sizeof (name), vl) != 0) + { + ERROR ("uc_get_state: FORMAT_VL failed."); + return (STATE_ERROR); + } + + pthread_mutex_lock (&cache_lock); + + if (c_avl_get (cache_tree, name, (void *) &ce) == 0) + { + assert (ce != NULL); + ret = ce->hits; + ce->hits = ret + step; + } + + pthread_mutex_unlock (&cache_lock); + + return (ret); +} /* int uc_inc_hits */ + /* * Meta data interface */ diff --git a/src/utils_cache.h b/src/utils_cache.h index 91a6a6d8..f8059eca 100644 --- a/src/utils_cache.h +++ b/src/utils_cache.h @@ -39,6 +39,9 @@ int uc_get_names (char ***ret_names, time_t **ret_times, size_t *ret_number); int uc_get_state (const data_set_t *ds, const value_list_t *vl); int uc_set_state (const data_set_t *ds, const value_list_t *vl, int state); +int uc_get_hits (const data_set_t *ds, const value_list_t *vl); +int uc_set_hits (const data_set_t *ds, const value_list_t *vl, int hits); +int uc_inc_hits (const data_set_t *ds, const value_list_t *vl, int step); int uc_get_history (const data_set_t *ds, const value_list_t *vl, gauge_t *ret_history, size_t num_steps, size_t num_ds); -- 2.30.2