Code

perl plugin: Use the `BaseName' only if it is non-empty.
[collectd.git] / src / load.c
index e715dcf951bdc393a96ab1ebc471449659943a46..7be594d15f26bd8b9a7de2c08f9b93b12716eff1 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
@@ -24,8 +23,6 @@
 #include "common.h"
 #include "plugin.h"
 
-#define MODULE_NAME "load"
-
 #if defined(HAVE_GETLOADAVG) || defined(KERNEL_LINUX) || defined(HAVE_LIBSTATGRAB)
 # define LOAD_HAVE_READ 1
 #else
@@ -57,28 +54,20 @@ static data_set_t ds =
 };
 
 #if LOAD_HAVE_READ
-static void load_submit (double snum, double mnum, double lnum)
+static void load_submit (gauge_t snum, gauge_t mnum, gauge_t lnum)
 {
        value_t values[3];
-       value_list_t vl;
+       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.values_len = STATIC_ARRAY_SIZE (values);
        vl.time = time (NULL);
-       /* FIXME: do this globally */
-       if (gethostname (vl.host, sizeof (vl.host)) != 0)
-       {
-               syslog (LOG_ERR, "load plugin: gethostname failed: %s",
-                               strerror (errno));
-               return;
-       }
+       strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "load");
-       strcpy (vl.plugin_instance, "");
-       strcpy (vl.type_instance, "");
 
        plugin_dispatch_values ("load", &vl);
 }
@@ -91,11 +80,15 @@ static int load_read (void)
        if (getloadavg (load, 3) == 3)
                load_submit (load[LOADAVG_1MIN], load[LOADAVG_5MIN], load[LOADAVG_15MIN]);
        else
-               syslog (LOG_WARNING, "load: getloadavg failed: %s", strerror (errno));
+       {
+               char errbuf[1024];
+               WARNING ("load: getloadavg failed: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+       }
 /* #endif HAVE_GETLOADAVG */
 
 #elif defined(KERNEL_LINUX)
-       double snum, mnum, lnum;
+       gauge_t snum, mnum, lnum;
        FILE *loadavg;
        char buffer[16];
 
@@ -104,19 +97,27 @@ static int load_read (void)
        
        if ((loadavg = fopen ("/proc/loadavg", "r")) == NULL)
        {
-               syslog (LOG_WARNING, "load: fopen: %s", strerror (errno));
+               char errbuf[1024];
+               WARNING ("load: fopen: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return;
        }
 
        if (fgets (buffer, 16, loadavg) == NULL)
        {
-               syslog (LOG_WARNING, "load: fgets: %s", strerror (errno));
+               char errbuf[1024];
+               WARNING ("load: fgets: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                fclose (loadavg);
                return;
        }
 
        if (fclose (loadavg))
-               syslog (LOG_WARNING, "load: fclose: %s", strerror (errno));
+       {
+               char errbuf[1024];
+               WARNING ("load: fclose: %s",
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
+       }
 
        numfields = strsplit (buffer, fields, 8);
 
@@ -131,7 +132,7 @@ static int load_read (void)
 /* #endif KERNEL_LINUX */
 
 #elif defined(HAVE_LIBSTATGRAB)
-       double snum, mnum, lnum;
+       gauge_t snum, mnum, lnum;
        sg_load_stats *ls;
 
        if ((ls = sg_get_load_stats ()) == NULL)
@@ -148,12 +149,13 @@ static int load_read (void)
 }
 #endif /* LOAD_HAVE_READ */
 
-void module_register (void)
+void module_register (modreg_e load)
 {
-       plugin_register_data_set (&ds);
+       if (load & MR_DATASETS)
+               plugin_register_data_set (&ds);
+
 #if LOAD_HAVE_READ
-       plugin_register_read ("load", load_read);
+       if (load & MR_READ)
+               plugin_register_read ("load", load_read);
 #endif
-}
-
-#undef MODULE_NAME
+} /* void module_register */