b8febcecf748de0ec86030639a990f2eb0edb521
[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       if (strcmp(inst, "dirtied") == 0 || strcmp(inst, "written") == 0)
154       {
155         value_t value = { .derive = counter };
156         submit_one (NULL, "vmpage_action", inst, value);
157       }
158       else
159       {
160         value_t value = { .gauge = gauge };
161         submit_one (NULL, "vmpage_number", inst, value);
162       }
163     }
164
165     /* 
166      * Page in and page outs. For memory and swap.
167      */
168     else if (strcmp ("pgpgin", key) == 0)
169     {
170       pgpgin = counter;
171       pgpgvalid |= 0x01;
172     }
173     else if (strcmp ("pgpgout", key) == 0)
174     {
175       pgpgout = counter;
176       pgpgvalid |= 0x02;
177     }
178     else if (strcmp ("pswpin", key) == 0)
179     {
180       pswpin = counter;
181       pswpvalid |= 0x01;
182     }
183     else if (strcmp ("pswpout", key) == 0)
184     {
185       pswpout = counter;
186       pswpvalid |= 0x02;
187     }
188
189     /*
190      * Pagefaults
191      */
192     else if (strcmp ("pgfault", key) == 0)
193     {
194       pgfault = counter;
195       pgfaultvalid |= 0x01;
196     }
197     else if (strcmp ("pgmajfault", key) == 0)
198     {
199       pgmajfault = counter;
200       pgfaultvalid |= 0x02;
201     }
202
203     /*
204      * Skip the other statistics if verbose output is disabled.
205      */
206     else if (verbose_output == 0)
207       continue;
208
209     /*
210      * Number of page allocations, refills, steals and scans. This is collected
211      * ``per zone'', i. e. for DMA, DMA32, normal and possibly highmem.
212      */
213     else if (strncmp ("pgalloc_", key, strlen ("pgalloc_")) == 0)
214     {
215       char *inst = key + strlen ("pgalloc_");
216       value_t value  = { .derive = counter };
217       submit_one (inst, "vmpage_action", "alloc", value);
218     }
219     else if (strncmp ("pgrefill_", key, strlen ("pgrefill_")) == 0)
220     {
221       char *inst = key + strlen ("pgrefill_");
222       value_t value  = { .derive = counter };
223       submit_one (inst, "vmpage_action", "refill", value);
224     }
225     else if (strncmp ("pgsteal_", key, strlen ("pgsteal_")) == 0)
226     {
227       char *inst = key + strlen ("pgsteal_");
228       value_t value  = { .derive = counter };
229       submit_one (inst, "vmpage_action", "steal", value);
230     }
231     else if (strncmp ("pgscan_kswapd_", key, strlen ("pgscan_kswapd_")) == 0)
232     {
233       char *inst = key + strlen ("pgscan_kswapd_");
234       value_t value  = { .derive = counter };
235       submit_one (inst, "vmpage_action", "scan_kswapd", value);
236     }
237     else if (strncmp ("pgscan_direct_", key, strlen ("pgscan_direct_")) == 0)
238     {
239       char *inst = key + strlen ("pgscan_direct_");
240       value_t value  = { .derive = counter };
241       submit_one (inst, "vmpage_action", "scan_direct", value);
242     }
243
244     /*
245      * Page action
246      *
247      * number of pages moved to the active or inactive lists and freed, i. e.
248      * removed from either list.
249      */
250     else if (strcmp ("pgfree", key) == 0)
251     {
252       value_t value  = { .derive = counter };
253       submit_one (NULL, "vmpage_action", "free", value);
254     }
255     else if (strcmp ("pgactivate", key) == 0)
256     {
257       value_t value  = { .derive = counter };
258       submit_one (NULL, "vmpage_action", "activate", value);
259     }
260     else if (strcmp ("pgdeactivate", key) == 0)
261     {
262       value_t value  = { .derive = counter };
263       submit_one (NULL, "vmpage_action", "deactivate", value);
264     }
265   } /* while (fgets) */
266
267   fclose (fh);
268   fh = NULL;
269
270   if (pgfaultvalid == 0x03)
271     submit_two (NULL, "vmpage_faults", NULL, pgfault, pgmajfault);
272
273   if (pgpgvalid == 0x03)
274     submit_two (NULL, "vmpage_io", "memory", pgpgin, pgpgout);
275
276   if (pswpvalid == 0x03)
277     submit_two (NULL, "vmpage_io", "swap", pswpin, pswpout);
278 #endif /* KERNEL_LINUX */
279
280   return (0);
281 } /* int vmem_read */
282
283 void module_register (void)
284 {
285   plugin_register_config ("vmem", vmem_config,
286       config_keys, config_keys_num);
287   plugin_register_read ("vmem", vmem_read);
288 } /* void module_register */
289
290 /* vim: set sw=2 sts=2 ts=8 : */