Merge branch 'pull/collectd-4' into collectd-4
[collectd.git] / src / sensors.c
index 14d8abd..551d267 100644 (file)
@@ -1,11 +1,10 @@
 /**
  * collectd - src/sensors.c
- * Copyright (C) 2005,2006  Florian octo Forster
+ * Copyright (C) 2005-2007  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
- * 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
  *   - config IgnoreSelected option
  **/
 
-
-
 #include "collectd.h"
 #include "common.h"
 #include "plugin.h"
 #include "configfile.h"
 #include "utils_ignorelist.h"
-#include "utils_debug.h"
-
-/*
- * This weird macro cascade forces the glibc to define `NAN'. I don't know
- * another way to solve this, so more intelligent solutions are welcome. -octo
- */
-#ifndef __USE_ISOC99
-# define DISABLE__USE_ISOC99 1
-# define __USE_ISOC99 1
-#endif
-#include <math.h>
-#ifdef DISABLE__USE_ISOC99
-# undef DISABLE__USE_ISOC99
-# undef __USE_ISOC99
-#endif
 
 #if defined(HAVE_SENSORS_SENSORS_H)
 # include <sensors/sensors.h>
 # define SENSORS_HAVE_READ 0
 #endif
 
-static data_source_t data_source[1] =
+static data_source_t data_source_fanspeed[1] =
 {
-       {"value", DS_TYPE_GAUGE, NAN, NAN}
+       {"value", DS_TYPE_GAUGE, 0, NAN}
 };
+
 static data_set_t fanspeed_ds =
 {
-       "fanspeed", 1, data_source
+       "fanspeed", 1, data_source_fanspeed
+};
+
+static data_source_t data_source_temperature[1] =
+{
+       {"value", DS_TYPE_GAUGE, -273.15, NAN}
 };
 
 static data_set_t temperature_ds =
 {
-       "temperature", 1, data_source
+       "temperature", 1, data_source_temperature
+};
+
+static data_source_t data_source_voltage[1] =
+{
+       {"value", DS_TYPE_GAUGE, NAN, NAN}
 };
 
 static data_set_t voltage_ds =
 {
-       "voltage", 1, data_source
+       "voltage", 1, data_source_voltage
 };
 
 #if SENSORS_HAVE_READ
@@ -160,9 +153,9 @@ static sensors_labeltypes_t known_features[] =
        { "3.3V", SENSOR_TYPE_VOLTAGE },
        { "2.5V", SENSOR_TYPE_VOLTAGE },
        { "2.0V", SENSOR_TYPE_VOLTAGE },
-       { "12V", SENSOR_TYPE_VOLTAGE },
-       { (char *) 0, SENSOR_TYPE_UNKNOWN }
+       { "12V", SENSOR_TYPE_VOLTAGE }
 };
+static int known_features_num = STATIC_ARRAY_SIZE (known_features);
 /* end new naming */
 
 static const char *config_keys[] =
