iptables plugin: Adds a plugin to collect iptables'-counters.
[collectd.git] / src / plugin.c
index cbdb942..d708b6b 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/plugin.c
- * Copyright (C) 2005  Florian octo Forster
+ * Copyright (C) 2005,2006  Florian octo Forster
  *
  * 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
@@ -34,14 +34,13 @@ typedef struct plugin
        void (*init) (void);
        void (*read) (void);
        void (*write) (char *host, char *inst, char *val);
+       void (*shutdown) (void);
        struct plugin *next;
 } plugin_t;
 
 static plugin_t *first_plugin = NULL;
 
-#ifdef HAVE_LIBRRD
 extern int operating_mode;
-#endif
 
 static char *plugindir = NULL;
 
@@ -131,7 +130,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 ());
@@ -278,16 +277,29 @@ void plugin_init_all (void)
 /*
  * Call `read' on all plugins (if given)
  */
-void plugin_read_all (void)
+void plugin_read_all (const int *loop)
 {
        plugin_t *p;
 
-       for (p = first_plugin; p != NULL; p = p->next)
+       for (p = first_plugin; (*loop == 0) && (p != NULL); p = p->next)
                if (p->read != NULL)
                        (*p->read) ();
 }
 
 /*
+ * Call `shutdown' on all plugins (if given)
+ */
+void plugin_shutdown_all (void)
+{
+       plugin_t *p;
+
+       for (p = first_plugin; NULL != p; p = p->next)
+               if (NULL != p->shutdown)
+                       (*p->shutdown) ();
+       return;
+}
+
+/*
  * Add plugin to the linked list of registered plugins.
  */
 void plugin_register (char *type,
@@ -301,7 +313,7 @@ void plugin_register (char *type,
                return;
 
 #ifdef HAVE_LIBRRD
-       if ((operating_mode == MODE_LOCAL) || (operating_mode == MODE_CLIENT))
+       if (operating_mode != MODE_SERVER)
 #endif
                if ((init != NULL) && (read == NULL))
                        syslog (LOG_NOTICE, "Plugin `%s' doesn't provide a read function.", type);
@@ -319,15 +331,30 @@ void plugin_register (char *type,
        p->read  = read;
        p->write = write;
 
+       p->shutdown = NULL;
+
        p->next = first_plugin;
        first_plugin = p;
 }
 
 /*
+ * Register the shutdown function (optional).
+ */
+int plugin_register_shutdown (char *type, void (*shutdown) (void))
+{
+       plugin_t *p = plugin_search (type);
+
+       if (NULL == p)
+               return -1;
+
+       p->shutdown = shutdown;
+       return 0;
+}
+
+/*
  * Send received data back to the plugin/module which will append DS
  * definitions and pass it on to ``rrd_update_file''.
  */
-#ifdef HAVE_LIBRRD
 void plugin_write (char *host, char *type, char *inst, char *val)
 {
        plugin_t *p;
@@ -340,7 +367,6 @@ void plugin_write (char *host, char *type, char *inst, char *val)
 
        (*p->write) (host, inst, val);
 }
-#endif /* HAVE_LIBRRD */
 
 /*
  * Receive data from the plugin/module and get it somehow to ``plugin_write'':
@@ -349,14 +375,71 @@ void plugin_write (char *host, char *type, char *inst, char *val)
  */
 void plugin_submit (char *type, char *inst, char *val)
 {
-#ifdef HAVE_LIBRRD
-       if (operating_mode == MODE_LOCAL)
-               plugin_write (NULL, type, inst, val);
-       else if (operating_mode == MODE_CLIENT)
+       if (inst == NULL)
+               inst = "-";
+
+       if ((type == NULL) || (val == NULL))
+       {
+               DBG ("Help! NULL-pointer! type = %s; inst = %s; val = %s;",
+                               (type == NULL) ? "(null)" : type,
+                               inst,
+                               (val == NULL) ? "(null)" : val);
+               return;
+       }
+
+        if (operating_mode == MODE_CLIENT)
                network_send (type, inst, val);
-       else /* operating_mode == MODE_SERVER */
-               syslog (LOG_ERR, "WTF is the server doing in ``plugin_submit''?!?\n");
-#else
-       network_send (type, inst, val);
-#endif
+       else
+               plugin_write (NULL, type, inst, val);
+}
+
+void plugin_complain (int level, complain_t *c, const char *format, ...)
+{
+       char message[512];
+       va_list ap;
+       int step;
+
+       if (c->delay > 0)
+       {
+               c->delay--;
+               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;
+
+       c->delay = c->interval / step;
+
+       va_start (ap, format);
+       vsnprintf (message, 512, format, ap);
+       message[511] = '\0';
+       va_end (ap);
+
+       syslog (level, message);
+}
+
+void plugin_relief (int level, complain_t *c, const char *format, ...)
+{
+       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);
 }