X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;ds=sidebyside;f=src%2Fplugin.c;h=23ea9017cce27aa71370c32b41d7496b0944c837;hb=c96e6f04275cad2ba1622555f3f4e58bda4d92df;hp=86ffbde7bc395dd20d211a8b7711f3100f241257;hpb=0b309ea35fcd30a8ba14b5af2295e3f3b1da2a3f;p=collectd.git diff --git a/src/plugin.c b/src/plugin.c index 86ffbde7..23ea9017 100644 --- a/src/plugin.c +++ b/src/plugin.c @@ -133,7 +133,7 @@ static int plugin_load_file (char *file) const char *error = lt_dlerror (); ERROR ("lt_dlopen failed: %s", error); - DEBUG ("lt_dlopen failed: %s", error); + fprintf (stderr, "lt_dlopen failed: %s\n", error); return (1); } @@ -186,13 +186,15 @@ static void *plugin_read_thread (void *args) if (status != 0) { + if (rf->wait_time < interval_g) + rf->wait_time = interval_g; rf->wait_left = rf->wait_time; rf->wait_time = rf->wait_time * 2; if (rf->wait_time > 86400) rf->wait_time = 86400; NOTICE ("read-function of plugin `%s' " - "failed. Will syspend it for %i " + "failed. Will suspend it for %i " "seconds.", le->key, rf->wait_left); } else @@ -266,7 +268,7 @@ static void stop_threads (void) { ERROR ("plugin: stop_threads: pthread_join failed."); } - read_threads[i] = -1; + read_threads[i] = (pthread_t) 0; } sfree (read_threads); read_threads_num = 0; @@ -354,6 +356,10 @@ int plugin_load (const char *type) ret = 0; break; } + else + { + fprintf (stderr, "Unable to load plugin %s.\n", type); + } } closedir (dh); @@ -372,6 +378,12 @@ int plugin_register_config (const char *name, return (0); } /* int plugin_register_config */ +int plugin_register_complex_config (const char *type, + int (*callback) (oconfig_item_t *)) +{ + return (cf_register_complex (type, callback)); +} /* int plugin_register_complex_config */ + int plugin_register_init (const char *name, int (*callback) (void)) { @@ -415,7 +427,33 @@ int plugin_register_shutdown (char *name, int plugin_register_data_set (const data_set_t *ds) { - return (register_callback (&list_data_set, ds->type, (void *) ds)); + data_set_t *ds_copy; + int i; + + if ((list_data_set != NULL) + && (llist_search (list_data_set, ds->type) != NULL)) + { + NOTICE ("Replacing DS `%s' with another version.", ds->type); + plugin_unregister_data_set (ds->type); + } + + ds_copy = (data_set_t *) malloc (sizeof (data_set_t)); + if (ds_copy == NULL) + return (-1); + memcpy(ds_copy, ds, sizeof (data_set_t)); + + ds_copy->ds = (data_source_t *) malloc (sizeof (data_source_t) + * ds->ds_num); + if (ds_copy->ds == NULL) + { + free (ds_copy); + return (-1); + } + + for (i = 0; i < ds->ds_num; i++) + memcpy (ds_copy->ds + i, ds->ds + i, sizeof (data_source_t)); + + return (register_callback (&list_data_set, ds->type, (void *) ds_copy)); } /* int plugin_register_data_set */ int plugin_register_log (char *name, @@ -430,6 +468,12 @@ int plugin_unregister_config (const char *name) return (0); } /* int plugin_unregister_config */ +int plugin_unregister_complex_config (const char *name) +{ + cf_unregister_complex (name); + return (0); +} /* int plugin_unregister_complex_config */ + int plugin_unregister_init (const char *name) { return (plugin_unregister (list_init, name)); @@ -437,7 +481,6 @@ int plugin_unregister_init (const char *name) int plugin_unregister_read (const char *name) { - return (plugin_unregister (list_read, name)); llentry_t *e; e = llist_search (list_read, name); @@ -464,8 +507,26 @@ int plugin_unregister_shutdown (const char *name) int plugin_unregister_data_set (const char *name) { - return (plugin_unregister (list_data_set, name)); -} + llentry_t *e; + data_set_t *ds; + + if (list_data_set == NULL) + return (-1); + + e = llist_search (list_data_set, name); + + if (e == NULL) + return (-1); + + llist_remove (list_data_set, e); + ds = (data_set_t *) e->value; + llentry_destroy (e); + + sfree (ds->ds); + sfree (ds); + + return (0); +} /* int plugin_unregister_data_set */ int plugin_unregister_log (const char *name) { @@ -494,15 +555,16 @@ void plugin_init_all (void) le = llist_head (list_init); while (le != NULL) { - callback = le->value; + callback = (int (*) (void)) le->value; status = (*callback) (); if (status != 0) { ERROR ("Initialization of plugin `%s' " "failed with status %i. " - "Plugin will be unloaded. TODO!", + "Plugin will be unloaded.", le->key, status); + /* FIXME: Unload _all_ functions */ plugin_unregister_read (le->key); } @@ -526,7 +588,10 @@ void plugin_read_all (const int *loop) rf = (read_func_t *) le->value; if (rf->needs_read != DONE) + { + le = le->next; continue; + } if (rf->wait_left > 0) rf->wait_left -= interval_g; @@ -557,20 +622,25 @@ void plugin_shutdown_all (void) le = llist_head (list_shutdown); while (le != NULL) { - callback = le->value; - (*callback) (); + callback = (int (*) (void)) le->value; + /* Advance the pointer before calling the callback allows + * shutdown functions to unregister themselves. If done the + * other way around the memory `le' points to will be freed + * after callback returns. */ le = le->next; + + (*callback) (); } } /* void plugin_shutdown_all */ -int plugin_dispatch_values (const char *name, const value_list_t *vl) +int plugin_dispatch_values (const char *name, value_list_t *vl) { int (*callback) (const data_set_t *, const value_list_t *); data_set_t *ds; llentry_t *le; - if (list_write == NULL) + if ((list_write == NULL) || (list_data_set == NULL)) return (-1); le = llist_search (list_data_set, name); @@ -582,24 +652,43 @@ int plugin_dispatch_values (const char *name, const value_list_t *vl) ds = (data_set_t *) le->value; - DEBUG ("plugin: plugin_dispatch_values: time = %u; host = %s; " - "plugin = %s; plugin_instance = %s; type = %s; " - "type_instance = %s;", - (unsigned int) vl->time, vl->host, + DEBUG ("plugin: plugin_dispatch_values: time = %u; interval = %i; " + "host = %s; " + "plugin = %s; plugin_instance = %s; " + "type = %s; type_instance = %s;", + (unsigned int) vl->time, vl->interval, + vl->host, vl->plugin, vl->plugin_instance, ds->type, vl->type_instance); +#if COLLECT_DEBUG + assert (ds->ds_num == vl->values_len); +#else + if (ds->ds_num != vl->values_len) + { + ERROR ("plugin: ds->type = %s: (ds->ds_num = %i) != " + "(vl->values_len = %i)", + ds->type, ds->ds_num, vl->values_len); + return (-1); + } +#endif + + escape_slashes (vl->host, sizeof (vl->host)); + escape_slashes (vl->plugin, sizeof (vl->plugin)); + escape_slashes (vl->plugin_instance, sizeof (vl->plugin_instance)); + escape_slashes (vl->type_instance, sizeof (vl->type_instance)); + le = llist_head (list_write); while (le != NULL) { - callback = le->value; + callback = (int (*) (const data_set_t *, const value_list_t *)) le->value; (*callback) (ds, vl); le = le->next; } return (0); -} +} /* int plugin_dispatch_values */ void plugin_log (int level, const char *format, ...) { @@ -625,7 +714,7 @@ void plugin_log (int level, const char *format, ...) le = llist_head (list_log); while (le != NULL) { - callback = le->value; + callback = (void (*) (int, const char *)) le->value; (*callback) (level, msg); le = le->next;