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