2 * collectd - src/vmem.c
3 * Copyright (C) 2008 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 verplant.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 vl.time = time (NULL);
49 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
50 sstrncpy (vl.plugin, "vmem", sizeof (vl.plugin));
51 if (plugin_instance != NULL)
52 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
53 if (type_instance != NULL)
54 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
56 plugin_dispatch_values (type, &vl);
57 } /* void vmem_submit */
59 static void submit_two (const char *plugin_instance, const char *type,
60 const char *type_instance, counter_t c0, counter_t c1)
64 values[0].counter = c0;
65 values[1].counter = 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)
80 if ((strcasecmp ("true", value) == 0)
81 || (strcasecmp ("yes", value) == 0)
82 || (strcasecmp ("on", value) == 0))
93 } /* int vmem_config */
95 static int vmem_read (void)
99 counter_t pgpgout = 0;
102 counter_t pswpin = 0;
103 counter_t pswpout = 0;
106 counter_t pgfault = 0;
107 counter_t pgmajfault = 0;
108 int pgfaultvalid = 0;
113 fh = fopen ("/proc/vmstat", "r");
117 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
118 sstrerror (errno, errbuf, sizeof (errbuf)));
122 while (fgets (buffer, sizeof (buffer), fh) != NULL)
131 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
138 counter = strtoll (fields[1], &endptr, 10);
139 if (fields[1] == endptr)
143 gauge = strtod (fields[1], &endptr);
144 if (fields[1] == endptr)
150 * The total number of {inst} pages, e. g dirty pages.
152 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
154 char *inst = key + strlen ("nr_");
155 value_t value = { .gauge = gauge };
156 submit_one (NULL, "vmpage_number", inst, value);
160 * Page in and page outs. For memory and swap.
162 else if (strcmp ("pgpgin", key) == 0)
167 else if (strcmp ("pgpgout", key) == 0)
172 else if (strcmp ("pswpin", key) == 0)
177 else if (strcmp ("pswpout", key) == 0)
186 else if (strcmp ("pgfault", key) == 0)
189 pgfaultvalid |= 0x01;
191 else if (strcmp ("pgmajfault", key) == 0)
193 pgmajfault = counter;
194 pgfaultvalid |= 0x02;
198 * Skip the other statistics if verbose output is disabled.
200 else if (verbose_output == 0)
204 * Number of page allocations, refills, steals and scans. This is collected
205 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
207 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
209 char *inst = key + strlen ("pgalloc_");
210 value_t value = { .counter = counter };
211 submit_one (inst, "vmpage_action", "alloc", value);
213 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
215 char *inst = key + strlen ("pgrefill_");
216 value_t value = { .counter = counter };
217 submit_one (inst, "vmpage_action", "refill", value);
219 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
221 char *inst = key + strlen ("pgsteal_");
222 value_t value = { .counter = counter };
223 submit_one (inst, "vmpage_action", "steal", value);
225 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
227 char *inst = key + strlen ("pgscan_kswapd_");
228 value_t value = { .counter = counter };
229 submit_one (inst, "vmpage_action", "scan_kswapd", value);
231 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
233 char *inst = key + strlen ("pgscan_direct_");
234 value_t value = { .counter = counter };
235 submit_one (inst, "vmpage_action", "scan_direct", value);
241 * number of pages moved to the active or inactive lists and freed, i. e.
242 * removed from either list.
244 else if (strcmp ("pgfree", key) == 0)
246 value_t value = { .counter = counter };
247 submit_one (NULL, "vmpage_action", "free", value);
249 else if (strcmp ("pgactivate", key) == 0)
251 value_t value = { .counter = counter };
252 submit_one (NULL, "vmpage_action", "activate", value);
254 else if (strcmp ("pgdeactivate", key) == 0)
256 value_t value = { .counter = counter };
257 submit_one (NULL, "vmpage_action", "deactivate", value);
259 } /* while (fgets) */
264 if (pgfaultvalid == 0x03)
265 submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
267 if (pgpgvalid == 0x03)
268 submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
270 if (pswpvalid == 0x03)
271 submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
272 #endif /* KERNEL_LINUX */
275 } /* int vmem_read */
277 void module_register (void)
279 plugin_register_config ("vmem", vmem_config,
280 config_keys, config_keys_num);
281 plugin_register_read ("vmem", vmem_read);
282 } /* void module_register */
284 /* vim: set sw=2 sts=2 ts=8 : */