X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fsensors.c;h=a30549fea5ebf1b8dcbde4447632b9c8261aa3f1;hb=1a7050de376608268d11293d4e5faa3fb8516c5a;hp=99b925685fa755de65013176d98a6e8cdaff8926;hpb=b8be503b2b50366a9c498ba7e4ab66a228c6dfbf;p=collectd.git diff --git a/src/sensors.c b/src/sensors.c index 99b92568..a30549fe 100644 --- a/src/sensors.c +++ b/src/sensors.c @@ -33,51 +33,11 @@ #include "plugin.h" #include "configfile.h" #include "utils_ignorelist.h" -#include "utils_debug.h" #if defined(HAVE_SENSORS_SENSORS_H) # include -#else -# undef HAVE_LIBSENSORS #endif -#if defined(HAVE_LIBSENSORS) -# define SENSORS_HAVE_READ 1 -#else -# define SENSORS_HAVE_READ 0 -#endif - -static data_source_t data_source_fanspeed[1] = -{ - {"value", DS_TYPE_GAUGE, 0, NAN} -}; - -static data_set_t fanspeed_ds = -{ - "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 -}; - -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 -}; - -#if SENSORS_HAVE_READ #define SENSOR_TYPE_VOLTAGE 0 #define SENSOR_TYPE_FANSPEED 1 #define SENSOR_TYPE_TEMPERATURE 2 @@ -154,9 +114,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[] = @@ -200,7 +160,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); } @@ -239,7 +199,7 @@ void sensors_free_features (void) first_feature = NULL; } -static void sensors_load_conf (void) +static int sensors_load_conf (void) { FILE *fh; featurelist_t *last_feature = NULL; @@ -257,18 +217,19 @@ 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; } if ((sensors_config_mtime != 0) && (sensors_config_mtime == statbuf.st_mtime)) - return; + return (0); 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; @@ -277,18 +238,19 @@ 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)); - return; + 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 (status != 0) { - syslog (LOG_ERR, "sensors plugin: Cannot initialize sensors. " + ERROR ("sensors plugin: Cannot initialize sensors. " "Data will not be collected."); - return; + return (-1); } sensors_config_mtime = statbuf.st_mtime; @@ -309,7 +271,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; @@ -318,16 +280,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_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; } @@ -356,10 +318,13 @@ 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."); + return (-1); } -} /* void sensors_load_conf */ + + return (0); +} /* int sensors_load_conf */ static int sensors_shutdown (void) { @@ -373,18 +338,31 @@ static void sensors_submit (const char *plugin_instance, const char *type, const char *type_instance, double val) { + char match_key[1024]; + int status; + value_t values[1]; value_list_t vl = VALUE_LIST_INIT; - if (ignorelist_match (sensor_list, type_instance)) + status = snprintf (match_key, sizeof (match_key), "%s/%s-%s", + plugin_instance, type, type_instance); + if ((status < 1) || (status >= sizeof (match_key))) return; + match_key[sizeof (match_key) - 1] = '\0'; + + if (sensor_list != NULL) + { + DEBUG ("sensors plugin: Checking ignorelist for `%s'", match_key); + if (ignorelist_match (sensor_list, match_key)) + return; + } values[0].gauge = val; 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); @@ -400,7 +378,8 @@ static int sensors_read (void) char plugin_instance[DATA_MAX_NAME_LEN]; char type_instance[DATA_MAX_NAME_LEN]; - sensors_load_conf (); + if (sensors_load_conf () != 0) + return (-1); for (feature = first_feature; feature != NULL; feature = feature->next) { @@ -445,18 +424,11 @@ static int sensors_read (void) return (0); } /* int sensors_read */ -#endif /* SENSORS_HAVE_READ */ void module_register (void) { - 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); -#endif } /* void module_register */