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 value_t value = { .gauge = gauge };
159 submit_one (NULL, "vmpage_number", inst, value);
163 * Page in and page outs. For memory and swap.
165 else if (strcmp ("pgpgin", key) == 0)
170 else if (strcmp ("pgpgout", key) == 0)
175 else if (strcmp ("pswpin", key) == 0)
180 else if (strcmp ("pswpout", key) == 0)
189 else if (strcmp ("pgfault", key) == 0)
192 pgfaultvalid |= 0x01;
194 else if (strcmp ("pgmajfault", key) == 0)
196 pgmajfault = counter;
197 pgfaultvalid |= 0x02;
201 * Skip the other statistics if verbose output is disabled.
203 else if (verbose_output == 0)
207 * Number of page allocations, refills, steals and scans. This is collected
208 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
210 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
212 char *inst = key + strlen ("pgalloc_");
213 value_t value = { .derive = counter };
214 submit_one (inst, "vmpage_action", "alloc", value);
216 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
218 char *inst = key + strlen ("pgrefill_");
219 value_t value = { .derive = counter };
220 submit_one (inst, "vmpage_action", "refill", value);
222 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
224 char *inst = key + strlen ("pgsteal_");
225 value_t value = { .derive = counter };
226 submit_one (inst, "vmpage_action", "steal", value);
228 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
230 char *inst = key + strlen ("pgscan_kswapd_");
231 value_t value = { .derive = counter };
232 submit_one (inst, "vmpage_action", "scan_kswapd", value);
234 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
236 char *inst = key + strlen ("pgscan_direct_");
237 value_t value = { .derive = counter };
238 submit_one (inst, "vmpage_action", "scan_direct", value);
244 * number of pages moved to the active or inactive lists and freed, i. e.
245 * removed from either list.
247 else if (strcmp ("pgfree", key) == 0)
249 value_t value = { .derive = counter };
250 submit_one (NULL, "vmpage_action", "free", value);
252 else if (strcmp ("pgactivate", key) == 0)
254 value_t value = { .derive = counter };
255 submit_one (NULL, "vmpage_action", "activate", value);
257 else if (strcmp ("pgdeactivate", key) == 0)
259 value_t value = { .derive = counter };
260 submit_one (NULL, "vmpage_action", "deactivate", value);
262 } /* while (fgets) */
267 if (pgfaultvalid == 0x03)
268 submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
270 if (pgpgvalid == 0x03)
271 submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
273 if (pswpvalid == 0x03)
274 submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
275 #endif /* KERNEL_LINUX */
278 } /* int vmem_read */
280 void module_register (void)
282 plugin_register_config ("vmem", vmem_config,
283 config_keys, config_keys_num);
284 plugin_register_read ("vmem", vmem_read);
285 } /* void module_register */
287 /* vim: set sw=2 sts=2 ts=8 : */