rrdtool plugin: Make sure "cache_timeout + random_variation" dosn't get negative.
[collectd.git] / src / thermal.c
index a6ebe4e..0ad0d90 100644 (file)
@@ -1,6 +1,6 @@
 /**
  * collectd - src/thermal.c
- * Copyright (C) 2008  Micha³ Miros³aw
+ * Copyright (C) 2008  Michał Mirosław
  *
  * 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 +16,7 @@
  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  *
  * Authors:
- *   Micha³ Miros³aw <mirq-linux at rere.qmqm.pl>
+ *   Michał Mirosław <mirq-linux at rere.qmqm.pl>
  **/
 
 #include "collectd.h"
 # error "This module is for Linux only."
 #endif
 
+static const char *config_keys[] = {
+       "Device",
+       "IgnoreSelected",
+       "ForceUseProcfs"
+};
+
 const char *const dirname_sysfs = "/sys/class/thermal";
 const char *const dirname_procfs = "/proc/acpi/thermal_zone";
 
-static char force_procfs = 0;
+static _Bool force_procfs = 0;
 static ignorelist_t *device_list;
-static value_list_t vl_temp_template = VALUE_LIST_STATIC;
-static value_list_t vl_state_template = VALUE_LIST_STATIC;
 
 enum dev_type {
        TEMP = 0,
        COOLING_DEV
 };
 
-static void thermal_submit (const char *plugin_instance, enum dev_type dt, double value)
-{
-       value_list_t vl = dt == TEMP ? vl_temp_template : vl_state_template;
-       value_t vt;
-
-       vt.gauge = value;
-
-       vl.values = &vt;
-       vl.time = time (NULL);
-       sstrncpy (vl.plugin_instance, plugin_instance, sizeof(vl.plugin_instance));
-
-       plugin_dispatch_values (dt == TEMP ? "temperature" : "gauge", &vl);
-}
-
-static int read_file_contents (const char *filename, char *buf, int bufsize)
+static void thermal_submit (const char *plugin_instance, enum dev_type dt,
+               gauge_t value)
 {
-       FILE *fh;
-       int n;
+       value_list_t vl = VALUE_LIST_INIT;
+       value_t v;
 
-       if ((fh = fopen (filename, "r")) == NULL)
-               return -1;
+       v.gauge = value;
+       vl.values = &v;
 
-       n = fread(buf, 1, bufsize, fh);
-       fclose(fh);
+       sstrncpy (vl.plugin, "thermal", sizeof(vl.plugin));
+       if (plugin_instance != NULL)
+               sstrncpy (vl.plugin_instance, plugin_instance,
+                               sizeof (vl.plugin_instance));
+       sstrncpy (vl.type,
+                       (dt == TEMP) ? "temperature" : "gauge",
+                       sizeof (vl.type));
 
-       return n;
+       plugin_dispatch_values (&vl);
 }
 
-static int thermal_sysfs_device_read (const char *name)
+static int thermal_sysfs_device_read (const char __attribute__((unused)) *dir,
+               const char *name, void __attribute__((unused)) *user_data)
 {
        char filename[256];
        char data[1024];
        int len;
-       int ok = 0;
+       _Bool success = 0;
+
+       if (device_list && ignorelist_match (device_list, name))
+               return -1;
 
-       len = snprintf (filename, sizeof (filename), "%s/%s/temp", dirname_sysfs, name);
-       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
+       len = snprintf (filename, sizeof (filename),
+                       "%s/%s/temp", dirname_sysfs, name);
+       if ((len < 0) || ((size_t) len >= sizeof (filename)))
                return -1;
 
        len = read_file_contents (filename, data, sizeof(data));
@@ -92,12 +93,13 @@ static int thermal_sysfs_device_read (const char *name)
 
                if (endptr == data + len && errno == 0) {
                        thermal_submit(name, TEMP, temp);
-                       ++ok;
+                       success = 1;
                }
        }
 
-       len = snprintf (filename, sizeof (filename), "%s/%s/cur_state", dirname_sysfs, name);
-       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
+       len = snprintf (filename, sizeof (filename),
+                       "%s/%s/cur_state", dirname_sysfs, name);
+       if ((len < 0) || ((size_t) len >= sizeof (filename)))
                return -1;
 
        len = read_file_contents (filename, data, sizeof(data));
@@ -111,44 +113,51 @@ static int thermal_sysfs_device_read (const char *name)
 
                if (endptr == data + len && errno == 0) {
                        thermal_submit(name, COOLING_DEV, state);
-                       ++ok;
+                       success = 1;
                }
        }
 
-       return ok ? 0 : -1;
+       return (success ? 0 : -1);
 }
 
