cpufreq plugin: Converted to the new plugin interface.
[collectd.git] / src / cpufreq.c
1 /**
2  * collectd - src/cpufreq.c
3  * Copyright (C) 2005-2007  Peter Holik
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Peter Holik <peter at holik.at>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #define MODULE_NAME "cpufreq"
28
29 #if defined(KERNEL_LINUX)
30 # define CPUFREQ_HAVE_READ 1
31 #else
32 # define CPUFREQ_HAVE_READ 0
33 #endif
34
35 static data_source_t data_source[1] =
36 {
37         {"value", DS_TYPE_GAUGE, 0, NAN}
38 };
39
40 static data_set_t data_set =
41 {
42         "cpufreq", 1, data_source
43 };
44
45 #if CPUFREQ_HAVE_READ
46 #ifdef KERNEL_LINUX
47 static int num_cpu = 0;
48 #endif
49
50 static int cpufreq_init (void)
51 {
52 #ifdef KERNEL_LINUX
53         int status;
54         char filename[256];
55
56         num_cpu = 0;
57
58         while (1)
59         {
60                 status = snprintf (filename, sizeof (filename),
61                                 "/sys/devices/system/cpu/cpu%d/cpufreq/"
62                                 "scaling_cur_freq", num_cpu);
63                 if (status < 1 || status >= sizeof (filename))
64                         break;
65
66                 if (access (filename, R_OK))
67                         break;
68
69                 num_cpu++;
70         }
71
72         syslog (LOG_INFO, "cpufreq plugin: Found %d CPU%s", num_cpu,
73                         (num_cpu == 1) ? "" : "s");
74
75         if (num_cpu == 0)
76                 plugin_unregister_read ("cpufreq");
77 #endif /* defined(KERNEL_LINUX) */
78
79         return (0);
80 } /* int cpufreq_init */
81
82 static void cpufreq_submit (int cpu_num, double value)
83 {
84         value_t values[1];
85         value_list_t vl = VALUE_LIST_INIT;
86
87         values[0].gauge = value;
88
89         vl.values = values;
90         vl.values_len = 1;
91         vl.time = time (NULL);
92         strcpy (vl.host, hostname);
93         strcpy (vl.plugin, "cpufreq");
94         snprintf (vl.type_instance, sizeof (vl.type_instance),
95                         "%i", cpu_num);
96
97         plugin_dispatch_values ("cpufreq", &vl);
98 }
99
100 static int cpufreq_read (void)
101 {
102 #ifdef KERNEL_LINUX
103         int status;
104         unsigned long long val;
105         int i = 0;
106         FILE *fp;
107         char filename[256];
108         char buffer[16];
109
110         for (i = 0; i < num_cpu; i++)
111         {
112                 status = snprintf (filename, sizeof (filename),
113                                 "/sys/devices/system/cpu/cpu%d/cpufreq/"
114                                 "scaling_cur_freq", i);
115                 if (status < 1 || status >= sizeof (filename))
116                         return (-1);
117
118                 if ((fp = fopen (filename, "r")) == NULL)
119                 {
120                         syslog (LOG_WARNING, "cpufreq: fopen: %s", strerror (errno));
121                         return (-1);
122                 }
123
124                 if (fgets (buffer, 16, fp) == NULL)
125                 {
126                         syslog (LOG_WARNING, "cpufreq: fgets: %s", strerror (errno));
127                         fclose (fp);
128                         return (-1);
129                 }
130
131                 if (fclose (fp))
132                         syslog (LOG_WARNING, "cpufreq: fclose: %s", strerror (errno));
133
134                 /* You're seeing correctly: The file is reporting kHz values.. */
135                 val = atoll (buffer) * 1000;
136
137                 cpufreq_submit (i, val);
138         }
139 #endif /* defined(KERNEL_LINUX) */
140
141         return (0);
142 } /* int cpufreq_read */
143 #endif /* CPUFREQ_HAVE_READ */
144 #undef BUFSIZE
145
146 void module_register (void)
147 {
148         plugin_register_data_set (&data_set);
149
150 #if CPUFREQ_HAVE_READ
151         plugin_register_init ("cpufreq", cpufreq_init);
152         plugin_register_read ("cpufreq", cpufreq_read);
153 #endif /* CPUFREQ_HAVE_READ */
154 }