src/plugin.[ch]: Store the hostname in a global variable to minimize calls to `gethos...
[collectd.git] / src / load.c
index d082816..3e60a43 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/load.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
  *   Florian octo Forster <octo at verplant.org>
  **/
 
-#include "load.h"
+#include "collectd.h"
+#include "common.h"
+#include "plugin.h"
 
-#if COLLECT_LOAD
 #define MODULE_NAME "load"
 
-#include "plugin.h"
-#include "common.h"
+#if defined(HAVE_GETLOADAVG) || defined(KERNEL_LINUX) || defined(HAVE_LIBSTATGRAB)
+# define LOAD_HAVE_READ 1
+#else
+# define LOAD_HAVE_READ 0
+#endif
 
 #ifdef HAVE_SYS_LOADAVG_H
 #include <sys/loadavg.h>
 #endif
 #endif /* defined(HAVE_GETLOADAVG) */
 
-static char *load_file = "load.rrd";
-
-static char *ds_def[] =
+static data_source_t dsrc[3] =
 {
-       "DS:shortterm:GAUGE:25:0:100",
-       "DS:midterm:GAUGE:25:0:100",
-       "DS:longterm:GAUGE:25:0:100",
-       NULL
+       {"shortterm", DS_TYPE_GAUGE, 0.0, 100.0},
+       {"midterm",   DS_TYPE_GAUGE, 0.0, 100.0},
+       {"longterm",  DS_TYPE_GAUGE, 0.0, 100.0}
 };
-static int ds_num = 3;
-
-void load_init (void)
-{
-       return;
-}
 
-void load_write (char *host, char *inst, char *val)
+static data_set_t ds =
 {
-       rrd_update_file (host, load_file, val, ds_def, ds_num);
-}
+       "load", 3, dsrc
+};
 
-#define BUFSIZE 256
-void load_submit (double snum, double mnum, double lnum)
+#if LOAD_HAVE_READ
+static void load_submit (double snum, double mnum, double lnum)
 {
-       char buf[BUFSIZE];
-
-       if (snprintf (buf, BUFSIZE, "%u:%.2f:%.2f:%.2f", (unsigned int) curtime,
-                               snum, mnum, lnum) >= BUFSIZE)
-               return;
-
-       plugin_submit (MODULE_NAME, "-", buf);
+       value_t values[3];
+       value_list_t vl = VALUE_LIST_INIT;
+
+       values[0].gauge = snum;
+       values[1].gauge = mnum;
+       values[2].gauge = lnum;
+
+       vl.values = values;
+       vl.values_len = 3;
+       vl.time = time (NULL);
+       strcpy (vl.host, hostname);
+       strcpy (vl.plugin, "load");
+       strcpy (vl.plugin_instance, "");
+       strcpy (vl.type_instance, "");
+
+       plugin_dispatch_values ("load", &vl);
 }
-#undef BUFSIZE
 
-void load_read (void)
+static int load_read (void)
 {
 #if defined(HAVE_GETLOADAVG)
        double load[3];
@@ -102,6 +105,7 @@ void load_read (void)
        if (fgets (buffer, 16, loadavg) == NULL)
        {
                syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
+               fclose (loadavg);
                return;
        }
 
@@ -133,12 +137,17 @@ void load_read (void)
 
        load_submit (snum, mnum, lnum);
 #endif /* HAVE_LIBSTATGRAB */
+
+       return (0);
 }
+#endif /* LOAD_HAVE_READ */
 
 void module_register (void)
 {
-       plugin_register (MODULE_NAME, load_init, load_read, load_write);
+       plugin_register_data_set (&ds);
+#if LOAD_HAVE_READ
+       plugin_register_read ("load", load_read);
+#endif
 }
 
 #undef MODULE_NAME
-#endif /* COLLECT_LOAD */