2 * collectd - src/cgroups_cpuacct.c
3 * Copyright (C) 2011 Michael Stapelberg
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; only version 2 of the license is applicable.
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
14 * You should have received a copy of the GNU General Public License along
15 * with this program; if not, write to the Free Software Foundation, Inc.,
16 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 * Michael Stapelberg <michael at stapelberg.de>
25 #include "configfile.h"
26 #include "utils_mount.h"
27 #include "utils_ignorelist.h"
29 static char const *config_keys[] =
34 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
36 static ignorelist_t *il_cgroup = NULL;
38 __attribute__ ((nonnull(1)))
39 __attribute__ ((nonnull(2)))
40 static void cgroups_submit_one (char const *plugin_instance,
41 char const *type_instance, value_t value)
43 value_list_t vl = VALUE_LIST_INIT;
47 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
48 sstrncpy (vl.plugin, "cgroups_cpuacct", sizeof (vl.plugin));
49 sstrncpy (vl.plugin_instance, plugin_instance,
50 sizeof (vl.plugin_instance));
51 sstrncpy (vl.type, "cpu", sizeof (vl.type));
52 sstrncpy (vl.type_instance, type_instance,
53 sizeof (vl.type_instance));
55 plugin_dispatch_values (&vl);
56 } /* void cgroups_submit_one */
59 * This callback reads the user/system CPU time for each cgroup.
61 static int read_cpuacct_procs (const char *dirname, char const *cgroup_name,
64 char abs_path[PATH_MAX];
71 if (ignorelist_match (il_cgroup, cgroup_name))
74 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, cgroup_name);
76 status = lstat (abs_path, &statbuf);
79 ERROR ("cgroups_cpuacct plugin: stat (\"%s\") failed.",
84 /* We are only interested in directories, so skip everything else. */
85 if (!S_ISDIR (statbuf.st_mode))
88 ssnprintf (abs_path, sizeof (abs_path), "%s/%s/cpuacct.stat",
89 dirname, cgroup_name);
90 fh = fopen (abs_path, "r");
94 ERROR ("cgroups_cpuacct pluign: fopen (\"%s\") failed: %s",
96 sstrerror (errno, errbuf, sizeof (errbuf)));
100 while (fgets (buf, sizeof (buf), fh) != NULL)
113 strstripnewline (buf);
114 numfields = strsplit (buf, fields, STATIC_ARRAY_SIZE (fields));
119 key_len = strlen (key);
123 /* Strip colon off the first column */
124 if (key[key_len - 1] != ':')
126 key[key_len - 1] = 0;
128 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
132 cgroups_submit_one (cgroup_name, key, value);
137 } /* int read_cpuacct_procs */
140 * Gets called for every file/folder in /sys/fs/cgroup/cpu,cpuacct (or
141 * whereever cpuacct is mounted on the system). Calls walk_directory with the
142 * read_cpuacct_procs callback on every folder it finds, such as "system".
144 static int read_cpuacct_root (const char *dirname, const char *filename,
147 char abs_path[PATH_MAX];
151 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
153 status = lstat (abs_path, &statbuf);
156 ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
160 if (S_ISDIR (statbuf.st_mode))
162 status = walk_directory (abs_path, read_cpuacct_procs,
163 /* user_data = */ NULL,
164 /* include_hidden = */ 0);
171 static int cgroups_init (void)
173 if (il_cgroup == NULL)
174 il_cgroup = ignorelist_create (1);
179 static int cgroups_config (const char *key, const char *value)
183 if (strcasecmp (key, "CGroup") == 0)
185 if (ignorelist_add (il_cgroup, value))
189 else if (strcasecmp (key, "IgnoreSelected") == 0)
192 ignorelist_set_invert (il_cgroup, 0);
194 ignorelist_set_invert (il_cgroup, 1);
201 static int cgroups_read (void)
203 cu_mount_t *mnt_list;
205 _Bool cgroup_found = 0;
208 if (cu_mount_getlist (&mnt_list) == NULL)
210 ERROR ("cgroups_cpuacct plugin: cu_mount_getlist failed.");
214 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
216 /* Find the cgroup mountpoint which contains the cpuacct
218 if (strcmp(mnt_ptr->type, "cgroup") != 0 ||
219 !cu_mount_getoptionvalue(mnt_ptr->options, "cpuacct"))
222 walk_directory (mnt_ptr->dir, read_cpuacct_root,
223 /* user_data = */ NULL,
224 /* include_hidden = */ 0);
226 /* It doesn't make sense to check other cpuacct mount-points
227 * (if any), they contain the same data. */
231 cu_mount_freelist (mnt_list);
235 WARNING ("cgroups_cpuacct plugin: Unable to find cgroup "
236 "mount-point with the \"cpuacct\" option.");
241 } /* int cgroup_read */
243 void module_register (void)
245 plugin_register_config ("cgroups_cpuacct", cgroups_config,
246 config_keys, config_keys_num);
247 plugin_register_init ("cgroups_cpuacct", cgroups_init);
248 plugin_register_read ("cgroups_cpuacct", cgroups_read);
249 } /* void module_register */