-static int thermal_procfs_device_read (const char *name)
+static int thermal_procfs_device_read (const char __attribute__((unused)) *dir,
+               const char *name, void __attribute__((unused)) *user_data)
 {
        const char str_temp[] = "temperature:";
        char filename[256];
        char data[1024];
        int len;
 
+       if (device_list && ignorelist_match (device_list, name))
+               return -1;
+
        /**
         * rechot ~ # cat /proc/acpi/thermal_zone/THRM/temperature
         * temperature:             55 C
         */
        
-       len = snprintf (filename, sizeof (filename), "%s/%s/temperature", dirname_procfs, name);
-       if ((len < 0) || ((unsigned int)len >= sizeof (filename)))
+       len = snprintf (filename, sizeof (filename),
+                       "%s/%s/temperature", dirname_procfs, name);
+       if ((len < 0) || ((size_t) len >= sizeof (filename)))
                return -1;
 
        len = read_file_contents (filename, data, sizeof(data));
-       if (len > sizeof(str_temp) && data[--len] == '\n' && !strncmp(data, str_temp, sizeof(str_temp)-1)) {
+       if ((len > 0) && ((size_t) len > sizeof(str_temp))
+                       && (data[--len] == '\n')
+                       && (! strncmp(data, str_temp, sizeof(str_temp)-1))) {
                char *endptr = NULL;
                double temp;
-               double celsius, add;
+               double factor, add;
                
                if (data[--len] == 'C') {
                        add = 0;
-                       celsius = 1;
+                       factor = 1.0;
                } else if (data[len] == 'F') {
                        add = -32;
-                       celsius = 5/9;
+                       factor = 5.0/9.0;
                } else if (data[len] == 'K') {
                        add = -273.15;
-                       celsius = 1;
+                       factor = 1.0;
                } else
                        return -1;
 
@@ -161,7 +170,7 @@ static int thermal_procfs_device_read (const char *name)
                ++len;
 
                errno = 0;
-               temp = (strtod (data + len, &endptr) + add) * celsius;
+               temp = (strtod (data + len, &endptr) + add) * factor;
 
                if (endptr != data + len && errno == 0) {
                        thermal_submit(name, TEMP, temp);
@@ -172,12 +181,6 @@ static int thermal_procfs_device_read (const char *name)
        return -1;
 }
 
-static const char *config_keys[] = {
-       "Device",
-       "IgnoreSelected",
-       "ForceUseProcfs"
-};
-
 static int thermal_config (const char *key, const char *value)
 {
        if (device_list == NULL)
@@ -195,17 +198,13 @@ static int thermal_config (const char *key, const char *value)
        else if (strcasecmp (key, "IgnoreSelected") == 0)
        {
                ignorelist_set_invert (device_list, 1);
-               if ((strcasecmp (value, "True") == 0)
-                               || (strcasecmp (value, "Yes") == 0)
-                               || (strcasecmp (value, "On") == 0))
+               if (IS_TRUE (value))
                        ignorelist_set_invert (device_list, 0);
        }
        else if (strcasecmp (key, "ForceUseProcfs") == 0)
        {
                force_procfs = 0;
-               if ((strcasecmp (value, "True") == 0)
-                               || (strcasecmp (value, "Yes") == 0)
-                               || (strcasecmp (value, "On") == 0))
+               if (IS_TRUE (value))
                        force_procfs = 1;
        }
        else
@@ -216,48 +215,16 @@ static int thermal_config (const char *key, const char *value)
        return 0;
 }
 
-static int walk_directory (const char *dir, int (*callback)(const char *dev))
-{
-       struct dirent *ent;
-       DIR *dh;
-       int ok = 0;
-
-       if ((dh = opendir (dir)) == NULL)
-       {
-               char errbuf[1024];
-               ERROR ("Cannot open '%s': %s", dir,
-                               sstrerror (errno, errbuf, sizeof (errbuf)));
-               return -1;
-       }
-
-       while ((ent = readdir (dh)) != NULL)
-       {
-               if (ent->d_name[0] == '.')
-                       continue;
-
-               if (device_list) {
-                       DEBUG ("thermal plugin: Checking ignorelist for '%s'", ent->d_name);
-                       if (ignorelist_match (device_list, ent->d_name))
-                               continue;
-               }
-
-               if (!callback(ent->d_name))
-                       ++ok;
-       }
-
-       closedir (dh);
-
-       return ok ? 0 : -1;
-}
-
 static int thermal_sysfs_read (void)
 {
-       return walk_directory (dirname_sysfs, thermal_sysfs_device_read);
+       return walk_directory (dirname_sysfs, thermal_sysfs_device_read,
+                       /* user_data = */ NULL, /* include hidden */ 0);
 }
 
 static int thermal_procfs_read (void)
 {
-       return walk_directory (dirname_procfs, thermal_procfs_device_read);
+       return walk_directory (dirname_procfs, thermal_procfs_device_read,
+                       /* user_data = */ NULL, /* include hidden */ 0);
 }
 
 static int thermal_init (void)
@@ -270,21 +237,6 @@ static int thermal_init (void)
                ret = plugin_register_read ("thermal", thermal_procfs_read);
        }
 
-       if (!ret) {
-               vl_temp_template.values_len = 1;
-               vl_temp_template.interval = interval_g;
-               sstrncpy (vl_temp_template.host, hostname_g,
-                       sizeof(vl_temp_template.host));
-               sstrncpy (vl_temp_template.plugin, "thermal",
-                       sizeof(vl_temp_template.plugin));
-               sstrncpy (vl_temp_template.type_instance, "temperature",
-                       sizeof(vl_temp_template.type_instance));
-
-               vl_state_template = vl_temp_template;
-               sstrncpy (vl_state_template.type_instance, "cooling_state",
-                       sizeof(vl_state_template.type_instance));
-       }
-
        return ret;
 }