src/utils_threshold.c: Added creation of a notification.
[collectd.git] / src / plugin.c
index 248e583..6b17290 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;
 
@@ -133,6 +136,7 @@ static int plugin_load_file (char *file)
                const char *error = lt_dlerror ();
 
                ERROR ("lt_dlopen failed: %s", error);
+               fprintf (stderr, "lt_dlopen failed: %s\n", error);
                return (1);
        }
 
@@ -355,6 +359,10 @@ int plugin_load (const char *type)
                        ret = 0;
                        break;
                }
+               else
+               {
+                       fprintf (stderr, "Unable to load plugin %s.\n", type);
+               }
        }
 
        closedir (dh);
@@ -457,6 +465,12 @@ 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);
@@ -528,6 +542,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);
@@ -544,6 +563,9 @@ void plugin_init_all (void)
                start_threads ((num > 0) ? num : 5);
        }
 
+       /* Init the value cache */
+       uc_init ();
+
        if (list_init == NULL)
                return;
 
@@ -647,10 +669,12 @@ int plugin_dispatch_values (const char *name, 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);
 
@@ -671,6 +695,10 @@ int plugin_dispatch_values (const char *name, value_list_t *vl)
        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)
        {
@@ -683,6 +711,33 @@ int plugin_dispatch_values (const char *name, 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, ...)
 {
        char msg[512];