1 /**
2 * collectd - src/cgroups_cpuacct.c
3 * Copyright (C) 2011 Michael Stapelberg
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Michael Stapelberg <michael at stapelberg.de>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25 #include "configfile.h"
26 #include "utils_mount.h"
27 #include "utils_ignorelist.h"
29 static char const *config_keys[] =
30 {
31 "CGroup",
32 "IgnoreSelected"
33 };
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)
42 {
43 value_list_t vl = VALUE_LIST_INIT;
45 vl.values = &value;
46 vl.values_len = 1;
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 */
58 /*
59 * This callback reads the user/system CPU time for each cgroup.
60 */
61 static int read_cpuacct_procs (const char *dirname, char const *cgroup_name,
62 void *user_data)
63 {
64 char abs_path[PATH_MAX];
65 struct stat statbuf;
66 char buf[1024];
67 int status;
69 FILE *fh;
71 if (ignorelist_match (il_cgroup, cgroup_name))
72 return (0);
74 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, cgroup_name);
76 status = lstat (abs_path, &statbuf);
77 if (status != 0)
78 {
79 ERROR ("cgroups_cpuacct plugin: stat (\"%s\") failed.",
80 abs_path);
81 return (-1);
82 }
84 /* We are only interested in directories, so skip everything else. */
85 if (!S_ISDIR (statbuf.st_mode))
86 return (0);
88 ssnprintf (abs_path, sizeof (abs_path), "%s/%s/cpuacct.stat",
89 dirname, cgroup_name);
90 fh = fopen (abs_path, "r");
91 if (fh == NULL)
92 {
93 char errbuf[1024];
94 ERROR ("cgroups_cpuacct pluign: fopen (\"%s\") failed: %s",
95 abs_path,
96 sstrerror (errno, errbuf, sizeof (errbuf)));
97 return (-1);
98 }
100 while (fgets (buf, sizeof (buf), fh) != NULL)
101 {
102 char *fields[8];
103 int numfields = 0;
104 char *key;
105 size_t key_len;
106 value_t value;
108 /* Expected format:
109 *
110 * user: 12345
111 * system: 23456
112 */
113 strstripnewline (buf);
114 numfields = strsplit (buf, fields, STATIC_ARRAY_SIZE (fields));
115 if (numfields != 2)
116 continue;
118 key = fields[0];
119 key_len = strlen (key);
120 if (key_len < 2)
121 continue;
123 /* Strip colon off the first column */
124 if (key[key_len - 1] != ':')
125 continue;
126 key[key_len - 1] = 0;
128 status = parse_value (fields[1], &value, DS_TYPE_DERIVE);
129 if (status != 0)
130 continue;
132 cgroups_submit_one (cgroup_name, key, value);
133 }
135 fclose (fh);
136 return (0);
137 } /* int read_cpuacct_procs */
139 /*
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".
143 */
144 static int read_cpuacct_root (const char *dirname, const char *filename,
145 void *user_data)
146 {
147 char abs_path[PATH_MAX];
148 struct stat statbuf;
149 int status;
151 ssnprintf (abs_path, sizeof (abs_path), "%s/%s", dirname, filename);
153 status = lstat (abs_path, &statbuf);
154 if (status != 0)
155 {
156 ERROR ("cgroups_cpuacct plugin: stat (%s) failed.", abs_path);
157 return (-1);
158 }
160 if (S_ISDIR (statbuf.st_mode))
161 {
162 status = walk_directory (abs_path, read_cpuacct_procs,
163 /* user_data = */ NULL,
164 /* include_hidden = */ 0);
165 return (status);
166 }
168 return (0);
169 }
171 static int cgroups_init (void)
172 {
173 if (il_cgroup == NULL)
174 il_cgroup = ignorelist_create (1);
176 return (0);
177 }
179 static int cgroups_config (const char *key, const char *value)
180 {
181 cgroups_init ();
183 if (strcasecmp (key, "CGroup") == 0)
184 {
185 if (ignorelist_add (il_cgroup, value))
186 return (1);
187 return (0);
188 }
189 else if (strcasecmp (key, "IgnoreSelected") == 0)
190 {
191 if (IS_TRUE (value))
192 ignorelist_set_invert (il_cgroup, 0);
193 else
194 ignorelist_set_invert (il_cgroup, 1);
195 return (0);
196 }
198 return (-1);
199 }
201 static int cgroups_read (void)
202 {
203 cu_mount_t *mnt_list;
204 cu_mount_t *mnt_ptr;
205 _Bool cgroup_found = 0;
207 mnt_list = NULL;
208 if (cu_mount_getlist (&mnt_list) == NULL)
209 {
210 ERROR ("cgroups_cpuacct plugin: cu_mount_getlist failed.");
211 return (-1);
212 }
214 for (mnt_ptr = mnt_list; mnt_ptr != NULL; mnt_ptr = mnt_ptr->next)
215 {
216 /* Find the cgroup mountpoint which contains the cpuacct
217 * controller. */
218 if (strcmp(mnt_ptr->type, "cgroup") != 0 ||
219 !cu_mount_getoptionvalue(mnt_ptr->options, "cpuacct"))
220 continue;
222 walk_directory (mnt_ptr->dir, read_cpuacct_root,
223 /* user_data = */ NULL,
224 /* include_hidden = */ 0);
225 cgroup_found = 1;
226 /* It doesn't make sense to check other cpuacct mount-points
227 * (if any), they contain the same data. */
228 break;
229 }
231 cu_mount_freelist (mnt_list);
233 if (!cgroup_found)
234 {
235 WARNING ("cgroups_cpuacct plugin: Unable to find cgroup "
236 "mount-point with the \"cpuacct\" option.");
237 return (-1);
238 }
240 return (0);
241 } /* int cgroup_read */
243 void module_register (void)
244 {
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 */