2 * collectd - src/vmem.c
3 * Copyright (C) 2008-2010 Florian octo Forster
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 * Florian octo Forster <octo at collectd.org>
27 static const char *config_keys[] =
31 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
33 static int verbose_output = 0;
34 /* #endif KERNEL_LINUX */
37 # error "No applicable input method."
38 #endif /* HAVE_LIBSTATGRAB */
40 static void submit (const char *plugin_instance, const char *type,
41 const char *type_instance, value_t *values, int values_len)
43 value_list_t vl = VALUE_LIST_INIT;
46 vl.values_len = values_len;
48 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
49 sstrncpy (vl.plugin, "vmem", sizeof (vl.plugin));
50 if (plugin_instance != NULL)
51 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
52 sstrncpy (vl.type, type, sizeof (vl.type));
53 if (type_instance != NULL)
54 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
56 plugin_dispatch_values (&vl);
57 } /* void vmem_submit */
59 static void submit_two (const char *plugin_instance, const char *type,
60 const char *type_instance, derive_t c0, derive_t c1)
64 values[0].derive = c0;
65 values[1].derive = c1;
67 submit (plugin_instance, type, type_instance, values, 2);
68 } /* void submit_one */
70 static void submit_one (const char *plugin_instance, const char *type,
71 const char *type_instance, value_t value)
73 submit (plugin_instance, type, type_instance, &value, 1);
74 } /* void submit_one */
76 static int vmem_config (const char *key, const char *value)
78 if (strcasecmp ("Verbose", key) == 0)
91 } /* int vmem_config */
93 static int vmem_read (void)
101 derive_t pswpout = 0;
104 derive_t pgfault = 0;
105 derive_t pgmajfault = 0;
106 int pgfaultvalid = 0;
111 fh = fopen ("/proc/vmstat", "r");
115 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
116 sstrerror (errno, errbuf, sizeof (errbuf)));
120 while (fgets (buffer, sizeof (buffer), fh) != NULL)
129 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
136 counter = strtoll (fields[1], &endptr, 10);
137 if (fields[1] == endptr)
141 gauge = strtod (fields[1], &endptr);
142 if (fields[1] == endptr)
148 * The total number of {inst} pages, e. g dirty pages.
150 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
152 char *inst = key + strlen ("nr_");
153 if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0)
155 value_t value = { .derive = counter };
156 submit_one (NULL, "vmpage_action", inst, value);
160 value_t value = { .gauge = gauge };
161 submit_one (NULL, "vmpage_number", inst, value);
166 * Page in and page outs. For memory and swap.
168 else if (strcmp ("pgpgin", key) == 0)
173 else if (strcmp ("pgpgout", key) == 0)
178 else if (strcmp ("pswpin", key) == 0)
183 else if (strcmp ("pswpout", key) == 0)
192 else if (strcmp ("pgfault", key) == 0)
195 pgfaultvalid |= 0x01;
197 else if (strcmp ("pgmajfault", key) == 0)
199 pgmajfault = counter;
200 pgfaultvalid |= 0x02;
204 * Skip the other statistics if verbose output is disabled.
206 else if (verbose_output == 0)
210 * Number of page allocations, refills, steals and scans. This is collected
211 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
213 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
215 char *inst = key + strlen ("pgalloc_");
216 value_t value = { .derive = counter };
217 submit_one (inst, "vmpage_action", "alloc", value);
219 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
221 char *inst = key + strlen ("pgrefill_");
222 value_t value = { .derive = counter };
223 submit_one (inst, "vmpage_action", "refill", value);
225 else if (strncmp ("pgsteal_kswapd_", key, strlen ("pgsteal_kswapd_")) == 0)
227 char *inst = key + strlen ("pgsteal_kswapd_");
228 value_t value = { .derive = counter };
229 submit_one (inst, "vmpage_action", "steal_kswapd", value);
231 else if (strncmp ("pgsteal_direct_", key, strlen ("pgsteal_direct_")) == 0)
233 char *inst = key + strlen ("pgsteal_direct_");
234 value_t value = { .derive = counter };
235 submit_one (inst, "vmpage_action", "steal_direct", value);
237 /* For backwards compatibility (somewhen before 4.2.3) */
238 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
240 char *inst = key + strlen ("pgsteal_");
241 value_t value = { .derive = counter };
242 submit_one (inst, "vmpage_action", "steal", value);
244 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
246 char *inst = key + strlen ("pgscan_kswapd_");
247 value_t value = { .derive = counter };
248 submit_one (inst, "vmpage_action", "scan_kswapd", value);
250 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
252 char *inst = key + strlen ("pgscan_direct_");
253 value_t value = { .derive = counter };
254 submit_one (inst, "vmpage_action", "scan_direct", value);
260 * number of pages moved to the active or inactive lists and freed, i. e.
261 * removed from either list.
263 else if (strcmp ("pgfree", key) == 0)
265 value_t value = { .derive = counter };
266 submit_one (NULL, "vmpage_action", "free", value);
268 else if (strcmp ("pgactivate", key) == 0)
270 value_t value = { .derive = counter };
271 submit_one (NULL, "vmpage_action", "activate", value);
273 else if (strcmp ("pgdeactivate", key) == 0)
275 value_t value = { .derive = counter };
276 submit_one (NULL, "vmpage_action", "deactivate", value);
278 } /* while (fgets) */
283 if (pgfaultvalid == 0x03)
284 submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
286 if (pgpgvalid == 0x03)
287 submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
289 if (pswpvalid == 0x03)
290 submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
291 #endif /* KERNEL_LINUX */
294 } /* int vmem_read */
296 void module_register (void)
298 plugin_register_config ("vmem", vmem_config,
299 config_keys, config_keys_num);
300 plugin_register_read ("vmem", vmem_read);
301 } /* void module_register */
303 /* vim: set sw=2 sts=2 ts=8 : */