2 * collectd - src/cgroups.c
3 * Copyright (C) 2011 Michael Stapelberg
4 * Copyright (C) 2013 Florian Forster
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.
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.
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
20 * Michael Stapelberg <michael at stapelberg.de>
21 * Florian Forster <octo at collectd.org>
27 #include "configfile.h"
28 #include "utils_mount.h"
29 #include "utils_ignorelist.h"
31 static char const *config_keys[] =
36 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
38 static ignorelist_t *il_cgroup = NULL;
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)
45 value_list_t vl = VALUE_LIST_INIT;
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));
57 plugin_dispatch_values (&vl);
58 } /* void cgroups_submit_one */
61 * This callback reads the user/system CPU time for each cgroup.
63 static int read_cpuacct_procs (const char *dirname, char const *cgroup_name,
66 char abs_path[PATH_MAX];
73 if (ignorelist_match (il_cgroup, cgroup_name))
76 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, cgroup_name);
78 status = lstat (abs_path, &statbuf);
81 ERROR ("cgroups plugin: stat (\"%s\") failed.",
86 /* We are only interested in directories, so skip everything else. */
87 if (!S_ISDIR (statbuf.st_mode))
90 ssnprintf (abs_path, sizeof (abs_path), "%s/%s/cpuacct.stat",
91 dirname, cgroup_name);
92 fh = fopen (abs_path, "r");
96 ERROR ("cgroups plugin: fopen (\"%s\") failed: %s",
98 sstrerror (errno, errbuf, sizeof (errbuf)));
102 while (fgets (buf, sizeof (buf), fh) != NULL)
120 strstripnewline (buf);
121 numfields = strsplit (buf, fields, STATIC_ARRAY_SIZE (fields));
126 key_len = strlen (key);
130 /* Strip colon off the first column, if found */
131 if (key[key_len - 1] == ':')
132 key[key_len - 1] = 0;
134 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
138 cgroups_submit_one (cgroup_name, key, value);
143 } /* int read_cpuacct_procs */
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".
150 static int read_cpuacct_root (const char *dirname, const char *filename,
153 char abs_path[PATH_MAX];
157 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
159 status = lstat (abs_path, &statbuf);
162 ERROR ("cgroups plugin: stat (%s) failed.", abs_path);
166 if (S_ISDIR (statbuf.st_mode))
168 status = walk_directory (abs_path, read_cpuacct_procs,
169 /* user_data = */ NULL,
170 /* include_hidden = */ 0);
177 static int cgroups_init (void)
179 if (il_cgroup == NULL)
180 il_cgroup = ignorelist_create (1);
185 static int cgroups_config (const char *key, const char *value)
189 if (strcasecmp (key, "CGroup") == 0)
191 if (ignorelist_add (il_cgroup, value))
195 else if (strcasecmp (key, "IgnoreSelected") == 0)
198 ignorelist_set_invert (il_cgroup, 0);
200 ignorelist_set_invert (il_cgroup, 1);
207 static int cgroups_read (void)
209 cu_mount_t *mnt_list;
211 _Bool cgroup_found = 0;
214 if (cu_mount_getlist (&mnt_list) == NULL)
216 ERROR ("cgroups plugin: cu_mount_getlist failed.");
220 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
222 /* Find the cgroup mountpoint which contains the cpuacct
224 if ((strcmp(mnt_ptr->type, "cgroup") != 0)
225 || !cu_mount_checkoption(mnt_ptr->options,
226 "cpuacct", /* full = */ 1))
229 walk_directory (mnt_ptr->dir, read_cpuacct_root,
230 /* user_data = */ NULL,
231 /* include_hidden = */ 0);
233 /* It doesn't make sense to check other cpuacct mount-points
234 * (if any), they contain the same data. */
238 cu_mount_freelist (mnt_list);
242 WARNING ("cgroups plugin: Unable to find cgroup "
243 "mount-point with the \"cpuacct\" option.");
248 } /* int cgroup_read */
250 void module_register (void)
252 plugin_register_config ("cgroups", cgroups_config,
253 config_keys, config_keys_num);
254 plugin_register_init ("cgroups", cgroups_init);
255 plugin_register_read ("cgroups", cgroups_read);
256 } /* void module_register */