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>
32 static const char *config_keys[] =
36 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
38 static int verbose_output = 0;
39 /* #endif KERNEL_LINUX */
42 # error "No applicable input method."
43 #endif /* HAVE_LIBSTATGRAB */
45 static void submit (const char *plugin_instance, const char *type,
46 const char *type_instance, value_t *values, int values_len)
48 value_list_t vl = VALUE_LIST_INIT;
51 vl.values_len = values_len;
53 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
54 sstrncpy (vl.plugin, "vmem", sizeof (vl.plugin));
55 if (plugin_instance != NULL)
56 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
57 sstrncpy (vl.type, type, sizeof (vl.type));
58 if (type_instance != NULL)
59 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
61 plugin_dispatch_values (&vl);
62 } /* void vmem_submit */
64 static void submit_two (const char *plugin_instance, const char *type,
65 const char *type_instance, derive_t c0, derive_t c1)
69 values[0].derive = c0;
70 values[1].derive = c1;
72 submit (plugin_instance, type, type_instance, values, 2);
73 } /* void submit_one */
75 static void submit_one (const char *plugin_instance, const char *type,
76 const char *type_instance, value_t value)
78 submit (plugin_instance, type, type_instance, &value, 1);
79 } /* void submit_one */
81 static int vmem_config (const char *key, const char *value)
83 if (strcasecmp ("Verbose", key) == 0)
96 } /* int vmem_config */
98 static int vmem_read (void)
102 derive_t pgpgout = 0;
106 derive_t pswpout = 0;
109 derive_t pgfault = 0;
110 derive_t pgmajfault = 0;
111 int pgfaultvalid = 0;
116 fh = fopen ("/proc/vmstat", "r");
120 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
121 sstrerror (errno, errbuf, sizeof (errbuf)));
125 while (fgets (buffer, sizeof (buffer), fh) != NULL)
134 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
141 counter = strtoll (fields[1], &endptr, 10);
142 if (fields[1] == endptr)
146 gauge = strtod (fields[1], &endptr);
147 if (fields[1] == endptr)
153 * The total number of {inst} pages, e. g dirty pages.
155 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
157 char *inst = key + strlen ("nr_");
158 if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0)
160 value_t value = { .derive = counter };
161 submit_one (NULL, "vmpage_action", inst, value);
165 value_t value = { .gauge = gauge };
166 submit_one (NULL, "vmpage_number", inst, value);
171 * Page in and page outs. For memory and swap.
173 else if (strcmp ("pgpgin", key) == 0)
178 else if (strcmp ("pgpgout", key) == 0)
183 else if (strcmp ("pswpin", key) == 0)
188 else if (strcmp ("pswpout", key) == 0)
197 else if (strcmp ("pgfault", key) == 0)
200 pgfaultvalid |= 0x01;
202 else if (strcmp ("pgmajfault", key) == 0)
204 pgmajfault = counter;
205 pgfaultvalid |= 0x02;
209 * Skip the other statistics if verbose output is disabled.
211 else if (verbose_output == 0)
215 * Number of page allocations, refills, steals and scans. This is collected
216 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
218 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
220 char *inst = key + strlen ("pgalloc_");
221 value_t value = { .derive = counter };
222 submit_one (inst, "vmpage_action", "alloc", value);
224 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
226 char *inst = key + strlen ("pgrefill_");
227 value_t value = { .derive = counter };
228 submit_one (inst, "vmpage_action", "refill", value);
230 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
232 char *inst = key + strlen ("pgsteal_");
233 value_t value = { .derive = counter };
234 submit_one (inst, "vmpage_action", "steal", value);
236 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
238 char *inst = key + strlen ("pgscan_kswapd_");
239 value_t value = { .derive = counter };
240 submit_one (inst, "vmpage_action", "scan_kswapd", value);
242 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
244 char *inst = key + strlen ("pgscan_direct_");
245 value_t value = { .derive = counter };
246 submit_one (inst, "vmpage_action", "scan_direct", value);
252 * number of pages moved to the active or inactive lists and freed, i. e.
253 * removed from either list.
255 else if (strcmp ("pgfree", key) == 0)
257 value_t value = { .derive = counter };
258 submit_one (NULL, "vmpage_action", "free", value);
260 else if (strcmp ("pgactivate", key) == 0)
262 value_t value = { .derive = counter };
263 submit_one (NULL, "vmpage_action", "activate", value);
265 else if (strcmp ("pgdeactivate", key) == 0)
267 value_t value = { .derive = counter };
268 submit_one (NULL, "vmpage_action", "deactivate", value);
270 } /* while (fgets) */
275 if (pgfaultvalid == 0x03)
276 submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
278 if (pgpgvalid == 0x03)
279 submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
281 if (pswpvalid == 0x03)
282 submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
283 #endif /* KERNEL_LINUX */
286 } /* int vmem_read */
288 void module_register (void)
290 plugin_register_config ("vmem", vmem_config,
291 config_keys, config_keys_num);
292 plugin_register_read ("vmem", vmem_read);
293 } /* void module_register */
295 /* vim: set sw=2 sts=2 ts=8 : */