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