cgroups_cpuacct plugin: Use type "cpu" to submit value lists.
[collectd.git] / src / cgroups_cpuacct.c
1 /**
2  * collectd - src/cgroups_cpuacct.c
3  * Copyright (C) 2011  Michael Stapelberg
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; only version 2 of the license is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Michael Stapelberg <michael at stapelberg.de>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_mount.h"
27 #include "utils_ignorelist.h"
28
29 static const char *config_keys[] =
30 {
31         "CGroup",
32         "IgnoreSelected"
33 };
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
35
36 static ignorelist_t *il_cgroup = NULL;
37
38 __attribute__ ((nonnull(1)))
39 __attribute__ ((nonnull(2)))
40 static void cgroups_submit_one (char const *plugin_instance,
41                 char const *type_instance, value_t value)
42 {
43         value_list_t vl = VALUE_LIST_INIT;
44
45         vl.values = &value;
46         vl.values_len = 1;
47         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
48         sstrncpy (vl.plugin, "cgroups_cpuacct", sizeof (vl.plugin));
49         sstrncpy (vl.plugin_instance, plugin_instance,
50                         sizeof (vl.plugin_instance));
51         sstrncpy (vl.type, "cpu", sizeof (vl.type));
52         sstrncpy (vl.type_instance, type_instance,
53                         sizeof (vl.type_instance));
54
55         plugin_dispatch_values (&vl);
56 } /* void cgroups_submit_one */
57
58 /*
59  * This callback reads the user/system CPU time for each cgroup.
60  */
61 static int read_cpuacct_procs (const char *dirname, char const *cgroup_name,
62     void *user_data)
63 {
64         char abs_path[PATH_MAX];
65         struct stat statbuf;
66         char buf[1024];
67         int status;
68
69         char *fields[8];
70         int numfields = 0;
71
72         value_t usertime;
73         value_t systemtime;
74
75         if (ignorelist_match (il_cgroup, cgroup_name))
76                 return (0);
77
78         ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, cgroup_name);
79
80         status = lstat (abs_path, &statbuf);
81         if (status != 0)
82         {
83                 ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
84                 return (-1);
85         }
86
87         /* We are only interested in directories, so skip everything else. */
88         if (!S_ISDIR (statbuf.st_mode))
89                 return (0);
90
91         ssnprintf (abs_path, sizeof (abs_path), "%s/%s/cpuacct.stat",
92                         dirname, cgroup_name);
93         status = (int) read_file_contents (abs_path, buf, sizeof (buf) - 1);
94         if (status <= 0)
95         {
96                 char errbuf[1024];
97                 ERROR ("cgroups_cpuacct plugin: read_file_contents(%s): %s",
98                                 abs_path, sstrerror (errno, errbuf, sizeof (errbuf)));
99                 return (-1);
100         }
101         buf[status] = '\0';
102
103         numfields = strsplit (buf, fields, STATIC_ARRAY_SIZE (fields));
104         if (numfields != 4)
105         {
106                 ERROR ("cgroups_cpuacct plugin: unexpected content in file %s",
107                                 abs_path);
108                 return (-1);
109         }
110
111         status = parse_value (fields[1], &usertime, DS_TYPE_DERIVE);
112         if (status == 0)
113                 cgroups_submit_one (cgroup_name, "user", usertime);
114
115         status = parse_value (fields[3], &systemtime, DS_TYPE_DERIVE);
116         if (status == 0)
117                 cgroups_submit_one (cgroup_name, "system", systemtime);
118
119         return (0);
120 }
121
122 /*
123  * Gets called for every file/folder in /sys/fs/cgroup/cpu,cpuacct (or
124  * whereever cpuacct is mounted on the system). Calls walk_directory with the
125  * read_cpuacct_procs callback on every folder it finds, such as "system".
126  *
127  */
128 static int read_cpuacct_root (const char *dirname, const char *filename,
129     void *user_data)
130 {
131   char abs_path[PATH_MAX];
132   struct stat statbuf;
133   int status;
134
135   ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
136
137   status = lstat (abs_path, &statbuf);
138   if (status != 0)
139   {
140     ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
141     return (-1);
142   }
143
144   if (S_ISDIR (statbuf.st_mode))
145   {
146     status = walk_directory (abs_path, read_cpuacct_procs,
147                     /* user_data = */ NULL,
148                     /* include_hidden = */ 0);
149     return (status);
150   }
151
152   return (0);
153 }
154
155 static int cgroups_init (void)
156 {
157         if (il_cgroup == NULL)
158                 il_cgroup = ignorelist_create (1);
159
160         return (0);
161 }
162
163 static int cgroups_config (const char *key, const char *value)
164 {
165         cgroups_init ();
166
167         if (strcasecmp (key, "CGroup") == 0)
168         {
169                 if (ignorelist_add (il_cgroup, value))
170                         return (1);
171                 return (0);
172         }
173         else if (strcasecmp (key, "IgnoreSelected") == 0)
174         {
175                 if (IS_TRUE (value))
176                         ignorelist_set_invert (il_cgroup, 0);
177                 else
178                         ignorelist_set_invert (il_cgroup, 1);
179                 return (0);
180         }
181
182         return (-1);
183 }
184
185 static int cgroups_read (void)
186 {
187         cu_mount_t *mnt_list;
188         cu_mount_t *mnt_ptr;
189         _Bool cgroup_found = 0;
190
191         mnt_list = NULL;
192         if (cu_mount_getlist (&mnt_list) == NULL)
193         {
194                 ERROR ("cgroups_cpuacct plugin: cu_mount_getlist failed.");
195                 return (-1);
196         }
197
198         for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
199         {
200                 /* Find the cgroup mountpoint which contains the cpuacct
201                  * controller. */
202                 if (strcmp(mnt_ptr->type, "cgroup") != 0 ||
203                         !cu_mount_getoptionvalue(mnt_ptr->options, "cpuacct"))
204                         continue;
205
206                 walk_directory (mnt_ptr->dir, read_cpuacct_root,
207                                 /* user_data = */ NULL,
208                                 /* include_hidden = */ 0);
209                 cgroup_found = 1;
210                 /* It doesn't make sense to check other cpuacct mount-points
211                  * (if any), they contain the same data. */
212                 break;
213         }
214
215         cu_mount_freelist (mnt_list);
216
217         if (!cgroup_found)
218         {
219                 WARNING ("cgroups_cpuacct plugin: Unable to find cgroup "
220                                 "mount-point with the \"cpuacct\" option.");
221                 return (-1);
222         }
223
224         return (0);
225 } /* int cgroup_read */
226
227 void module_register (void)
228 {
229         plugin_register_config ("cgroups_cpuacct", cgroups_config,
230                         config_keys, config_keys_num);
231         plugin_register_init ("cgroups_cpuacct", cgroups_init);
232         plugin_register_read ("cgroups_cpuacct", cgroups_read);
233 } /* void module_register */