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