processes plugin: Comment out nonexistent struct members.
[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 static int num_cpu = 0;
30
31 static int cpufreq_init (void)
32 {
33         int status;
34         char filename[256];
35
36         num_cpu = 0;
37
38         while (1)
39         {
40                 status = ssnprintf (filename, sizeof (filename),
41                                 "/sys/devices/system/cpu/cpu%d/cpufreq/"
42                                 "scaling_cur_freq", num_cpu);
43                 if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
44                         break;
45
46                 if (access (filename, R_OK))
47                         break;
48
49                 num_cpu++;
50         }
51
52         INFO ("cpufreq plugin: Found %d CPU%s", num_cpu,
53                         (num_cpu == 1) ? "" : "s");
54
55         if (num_cpu == 0)
56                 plugin_unregister_read ("cpufreq");
57
58         return (0);
59 } /* int cpufreq_init */
60
61 static void cpufreq_submit (int cpu_num, double value)
62 {
63         value_t values[1];
64         value_list_t vl = VALUE_LIST_INIT;
65
66         values[0].gauge = value;
67
68         vl.values = values;
69         vl.values_len = 1;
70         vl.time = time (NULL);
71         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
72         sstrncpy (vl.plugin, "cpufreq", sizeof (vl.plugin));
73         sstrncpy (vl.type, "cpufreq", sizeof (vl.type));
74         ssnprintf (vl.type_instance, sizeof (vl.type_instance),
75                         "%i", cpu_num);
76
77         plugin_dispatch_values (&vl);
78 }
79
80 static int cpufreq_read (void)
81 {
82         int status;
83         unsigned long long val;
84         int i = 0;
85         FILE *fp;
86         char filename[256];
87         char buffer[16];
88
89         for (i = 0; i < num_cpu; i++)
90         {
91                 status = ssnprintf (filename, sizeof (filename),
92                                 "/sys/devices/system/cpu/cpu%d/cpufreq/"
93                                 "scaling_cur_freq", i);
94                 if ((status < 1) || ((unsigned int)status >= sizeof (filename)))
95                         return (-1);
96
97                 if ((fp = fopen (filename, "r")) == NULL)
98                 {
99                         char errbuf[1024];
100                         WARNING ("cpufreq: fopen (%s): %s", filename,
101                                         sstrerror (errno, errbuf,
102                                                 sizeof (errbuf)));
103                         return (-1);
104                 }
105
106                 if (fgets (buffer, 16, fp) == NULL)
107                 {
108                         char errbuf[1024];
109                         WARNING ("cpufreq: fgets: %s",
110                                         sstrerror (errno, errbuf,
111                                                 sizeof (errbuf)));
112                         fclose (fp);
113                         return (-1);
114                 }
115
116                 if (fclose (fp))
117                 {
118                         char errbuf[1024];
119                         WARNING ("cpufreq: fclose: %s",
120                                         sstrerror (errno, errbuf,
121                                                 sizeof (errbuf)));
122                 }
123
124
125                 /* You're seeing correctly: The file is reporting kHz values.. */
126                 val = atoll (buffer) * 1000;
127
128                 cpufreq_submit (i, val);
129         }
130
131         return (0);
132 } /* int cpufreq_read */
133
134 void module_register (void)
135 {
136         plugin_register_init ("cpufreq", cpufreq_init);
137         plugin_register_read ("cpufreq", cpufreq_read);
138 }