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 sstrncpy (vl.type, type, sizeof (vl.type));
54 if (type_instance != NULL)
55 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
57 plugin_dispatch_values (&vl);
58 } /* void vmem_submit */
60 static void submit_two (const char *plugin_instance, const char *type,
61 const char *type_instance, counter_t c0, counter_t c1)
65 values[0].counter = c0;
66 values[1].counter = c1;
68 submit (plugin_instance, type, type_instance, values, 2);
69 } /* void submit_one */
71 static void submit_one (const char *plugin_instance, const char *type,
72 const char *type_instance, value_t value)
74 submit (plugin_instance, type, type_instance, &value, 1);
75 } /* void submit_one */
77 static int vmem_config (const char *key, const char *value)
79 if (strcasecmp ("Verbose", key) == 0)
81 if ((strcasecmp ("true", value) == 0)
82 || (strcasecmp ("yes", value) == 0)
83 || (strcasecmp ("on", value) == 0))
94 } /* int vmem_config */
96 static int vmem_read (void)
100 counter_t pgpgout = 0;
103 counter_t pswpin = 0;
104 counter_t pswpout = 0;
107 counter_t pgfault = 0;
108 counter_t pgmajfault = 0;
109 int pgfaultvalid = 0;
114 fh = fopen ("/proc/vmstat", "r");
118 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
119 sstrerror (errno, errbuf, sizeof (errbuf)));
123 while (fgets (buffer, sizeof (buffer), fh) != NULL)
132 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
139 counter = strtoll (fields[1], &endptr, 10);
140 if (fields[1] == endptr)
144 gauge = strtod (fields[1], &endptr);
145 if (fields[1] == endptr)
151 * The total number of {inst} pages, e. g dirty pages.
153 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
155 char *inst = key + strlen ("nr_");
156 value_t value = { .gauge = gauge };
157 submit_one (NULL, "vmpage_number", inst, value);
161 * Page in and page outs. For memory and swap.
163 else if (strcmp ("pgpgin", key) == 0)
168 else if (strcmp ("pgpgout", key) == 0)
173 else if (strcmp ("pswpin", key) == 0)
178 else if (strcmp ("pswpout", key) == 0)
187 else if (strcmp ("pgfault", key) == 0)
190 pgfaultvalid |= 0x01;
192 else if (strcmp ("pgmajfault", key) == 0)
194 pgmajfault = counter;
195 pgfaultvalid |= 0x02;
199 * Skip the other statistics if verbose output is disabled.
201 else if (verbose_output == 0)
205 * Number of page allocations, refills, steals and scans. This is collected
206 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
208 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
210 char *inst = key + strlen ("pgalloc_");
211 value_t value = { .counter = counter };
212 submit_one (inst, "vmpage_action", "alloc", value);
214 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
216 char *inst = key + strlen ("pgrefill_");
217 value_t value = { .counter = counter };
218 submit_one (inst, "vmpage_action", "refill", value);
220 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
222 char *inst = key + strlen ("pgsteal_");
223 value_t value = { .counter = counter };
224 submit_one (inst, "vmpage_action", "steal", value);
226 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
228 char *inst = key + strlen ("pgscan_kswapd_");
229 value_t value = { .counter = counter };
230 submit_one (inst, "vmpage_action", "scan_kswapd", value);
232 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
234 char *inst = key + strlen ("pgscan_direct_");
235 value_t value = { .counter = counter };
236 submit_one (inst, "vmpage_action", "scan_direct", value);
242 * number of pages moved to the active or inactive lists and freed, i. e.
243 * removed from either list.
245 else if (strcmp ("pgfree", key) == 0)
247 value_t value = { .counter = counter };
248 submit_one (NULL, "vmpage_action", "free", value);
250 else if (strcmp ("pgactivate", key) == 0)
252 value_t value = { .counter = counter };
253 submit_one (NULL, "vmpage_action", "activate", value);
255 else if (strcmp ("pgdeactivate", key) == 0)
257 value_t value = { .counter = counter };
258 submit_one (NULL, "vmpage_action", "deactivate", value);
260 } /* while (fgets) */
265 if (pgfaultvalid == 0x03)
266 submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
268 if (pgpgvalid == 0x03)
269 submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
271 if (pswpvalid == 0x03)
272 submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
273 #endif /* KERNEL_LINUX */
276 } /* int vmem_read */
278 void module_register (void)
280 plugin_register_config ("vmem", vmem_config,
281 config_keys, config_keys_num);
282 plugin_register_read ("vmem", vmem_read);
283 } /* void module_register */
285 /* vim: set sw=2 sts=2 ts=8 : */