Merge branch 'pr/1918'
[collectd.git] / src / vmem.c
1 /**
2  * collectd - src/vmem.c
3  * Copyright (C) 2008-2010  Florian octo Forster
4  *
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:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
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.
22  *
23  * Authors:
24  *   Florian octo Forster <octo at collectd.org>
25  **/
26
27 #include "collectd.h"
28
29 #include "common.h"
30 #include "plugin.h"
31
32 #if KERNEL_LINUX
33 static const char *config_keys[] =
34 {
35   "Verbose"
36 };
37 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
38
39 static int verbose_output = 0;
40 /* #endif KERNEL_LINUX */
41
42 #else
43 # error "No applicable input method."
44 #endif /* HAVE_LIBSTATGRAB */
45
46 static void submit (const char *plugin_instance, const char *type,
47     const char *type_instance, value_t *values, int values_len)
48 {
49   value_list_t vl = VALUE_LIST_INIT;
50
51   vl.values = values;
52   vl.values_len = values_len;
53
54   sstrncpy (vl.host, hostname_g, sizeof (vl.host));
55   sstrncpy (vl.plugin, "vmem", sizeof (vl.plugin));
56   if (plugin_instance != NULL)
57     sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
58   sstrncpy (vl.type, type, sizeof (vl.type));
59   if (type_instance != NULL)
60     sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
61
62   plugin_dispatch_values (&vl);
63 } /* void vmem_submit */
64
65 static void submit_two (const char *plugin_instance, const char *type,
66     const char *type_instance, derive_t c0, derive_t c1)
67 {
68   value_t values[] = {
69     { .derive = c0 },
70     { .derive = c1 },
71   };
72
73   submit (plugin_instance, type, type_instance,
74       values, STATIC_ARRAY_SIZE (values));
75 } /* void submit_one */
76
77 static void submit_one (const char *plugin_instance, const char *type,
78     const char *type_instance, value_t value)
79 {
80   submit (plugin_instance, type, type_instance, &value, 1);
81 } /* void submit_one */
82
83 static int vmem_config (const char *key, const char *value)
84 {
85   if (strcasecmp ("Verbose", key) == 0)
86   {
87     if (IS_TRUE (value))
88       verbose_output = 1;
89     else
90       verbose_output = 0;
91   }
92   else
93   {
94     return (-1);
95   }
96
97   return (0);
98 } /* int vmem_config */
99
100 static int vmem_read (void)
101 {
102 #if KERNEL_LINUX
103   derive_t pgpgin = 0;
104   derive_t pgpgout = 0;
105   int pgpgvalid = 0;
106
107   derive_t pswpin = 0;
108   derive_t pswpout = 0;
109   int pswpvalid = 0;
110
111   derive_t pgfault = 0;
112   derive_t pgmajfault = 0;
113   int pgfaultvalid = 0;
114
115   FILE *fh;
116   char buffer[1024];
117
118   fh = fopen ("/proc/vmstat", "r");
119   if (fh == NULL)
120   {
121     char errbuf[1024];
122     ERROR ("vmem plugin: fopen (/proc/vmstat) failed: %s",
123         sstrerror (errno, errbuf, sizeof (errbuf)));
124     return (-1);
125   }
126
127   while (fgets (buffer, sizeof (buffer), fh) != NULL)
128   {
129     char *fields[4];
130     int fields_num;
131     char *key;
132     char *endptr;
133     derive_t counter;
134     gauge_t gauge;
135
136     fields_num = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
137     if (fields_num != 2)
138       continue;
139
140     key = fields[0];
141
142     endptr = NULL;
143     counter = strtoll (fields[1], &endptr, 10);
144     if (fields[1] == endptr)
145       continue;
146
147     endptr = NULL;
148     gauge = strtod (fields[1], &endptr);
149     if (fields[1] == endptr)
150       continue;
151
152     /*
153      * Number of pages
154      *
155      * The total number of {inst} pages, e. g dirty pages.
156      */
157     if (strncmp ("nr_", key, strlen ("nr_")) == 0)
158     {
159       char *inst = key + strlen ("nr_");
160       if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0)
161       {
162         value_t value = { .derive = counter };
163         submit_one (NULL, "vmpage_action", inst, value);
164       }
165       else
166       {
167         value_t value = { .gauge = gauge };
168         submit_one (NULL, "vmpage_number", inst, value);
169       }
170     }
171
172     /*
173      * Page in and page outs. For memory and swap.
174      */
175     else if (strcmp ("pgpgin", key) == 0)
176     {
177       pgpgin = counter;
178       pgpgvalid |= 0x01;
179     }
180     else if (strcmp ("pgpgout", key) == 0)
181     {
182       pgpgout = counter;
183       pgpgvalid |= 0x02;
184     }
185     else if (strcmp ("pswpin", key) == 0)
186     {
187       pswpin = counter;
188       pswpvalid |= 0x01;
189     }
190     else if (strcmp ("pswpout", key) == 0)
191     {
192       pswpout = counter;
193       pswpvalid |= 0x02;
194     }
195
196     /*
197      * Pagefaults
198      */
199     else if (strcmp ("pgfault", key) == 0)
200     {
201       pgfault = counter;
202       pgfaultvalid |= 0x01;
203     }
204     else if (strcmp ("pgmajfault", key) == 0)
205     {
206       pgmajfault = counter;
207       pgfaultvalid |= 0x02;
208     }
209
210     /*
211      * Skip the other statistics if verbose output is disabled.
212      */
213     else if (verbose_output == 0)
214       continue;
215
216     /*
217      * Number of page allocations, refills, steals and scans. This is collected
218      * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
219      */
220     else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
221     {
222       char *inst = key + strlen ("pgalloc_");
223       value_t value  = { .derive = counter };
224       submit_one (inst, "vmpage_action", "alloc", value);
225     }
226     else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
227     {
228       char *inst = key + strlen ("pgrefill_");
229       value_t value  = { .derive = counter };
230       submit_one (inst, "vmpage_action", "refill", value);
231     }
232     else if (strncmp ("pgsteal_kswapd_", key, strlen ("pgsteal_kswapd_")) == 0)
233     {
234       char *inst = key + strlen ("pgsteal_kswapd_");
235       value_t value  = { .derive = counter };
236       submit_one (inst, "vmpage_action", "steal_kswapd", value);
237     }
238     else if (strncmp ("pgsteal_direct_", key, strlen ("pgsteal_direct_")) == 0)
239     {
240       char *inst = key + strlen ("pgsteal_direct_");
241       value_t value  = { .derive = counter };
242       submit_one (inst, "vmpage_action", "steal_direct", value);
243     }
244     /* For backwards compatibility (somewhen before 4.2.3) */
245     else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
246     {
247       char *inst = key + strlen ("pgsteal_");
248       value_t value  = { .derive = counter };
249       submit_one (inst, "vmpage_action", "steal", value);
250     }
251     else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
252     {
253       char *inst = key + strlen ("pgscan_kswapd_");
254       value_t value  = { .derive = counter };
255       submit_one (inst, "vmpage_action", "scan_kswapd", value);
256     }
257     else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
258     {
259       char *inst = key + strlen ("pgscan_direct_");
260       value_t value  = { .derive = counter };
261       submit_one (inst, "vmpage_action", "scan_direct", value);
262     }
263
264     /*
265      * Page action
266      *
267      * number of pages moved to the active or inactive lists and freed, i. e.
268      * removed from either list.
269      */
270     else if (strcmp ("pgfree", key) == 0)
271     {
272       value_t value  = { .derive = counter };
273       submit_one (NULL, "vmpage_action", "free", value);
274     }
275     else if (strcmp ("pgactivate", key) == 0)
276     {
277       value_t value  = { .derive = counter };
278       submit_one (NULL, "vmpage_action", "activate", value);
279     }
280     else if (strcmp ("pgdeactivate", key) == 0)
281     {
282       value_t value  = { .derive = counter };
283       submit_one (NULL, "vmpage_action", "deactivate", value);
284     }
285   } /* while (fgets) */
286
287   fclose (fh);
288   fh = NULL;
289
290   if (pgfaultvalid == 0x03)
291     submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
292
293   if (pgpgvalid == 0x03)
294     submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
295
296   if (pswpvalid == 0x03)
297     submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
298 #endif /* KERNEL_LINUX */
299
300   return (0);
301 } /* int vmem_read */
302
303 void module_register (void)
304 {
305   plugin_register_config ("vmem", vmem_config,
306       config_keys, config_keys_num);
307   plugin_register_read ("vmem", vmem_read);
308 } /* void module_register */
309
310 /* vim: set sw=2 sts=2 ts=8 : */