X-Git-Url: https://git.octo.it/?p=collectd.git;a=blobdiff_plain;f=src%2Fsensors.c;h=738944908185ae5abc6a3977855edd02768d9f1a;hp=0968e311fb170c63ba8c01727ed8ea7e423bf733;hb=633c3966f770e4d46651a2fe219a18d8a9907a9f;hpb=66e0fe82631c8a8e44ffcad8ffd378fab83bc83f diff --git a/src/sensors.c b/src/sensors.c index 0968e311..73894490 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -1,6 +1,7 @@ /** * collectd - src/sensors.c * Copyright (C) 2005-2008 Florian octo Forster + * Copyright (C) 2006 Luboš Staněk * * 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 @@ -16,7 +17,7 @@ * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA * * Authors: - * Florian octo Forster + * Florian octo Forster * * Lubos Stanek Wed Oct 27, 2006 * - config ExtendedSensorNaming option @@ -26,6 +27,10 @@ * - honor sensors.conf's ignored * - config Sensor option * - config IgnoreSelected option + * + * Henrique de Moraes Holschuh + * - use default libsensors config file on API 0x400 + * - config SensorConfigFile option **/ #include "collectd.h" @@ -131,7 +136,8 @@ static int known_features_num = STATIC_ARRAY_SIZE (known_features); static const char *config_keys[] = { "Sensor", - "IgnoreSelected" + "IgnoreSelected", + "SensorConfigFile" }; static int config_keys_num = STATIC_ARRAY_SIZE (config_keys); @@ -147,6 +153,7 @@ typedef struct featurelist # ifndef SENSORS_CONF_PATH # define SENSORS_CONF_PATH "/etc/sensors.conf" # endif +static char *conffile = SENSORS_CONF_PATH; /* #endif SENSORS_API_VERSION < 0x400 */ #elif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) @@ -158,9 +165,7 @@ typedef struct featurelist struct featurelist *next; } featurelist_t; -# ifndef SENSORS_CONF_PATH -# define SENSORS_CONF_PATH "/etc/sensors3.conf" -# endif +static char *conffile = NULL; /* #endif (SENSORS_API_VERSION >= 0x400) && (SENSORS_API_VERSION < 0x500) */ #else /* if SENSORS_API_VERSION >= 0x500 */ @@ -168,10 +173,8 @@ typedef struct featurelist "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; #if SENSORS_API_VERSION < 0x400 /* full chip name logic borrowed from lm_sensors */ @@ -182,7 +185,7 @@ static int sensors_snprintf_chip_name (char *buf, size_t buf_size, if (chip->bus == SENSORS_CHIP_NAME_BUS_ISA) { - status = snprintf (buf, buf_size, + status = ssnprintf (buf, buf_size, "%s-isa-%04x", chip->prefix, chip->addr); @@ -224,7 +227,19 @@ static int sensors_config (const char *key, const char *value) if (sensor_list == NULL) sensor_list = ignorelist_create (1); - if (strcasecmp (key, "Sensor") == 0) + /* TODO: This setting exists for compatibility with old versions of + * lm-sensors. Remove support for those ancient versions in the next + * major release. */ + if (strcasecmp (key, "SensorConfigFile") == 0) + { + char *tmp = strdup (value); + if (tmp != NULL) + { + sfree (conffile); + conffile = tmp; + } + } + else if (strcasecmp (key, "Sensor") == 0) { if (ignorelist_add (sensor_list, value)) { @@ -236,9 +251,7 @@ static int sensors_config (const char *key, const char *value) else if (strcasecmp (key, "IgnoreSelected") == 0) { ignorelist_set_invert (sensor_list, 1); - if ((strcasecmp (value, "True") == 0) - || (strcasecmp (value, "Yes") == 0) - || (strcasecmp (value, "On") == 0)) + if (IS_TRUE (value)) ignorelist_set_invert (sensor_list, 0); } else @@ -269,47 +282,37 @@ void sensors_free_features (void) static int sensors_load_conf (void) { - FILE *fh; + static int call_once = 0; + + FILE *fh = NULL; featurelist_t *last_feature = NULL; const sensors_chip_name *chip; int chip_num; - struct stat statbuf; int status; - - status = stat (conffile, &statbuf); - if (status != 0) - { - char errbuf[1024]; - ERROR ("sensors plugin: stat (%s) failed: %s", conffile, - sstrerror (errno, errbuf, sizeof (errbuf))); - sensors_config_mtime = 0; - } - if ((sensors_config_mtime != 0) - && (sensors_config_mtime == statbuf.st_mtime)) - return (0); + if (call_once) + return 0; - if (sensors_config_mtime != 0) - { - NOTICE ("sensors plugin: Reloading config from %s", - conffile); - sensors_free_features (); - sensors_config_mtime = 0; - } + call_once = 1; - fh = fopen (conffile, "r"); - if (fh == NULL) + if (conffile != NULL) { - char errbuf[1024]; - ERROR ("sensors plugin: fopen(%s) failed: %s", conffile, - sstrerror (errno, errbuf, sizeof (errbuf))); - return (-1); + fh = fopen (conffile, "r"); + if (fh == NULL) + { + char errbuf[1024]; + ERROR ("sensors plugin: fopen(%s) failed: %s", conffile, + sstrerror (errno, errbuf, sizeof (errbuf))); + return (-1); + } } status = sensors_init (fh); - fclose (fh); + if (fh) + fclose (fh); + if (status != 0) { ERROR ("sensors plugin: Cannot initialize sensors. " @@ -317,8 +320,6 @@ static int sensors_load_conf (void) return (-1); } - sensors_config_mtime = statbuf.st_mtime; - #if SENSORS_API_VERSION < 0x400 chip_num = 0; while ((chip = sensors_get_detected_chips (&chip_num)) != NULL) @@ -480,11 +481,10 @@ static void sensors_submit (const char *plugin_instance, value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - status = snprintf (match_key, sizeof (match_key), "%s/%s-%s", + status = ssnprintf (match_key, sizeof (match_key), "%s/%s-%s", plugin_instance, type, type_instance); if (status < 1) return; - match_key[sizeof (match_key) - 1] = '\0'; if (sensor_list != NULL) { @@ -497,19 +497,15 @@ static void sensors_submit (const char *plugin_instance, vl.values = values; vl.values_len = 1; - vl.time = time (NULL); - strncpy (vl.host, hostname_g, sizeof (vl.host)); - vl.host[sizeof (vl.host) - 1] = '\0'; - strncpy (vl.plugin, "sensors", sizeof (vl.plugin)); - vl.plugin[sizeof (vl.plugin) - 1] = '\0'; - strncpy (vl.plugin_instance, plugin_instance, + sstrncpy (vl.host, hostname_g, sizeof (vl.host)); + sstrncpy (vl.plugin, "sensors", sizeof (vl.plugin)); + sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance)); - vl.plugin_instance[sizeof (vl.plugin_instance) - 1] = '\0'; - strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - vl.type_instance[sizeof (vl.type_instance) - 1] = '\0'; + sstrncpy (vl.type, type, sizeof (vl.type)); + sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance)); - plugin_dispatch_values (type, &vl); + plugin_dispatch_values (&vl); } /* void sensors_submit */ static int sensors_read (void) @@ -536,11 +532,9 @@ static int sensors_read (void) sizeof (plugin_instance), fl->chip); if (status < 0) continue; - plugin_instance[sizeof (plugin_instance) - 1] = '\0'; - strncpy (type_instance, fl->data->name, + sstrncpy (type_instance, fl->data->name, sizeof (type_instance)); - type_instance[sizeof (type_instance) - 1] = '\0'; sensors_submit (plugin_instance, sensor_type_name_map[fl->type], @@ -567,11 +561,9 @@ static int sensors_read (void) sizeof (plugin_instance), fl->chip); if (status < 0) continue; - plugin_instance[sizeof (plugin_instance) - 1] = '\0'; - strncpy (type_instance, fl->feature->name, + sstrncpy (type_instance, fl->feature->name, sizeof (type_instance)); - type_instance[sizeof (type_instance) - 1] = '\0'; if (fl->feature->type == SENSORS_FEATURE_IN) type = "voltage";