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 "plugin.h"
27 #include "utils/common/common.h"
28 #include "utils/ignorelist/ignorelist.h"
29 #include "utils/mount/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;
35
36 __attribute__((nonnull(1))) __attribute__((nonnull(2))) static void
cgroups_submit_one(char const * plugin_instance,char const * type_instance,value_t value)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 */
read_cpuacct_procs(const char * dirname,char const * cgroup_name,void * user_data)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 ERROR("cgroups plugin: fopen (\"%s\") failed: %s", abs_path, STRERRNO);
83 return -1;
84 }
85
86 while (fgets(buf, sizeof(buf), fh) != NULL) {
87 char *fields[8];
88 int numfields = 0;
89 char *key;
90 size_t key_len;
91 value_t value;
92
93 /* Expected format:
94 *
95 * user: 12345
96 * system: 23456
97 *
98 * Or:
99 *
100 * user 12345
101 * system 23456
102 */
103 strstripnewline(buf);
104 numfields = strsplit(buf, fields, STATIC_ARRAY_SIZE(fields));
105 if (numfields != 2)
106 continue;
107
108 key = fields[0];
109 key_len = strlen(key);
110 if (key_len < 2)
111 continue;
112
113 /* Strip colon off the first column, if found */
114 if (key[key_len - 1] == ':')
115 key[key_len - 1] = '\0';
116
117 status = parse_value(fields[1], &value, DS_TYPE_DERIVE);
118 if (status != 0)
119 continue;
120
121 cgroups_submit_one(cgroup_name, key, value);
122 }
123
124 fclose(fh);
125 return 0;
126 } /* int read_cpuacct_procs */
127
128 /*
129 * Gets called for every file/folder in /sys/fs/cgroup/cpu,cpuacct (or
130 * wherever cpuacct is mounted on the system). Calls walk_directory with the
131 * read_cpuacct_procs callback on every folder it finds, such as "system".
132 */
read_cpuacct_root(const char * dirname,const char * filename,void * user_data)133 static int read_cpuacct_root(const char *dirname, const char *filename,
134 void *user_data) {
135 char abs_path[PATH_MAX];
136 struct stat statbuf;
137 int status;
138
139 snprintf(abs_path, sizeof(abs_path), "%s/%s", dirname, filename);
140
141 status = lstat(abs_path, &statbuf);
142 if (status != 0) {
143 ERROR("cgroups plugin: stat (%s) failed.", abs_path);
144 return -1;
145 }
146
147 if (S_ISDIR(statbuf.st_mode)) {
148 status = walk_directory(abs_path, read_cpuacct_procs,
149 /* user_data = */ NULL,
150 /* include_hidden = */ 0);
151 return status;
152 }
153
154 return 0;
155 }
156
cgroups_init(void)157 static int cgroups_init(void) {
158 if (il_cgroup == NULL)
159 il_cgroup = ignorelist_create(1);
160
161 return 0;
162 }
163
cgroups_config(const char * key,const char * value)164 static int cgroups_config(const char *key, const char *value) {
165 cgroups_init();
166
167 if (strcasecmp(key, "CGroup") == 0) {
168 if (ignorelist_add(il_cgroup, value))
169 return 1;
170 return 0;
171 } else if (strcasecmp(key, "IgnoreSelected") == 0) {
172 if (IS_TRUE(value))
173 ignorelist_set_invert(il_cgroup, 0);
174 else
175 ignorelist_set_invert(il_cgroup, 1);
176 return 0;
177 }
178
179 return -1;
180 }
181
cgroups_read(void)182 static int cgroups_read(void) {
183 cu_mount_t *mnt_list = NULL;
184 bool cgroup_found = false;
185
186 if (cu_mount_getlist(&mnt_list) == NULL) {
187 ERROR("cgroups plugin: cu_mount_getlist failed.");
188 return -1;
189 }
190
191 for (cu_mount_t *mnt_ptr = mnt_list; mnt_ptr != NULL;
192 mnt_ptr = mnt_ptr->next) {
193 /* Find the cgroup mountpoint which contains the cpuacct
194 * controller. */
195 if ((strcmp(mnt_ptr->type, "cgroup") != 0) ||
196 !cu_mount_checkoption(mnt_ptr->options, "cpuacct", /* full = */ 1))
197 continue;
198
199 walk_directory(mnt_ptr->dir, read_cpuacct_root,
200 /* user_data = */ NULL,
201 /* include_hidden = */ 0);
202 cgroup_found = true;
203 /* It doesn't make sense to check other cpuacct mount-points
204 * (if any), they contain the same data. */
205 break;
206 }
207
208 cu_mount_freelist(mnt_list);
209
210 if (!cgroup_found) {
211 WARNING("cgroups plugin: Unable to find cgroup "
212 "mount-point with the \"cpuacct\" option.");
213 return -1;
214 }
215
216 return 0;
217 } /* int cgroup_read */
218
module_register(void)219 void module_register(void) {
220 plugin_register_config("cgroups", cgroups_config, config_keys,
221 config_keys_num);
222 plugin_register_init("cgroups", cgroups_init);
223 plugin_register_read("cgroups", cgroups_read);
224 } /* void module_register */
225