Merge branch 'pr/1649'
[collectd.git] / src / cpufreq.c
index e53e495..a2cc3ad 100644 (file)
  **/
 
 #include "collectd.h"
+
 #include "common.h"
 #include "plugin.h"
 
-#define MODULE_NAME "cpufreq"
-
-#if defined(KERNEL_LINUX)
-# define CPUFREQ_HAVE_READ 1
-#else
-# define CPUFREQ_HAVE_READ 0
-#endif
-
-static data_source_t data_source[1] =
-{
-       {"value", DS_TYPE_GAUGE, 0, NAN}
-};
-
-static data_set_t data_set =
-{
-       "cpufreq", 1, data_source
-};
-
-#if CPUFREQ_HAVE_READ
-#ifdef KERNEL_LINUX
 static int num_cpu = 0;
-#endif
 
 static int cpufreq_init (void)
 {
-#ifdef KERNEL_LINUX
         int status;
        char filename[256];
 
@@ -57,10 +36,10 @@ static int cpufreq_init (void)
 
        while (1)
        {
-               status = snprintf (filename, sizeof (filename),
+               status = ssnprintf (filename, sizeof (filename),
                                "/sys/devices/system/cpu/cpu%d/cpufreq/"
                                "scaling_cur_freq", num_cpu);
-               if (status < 1 || status >= sizeof (filename))
+               if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
                        break;
 
                if (access (filename, R_OK))
@@ -69,86 +48,55 @@ static int cpufreq_init (void)
                num_cpu++;
        }
 
-       syslog (LOG_INFO, "cpufreq plugin: Found %d CPU%s", num_cpu,
+       INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
                        (num_cpu == 1) ? "" : "s");
 
        if (num_cpu == 0)
                plugin_unregister_read ("cpufreq");
-#endif /* defined(KERNEL_LINUX) */
 
        return (0);
 } /* int cpufreq_init */
 
-static void cpufreq_submit (int cpu_num, double value)
+static void cpufreq_submit (int cpu_num, value_t value)
 {
-       value_t values[1];
        value_list_t vl = VALUE_LIST_INIT;
 
-       values[0].gauge = value;
-
-       vl.values = values;
+       vl.values = &value;
        vl.values_len = 1;
-       vl.time = time (NULL);
-       strcpy (vl.host, hostname);
-       strcpy (vl.plugin, "cpufreq");
-       snprintf (vl.type_instance, sizeof (vl.type_instance),
-                       "%i", cpu_num);
+       sstrncpy (vl.host, hostname_g, sizeof (vl.host));
+       sstrncpy (vl.plugin, "cpufreq", sizeof (vl.plugin));
+       sstrncpy (vl.type, "cpufreq", sizeof (vl.type));
+       ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i", cpu_num);
 
-       plugin_dispatch_values ("cpufreq", &vl);
+       plugin_dispatch_values (&vl);
 }
 
 static int cpufreq_read (void)
 {
-#ifdef KERNEL_LINUX
-        int status;
-       unsigned long long val;
-       int i = 0;
-       FILE *fp;
-       char filename[256];
-       char buffer[16];
-
-       for (i = 0; i < num_cpu; i++)
+       for (int i = 0; i < num_cpu; i++)
        {
-               status = snprintf (filename, sizeof (filename),
-                               "/sys/devices/system/cpu/cpu%d/cpufreq/"
-                               "scaling_cur_freq", i);
-               if (status < 1 || status >= sizeof (filename))
-                       return (-1);
-
-               if ((fp = fopen (filename, "r")) == NULL)
-               {
-                       syslog (LOG_WARNING, "cpufreq: fopen: %s", strerror (errno));
-                       return (-1);
-               }
+               char filename[PATH_MAX];
+               ssnprintf (filename, sizeof (filename),
+                               "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i);
 
-               if (fgets (buffer, 16, fp) == NULL)
+               value_t v;
+               if (parse_value_file (filename, &v, DS_TYPE_GAUGE) != 0)
                {
-                       syslog (LOG_WARNING, "cpufreq: fgets: %s", strerror (errno));
-                       fclose (fp);
-                       return (-1);
+                       WARNING ("cpufreq plugin: Reading \"%s\" failed.", filename);
+                       continue;
                }
 
-               if (fclose (fp))
-                       syslog (LOG_WARNING, "cpufreq: fclose: %s", strerror (errno));
+               /* convert kHz to Hz */
+               v.gauge *= 1000.0;
 
-               /* You're seeing correctly: The file is reporting kHz values.. */
-               val = atoll (buffer) * 1000;
-
-               cpufreq_submit (i, val);
+               cpufreq_submit (i, v);
        }
-#endif /* defined(KERNEL_LINUX) */
 
        return (0);
 } /* int cpufreq_read */
-#endif /* CPUFREQ_HAVE_READ */
-#undef BUFSIZE
 
 void module_register (void)
 {
-       plugin_register_data_set (&data_set);
-
-#if CPUFREQ_HAVE_READ
        plugin_register_init ("cpufreq", cpufreq_init);
        plugin_register_read ("cpufreq", cpufreq_read);
-#endif /* CPUFREQ_HAVE_READ */
 }