From: Florian Forster Date: Mon, 12 Sep 2016 07:02:26 +0000 (+0200) Subject: cpufreq plugin: Use parse_value_file(). X-Git-Tag: collectd-5.7.0~98^2~3 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=270994227b52651b91a6e2b46e6b8eba32d789d5;p=collectd.git cpufreq plugin: Use parse_value_file(). --- diff --git a/src/cpufreq.c b/src/cpufreq.c index 1e9e8579..a2cc3ad2 100644 --- a/src/cpufreq.c +++ b/src/cpufreq.c @@ -25,8 +25,6 @@ #include "common.h" #include "plugin.h" -#define MODULE_NAME "cpufreq" - static int num_cpu = 0; static int cpufreq_init (void) @@ -59,72 +57,39 @@ static int cpufreq_init (void) 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; 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); + ssnprintf (vl.type_instance, sizeof (vl.type_instance), "%i", cpu_num); plugin_dispatch_values (&vl); } static int cpufreq_read (void) { - int status; - unsigned long long val; - FILE *fp; - char filename[256]; - char buffer[16]; - for (int i = 0; i < num_cpu; i++) { - status = ssnprintf (filename, sizeof (filename), - "/sys/devices/system/cpu/cpu%d/cpufreq/" - "scaling_cur_freq", i); - if ((status < 1) || ((unsigned int)status >= sizeof (filename))) - return (-1); + char filename[PATH_MAX]; + ssnprintf (filename, sizeof (filename), + "/sys/devices/system/cpu/cpu%d/cpufreq/scaling_cur_freq", i); - if ((fp = fopen (filename, "r")) == NULL) + value_t v; + if (parse_value_file (filename, &v, DS_TYPE_GAUGE) != 0) { - char errbuf[1024]; - WARNING ("cpufreq: fopen (%s): %s", filename, - sstrerror (errno, errbuf, - sizeof (errbuf))); - return (-1); + WARNING ("cpufreq plugin: Reading \"%s\" failed.", filename); + continue; } - if (fgets (buffer, 16, fp) == NULL) - { - char errbuf[1024]; - WARNING ("cpufreq: fgets: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - fclose (fp); - return (-1); - } - - if (fclose (fp)) - { - char errbuf[1024]; - WARNING ("cpufreq: fclose: %s", - sstrerror (errno, errbuf, - sizeof (errbuf))); - } - - - /* You're seeing correctly: The file is reporting kHz values.. */ - val = atoll (buffer) * 1000; + /* convert kHz to Hz */ + v.gauge *= 1000.0; - cpufreq_submit (i, val); + cpufreq_submit (i, v); } return (0);