X-Git-Url: https://git.octo.it/?a=blobdiff_plain;f=src%2Fdaemon%2Fplugin.c;h=73d7c8414ce65c8ec6c49dfd6e0c897a201f4553;hb=829683c47113c0f6305c9089424170ff706d047c;hp=6752a6c12a1f4b32718f17e001de4a0d7bb3fe7d;hpb=4e89060ceb1a14ec7f9abfe9caa6b0da7e76bd5c;p=collectd.git diff --git a/src/daemon/plugin.c b/src/daemon/plugin.c index 6752a6c1..73d7c841 100644 --- a/src/daemon/plugin.c +++ b/src/daemon/plugin.c @@ -94,7 +94,7 @@ typedef struct flush_callback_s flush_callback_t; /* * Private variables */ -static c_avl_tree_t *plugins_loaded = NULL; +static c_avl_tree_t *plugins_loaded; static llist_t *list_init; static llist_t *list_write; @@ -104,41 +104,41 @@ static llist_t *list_shutdown; static llist_t *list_log; static llist_t *list_notification; -static fc_chain_t *pre_cache_chain = NULL; -static fc_chain_t *post_cache_chain = NULL; +static fc_chain_t *pre_cache_chain; +static fc_chain_t *post_cache_chain; static c_avl_tree_t *data_sets; -static char *plugindir = NULL; +static char *plugindir; #ifndef DEFAULT_MAX_READ_INTERVAL #define DEFAULT_MAX_READ_INTERVAL TIME_T_TO_CDTIME_T_STATIC(86400) #endif -static c_heap_t *read_heap = NULL; +static c_heap_t *read_heap; static llist_t *read_list; static int read_loop = 1; static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t read_cond = PTHREAD_COND_INITIALIZER; -static pthread_t *read_threads = NULL; -static size_t read_threads_num = 0; +static pthread_t *read_threads; +static size_t read_threads_num; static cdtime_t max_read_interval = DEFAULT_MAX_READ_INTERVAL; static write_queue_t *write_queue_head; static write_queue_t *write_queue_tail; -static long write_queue_length = 0; +static long write_queue_length; static bool write_loop = true; static pthread_mutex_t write_lock = PTHREAD_MUTEX_INITIALIZER; static pthread_cond_t write_cond = PTHREAD_COND_INITIALIZER; -static pthread_t *write_threads = NULL; -static size_t write_threads_num = 0; +static pthread_t *write_threads; +static size_t write_threads_num; static pthread_key_t plugin_ctx_key; static bool plugin_ctx_key_initialized; -static long write_limit_high = 0; -static long write_limit_low = 0; +static long write_limit_high; +static long write_limit_low; -static derive_t stats_values_dropped = 0; +static derive_t stats_values_dropped; static bool record_statistics; /* @@ -292,10 +292,10 @@ static int register_callback(llist_t **list, /* {{{ */ old_cf = le->value; le->value = cf; - WARNING("plugin: register_callback: " - "a callback named `%s' already exists - " - "overwriting the old entry!", - name); + P_WARNING("register_callback: " + "a callback named `%s' already exists - " + "overwriting the old entry!", + name); destroy_callback(old_cf); sfree(key); @@ -315,7 +315,7 @@ static void log_list_callbacks(llist_t **list, /* {{{ */ n = llist_size(*list); if (n == 0) { - INFO("%s [none]", comment); + INFO("%s: [none]", comment); return; } @@ -346,19 +346,22 @@ static void log_list_callbacks(llist_t **list, /* {{{ */ static int create_register_callback(llist_t **list, /* {{{ */ const char *name, void *callback, user_data_t const *ud) { - callback_func_t *cf; - cf = calloc(1, sizeof(*cf)); + if (name == NULL || callback == NULL) + return EINVAL; + + callback_func_t *cf = calloc(1, sizeof(*cf)); if (cf == NULL) { free_userdata(ud); ERROR("plugin: create_register_callback: calloc failed."); - return -1; + return ENOMEM; } cf->cf_callback = callback; if (ud == NULL) { - cf->cf_udata.data = NULL; - cf->cf_udata.free_func = NULL; + cf->cf_udata = (user_data_t){ + .data = NULL, .free_func = NULL, + }; } else { cf->cf_udata = *ud; } @@ -710,25 +713,8 @@ plugin_value_list_clone(value_list_t const *vl_orig) /* {{{ */ vl->time = cdtime(); /* Fill in the interval from the thread context, if it is zero. */ - if (vl->interval == 0) { - plugin_ctx_t ctx = plugin_get_ctx(); - - if (ctx.interval != 0) - vl->interval = ctx.interval; - else { - char name[6 * DATA_MAX_NAME_LEN]; - FORMAT_VL(name, sizeof(name), vl); - ERROR("plugin_value_list_clone: Unable to determine " - "interval from context for " - "value list \"%s\". " - "This indicates a broken plugin. " - "Please report this problem to the " - "collectd mailing list or at " - ".", - name); - vl->interval = cf_get_default_interval(); - } - } + if (vl->interval == 0) + vl->interval = plugin_get_interval(); return vl; } /* }}} value_list_t *plugin_value_list_clone */ @@ -861,7 +847,7 @@ static void stop_write_threads(void) /* {{{ */ INFO("collectd: Stopping %" PRIsz " write threads.", write_threads_num); pthread_mutex_lock(&write_lock); - write_loop = 0; + write_loop = false; DEBUG("plugin: stop_write_threads: Signalling `write_cond'"); pthread_cond_broadcast(&write_cond); pthread_mutex_unlock(&write_lock); @@ -988,7 +974,7 @@ int plugin_load(char const *plugin_name, bool global) { */ if ((strcasecmp("perl", plugin_name) == 0) || (strcasecmp("python", plugin_name) == 0)) - global = 1; + global = true; /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the * type when matching the filename */ @@ -1111,9 +1097,9 @@ static int plugin_insert_read(read_func_t *rf) { le = llist_search(read_list, rf->rf_name); if (le != NULL) { pthread_mutex_unlock(&read_lock); - WARNING("The read function \"%s\" is already registered. " - "Check for duplicates in your configuration!", - rf->rf_name); + P_WARNING("The read function \"%s\" is already registered. " + "Check for duplicates in your configuration!", + rf->rf_name); return EINVAL; } @@ -1159,6 +1145,7 @@ int plugin_register_read(const char *name, int (*callback)(void)) { rf->rf_name = strdup(name); rf->rf_type = RF_SIMPLE; rf->rf_interval = plugin_get_interval(); + rf->rf_ctx.interval = rf->rf_interval; status = plugin_insert_read(rf); if (status != 0) { @@ -1200,6 +1187,7 @@ int plugin_register_complex_read(const char *group, const char *name, } rf->rf_ctx = plugin_get_ctx(); + rf->rf_ctx.interval = rf->rf_interval; status = plugin_insert_read(rf); if (status != 0) { @@ -1545,7 +1533,7 @@ int plugin_init_all(void) { uc_init(); if (IS_TRUE(global_option_get("CollectInternalStats"))) { - record_statistics = 1; + record_statistics = true; plugin_register_read("collectd", plugin_update_internal_statistics); } @@ -1714,8 +1702,12 @@ int plugin_write(const char *plugin, /* {{{ */ callback_func_t *cf = le->value; plugin_write_cb callback; - /* do not switch plugin context; rather keep the context (interval) - * information of the calling read plugin */ + /* Keep the read plugin's interval and flush information but update the + * plugin name. */ + plugin_ctx_t old_ctx = plugin_get_ctx(); + plugin_ctx_t ctx = old_ctx; + ctx.name = cf->cf_ctx.name; + plugin_set_ctx(ctx); DEBUG("plugin: plugin_write: Writing values via %s.", le->key); callback = cf->cf_callback; @@ -1725,6 +1717,7 @@ int plugin_write(const char *plugin, /* {{{ */ else success++; + plugin_set_ctx(old_ctx); le = le->next; } @@ -1890,7 +1883,7 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { int status; static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC; - bool free_meta_data = 0; + bool free_meta_data = false; assert(vl != NULL); @@ -1910,7 +1903,7 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { * this case matches and targets may add some and the calling function * may not expect (and therefore free) that data. */ if (vl->meta == NULL) - free_meta_data = 1; + free_meta_data = true; if (list_write == NULL) c_complain_once(LOG_WARNING, &no_write_complaint, @@ -1994,7 +1987,7 @@ static int plugin_dispatch_values_internal(value_list_t *vl) { } else fc_default_action(ds, vl); - if ((free_meta_data != 0) && (vl->meta != NULL)) { + if ((free_meta_data == true) && (vl->meta != NULL)) { meta_data_destroy(vl->meta); vl->meta = NULL; } @@ -2025,7 +2018,7 @@ static double get_drop_probability(void) /* {{{ */ static bool check_drop_value(void) /* {{{ */ { - static cdtime_t last_message_time = 0; + static cdtime_t last_message_time; static pthread_mutex_t last_message_lock = PTHREAD_MUTEX_INITIALIZER; double p; @@ -2033,11 +2026,11 @@ static bool check_drop_value(void) /* {{{ */ int status; if (write_limit_high == 0) - return 0; + return false; p = get_drop_probability(); if (p == 0.0) - return 0; + return false; status = pthread_mutex_trylock(&last_message_lock); if (status == 0) { @@ -2054,13 +2047,13 @@ static bool check_drop_value(void) /* {{{ */ } if (p == 1.0) - return 1; + return true; q = cdrand_d(); if (q > p) - return 1; + return true; else - return 0; + return false; } /* }}} bool check_drop_value */ int plugin_dispatch_values(value_list_t const *vl) { @@ -2236,6 +2229,21 @@ void plugin_log(int level, const char *format, ...) { } } /* void plugin_log */ +void daemon_log(int level, const char *format, ...) { + char msg[1024] = ""; // Size inherits from plugin_log() + + char const *name = plugin_get_ctx().name; + if (name == NULL) + name = "UNKNOWN"; + + va_list ap; + va_start(ap, format); + vsnprintf(msg, sizeof(msg), format, ap); + va_end(ap); + + plugin_log(level, "%s plugin: %s", name, msg); +} /* void daemon_log */ + int parse_log_severity(const char *severity) { int log_level = -1; @@ -2275,7 +2283,7 @@ const data_set_t *plugin_get_ds(const char *name) { data_set_t *ds; if (data_sets == NULL) { - ERROR("plugin_get_ds: No data sets are defined yet."); + P_ERROR("plugin_get_ds: No data sets are defined yet."); return NULL; } @@ -2460,7 +2468,7 @@ static plugin_ctx_t *plugin_ctx_create(void) { void plugin_init_ctx(void) { pthread_key_create(&plugin_ctx_key, plugin_ctx_destructor); - plugin_ctx_key_initialized = 1; + plugin_ctx_key_initialized = true; } /* void plugin_init_ctx */ plugin_ctx_t plugin_get_ctx(void) { @@ -2506,6 +2514,8 @@ cdtime_t plugin_get_interval(void) { if (interval > 0) return interval; + P_ERROR("plugin_get_interval: Unable to determine Interval from context."); + return cf_get_default_interval(); } /* cdtime_t plugin_get_interval */