@@ -206,7 +199,7 @@ static int sensors_config (const char *key, const char *value)
        {
                if (ignorelist_add (sensor_list, value))
                {
-                       syslog (LOG_ERR, "sensors plugin: "
+                       ERROR ("sensors plugin: "
                                        "Cannot add value to ignorelist.");
                        return (1);
                }
@@ -263,8 +256,9 @@ static void sensors_load_conf (void)
        status = stat (conffile, &statbuf);
        if (status != 0)
        {
-               syslog (LOG_ERR, "sensors plugin: stat (%s) failed: %s",
-                               conffile, strerror (errno));
+               char errbuf[1024];
+               ERROR ("sensors plugin: stat (%s) failed: %s", conffile,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                sensors_config_mtime = 0;
        }
 
@@ -274,7 +268,7 @@ static void sensors_load_conf (void)
 
        if (sensors_config_mtime != 0)
        {
-               syslog (LOG_NOTICE, "sensors plugin: Reloading config from %s",
+               NOTICE ("sensors plugin: Reloading config from %s",
                                conffile);
                sensors_free_features ();
                sensors_config_mtime = 0;
@@ -283,8 +277,9 @@ static void sensors_load_conf (void)
        fh = fopen (conffile, "r");
        if (fh == NULL)
        {
-               syslog (LOG_ERR, "sensors plugin: fopen(%s) failed: %s",
-                               conffile, strerror(errno));
+               char errbuf[1024];
+               ERROR ("sensors plugin: fopen(%s) failed: %s", conffile,
+                               sstrerror (errno, errbuf, sizeof (errbuf)));
                return;
        }
 
@@ -292,7 +287,7 @@ static void sensors_load_conf (void)
        fclose (fh);
        if (status != 0)
        {
-               syslog (LOG_ERR, "sensors plugin: Cannot initialize sensors. "
+               ERROR ("sensors plugin: Cannot initialize sensors. "
                                "Data will not be collected.");
                return;
        }
@@ -315,7 +310,7 @@ static void sensors_load_conf (void)
                                continue;
 
                        /* Only known features */
-                       for (i = 0; known_features[i].type >= 0; i++)
+                       for (i = 0; i < known_features_num; i++)
                        {
                                if (strcmp (data->name, known_features[i].label) != 0)
                                        continue;
@@ -324,16 +319,16 @@ static void sensors_load_conf (void)
                                if (sensors_get_ignored (*chip, data->number) == 0)
                                        break;
 
-                               DBG ("Adding feature: %s-%s-%s",
+                               DEBUG ("Adding feature: %s-%s-%s",
                                                chip->prefix,
-                                               sensor_type_prefix[known_features[i].type],
+                                               sensor_to_type[known_features[i].type],
                                                data->name);
 
                                if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
                                {
-                                       DBG ("malloc: %s", strerror (errno));
-                                       syslog (LOG_ERR, "sensors plugin:  malloc: %s",
-                                                       strerror (errno));
+                                       char errbuf[1024];
+                                       ERROR ("sensors plugin: malloc: %s",
+                                                       sstrerror (errno, errbuf, sizeof (errbuf)));
                                        break;
                                }
 
@@ -362,7 +357,7 @@ static void sensors_load_conf (void)
        if (first_feature == NULL)
        {
                sensors_cleanup ();
-               syslog (LOG_INFO, "sensors plugin: lm_sensors reports no "
+               INFO ("sensors plugin: lm_sensors reports no "
                                "features. Data will not be collected.");
        }
 } /* void sensors_load_conf */
@@ -382,7 +377,8 @@ static void sensors_submit (const char *plugin_instance,
        value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
 
-       if (ignorelist_match (sensor_list, type_instance))
+       if ((sensor_list != NULL)
+                       && (ignorelist_match (sensor_list, type_instance)))
                return;
 
        values[0].gauge = val;
@@ -390,7 +386,7 @@ static void sensors_submit (const char *plugin_instance,
        vl.values = values;
        vl.values_len = 1;
        vl.time = time (NULL);
-       strcpy (vl.host, hostname);
+       strcpy (vl.host, hostname_g);
        strcpy (vl.plugin, "sensors");
        strcpy (vl.plugin_instance, plugin_instance);
        strcpy (vl.type_instance, type_instance);
@@ -402,7 +398,6 @@ static int sensors_read (void)
 {
        featurelist_t *feature;
        double value;
-       char chip_fullprefix[512];
 
        char plugin_instance[DATA_MAX_NAME_LEN];
        char type_instance[DATA_MAX_NAME_LEN];
@@ -445,23 +440,31 @@ static int sensors_read (void)
                strncpy (type_instance, feature->data->name, DATA_MAX_NAME_LEN);
 
                sensors_submit (plugin_instance,
-                               sensor_to_type[feature->type]
+                               sensor_to_type[feature->type],
                                type_instance,
                                value);
        } /* for feature = first_feature .. NULL */
+
+       return (0);
 } /* int sensors_read */
 #endif /* SENSORS_HAVE_READ */
 
-void module_register (void)
+void module_register (modreg_e load)
 {
-       plugin_register_data_set (&fanspeed_ds);
-       plugin_register_data_set (&temperature_ds);
-       plugin_register_data_set (&voltage_ds);
+       if (load & MR_DATASETS)
+       {
+               plugin_register_data_set (&fanspeed_ds);
+               plugin_register_data_set (&temperature_ds);
+               plugin_register_data_set (&voltage_ds);
+       }
 
 #if SENSORS_HAVE_READ
-       plugin_register_config ("sensors", sensors_config,
-                       config_keys, config_keys_num);
-       plugin_register_read ("sensors", sensors_read);
-       plugin_register_shutdown ("sensors", sensors_shutdown);
+       if (load & MR_READ)
+       {
+               plugin_register_config ("sensors", sensors_config,
+                               config_keys, config_keys_num);
+               plugin_register_read ("sensors", sensors_read);
+               plugin_register_shutdown ("sensors", sensors_shutdown);
+       }
 #endif
 } /* void module_register */