src/utils_llist.[ch]: Changed the semantic so module doesn't copy the `key'.
[collectd.git] / src / plugin.c
index 64dbf78..44bdc8f 100644 (file)
@@ -31,6 +31,8 @@
 #include "plugin.h"
 #include "configfile.h"
 #include "utils_llist.h"
+#include "utils_cache.h"
+#include "utils_threshold.h"
 
 /*
  * Private structures
@@ -53,6 +55,7 @@ static llist_t *list_write;
 static llist_t *list_shutdown;
 static llist_t *list_data_set;
 static llist_t *list_log;
+static llist_t *list_notification;
 
 static char *plugindir = NULL;
 
@@ -76,6 +79,7 @@ static const char *plugin_get_dir (void)
 static int register_callback (llist_t **list, const char *name, void *callback)
 {
        llentry_t *le;
+       char *key;
 
        if ((*list == NULL)
                        && ((*list = llist_create ()) == NULL))
@@ -84,9 +88,16 @@ static int register_callback (llist_t **list, const char *name, void *callback)
        le = llist_search (*list, name);
        if (le == NULL)
        {
-               le = llentry_create (name, callback);
+               key = strdup (name);
+               if (key == NULL)
+                       return (-1);
+
+               le = llentry_create (key, callback);
                if (le == NULL)
+               {
+                       free (key);
                        return (-1);
+               }
 
                llist_append (*list, le);
        }
@@ -108,6 +119,7 @@ static int plugin_unregister (llist_t *list, const char *name)
                return (-1);
 
        llist_remove (list, e);
+       free (e->key);
        llentry_destroy (e);
 
        return (0);
@@ -133,7 +145,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);
        }
 
@@ -194,7 +206,7 @@ static void *plugin_read_thread (void *args)
                                        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
@@ -356,6 +368,10 @@ int plugin_load (const char *type)
                        ret = 0;
                        break;
                }
+               else
+               {
+                       fprintf (stderr, "Unable to load plugin %s.\n", type);
+               }
        }
 
        closedir (dh);
@@ -374,6 +390,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))
 {
@@ -452,12 +474,24 @@ int plugin_register_log (char *name,
        return (register_callback (&list_log, name, (void *) callback));
 } /* int plugin_register_log */
 
+int plugin_register_notification (const char *name,
+               int (*callback) (const notification_t *notif))
+{
+       return (register_callback (&list_log, name, (void *) callback));
+} /* int plugin_register_log */
+
 int plugin_unregister_config (const char *name)
 {
        cf_unregister (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));
@@ -474,6 +508,7 @@ int plugin_unregister_read (const char *name)
 
        llist_remove (list_read, e);
        free (e->value);
+       free (e->key);
        llentry_destroy (e);
 
        return (0);
@@ -504,6 +539,7 @@ int plugin_unregister_data_set (const char *name)
 
        llist_remove (list_data_set, e);
        ds = (data_set_t *) e->value;
+       free (e->key);
        llentry_destroy (e);
 
        sfree (ds->ds);
@@ -517,6 +553,11 @@ int plugin_unregister_log (const char *name)
        return (plugin_unregister (list_log, name));
 }
 
+int plugin_unregister_notification (const char *name)
+{
+       return (plugin_unregister (list_notification, name));
+}
+
 void plugin_init_all (void)
 {
        int (*callback) (void);
@@ -533,6 +574,9 @@ void plugin_init_all (void)
                start_threads ((num > 0) ? num : 5);
        }
 
+       /* Init the value cache */
+       uc_init ();
+
        if (list_init == NULL)
                return;
 
@@ -607,13 +651,18 @@ void plugin_shutdown_all (void)
        while (le != NULL)
        {
                callback = (int (*) (void)) le->value;
-               (*callback) ();
 
+               /* 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;
@@ -631,10 +680,12 @@ 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);
 
@@ -650,6 +701,15 @@ int plugin_dispatch_values (const char *name, const value_list_t *vl)
        }
 #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));
+
+       /* Update the value cache */
+       uc_update (ds, vl);
+       ut_check_threshold (ds, vl);
+
        le = llist_head (list_write);
        while (le != NULL)
        {
@@ -660,7 +720,34 @@ int plugin_dispatch_values (const char *name, const value_list_t *vl)
        }
 
        return (0);
-}
+} /* int plugin_dispatch_values */
+
+int plugin_dispatch_notification (const notification_t *notif)
+{
+       int (*callback) (const notification_t *);
+       llentry_t *le;
+       /* Possible TODO: Add flap detection here */
+
+       DEBUG ("plugin_dispatch_notification: severity = %i; message = %s; "
+                       "time = %u; host = %s;",
+                       notif->severity, notif->message,
+                       (unsigned int) notif->time, notif->host);
+
+       /* Nobody cares for notifications */
+       if (list_notification == NULL)
+               return (-1);
+
+       le = llist_head (list_notification);
+       while (le != NULL)
+       {
+               callback = (int (*) (const notification_t *)) le->value;
+               (*callback) (notif);
+
+               le = le->next;
+       }
+
+       return (0);
+} /* int plugin_dispatch_notification */
 
 void plugin_log (int level, const char *format, ...)
 {