Code

sensors plugin: Implemented support for libsensors3.
authorFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 25 Dec 2007 17:11:47 +0000 (18:11 +0100)
committerFlorian Forster <octo@leeloo.lan.home.verplant.org>
Tue, 25 Dec 2007 17:11:47 +0000 (18:11 +0100)
src/sensors.c

index a30549fea5ebf1b8dcbde4447632b9c8261aa3f1..9319f2048a46e8492e437af9d70f336d8461cdd2 100644 (file)
 # include <sensors/sensors.h>
 #endif
 
+#if !defined(SENSORS_API_VERSION)
+# define SENSORS_API_VERSION 0x000
+#endif
+
 #define SENSOR_TYPE_VOLTAGE     0
 #define SENSOR_TYPE_FANSPEED    1
 #define SENSOR_TYPE_TEMPERATURE 2
 #define SENSOR_TYPE_UNKNOWN     3
 
+#if SENSORS_API_VERSION < 0x400
 static char *sensor_to_type[] =
 {
        "voltage",
@@ -118,6 +123,7 @@ static sensors_labeltypes_t known_features[] =
 };
 static int known_features_num = STATIC_ARRAY_SIZE (known_features);
 /* end new naming */
+#endif /* SENSORS_API_VERSION < 0x400 */
 
 static const char *config_keys[] =
 {
@@ -127,20 +133,7 @@ static const char *config_keys[] =
 };
 static int config_keys_num = 2;
 
-static ignorelist_t *sensor_list;
-
-#ifndef SENSORS_CONF_PATH
-# define SENSORS_CONF_PATH "/etc/sensors.conf"
-#endif
-
-static const char *conffile = SENSORS_CONF_PATH;
-/* SENSORS_CONF_PATH */
-
-/*
- * remember stat of the loaded config
- */
-static time_t sensors_config_mtime = 0;
-
+#if SENSORS_API_VERSION < 0x400
 typedef struct featurelist
 {
        const sensors_chip_name    *chip;
@@ -149,7 +142,34 @@ typedef struct featurelist
        struct featurelist         *next;
 } featurelist_t;
 
+# ifndef SENSORS_CONF_PATH
+#  define SENSORS_CONF_PATH "/etc/sensors.conf"
+# endif
+/* #endif SENSORS_API_VERSION < 0x400 */
+
+#elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
+typedef struct featurelist
+{
+       const sensors_chip_name    *chip;
+       const sensors_feature      *feature;
+       const sensors_subfeature   *subfeature;
+       struct featurelist         *next;
+} featurelist_t;
+
+# ifndef SENSORS_CONF_PATH
+#  define SENSORS_CONF_PATH "/etc/sensors3.conf"
+# endif
+/* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
+
+#else /* if SENSORS_API_VERSION >= 0x500 */
+# error "This version of libsensors is not supported yet. Please report this " \
+       "as bug."
+#endif
+
+static const char *conffile = SENSORS_CONF_PATH;
 featurelist_t *first_feature = NULL;
