1 /**
2 * collectd - src/vmem.c
3 * Copyright (C) 2008-2010 Florian octo Forster
4 *
5 * Permission is hereby granted, free of charge, to any person obtaining a
6 * copy of this software and associated documentation files (the "Software"),
7 * to deal in the Software without restriction, including without limitation
8 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9 * and/or sell copies of the Software, and to permit persons to whom the
10 * Software is furnished to do so, subject to the following conditions:
11 *
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
14 *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21 * DEALINGS IN THE SOFTWARE.
22 *
23 * Authors:
24 * Florian octo Forster <octo at collectd.org>
25 **/
27 #include "collectd.h"
29 #include "common.h"
30 #include "plugin.h"
32 #if KERNEL_LINUX
33 static const char *config_keys[] =
34 {
35 "Verbose"
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 static int verbose_output = 0;
40 /* #endif KERNEL_LINUX */
42 #else
43 # error "No applicable input method."
44 #endif /* HAVE_LIBSTATGRAB */
46 static void submit (const char *plugin_instance, const char *type,
47 const char *type_instance, value_t *values, int values_len)
48 {
49 value_list_t vl = VALUE_LIST_INIT;
51 vl.values = values;
52 vl.values_len = values_len;
54 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
55 sstrncpy (vl.plugin, "vmem", sizeof (vl.plugin));
56 if (plugin_instance != NULL)
57 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
58 sstrncpy (vl.type, type, sizeof (vl.type));
59 if (type_instance != NULL)
60 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
62 plugin_dispatch_values (&vl);
63 } /* void vmem_submit */
65 static void submit_two (const char *plugin_instance, const char *type,
66 const char *type_instance, derive_t c0, derive_t c1)
67 {
68 value_t values[2];
70 values[0].derive = c0;
71 values[1].derive = c1;
73 submit (plugin_instance, type, type_instance, values, 2);
74 } /* void submit_one */
76 static void submit_one (const char *plugin_instance, const char *type,
77 const char *type_instance, value_t value)
78 {
79 submit (plugin_instance, type, type_instance, &value, 1);
80 } /* void submit_one */
82 static int vmem_config (const char *key, const char *value)
83 {
84 if (strcasecmp ("Verbose", key) == 0)
85 {
86 if (IS_TRUE (value))
87 verbose_output = 1;
88 else
89 verbose_output = 0;
90 }
91 else
92 {
93 return (-1);
94 }
96 return (0);
97 } /* int vmem_config */
99 static int vmem_read (void)
100 {
101 #if KERNEL_LINUX
102 derive_t pgpgin = 0;
103 derive_t pgpgout = 0;
104 int pgpgvalid = 0;
106 derive_t pswpin = 0;
107 derive_t pswpout = 0;
108 int pswpvalid = 0;
110 derive_t pgfault = 0;
111 derive_t pgmajfault = 0;
112 int pgfaultvalid = 0;
114 FILE *fh;
115 char buffer[1024];
117 fh = fopen ("/proc/vmstat", "r");
118 if (fh == NULL)
119 {
120 char errbuf[1024];
121 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
122 sstrerror (errno, errbuf, sizeof (errbuf)));
123 return (-1);
124 }
126 while (fgets (buffer, sizeof (buffer), fh) != NULL)
127 {
128 char *fields[4];
129 int fields_num;
130 char *key;
131 char *endptr;
132 derive_t counter;
133 gauge_t gauge;
135 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
136 if (fields_num != 2)
137 continue;
139 key = fields[0];
141 endptr = NULL;
142 counter = strtoll (fields[1], &endptr, 10);
143 if (fields[1] == endptr)
144 continue;
146 endptr = NULL;
147 gauge = strtod (fields[1], &endptr);
148 if (fields[1] == endptr)
149 continue;
151 /*
152 * Number of pages
153 *
154 * The total number of {inst} pages, e. g dirty pages.
155 */
156 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
157 {
158 char *inst = key + strlen ("nr_");
159 if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0)
160 {
161 value_t value = { .derive = counter };
162 submit_one (NULL, "vmpage_action", inst, value);
163 }
164 else
165 {
166 value_t value = { .gauge = gauge };
167 submit_one (NULL, "vmpage_number", inst, value);
168 }
169 }
171 /*
172 * Page in and page outs. For memory and swap.
173 */
174 else if (strcmp ("pgpgin", key) == 0)
175 {
176 pgpgin = counter;
177 pgpgvalid |= 0x01;
178 }
179 else if (strcmp ("pgpgout", key) == 0)
180 {
181 pgpgout = counter;
182 pgpgvalid |= 0x02;
183 }
184 else if (strcmp ("pswpin", key) == 0)
185 {
186 pswpin = counter;
187 pswpvalid |= 0x01;
188 }
189 else if (strcmp ("pswpout", key) == 0)
190 {
191 pswpout = counter;
192 pswpvalid |= 0x02;
193 }
195 /*
196 * Pagefaults
197 */
198 else if (strcmp ("pgfault", key) == 0)
199 {
200 pgfault = counter;
201 pgfaultvalid |= 0x01;
202 }
203 else if (strcmp ("pgmajfault", key) == 0)
204 {
205 pgmajfault = counter;
206 pgfaultvalid |= 0x02;
207 }
209 /*
210 * Skip the other statistics if verbose output is disabled.
211 */
212 else if (verbose_output == 0)
213 continue;
215 /*
216 * Number of page allocations, refills, steals and scans. This is collected
217 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
218 */
219 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
220 {
221 char *inst = key + strlen ("pgalloc_");
222 value_t value = { .derive = counter };
223 submit_one (inst, "vmpage_action", "alloc", value);
224 }
225 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
226 {
227 char *inst = key + strlen ("pgrefill_");
228 value_t value = { .derive = counter };
229 submit_one (inst, "vmpage_action", "refill", value);
230 }
231 else if (strncmp ("pgsteal_kswapd_", key, strlen ("pgsteal_kswapd_")) == 0)
232 {
233 char *inst = key + strlen ("pgsteal_kswapd_");
234 value_t value = { .derive = counter };
235 submit_one (inst, "vmpage_action", "steal_kswapd", value);
236 }
237 else if (strncmp ("pgsteal_direct_", key, strlen ("pgsteal_direct_")) == 0)
238 {
239 char *inst = key + strlen ("pgsteal_direct_");
240 value_t value = { .derive = counter };
241 submit_one (inst, "vmpage_action", "steal_direct", value);
242 }
243 /* For backwards compatibility (somewhen before 4.2.3) */
244 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
245 {
246 char *inst = key + strlen ("pgsteal_");
247 value_t value = { .derive = counter };
248 submit_one (inst, "vmpage_action", "steal", value);
249 }
250 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
251 {
252 char *inst = key + strlen ("pgscan_kswapd_");
253 value_t value = { .derive = counter };
254 submit_one (inst, "vmpage_action", "scan_kswapd", value);
255 }
256 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
257 {
258 char *inst = key + strlen ("pgscan_direct_");
259 value_t value = { .derive = counter };
260 submit_one (inst, "vmpage_action", "scan_direct", value);
261 }
263 /*
264 * Page action
265 *
266 * number of pages moved to the active or inactive lists and freed, i. e.
267 * removed from either list.
268 */
269 else if (strcmp ("pgfree", key) == 0)
270 {
271 value_t value = { .derive = counter };
272 submit_one (NULL, "vmpage_action", "free", value);
273 }
274 else if (strcmp ("pgactivate", key) == 0)
275 {
276 value_t value = { .derive = counter };
277 submit_one (NULL, "vmpage_action", "activate", value);
278 }
279 else if (strcmp ("pgdeactivate", key) == 0)
280 {
281 value_t value = { .derive = counter };
282 submit_one (NULL, "vmpage_action", "deactivate", value);
283 }
284 } /* while (fgets) */
286 fclose (fh);
287 fh = NULL;
289 if (pgfaultvalid == 0x03)
290 submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
292 if (pgpgvalid == 0x03)
293 submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
295 if (pswpvalid == 0x03)
296 submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
297 #endif /* KERNEL_LINUX */
299 return (0);
300 } /* int vmem_read */
302 void module_register (void)
303 {
304 plugin_register_config ("vmem", vmem_config,
305 config_keys, config_keys_num);
306 plugin_register_read ("vmem", vmem_read);
307 } /* void module_register */
309 /* vim: set sw=2 sts=2 ts=8 : */