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