+static ignorelist_t *sensor_list;
+static time_t sensors_config_mtime = 0;
 
 static int sensors_config (const char *key, const char *value)
 {
@@ -203,14 +223,10 @@ static int sensors_load_conf (void)
 {
        FILE *fh;
        featurelist_t *last_feature = NULL;
-       featurelist_t *new_feature = NULL;
        
        const sensors_chip_name *chip;
        int chip_num;
 
-       const sensors_feature_data *data;
-       int data_num0, data_num1;
-
        struct stat statbuf;
        int status;
        
@@ -255,65 +271,128 @@ static int sensors_load_conf (void)
 
        sensors_config_mtime = statbuf.st_mtime;
 
+#if SENSORS_API_VERSION < 0x400
        chip_num = 0;
        while ((chip = sensors_get_detected_chips (&chip_num)) != NULL)
        {
-               data = NULL;
-               data_num0 = data_num1 = 0;
+               int feature_num0 = 0;
+               int feature_num1 = 0;
 
-               while ((data = sensors_get_all_features (*chip, &data_num0, &data_num1))
-                               != NULL)
+               while (42)
                {
+                       const sensors_feature_data *feature;
                        int i;
 
+                       feature = sensors_get_all_features (*chip,
+                                       &feature_num0, &feature_num1);
+
+                       /* Check if all features have been read. */
+                       if (feature == NULL)
+                               break;
+
                        /* "master features" only */
-                       if (data->mapping != SENSORS_NO_MAPPING)
+                       if (feature->mapping != SENSORS_NO_MAPPING)
                                continue;
 
                        /* Only known features */
                        for (i = 0; i < known_features_num; i++)
                        {
-                               if (strcmp (data->name, known_features[i].label) != 0)
+                               featurelist_t *fl;
+
+                               if (strcasecmp (feature->name,
+                                                       known_features[i].label)
+                                               != 0)
                                        continue;
 
                                /* skip ignored in sensors.conf */
-                               if (sensors_get_ignored (*chip, data->number) == 0)
+                               if (sensors_get_ignored (*chip, feature->number) == 0)
                                        break;
 
                                DEBUG ("Adding feature: %s-%s-%s",
                                                chip->prefix,
                                                sensor_to_type[known_features[i].type],
-                                               data->name);
+                                               feature->name);
 
-                               if ((new_feature = (featurelist_t *) malloc (sizeof (featurelist_t))) == NULL)
+                               fl = (featurelist_t *) malloc (sizeof (featurelist_t));
+                               if (fl == NULL)
                                {
-                                       char errbuf[1024];
-                                       ERROR ("sensors plugin: malloc: %s",
-                                                       sstrerror (errno, errbuf, sizeof (errbuf)));
-                                       break;
+                                       ERROR ("sensors plugin: malloc failed.");
+                                       continue;
                                }
+                               memset (fl, '\0', sizeof (featurelist_t));
 
-                               new_feature->chip = chip;
-                               new_feature->data = data;
-                               new_feature->type = known_features[i].type;
-                               new_feature->next = NULL;
+                               fl->chip = chip;
+                               fl->data = feature;
+                               fl->type = known_features[i].type;
 
                                if (first_feature == NULL)
-                               {
-                                       first_feature = new_feature;
-                                       last_feature  = new_feature;
-                               }
+                                       first_feature = fl;
                                else
-                               {
-                                       last_feature->next = new_feature;
-                                       last_feature = new_feature;
-                               }
+                                       last_feature->next = fl;
+                               last_feature = fl;
 
                                /* stop searching known features at first found */
                                break;
                        } /* for i */
                } /* while sensors_get_all_features */
        } /* while sensors_get_detected_chips */
+/* #endif SENSORS_API_VERSION < 0x400 */
+
+#elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
+       chip_num = 0;
+       DEBUG ("sensors plugin: Calling sensors_get_detected_chips (NULL, %p)..", (void *) &chip_num);
+       while ((chip = sensors_get_detected_chips (NULL, &chip_num)) != NULL)
+       {
+               const sensors_feature *feature;
+               int feature_num = 0;
+
+               DEBUG ("sensors plugin: Handling chip with prefix `%s'",
+                               (chip->prefix == NULL)
+                               ? "(nil)"
+                               : chip->prefix);
+
+               while ((feature = sensors_get_features (chip, &feature_num)) != NULL)
+               {
+                       const sensors_subfeature *subfeature;
+                       int subfeature_num = 0;
+
+                       /* Only handle voltage, fanspeeds and temperatures */
+                       if ((feature->type != SENSORS_FEATURE_IN)
+                                       && (feature->type != SENSORS_FEATURE_FAN)
+                                       && (feature->type != SENSORS_FEATURE_TEMP))
+                               continue;
+
+                       while ((subfeature = sensors_get_all_subfeatures (chip,
+                                                       feature, &subfeature_num)) != NULL)
+                       {
+                               featurelist_t *fl;
+
+                               if ((subfeature->type != SENSORS_SUBFEATURE_IN_INPUT)
+                                               && (subfeature->type != SENSORS_SUBFEATURE_FAN_INPUT)
+                                               && (subfeature->type != SENSORS_SUBFEATURE_TEMP_INPUT))
+                                       continue;
+
+                               fl = (featurelist_t *) malloc (sizeof (featurelist_t));
+                               if (fl == NULL)
+                               {
+                                       ERROR ("sensors plugin: malloc failed.");
+                                       continue;
+                               }
+                               memset (fl, '\0', sizeof (featurelist_t));
+
+                               fl->chip = chip;
+                               fl->feature = feature;
+                               fl->subfeature = subfeature;
+
+                               if (first_feature == NULL)
+                                       first_feature = fl;
+                               else
+                                       last_feature->next = fl;
+                               last_feature  = fl;
+                       } /* while (subfeature) */
+               } /* while (feature) */
+       } /* while (chip) */
+#endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
 
        if (first_feature == NULL)
        {
@@ -372,55 +451,96 @@ static void sensors_submit (const char *plugin_instance,
 
 static int sensors_read (void)
 {
-       featurelist_t *feature;
-       double value;
-
-       char plugin_instance[DATA_MAX_NAME_LEN];
-       char type_instance[DATA_MAX_NAME_LEN];
+       featurelist_t *fl;
 
        if (sensors_load_conf () != 0)
                return (-1);
 
-       for (feature = first_feature; feature != NULL; feature = feature->next)
+#if SENSORS_API_VERSION < 0x400
+       for (fl = first_feature; fl != NULL; fl = fl->next)
        {
-               if (sensors_get_feature (*feature->chip, feature->data->number, &value) < 0)
+               double value;
+               int status;
+               char plugin_instance[DATA_MAX_NAME_LEN];
+               char type_instance[DATA_MAX_NAME_LEN];
+
+               status = sensors_get_feature (*fl->chip,
+                               fl->data->number, &value);
+               if (status < 0)
                        continue;
 
                /* full chip name logic borrowed from lm_sensors */
-               if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
+               if (fl->chip->bus == SENSORS_CHIP_NAME_BUS_ISA)
                {
-                       if (snprintf (plugin_instance, DATA_MAX_NAME_LEN, "%s-isa-%04x",
-                                               feature->chip->prefix,
-                                               feature->chip->addr)
-                                       >= 512)
-                               continue;
+                       snprintf (plugin_instance, DATA_MAX_NAME_LEN,
+                                       "%s-isa-%04x",
+                                       fl->chip->prefix,
+                                       fl->chip->addr);
                }
-               else if (feature->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
+               else if (fl->chip->bus == SENSORS_CHIP_NAME_BUS_DUMMY)
                {
-                       if (snprintf (plugin_instance, 512, "%s-%s-%04x",
-                                               feature->chip->prefix,
-                                               feature->chip->busname,
-                                               feature->chip->addr)
-                                       >= 512)
-                               continue;
+                       snprintf (plugin_instance, 512, "%s-%s-%04x",
+                                       fl->chip->prefix,
+                                       fl->chip->busname,
+                                       fl->chip->addr);
                }
                else
                {
-                       if (snprintf (plugin_instance, 512, "%s-i2c-%d-%02x",
-                                               feature->chip->prefix,
-                                               feature->chip->bus,
-                                               feature->chip->addr)
-                                       >= 512)
-                               continue;
+                       snprintf (plugin_instance, 512, "%s-i2c-%d-%02x",
+                                       fl->chip->prefix,
+                                       fl->chip->bus,
+                                       fl->chip->addr);
                }
+               plugin_instance[sizeof (plugin_instance) - 1] = '\0';
 
-               strncpy (type_instance, feature->data->name, DATA_MAX_NAME_LEN);
+               strncpy (type_instance, fl->data->name, sizeof (type_instance));
+               type_instance[sizeof (type_instance) - 1] = '\0';
 
                sensors_submit (plugin_instance,
-                               sensor_to_type[feature->type],
+                               sensor_to_type[fl->type],
                                type_instance,
                                value);
-       } /* for feature = first_feature .. NULL */
+       } /* for fl = first_feature .. NULL */
+/* #endif SENSORS_API_VERSION < 0x400 */
+
+#elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500)
+       for (fl = first_feature; fl != NULL; fl = fl->next)
+       {
+               double value;
+               int status;
+               char plugin_instance[DATA_MAX_NAME_LEN];
+               char type_instance[DATA_MAX_NAME_LEN];
+               const char *type;
+
+               status = sensors_get_value (fl->chip,
+                               fl->subfeature->number, &value);
+               if (status < 0)
+                       continue;
+
+               status = sensors_snprintf_chip_name (plugin_instance,
+                               sizeof (plugin_instance), fl->chip);
+               if (status < 0)
+                       continue;
+               plugin_instance[sizeof (plugin_instance) - 1] = '\0';
+
+               strncpy (type_instance, fl->feature->name,
+                               sizeof (type_instance));
+               type_instance[sizeof (type_instance) - 1] = '\0';
+
+               if (fl->feature->type == SENSORS_FEATURE_IN)
+                       type = "voltage";
+               else if (fl->feature->type
+                               == SENSORS_FEATURE_FAN)
+                       type = "fanspeed";
+               else if (fl->feature->type
+                               == SENSORS_FEATURE_TEMP)
+                       type = "input";
+               else
+                       continue;
+
+               sensors_submit (plugin_instance, type, type_instance, value);
+       } /* for fl = first_feature .. NULL */
+#endif /* (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */
 
        return (0);
 } /* int sensors_read */