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