src/common.c: Removed the (extern) variable `operating_mode'.
[collectd.git] / src / plugin.c
index 570b6a2..1a029e4 100644 (file)
@@ -4,8 +4,7 @@
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU General Public License as published by the
- * Free Software Foundation; either version 2 of the License, or (at your
- * option) any later version.
+ * Free Software Foundation; only version 2 of the License is applicable.
  *
  * This program is distributed in the hope that it will be useful, but
  * WITHOUT ANY WARRANTY; without even the implied warranty of
 #include <ltdl.h>
 
 #include "plugin.h"
-#include "network.h"
+#include "configfile.h"
+#include "utils_llist.h"
 #include "utils_debug.h"
 
-typedef struct plugin
+/*
+ * Private structures
+ */
+struct read_func_s
 {
-       char *type;
-       void (*init) (void);
-       void (*read) (void);
-       void (*write) (char *host, char *inst, char *val);
-       struct plugin *next;
-} plugin_t;
-
-static plugin_t *first_plugin = NULL;
+       int wait_time;
+       int wait_left;
+       int (*callback) (void);
+};
+typedef struct read_func_s read_func_t;
 
-extern int operating_mode;
+/*
+ * Private variables
+ */
+static llist_t *list_init;
+static llist_t *list_read;
+static llist_t *list_write;
+static llist_t *list_shutdown;
+static llist_t *list_data_set;
 
 static char *plugindir = NULL;
 
-char *plugin_get_dir (void)
+char hostname[DATA_MAX_NAME_LEN] = "localhost";
+
+/*
+ * Static functions
+ */
+static const char *plugin_get_dir (void)
 {
        if (plugindir == NULL)
                return (PLUGINDIR);
@@ -51,66 +63,52 @@ char *plugin_get_dir (void)
                return (plugindir);
 }
 
-void plugin_set_dir (const char *dir)
+static int register_callback (llist_t **list, const char *name, void *callback)
 {
-       if (plugindir != NULL)
-               free (plugindir);
+       llentry_t *le;
 
-       if (dir == NULL)
-               plugindir = NULL;
-       else if ((plugindir = strdup (dir)) == NULL)
-               syslog (LOG_ERR, "strdup: %s", strerror (errno));
-}
+       if ((*list == NULL)
+                       && ((*list = llist_create ()) == NULL))
+               return (-1);
 
-/*
- * Returns the number of plugins registered
- */
-int plugin_count (void)
-{
-       int i;
-       plugin_t *p;
+       le = llist_search (*list, name);
+       if (le == NULL)
+       {
+               le = llentry_create (name, callback);
+               if (le == NULL)
+                       return (-1);
 
-       for (i = 0, p = first_plugin; p != NULL; p = p->next)
-               i++;
+               llist_append (*list, le);
+       }
+       else
+       {
+               le->value = callback;
+       }
 
-       return (i);
-}
+       return (0);
+} /* int register_callback */
 
-/*
- * Returns the plugins with the type `type' or NULL if it's not found.
- */
-plugin_t *plugin_search (const char *type)
+static int plugin_unregister (llist_t *list, const char *name)
 {
-       plugin_t *ret;
+       llentry_t *e;
 
-       if (type == NULL)
-               return (NULL);
+       e = llist_search (list, name);
 
-       for (ret = first_plugin; ret != NULL; ret = ret->next)
-               if (strcmp (ret->type, type) == 0)
-                       break;
+       if (e == NULL)
+               return (-1);
 
-       return (ret);
-}
+       llist_remove (list, e);
+       llentry_destroy (e);
 
-/*
- * Returns true if the plugin is loaded (i.e. `exists') and false otherwise.
- * This is used in `configfile.c' to skip sections that are not needed..
- */
-int plugin_exists (char *type)
-{
-       if (plugin_search (type) == NULL)
-               return (0);
-       else
-               return (1);
-}
+       return (0);
+} /* int plugin_unregister */
 
 /*
  * (Try to) load the shared object `file'. Won't complain if it isn't a shared
  * object, but it will bitch about a shared object not having a
  * ``module_register'' symbol..
  */
