nut plugin: Added a plugin to query the `upsd' from the `network ups tools'.
[collectd.git] / src / plugin.c
index f2e205a..c6762de 100644 (file)
 
 #include <ltdl.h>
 
+#if HAVE_PTHREAD_H
+# include <pthread.h>
+#endif
+
+#include "common.h"
 #include "plugin.h"
 #include "configfile.h"
 #include "utils_llist.h"
-#include "utils_debug.h"
 
 /*
  * Private structures
@@ -36,6 +40,7 @@ struct read_func_s
        int wait_time;
        int wait_left;
        int (*callback) (void);
+       enum { DONE = 0, TODO = 1, ACTIVE = 2 } needs_read;
 };
 typedef struct read_func_s read_func_t;
 
@@ -51,6 +56,12 @@ static llist_t *list_log;
 
 static char *plugindir = NULL;
 
+static int             read_loop = 1;
+static pthread_mutex_t read_lock = PTHREAD_MUTEX_INITIALIZER;
+static pthread_cond_t  read_cond = PTHREAD_COND_INITIALIZER;
+static pthread_t      *read_threads = NULL;
+static int             read_threads_num = 0;
+
 /*
  * Static functions
  */
@@ -107,12 +118,12 @@ static int plugin_unregister (llist_t *list, const char *name)
  * object, but it will bitch about a shared object not having a
  * ``module_register'' symbol..
  */
-static int plugin_load_file (char *file)
+static int plugin_load_file (char *file, modreg_e load)
 {
        lt_dlhandle dlh;
-       void (*reg_handle) (void);
+       void (*reg_handle) (modreg_e mr);
 
-       DBG ("file = %s", file);
+       DEBUG ("file = %s", file);
 
        lt_dlinit ();
        lt_dlerror (); /* clear errors */
@@ -121,24 +132,146 @@ static int plugin_load_file (char *file)
        {
                const char *error = lt_dlerror ();
 
-               syslog (LOG_ERR, "lt_dlopen failed: %s", error);
-               DBG ("lt_dlopen failed: %s", error);
+               ERROR ("lt_dlopen failed: %s", error);
+               DEBUG ("lt_dlopen failed: %s", error);
                return (1);
        }
 
-       if ((reg_handle = (void (*) (void)) lt_dlsym (dlh, "module_register")) == NULL)
+       if ((reg_handle = (void (*) (modreg_e)) lt_dlsym (dlh, "module_register")) == NULL)
        {
-               syslog (LOG_WARNING, "Couldn't find symbol ``module_register'' in ``%s'': %s\n",
+               WARNING ("Couldn't find symbol ``module_register'' in ``%s'': %s\n",
                                file, lt_dlerror ());
                lt_dlclose (dlh);
                return (-1);
        }
 
-       (*reg_handle) ();
+       (*reg_handle) (load);
 
        return (0);
 }
 
+static void *plugin_read_thread (void *args)
+{
+       llentry_t   *le;
+       read_func_t *rf;
+       int          status;
+       int          done;
+
+       pthread_mutex_lock (&read_lock);
+
+       while (read_loop != 0)
+       {
+               le = llist_head (list_read);
+               done = 0;
+
+               while ((read_loop != 0) && (le != NULL))
+               {
+                       rf = (read_func_t *) le->value;
+
+                       if (rf->needs_read != TODO)
+                       {
+                               le = le->next;
+                               continue;
+                       }
+
+                       /* We will do this read function */
+                       rf->needs_read = ACTIVE;
+
+                       DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Handling %s",
+                                       (unsigned long int) pthread_self (), le->key);
+                       pthread_mutex_unlock (&read_lock);
+
+                       status = rf->callback ();
+                       done++;
+
+                       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;
+
+                               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 = interval_g;
+                       }
+
+                       pthread_mutex_lock (&read_lock);
+
+                       rf->needs_read = DONE;
+                       le = le->next;
+               } /* while (le != NULL) */
+
+               if ((read_loop != 0) && (done == 0))
+               {
+                       DEBUG ("[thread #%5lu] plugin: plugin_read_thread: Waiting on read_cond.",
+                                       (unsigned long int) pthread_self ());
+                       pthread_cond_wait (&read_cond, &read_lock);
+               }
+       } /* while (read_loop) */
+
+       pthread_mutex_unlock (&read_lock);
+
+       pthread_exit (NULL);
+} /* void *plugin_read_thread */
+
+static void start_threads (int num)
+{
+       int i;
+
+       if (read_threads != NULL)
+               return;
+
+       read_threads = (pthread_t *) calloc (num, sizeof (pthread_t));
+       if (read_threads == NULL)
+       {
+               ERROR ("plugin: start_threads: calloc failed.");
+               return;
+       }
+
+       read_threads_num = 0;
+       for (i = 0; i < num; i++)
+       {
+               if (pthread_create (read_threads + read_threads_num, NULL,
+                                       plugin_read_thread, NULL) == 0)
+               {
+                       read_threads_num++;
+               }
+               else
+               {
+                       ERROR ("plugin: start_threads: pthread_create failed.");
+                       return;
+               }
+       } /* for (i) */
+} /* void start_threads */
+
+static void stop_threads (void)
+{
+       int i;
+
+       pthread_mutex_lock (&read_lock);
+       read_loop = 0;
+       DEBUG ("plugin: stop_threads: Signalling `read_cond'");
+       pthread_cond_broadcast (&read_cond);
+       pthread_mutex_unlock (&read_lock);
+
+       for (i = 0; i < read_threads_num; i++)
+       {
+               if (pthread_join (read_threads[i], NULL) != 0)
+               {
+                       ERROR ("plugin: stop_threads: pthread_join failed.");
+               }
+               read_threads[i] = -1;
+       }
+       sfree (read_threads);
+       read_threads_num = 0;
+} /* void stop_threads */
+
 /*
  * Public functions
  */
