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