Code

Merge branch 'collectd-4.5'
[collectd.git] / src / plugin.c
index 8f1974ff25dfa13d1759e84c3c973bf119be4ef3..4d503f7c457196493cdb1ce5c9e852d9f44cc8a5 100644 (file)
@@ -55,6 +55,7 @@ typedef struct read_func_s read_func_t;
 static llist_t *list_init;
 static llist_t *list_read;
 static llist_t *list_write;
+static llist_t *list_filter;
 static llist_t *list_flush;
 static llist_t *list_shutdown;
 static llist_t *list_log;
@@ -442,6 +443,12 @@ int plugin_register_write (const char *name,
        return (register_callback (&list_write, name, (void *) callback));
 } /* int plugin_register_write */
 
+int plugin_register_filter (const char *name,
+               int (*callback) (const data_set_t *ds, value_list_t *vl))
+{
+       return (register_callback (&list_filter, name, (void *) callback));
+} /* int plugin_register_filter */
+
 int plugin_register_flush (const char *name,
                int (*callback) (const int timeout, const char *identifier))
 {
@@ -542,6 +549,11 @@ int plugin_unregister_write (const char *name)
        return (plugin_unregister (list_write, name));
 }
 
+int plugin_unregister_filter (const char *name)
+{
+       return (plugin_unregister (list_filter, name));
+}
+
 int plugin_unregister_flush (const char *name)
 {
        return (plugin_unregister (list_flush, name));
@@ -677,7 +689,7 @@ int plugin_flush (const char *plugin, int timeout, const char *identifier)
   while (le != NULL)
   {
     if ((plugin != NULL)
-       && (strcmp (plugin, le->key) != 0))
+        && (strcmp (plugin, le->key) != 0))
     {
       le = le->next;
       continue;
@@ -718,12 +730,13 @@ void plugin_shutdown_all (void)
 
 int plugin_dispatch_values (value_list_t *vl)
 {
-       static c_complain_t no_write_complaint = C_COMPLAIN_INIT;
+       static c_complain_t no_write_complaint = C_COMPLAIN_INIT_STATIC;
 
-       int (*callback) (const data_set_t *, const value_list_t *);
        data_set_t *ds;
        llentry_t *le;
 
+       int filter = 0;
+
        if ((vl == NULL) || (*vl->type == '\0')) {
                ERROR ("plugin_dispatch_values: Invalid value list.");
                return (-1);
@@ -785,15 +798,36 @@ int plugin_dispatch_values (value_list_t *vl)
        escape_slashes (vl->type, sizeof (vl->type));
        escape_slashes (vl->type_instance, sizeof (vl->type_instance));
 
+       le = llist_head (list_filter);
+       while (le != NULL)
+       {
+               int (*filter_callback) (const data_set_t *, value_list_t *) =
+                               (int (*) (const data_set_t *, value_list_t *)) le->value;
+
+               filter |= (*filter_callback) (ds, vl);
+
+               if (filter == FILTER_IGNORE)
+                       return (-1);
+
+               le = le->next;
+       }
+
        /* Update the value cache */
        uc_update (ds, vl);
-       ut_check_threshold (ds, vl);
+
+       if ((filter & FILTER_NOTHRESHOLD_CHECK) == 0)
+               ut_check_threshold (ds, vl);
+
+       if (filter & FILTER_NOWRITE)
+               return (0);
 
        le = llist_head (list_write);
        while (le != NULL)
        {
-               callback = (int (*) (const data_set_t *, const value_list_t *)) le->value;
-               (*callback) (ds, vl);
+               int (*write_callback) (const data_set_t *, const value_list_t *) =
+                               (int (*) (const data_set_t *, const value_list_t *)) le->value;
+
+               (*write_callback) (ds, vl);
 
                le = le->next;
        }
@@ -830,7 +864,7 @@ int plugin_dispatch_notification (const notification_t *notif)
 
 void plugin_log (int level, const char *format, ...)
 {
-       char msg[512];
+       char msg[1024];
        va_list ap;
 
        void (*callback) (int, const char *);
@@ -845,8 +879,8 @@ void plugin_log (int level, const char *format, ...)
 #endif
 
        va_start (ap, format);
-       vsnprintf (msg, 512, format, ap);
-       msg[511] = '\0';
+       vsnprintf (msg, sizeof (msg), format, ap);
+       msg[sizeof (msg) - 1] = '\0';
        va_end (ap);
 
        le = llist_head (list_log);
@@ -873,9 +907,9 @@ const data_set_t *plugin_get_ds (const char *name)
 } /* data_set_t *plugin_get_ds */
 
 static int plugin_notification_meta_add (notification_t *n,
-               const char *name,
-               enum notification_meta_type_e type,
-               const void *value)
+    const char *name,
+    enum notification_meta_type_e type,
+    const void *value)
 {
   notification_meta_t *meta;
   notification_meta_t *tail;
@@ -1000,19 +1034,19 @@ int plugin_notification_meta_copy (notification_t *dst,
   {
     if (meta->type == NM_TYPE_STRING)
       plugin_notification_meta_add_string (dst, meta->name,
-         meta->value_string);
+          meta->value_string);
     else if (meta->type == NM_TYPE_SIGNED_INT)
       plugin_notification_meta_add_signed_int (dst, meta->name,
-         meta->value_signed_int);
+          meta->value_signed_int);
     else if (meta->type == NM_TYPE_UNSIGNED_INT)
       plugin_notification_meta_add_unsigned_int (dst, meta->name,
-         meta->value_unsigned_int);
+          meta->value_unsigned_int);
     else if (meta->type == NM_TYPE_DOUBLE)
       plugin_notification_meta_add_double (dst, meta->name,
-         meta->value_double);
+          meta->value_double);
     else if (meta->type == NM_TYPE_BOOLEAN)
       plugin_notification_meta_add_boolean (dst, meta->name,
-         meta->value_boolean);
+          meta->value_boolean);
   }
 
   return (0);