Merge branch 'collectd-5.5'
[collectd.git] / src / cgroups.c
1 /**
2  * collectd - src/cgroups.c
3  * Copyright (C) 2011  Michael Stapelberg
4  * Copyright (C) 2013  Florian Forster
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation; only version 2 of the license is applicable.
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  *   Michael Stapelberg <michael at stapelberg.de>
21  *   Florian Forster <octo at collectd.org>
22  **/
23
24 #include "collectd.h"
25
26 #include "common.h"
27 #include "plugin.h"
28 #include "configfile.h"
29 #include "utils_mount.h"
30 #include "utils_ignorelist.h"
31
32 static char const *config_keys[] =
33 {
34         "CGroup",
35         "IgnoreSelected"
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
38
39 static ignorelist_t *il_cgroup = NULL;
40
41 __attribute__ ((nonnull(1)))
42 __attribute__ ((nonnull(2)))
43 static void cgroups_submit_one (char const *plugin_instance,
44                 char const *type_instance, value_t value)
45 {
46         value_list_t vl = VALUE_LIST_INIT;
47
48         vl.values = &value;
49         vl.values_len = 1;
50         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
51         sstrncpy (vl.plugin, "cgroups", sizeof (vl.plugin));
52         sstrncpy (vl.plugin_instance, plugin_instance,
53                         sizeof (vl.plugin_instance));
54         sstrncpy (vl.type, "cpu", sizeof (vl.type));
55         sstrncpy (vl.type_instance, type_instance,
56                         sizeof (vl.type_instance));
57
58         plugin_dispatch_values (&vl);
59 } /* void cgroups_submit_one */
60
61 /*
62  * This callback reads the user/system CPU time for each cgroup.
63  */
64 static int read_cpuacct_procs (const char *dirname, char const *cgroup_name,
65     void *user_data)
66 {
67         char abs_path[PATH_MAX];
68         struct stat statbuf;
69         char buf[1024];
70         int status;
71
72         FILE *fh;
73
74         if (ignorelist_match (il_cgroup, cgroup_name))
75                 return (0);
76
77         ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, cgroup_name);
78
79         status = lstat (abs_path, &statbuf);
80         if (status != 0)
81         {
82                 ERROR ("cgroups plugin: stat (\"%s\") failed.",
83                                 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         fh = fopen (abs_path, "r");
94         if (fh == NULL)
95         {
96                 char errbuf[1024];
97                 ERROR ("cgroups plugin: fopen (\"%s\") failed: %s",
98                                 abs_path,
99                                 sstrerror (errno, errbuf, sizeof (errbuf)));
100                 return (-1);
101         }
102
103         while (fgets (buf, sizeof (buf), fh) != NULL)
104         {
105                 char *fields[8];
106                 int numfields = 0;
107                 char *key;
108                 size_t key_len;
109                 value_t value;
110
111                 /* Expected format:
112                  *
113                  *   user: 12345
114                  *   system: 23456
115                  *
116                  * Or:
117                  *
118                  *   user 12345
119                  *   system 23456
120                  */
121                 strstripnewline (buf);
122                 numfields = strsplit (buf, fields, STATIC_ARRAY_SIZE (fields));
123                 if (numfields != 2)
124                         continue;
125
126                 key = fields[0];
127                 key_len = strlen (key);
128                 if (key_len < 2)
129                         continue;
130
131                 /* Strip colon off the first column, if found */
132                 if (key[key_len - 1] == ':')
133                         key[key_len - 1] = 0;
134
135                 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
136                 if (status != 0)
137                         continue;
138
139                 cgroups_submit_one (cgroup_name, key, value);
140         }
141
142         fclose (fh);
143         return (0);
144 } /* int read_cpuacct_procs */
145
146 /*
147  * Gets called for every file/folder in /sys/fs/cgroup/cpu,cpuacct (or
148  * wherever cpuacct is mounted on the system). Calls walk_directory with the
149  * read_cpuacct_procs callback on every folder it finds, such as "system".
150  */
151 static int read_cpuacct_root (const char *dirname, const char *filename,
152                 void *user_data)
153 {
154         char abs_path[PATH_MAX];
155         struct stat statbuf;
156         int status;
157
158         ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
159
160         status = lstat (abs_path, &statbuf);
161         if (status != 0)
162         {
163                 ERROR ("cgroups plugin: stat (%s) failed.", abs_path);
164                 return (-1);
165         }
166
167         if (S_ISDIR (statbuf.st_mode))
168         {
169                 status = walk_directory (abs_path, read_cpuacct_procs,
170                                 /* user_data = */ NULL,
171                                 /* include_hidden = */ 0);
172                 return (status);
173         }
174
175         return (0);
176 }
177
178 static int cgroups_init (void)
179 {
180         if (il_cgroup == NULL)
181                 il_cgroup = ignorelist_create (1);
182
183         return (0);
184 }
185
186 static int cgroups_config (const char *key, const char *value)
187 {
188         cgroups_init ();
189
190         if (strcasecmp (key, "CGroup") == 0)
191         {
192                 if (ignorelist_add (il_cgroup, value))
193                         return (1);
194                 return (0);
195         }
196         else if (strcasecmp (key, "IgnoreSelected") == 0)
197         {
198                 if (IS_TRUE (value))
199                         ignorelist_set_invert (il_cgroup, 0);
200                 else
201                         ignorelist_set_invert (il_cgroup, 1);
202                 return (0);
203         }
204
205         return (-1);
206 }
207
208 static int cgroups_read (void)
209 {
210         cu_mount_t *mnt_list = NULL;
211         _Bool cgroup_found = 0;
212
213         if (cu_mount_getlist (&mnt_list) == NULL)
214         {
215                 ERROR ("cgroups plugin: cu_mount_getlist failed.");
216                 return (-1);
217         }
218
219         for (cu_mount_t *mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
220         {
221                 /* Find the cgroup mountpoint which contains the cpuacct
222                  * controller. */
223                 if ((strcmp(mnt_ptr->type, "cgroup") != 0)
224                                 || !cu_mount_checkoption(mnt_ptr->options,
225                                         "cpuacct", /* full = */ 1))
226                         continue;
227
228                 walk_directory (mnt_ptr->dir, read_cpuacct_root,
229                                 /* user_data = */ NULL,
230                                 /* include_hidden = */ 0);
231                 cgroup_found = 1;
232                 /* It doesn't make sense to check other cpuacct mount-points
233                  * (if any), they contain the same data. */
234                 break;
235         }
236
237         cu_mount_freelist (mnt_list);
238
239         if (!cgroup_found)
240         {
241                 WARNING ("cgroups plugin: Unable to find cgroup "
242                                 "mount-point with the \"cpuacct\" option.");
243                 return (-1);
244         }
245
246         return (0);
247 } /* int cgroup_read */
248
249 void module_register (void)
250 {
251         plugin_register_config ("cgroups", cgroups_config,
252                         config_keys, config_keys_num);
253         plugin_register_init ("cgroups", cgroups_init);
254         plugin_register_read ("cgroups", cgroups_read);
255 } /* void module_register */