-int plugin_load_file (char *file)
+static int plugin_load_file (char *file)
 {
        lt_dlhandle dlh;
        void (*reg_handle) (void);
@@ -129,7 +127,7 @@ int plugin_load_file (char *file)
                return (1);
        }
 
-       if ((reg_handle = lt_dlsym (dlh, "module_register")) == NULL)
+       if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
        {
                syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
                                file, lt_dlerror ());
@@ -142,11 +140,25 @@ int plugin_load_file (char *file)
        return (0);
 }
 
+/*
+ * Public functions
+ */
+void plugin_set_dir (const char *dir)
+{
+       if (plugindir != NULL)
+               free (plugindir);
+
+       if (dir == NULL)
+               plugindir = NULL;
+       else if ((plugindir = strdup (dir)) == NULL)
+               syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
+}
+
 #define BUFSIZE 512
 int plugin_load (const char *type)
 {
        DIR  *dh;
-       char *dir;
+       const char *dir;
        char  filename[BUFSIZE];
        char  typename[BUFSIZE];
        int   typename_len;
@@ -159,10 +171,6 @@ int plugin_load (const char *type)
        dir = plugin_get_dir ();
        ret = 1;
 
-       /* don't load twice */
-       if (plugin_search (type) != NULL)
-               return (0);
-
        /* `cpu' should not match `cpufreq'. To solve this we add `.so' to the
         * type when matching the filename */
        if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
@@ -214,139 +222,276 @@ int plugin_load (const char *type)
 }
 
 /*
- * (Try to) load all plugins in `dir'. Returns the number of loaded plugins..
+ * The `register_*' functions follow
  */
-int plugin_load_all (char *dir)
+int plugin_register_config (const char *name,
+               int (*callback) (const char *key, const char *val),
+               const char **keys, int keys_num)
 {
-       DIR *dh;
-       struct dirent *de;
-       char filename[BUFSIZE];
-       struct stat statbuf;
+       cf_register (name, callback, keys, keys_num);
+       return (0);
+} /* int plugin_register_config */
 
-       if (dir == NULL)
-               dir = plugin_get_dir ();
-       else
-               plugin_set_dir (dir);
+int plugin_register_init (const char *name,
+               int (*callback) (void))
+{
+       return (register_callback (&list_init, name, (void *) callback));
+} /* plugin_register_init */
 
-       if ((dh = opendir (dir)) == NULL)
+int plugin_register_read (const char *name,
+               int (*callback) (void))
+{
+       read_func_t *rf;
+
+       rf = (read_func_t *) malloc (sizeof (read_func_t));
+       if (rf == NULL)
        {
-               syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
-               return (0);
+               syslog (LOG_ERR, "plugin_register_read: malloc failed: %s",
+                               strerror (errno));
+               return (-1);
        }
 
-       while ((de = readdir (dh)) != NULL)
-       {
-               if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
-               {
-                       syslog (LOG_WARNING, "snprintf: truncated: %s/%s", dir, de->d_name);
-                       continue;
-               }
+       memset (rf, '\0', sizeof (read_func_t));
+       rf->wait_time = atoi (COLLECTD_STEP);
+       rf->wait_left = 0;
+       rf->callback = callback;
 
-               if (lstat (filename, &statbuf) == -1)
-               {
-                       syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
-                       continue;
-               }
-               else if (!S_ISREG (statbuf.st_mode))
-               {
-                       continue;
-               }
+       return (register_callback (&list_read, name, (void *) rf));
+} /* int plugin_register_read */
 
-               plugin_load_file (filename);
-       }
+int plugin_register_write (const char *name,
+               int (*callback) (const data_set_t *ds, const value_list_t *vl))
+{
+       return (register_callback (&list_write, name, (void *) callback));
+} /* int plugin_register_write */
 