@@ -150,11 +283,15 @@ void plugin_set_dir (const char *dir)
        if (dir == NULL)
                plugindir = NULL;
        else if ((plugindir = strdup (dir)) == NULL)
-               syslog (LOG_ERR, "strdup failed: %s", strerror (errno));
+       {
+               char errbuf[1024];
+               ERROR ("strdup failed: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+       }
 }
 
 #define BUFSIZE 512
-int plugin_load (const char *type)
+int plugin_load (const char *type, modreg_e mr)
 {
        DIR  *dh;
        const char *dir;
@@ -165,7 +302,7 @@ int plugin_load (const char *type)
        struct stat    statbuf;
        struct dirent *de;
 
-       DBG ("type = %s", type);
+       DEBUG ("type = %s", type);
 
        dir = plugin_get_dir ();
        ret = 1;
@@ -174,14 +311,16 @@ int plugin_load (const char *type)
         * type when matching the filename */
        if (snprintf (typename, BUFSIZE, "%s.so", type) >= BUFSIZE)
        {
-               syslog (LOG_WARNING, "snprintf: truncated: `%s.so'", type);
+               WARNING ("snprintf: truncated: `%s.so'", type);
                return (-1);
        }
        typename_len = strlen (typename);
 
        if ((dh = opendir (dir)) == NULL)
        {
-               syslog (LOG_ERR, "opendir (%s): %s", dir, strerror (errno));
+               char errbuf[1024];
+               ERROR ("opendir (%s): %s", dir,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
 
@@ -192,13 +331,15 @@ int plugin_load (const char *type)
 
                if (snprintf (filename, BUFSIZE, "%s/%s", dir, de->d_name) >= BUFSIZE)
                {
-                       syslog (LOG_WARNING, "snprintf: truncated: `%s/%s'", dir, de->d_name);
+                       WARNING ("snprintf: truncated: `%s/%s'", dir, de->d_name);
                        continue;
                }
 
                if (lstat (filename, &statbuf) == -1)
                {
-                       syslog (LOG_WARNING, "stat %s: %s", filename, strerror (errno));
+                       char errbuf[1024];
+                       WARNING ("stat %s: %s", filename,
+                                       sstrerror (errno, errbuf, sizeof (errbuf)));
                        continue;
                }
                else if (!S_ISREG (statbuf.st_mode))
@@ -207,7 +348,7 @@ int plugin_load (const char *type)
                        continue;
                }
 
-               if (plugin_load_file (filename) == 0)
+               if (plugin_load_file (filename, mr) == 0)
                {
                        /* success */
                        ret = 0;
@@ -245,15 +386,17 @@ int plugin_register_read (const char *name,
        rf = (read_func_t *) malloc (sizeof (read_func_t));
        if (rf == NULL)
        {
-               syslog (LOG_ERR, "plugin_register_read: malloc failed: %s",
-                               strerror (errno));
+               char errbuf[1024];
+               ERROR ("plugin_register_read: malloc failed: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return (-1);
        }
 
        memset (rf, '\0', sizeof (read_func_t));
-       rf->wait_time = atoi (COLLECTD_STEP);
+       rf->wait_time = interval_g;
        rf->wait_left = 0;
        rf->callback = callback;
+       rf->needs_read = DONE;
 
        return (register_callback (&list_read, name, (void *) rf));
 } /* int plugin_register_read */
@@ -281,6 +424,12 @@ int plugin_register_log (char *name,
        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_init (const char *name)
 {
        return (plugin_unregister (list_init, name));
@@ -329,6 +478,16 @@ void plugin_init_all (void)
        llentry_t *le;
        int status;
 
+       /* Start read-threads */
+       if (list_read != NULL)
+       {
+               const char *rt;
+               int num;
+               rt = global_option_get ("ReadThreads");
+               num = atoi (rt);
+               start_threads ((num > 0) ? num : 5);
+       }
+
        if (list_init == NULL)
                return;
 
@@ -340,7 +499,7 @@ void plugin_init_all (void)
 
                if (status != 0)
                {
-                       syslog (LOG_ERR, "Initialization of plugin `%s' "
+                       ERROR ("Initialization of plugin `%s' "
                                        "failed with status %i. "
                                        "Plugin will be unloaded. TODO!",
                                        le->key, status);
@@ -355,47 +514,34 @@ void plugin_read_all (const int *loop)
 {
        llentry_t   *le;
        read_func_t *rf;
-       int          status;
-       int          step;
 
        if (list_read == NULL)
                return;
 
-       step = atoi (COLLECTD_STEP);
+       pthread_mutex_lock (&read_lock);
 
        le = llist_head (list_read);
-       while ((*loop == 0) && (le != NULL))
+       while (le != NULL)
        {
                rf = (read_func_t *) le->value;
 
-               if (rf->wait_left > 0)
-                       rf->wait_left -= step;
-               if (rf->wait_left > 0)
-               {
-                       le = le->next;
+               if (rf->needs_read != DONE)
                        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
+               if (rf->wait_left > 0)
+                       rf->wait_left -= interval_g;
+
+               if (rf->wait_left <= 0)
                {
-                       rf->wait_left = 0;
-                       rf->wait_time = step;
+                       rf->needs_read = TODO;
                }
 
                le = le->next;
-       } /* while ((*loop == 0) && (le != NULL)) */
+       }
+
+       DEBUG ("plugin: plugin_read_all: Signalling `read_cond'");
+       pthread_cond_broadcast (&read_cond);
+       pthread_mutex_unlock (&read_lock);
 } /* void plugin_read_all */
 
 void plugin_shutdown_all (void)
@@ -403,6 +549,8 @@ void plugin_shutdown_all (void)
        int (*callback) (void);
        llentry_t *le;
 
+       stop_threads ();
+
        if (list_shutdown == NULL)
                return;
 
@@ -428,15 +576,15 @@ int plugin_dispatch_values (const char *name, const value_list_t *vl)
        le = llist_search (list_data_set, name);
        if (le == NULL)
        {
-               DBG ("No such dataset registered: %s", name);
+               DEBUG ("No such dataset registered: %s", name);
                return (-1);
        }
 
        ds = (data_set_t *) le->value;
 
-       DBG ("time = %u; host = %s; "
-                       "plugin = %s; plugin_instance = %s; "
-                       "type = %s; type_instance = %s;",
+       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,
                        vl->plugin, vl->plugin_instance,
                        ds->type, vl->type_instance);
@@ -461,6 +609,9 @@ void plugin_log (int level, const char *format, ...)
        void (*callback) (int, const char *);
        llentry_t *le;
 
+       if (list_log == NULL)
+               return;
+
 #if !COLLECT_DEBUG
        if (level >= LOG_DEBUG)
                return;
@@ -485,7 +636,6 @@ void plugin_complain (int level, complain_t *c, const char *format, ...)
 {
        char message[512];
        va_list ap;
-       int step;
 
        if (c->delay > 0)
        {
@@ -493,25 +643,22 @@ void plugin_complain (int level, complain_t *c, const char *format, ...)
                return;
        }
 
-       step = atoi (COLLECTD_STEP);
-       assert (step > 0);
-
-       if (c->interval < step)
-               c->interval = step;
+       if (c->interval < interval_g)
+               c->interval = interval_g;
        else
                c->interval *= 2;
 
        if (c->interval > 86400)
                c->interval = 86400;
 
-       c->delay = c->interval / step;
+       c->delay = c->interval / interval_g;
 
        va_start (ap, format);
        vsnprintf (message, 512, format, ap);
        message[511] = '\0';
        va_end (ap);
 
-       syslog (level, message);
+       plugin_log (level, message);
 }
 
 void plugin_relief (int level, complain_t *c, const char *format, ...)
@@ -529,5 +676,22 @@ void plugin_relief (int level, complain_t *c, const char *format, ...)
        message[511] = '\0';
        va_end (ap);
 
-       syslog (level, message);
+       plugin_log (level, message);
 }
+
+const data_set_t *plugin_get_ds (const char *name)
+{
+       data_set_t *ds;
+       llentry_t *le;
+
+       le = llist_search (list_data_set, name);
+       if (le == NULL)
+       {
+               DEBUG ("No such dataset registered: %s", name);
+               return (NULL);
+       }
+
+       ds = (data_set_t *) le->value;
+
+       return (ds);
+} /* data_set_t *plugin_get_ds */