1 /**
2 * collectd - src/vmem.c
3 * Copyright (C) 2008 Florian octo Forster
4 *
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.
8 *
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.
13 *
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
17 *
18 * Authors:
19 * Florian octo Forster <octo at verplant.org>
20 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
26 #if KERNEL_LINUX
27 static const char *config_keys[] =
28 {
29 "Verbose"
30 };
31 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
33 static int verbose_output = 0;
34 /* #endif KERNEL_LINUX */
36 #else
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)
42 {
43 value_list_t vl = VALUE_LIST_INIT;
45 vl.values = values;
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)
62 {
63 value_t values[2];
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)
73 {
74 submit (plugin_instance, type, type_instance, &value, 1);
75 } /* void submit_one */
77 static int vmem_config (const char *key, const char *value)
78 {
79 if (strcasecmp ("Verbose", key) == 0)
80 {
81 if ((strcasecmp ("true", value) == 0)
82 || (strcasecmp ("yes", value) == 0)
83 || (strcasecmp ("on", value) == 0))
84 verbose_output = 1;
85 else
86 verbose_output = 0;
87 }
88 else
89 {
90 return (-1);
91 }
93 return (0);
94 } /* int vmem_config */
96 static int vmem_read (void)
97 {
98 #if KERNEL_LINUX
99 counter_t pgpgin = 0;
100 counter_t pgpgout = 0;
101 int pgpgvalid = 0;
103 counter_t pswpin = 0;
104 counter_t pswpout = 0;
105 int pswpvalid = 0;
107 counter_t pgfault = 0;
108 counter_t pgmajfault = 0;
109 int pgfaultvalid = 0;
111 FILE *fh;
112 char buffer[1024];
114 fh = fopen ("/proc/vmstat", "r");
115 if (fh == NULL)
116 {
117 char errbuf[1024];
118 ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
119 sstrerror (errno, errbuf, sizeof (errbuf)));
120 return (-1);
121 }
123 while (fgets (buffer, sizeof (buffer), fh) != NULL)
124 {
125 char *fields[4];
126 int fields_num;
127 char *key;
128 char *endptr;
129 counter_t counter;
130 gauge_t gauge;
132 fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
133 if (fields_num != 2)
134 continue;
136 key = fields[0];
138 endptr = NULL;
139 counter = strtoll (fields[1], &endptr, 10);
140 if (fields[1] == endptr)
141 continue;
143 endptr = NULL;
144 gauge = strtod (fields[1], &endptr);
145 if (fields[1] == endptr)
146 continue;
148 /*
149 * Number of pages
150 *
151 * The total number of {inst} pages, e. g dirty pages.
152 */
153 if (strncmp ("nr_", key, strlen ("nr_")) == 0)
154 {
155 char *inst = key + strlen ("nr_");
156 value_t value = { .gauge = gauge };
157 submit_one (NULL, "vmpage_number", inst, value);
158 }
160 /*
161 * Page in and page outs. For memory and swap.
162 */
163 else if (strcmp ("pgpgin", key) == 0)
164 {
165 pgpgin = counter;
166 pgpgvalid |= 0x01;
167 }
168 else if (strcmp ("pgpgout", key) == 0)
169 {
170 pgpgout = counter;
171 pgpgvalid |= 0x02;
172 }
173 else if (strcmp ("pswpin", key) == 0)
174 {
175 pswpin = counter;
176 pswpvalid |= 0x01;
177 }
178 else if (strcmp ("pswpout", key) == 0)
179 {
180 pswpout = counter;
181 pswpvalid |= 0x02;
182 }
184 /*
185 * Pagefaults
186 */
187 else if (strcmp ("pgfault", key) == 0)
188 {
189 pgfault = counter;
190 pgfaultvalid |= 0x01;
191 }
192 else if (strcmp ("pgmajfault", key) == 0)
193 {
194 pgmajfault = counter;
195 pgfaultvalid |= 0x02;
196 }
198 /*
199 * Skip the other statistics if verbose output is disabled.
200 */
201 else if (verbose_output == 0)
202 continue;
204 /*
205 * Number of page allocations, refills, steals and scans. This is collected
206 * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
207 */
208 else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
209 {
210 char *inst = key + strlen ("pgalloc_");
211 value_t value = { .counter = counter };
212 submit_one (inst, "vmpage_action", "alloc", value);
213 }
214 else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
215 {
216 char *inst = key + strlen ("pgrefill_");
217 value_t value = { .counter = counter };
218 submit_one (inst, "vmpage_action", "refill", value);
219 }
220 else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
221 {
222 char *inst = key + strlen ("pgsteal_");
223 value_t value = { .counter = counter };
224 submit_one (inst, "vmpage_action", "steal", value);
225 }
226 else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
227 {
228 char *inst = key + strlen ("pgscan_kswapd_");
229 value_t value = { .counter = counter };
230 submit_one (inst, "vmpage_action", "scan_kswapd", value);
231 }
232 else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
233 {
234 char *inst = key + strlen ("pgscan_direct_");
235 value_t value = { .counter = counter };
236 submit_one (inst, "vmpage_action", "scan_direct", value);
237 }
239 /*
240 * Page action
241 *
242 * number of pages moved to the active or inactive lists and freed, i. e.
243 * removed from either list.
244 */
245 else if (strcmp ("pgfree", key) == 0)
246 {
247 value_t value = { .counter = counter };
248 submit_one (NULL, "vmpage_action", "free", value);
249 }
250 else if (strcmp ("pgactivate", key) == 0)
251 {
252 value_t value = { .counter = counter };
253 submit_one (NULL, "vmpage_action", "activate", value);
254 }
255 else if (strcmp ("pgdeactivate", key) == 0)
256 {
257 value_t value = { .counter = counter };
258 submit_one (NULL, "vmpage_action", "deactivate", value);
259 }
260 } /* while (fgets) */
262 fclose (fh);
263 fh = NULL;
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 */
275 return (0);
276 } /* int vmem_read */
278 void module_register (void)
279 {
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 : */