-       closedir (dh);
+int plugin_register_shutdown (char *name,
+               int (*callback) (void))
+{
+       return (register_callback (&list_shutdown, name, (void *) callback));
+} /* int plugin_register_shutdown */
+
+int plugin_register_data_set (const data_set_t *ds)
+{
+       return (register_callback (&list_data_set, ds->type, (void *) ds));
+} /* int plugin_register_data_set */
 
-       return (plugin_count ());
+int plugin_unregister_init (const char *name)
+{
+       return (plugin_unregister (list_init, name));
 }
-#undef BUFSIZE
 
-/*
- * Call `init' on all plugins (if given)
- */
-void plugin_init_all (void)
+int plugin_unregister_read (const char *name)
 {
-       plugin_t *p;
+       return (plugin_unregister (list_read, name));
+       llentry_t *e;
+
+       e = llist_search (list_read, name);
+
+       if (e == NULL)
+               return (-1);
 
-       for (p = first_plugin; p != NULL; p = p->next)
-               if (p->init != NULL)
-                       (*p->init) ();
+       llist_remove (list_read, e);
+       free (e->value);
+       llentry_destroy (e);
+
+       return (0);
 }
 
-/*
- * Call `read' on all plugins (if given)
- */
-void plugin_read_all (void)
+int plugin_unregister_write (const char *name)
 {
-       plugin_t *p;
+       return (plugin_unregister (list_write, name));
+}
 
-       for (p = first_plugin; p != NULL; p = p->next)
-               if (p->read != NULL)
-                       (*p->read) ();
+int plugin_unregister_shutdown (const char *name)
+{
+       return (plugin_unregister (list_shutdown, name));
 }
 
-/*
- * Add plugin to the linked list of registered plugins.
- */
-void plugin_register (char *type,
-               void (*init) (void),
-               void (*read) (void),
-               void (*write) (char *, char *, char *))
+int plugin_unregister_data_set (const char *name)
+{
+       return (plugin_unregister (list_data_set, name));
+}
+
+void plugin_init_all (void)
 {
-       plugin_t *p;
+       int (*callback) (void);
+       llentry_t *le;
+       int status;
+
+       gethostname (hostname, sizeof (hostname));
 
-       if (plugin_search (type) != NULL)
+       if (list_init == NULL)
                return;
 
-#ifdef HAVE_LIBRRD
-       if (operating_mode != MODE_SERVER)
-#endif
-               if ((init != NULL) && (read == NULL))
-                       syslog (LOG_NOTICE, "Plugin `%s' doesn't provide a read function.", type);
+       le = llist_head (list_init);
+       while (le != NULL)
+       {
+               callback = le->value;
+               status = (*callback) ();
+
+               if (status != 0)
+               {
+                       syslog (LOG_ERR, "Initialization of plugin `%s' "
+                                       "failed with status %i. "
+                                       "Plugin will be unloaded. TODO!",
+                                       le->key, status);
+                       plugin_unregister_read (le->key);
+               }
+
+               le = le->next;
+       }
+} /* void plugin_init_all */
+
+void plugin_read_all (const int *loop)
+{
+       llentry_t   *le;
+       read_func_t *rf;
+       int          status;
+       int          step;
 
-       if ((p = (plugin_t *) malloc (sizeof (plugin_t))) == NULL)
+       if (list_read == NULL)
                return;
 
-       if ((p->type = strdup (type)) == NULL)
+       step = atoi (COLLECTD_STEP);
+
+       le = llist_head (list_read);
+       while ((*loop == 0) && (le != NULL))
        {
-               free (p);
+               rf = (read_func_t *) le->value;
+
+               if (rf->wait_left > 0)
+                       rf->wait_left -= step;
+               if (rf->wait_left > 0)
+               {
+                       le = le->next;
+                       continue;
+               }
+
+               status = rf->callback ();
+               if (status != 0)
+               {
+                       rf->wait_left = rf->wait_time;
+                       rf->wait_time = rf->wait_time * 2;
+                       if (rf->wait_time > 86400)
+                               rf->wait_time = 86400;
+
+                       syslog (LOG_NOTICE, "read-function of plugin `%s' "
+                                       "failed. Will syspend it for %i "
+                                       "seconds.", le->key, rf->wait_left);
+               }
+               else
+               {
+                       rf->wait_left = 0;
+                       rf->wait_time = step;
+               }
+
+               le = le->next;
+       } /* while ((*loop == 0) && (le != NULL)) */
+} /* void plugin_read_all */
+
+void plugin_shutdown_all (void)
+{
+       int (*callback) (void);
+       llentry_t *le;
+
+       if (list_shutdown == NULL)
                return;
+
+       le = llist_head (list_shutdown);
+       while (le != NULL)
+       {
+               callback = le->value;
+               (*callback) ();
+
+               le = le->next;
+       }
+} /* void plugin_shutdown_all */
+
+int plugin_dispatch_values (const char *name, const 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)
+               return (-1);
+
+       le = llist_search (list_data_set, name);
+       if (le == NULL)
+       {
+               DBG ("No such dataset registered: %s", name);
+               return (-1);
        }
 
