[collectd] [PATCH] sensors 03: reload lm_sensors' configuration when changed

Lubos Stanek kolektor<span style="display: none;">.trailing-username</span>(a)<span style="display: none;">leading-domain.</span>atlas.cz
Mon Dec 11 23:35:16 CET 2006


Signed-off-by: Stanek Lubos <kolektor<span style="display: none;">.trailing-username</span>(a)<span style="display: none;">leading-domain.</span>atlas.cz>
---
 src/sensors.c |  103 ++++++++++++++++++++++++++++++++++++++++++++++-----------
 1 files changed, 83 insertions(+), 20 deletions(-)

diff --git a/src/sensors.c b/src/sensors.c
index 6ba8154..7c05b7a 100644
--- a/src/sensors.c
+++ b/src/sensors.c
@@ -185,6 +185,12 @@ #endif
 static const char *conffile = SENSORS_CONF_PATH;
 /* SENSORS_CONF_PATH */
 
+/*
+ * remember stat of the loaded config
+ */
+static struct stat sensors_conf_stat;
+static int sensors_conf_loaded = 0;
+
 typedef struct featurelist
 {
 	const sensors_chip_name    *chip;
@@ -234,9 +240,28 @@ static int sensors_config (char *key, ch
 	return (0);
 }
 
-static void collectd_sensors_init (void)
-{
 #if SENSORS_HAVE_READ
+void sensors_free_features (void)
+{
+	featurelist_t *thisft;
+	featurelist_t *nextft;
+
+	if (sensors_conf_loaded)
+	{
+		sensors_cleanup ();
+		sensors_conf_loaded = 0;
+	}
+
+	for (thisft = first_feature; thisft != NULL; thisft = nextft)
+	{
+		nextft = thisft->next;
+		sfree (thisft);
+	}
+	first_feature = NULL;
+}
+
+static void sensors_load_conf (int firsttime)
+{
 	FILE *fh;
 	featurelist_t *last_feature = NULL;
 	featurelist_t *new_feature;
@@ -247,13 +272,8 @@ #if SENSORS_HAVE_READ
 	const sensors_feature_data *data;
 	int data_num0, data_num1;
 	
+	sensors_free_features ();
 	new_feature = first_feature;
-	while (new_feature != NULL)
-	{
-		last_feature = new_feature->next;
-		free (new_feature);
-		new_feature = last_feature;
-	}
 
 #ifdef assert
 	assert (new_feature == NULL);
@@ -261,7 +281,11 @@ #ifdef assert
 #endif
 
 	if ((fh = fopen (conffile, "r")) == NULL)
+	{
+		syslog (LOG_ERR, MODULE_NAME": cannot open %s: %s. "
+				"Data will not be collected.", conffile, strerror(errno));
 		return;
+	}
 
 	if (sensors_init (fh))
 	{
@@ -271,6 +295,15 @@ #endif
 		return;
 	}
 
+	/* remember file status to detect changes */
+	if (fstat (fileno (fh), &sensors_conf_stat) == -1)
+	{
+		fclose (fh);
+		syslog (LOG_ERR, MODULE_NAME": cannot fstat %s: %s "
+				"Data will not be collected.", conffile, strerror(errno));
+		return;
+	}
+
 	fclose (fh);
 
 	chip_num = 0;
@@ -333,27 +366,32 @@ #endif
 		} /* while sensors_get_all_features */
 	} /* while sensors_get_detected_chips */
 
+	if (!firsttime)
+		syslog (LOG_INFO, MODULE_NAME": lm_sensors' configuration reloaded.");
+
 	if (first_feature == NULL)
+	{
 		sensors_cleanup ();
+		sensors_conf_loaded = 0;
+		syslog (LOG_INFO, MODULE_NAME": lm_sensors reports no features. "
+			"Data will not be collected.");
+	}
+	else
+		sensors_conf_loaded = 1;
+}
 #endif /* if SENSORS_HAVE_READ */
 
-	return;
+static void collectd_sensors_init (void)
+{
+#if SENSORS_HAVE_READ
+	sensors_load_conf (1);
+#endif /* if SENSORS_HAVE_READ */
 }
 
 static void sensors_shutdown (void)
 {
 #if SENSORS_HAVE_READ
-	featurelist_t *thisft = first_feature;
-	featurelist_t *nextft;
-
-	while (thisft != NULL)
-	{
-		nextft = thisft->next;
-		sfree (thisft);
-		thisft = nextft;
-	}
-
-	sensors_cleanup ();
+	sensors_free_features ();
 #endif /* if SENSORS_HAVE_READ */
 
 	ignorelist_free (sensor_list);
@@ -439,6 +477,31 @@ static void sensors_read (void)
 	featurelist_t *feature;
 	double value;
 	char chip_fullprefix[BUFSIZE];
+	struct stat changedbuf;
+
+	/* check sensors.conf for changes */
+	if (sensors_conf_loaded)
+	{
+		if (stat (conffile, &changedbuf) == -1)
+		{
+			syslog (LOG_ERR, MODULE_NAME": cannot stat %s: %s",
+					conffile, strerror(errno));
+			/*
+			 * sensors.conf does not exist,
+			 * throw away previous conf
+			 */
+			sensors_load_conf (0);
+		}
+		else
+		{
+			if ((changedbuf.st_size != sensors_conf_stat.st_size) ||
+					(changedbuf.st_mtime != sensors_conf_stat.st_mtime) ||
+					(changedbuf.st_ino != sensors_conf_stat.st_ino))
+			{
+				sensors_load_conf (0);
+			}
+		}
+	}
 
 	for (feature = first_feature; feature != NULL; feature = feature->next)
 	{
-- 
1.4.2.4




More information about the collectd mailing list