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