-       p->init  = init;
-       p->read  = read;
-       p->write = write;
+       ds = (data_set_t *) le->value;
+
+       DBG ("time = %u; host = %s; "
+                       "plugin = %s; plugin_instance = %s; "
+                       "type = %s; type_instance = %s;",
+                       (unsigned int) vl->time, vl->host,
+                       vl->plugin, vl->plugin_instance,
+                       ds->type, vl->type_instance);
+
+       le = llist_head (list_write);
+       while (le != NULL)
+       {
+               callback = le->value;
+               (*callback) (ds, vl);
+
+               le = le->next;
+       }
 
-       p->next = first_plugin;
-       first_plugin = p;
+       return (0);
 }
 
-/*
- * Send received data back to the plugin/module which will append DS
- * definitions and pass it on to ``rrd_update_file''.
- */
-void plugin_write (char *host, char *type, char *inst, char *val)
+void plugin_complain (int level, complain_t *c, const char *format, ...)
 {
-       plugin_t *p;
+       char message[512];
+       va_list ap;
+       int step;
 
-       if ((p = plugin_search (type)) == NULL)
+       if (c->delay > 0)
+       {
+               c->delay--;
                return;
+       }
 
-       if (p->write == NULL)
-               return;
+       step = atoi (COLLECTD_STEP);
+       assert (step > 0);
+
+       if (c->interval < step)
+               c->interval = step;
+       else
+               c->interval *= 2;
+
+       if (c->interval > 86400)
+               c->interval = 86400;
 
-       (*p->write) (host, inst, val);
+       c->delay = c->interval / step;
+
+       va_start (ap, format);
+       vsnprintf (message, 512, format, ap);
+       message[511] = '\0';
+       va_end (ap);
+
+       syslog (level, message);
 }
 
-/*
- * Receive data from the plugin/module and get it somehow to ``plugin_write'':
- * Either using ``network_send'' (when in network/client mode) or call it
- * directly (in local mode).
- */
-void plugin_submit (char *type, char *inst, char *val)
+void plugin_relief (int level, complain_t *c, const char *format, ...)
 {
-        if (operating_mode == MODE_CLIENT)
-               network_send (type, inst, val);
-       else
-               plugin_write (NULL, type, inst, val);
+       char message[512];
+       va_list ap;
+
+       if (c->interval == 0)
+               return;
+
+       c->interval = 0;
+
+       va_start (ap, format);
+       vsnprintf (message, 512, format, ap);
+       message[511] = '\0';
+       va_end (ap);
+
+       syslog (level, message);
 }