2 * collectd - src/vmem.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
24 * Florian octo Forster <octo at collectd.org>
33 static const char *config_keys[] =
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
39 static int verbose_output = 0;
40 /* #endif KERNEL_LINUX */
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)
49 value_list_t vl = VALUE_LIST_INIT;
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)
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)
79 submit (plugin_instance, type, type_instance, &value, 1);
80 } /* void submit_one */
82 static int vmem_config (const char *key, const char *value)
84 if (strcasecmp ("Verbose", key) == 0)
97 } /* int vmem_config */
99 static int vmem_read (void)
103 derive_t pgpgout = 0;
107 derive_t pswpout = 0;
110 derive_t pgfault = 0;
111 derive_t pgmajfault = 0;
112 int pgfaultvalid = 0;
117 fh = fopen ("/proc/vmstat", "r");
121 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
122 sstrerror (errno, errbuf, sizeof (errbuf)));
126 while (fgets (buffer, sizeof (buffer), fh) != NULL)
135 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
142 counter = strtoll (fields[1], &endptr, 10);
143 if (fields[1] == endptr)
147 gauge = strtod (fields[1], &endptr);
148 if (fields[1] == endptr)
154 * The total number of {inst} pages, e. g dirty pages.
156 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
158 char *inst = key + strlen ("nr_");
159 if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0)
161 value_t value = { .derive = counter };
162 submit_one (NULL, "vmpage_action", inst, value);
166 value_t value = { .gauge = gauge };
167 submit_one (NULL, "vmpage_number", inst, value);
172 * Page in and page outs. For memory and swap.
174 else if (strcmp ("pgpgin", key) == 0)
179 else if (strcmp ("pgpgout", key) == 0)
184 else if (strcmp ("pswpin", key) == 0)
189 else if (strcmp ("pswpout", key) == 0)
198 else if (strcmp ("pgfault", key) == 0)
201 pgfaultvalid |= 0x01;
203 else if (strcmp ("pgmajfault", key) == 0)
205 pgmajfault = counter;
206 pgfaultvalid |= 0x02;
210 * Skip the other statistics if verbose output is disabled.
212 else if (verbose_output == 0)
216 * Number of page allocations, refills, steals and scans. This is collected
217 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
219 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
221 char *inst = key + strlen ("pgalloc_");
222 value_t value = { .derive = counter };
223 submit_one (inst, "vmpage_action", "alloc", value);
225 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
227 char *inst = key + strlen ("pgrefill_");
228 value_t value = { .derive = counter };
229 submit_one (inst, "vmpage_action", "refill", value);
231 else if (strncmp ("pgsteal_kswapd_", key, strlen ("pgsteal_kswapd_")) == 0)
233 char *inst = key + strlen ("pgsteal_kswapd_");
234 value_t value = { .derive = counter };
235 submit_one (inst, "vmpage_action", "steal_kswapd", value);
237 else if (strncmp ("pgsteal_direct_", key, strlen ("pgsteal_direct_")) == 0)
239 char *inst = key + strlen ("pgsteal_direct_");
240 value_t value = { .derive = counter };
241 submit_one (inst, "vmpage_action", "steal_direct", value);
243 /* For backwards compatibility (somewhen before 4.2.3) */
244 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
246 char *inst = key + strlen ("pgsteal_");
247 value_t value = { .derive = counter };
248 submit_one (inst, "vmpage_action", "steal", value);
250 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
252 char *inst = key + strlen ("pgscan_kswapd_");
253 value_t value = { .derive = counter };
254 submit_one (inst, "vmpage_action", "scan_kswapd", value);
256 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
258 char *inst = key + strlen ("pgscan_direct_");
259 value_t value = { .derive = counter };
260 submit_one (inst, "vmpage_action", "scan_direct", value);
266 * number of pages moved to the active or inactive lists and freed, i. e.
267 * removed from either list.
269 else if (strcmp ("pgfree", key) == 0)
271 value_t value = { .derive = counter };
272 submit_one (NULL, "vmpage_action", "free", value);
274 else if (strcmp ("pgactivate", key) == 0)
276 value_t value = { .derive = counter };
277 submit_one (NULL, "vmpage_action", "activate", value);
279 else if (strcmp ("pgdeactivate", key) == 0)
281 value_t value = { .derive = counter };
282 submit_one (NULL, "vmpage_action", "deactivate", value);
284 } /* while (fgets) */
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 */
300 } /* int vmem_read */
302 void module_register (void)
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 : */