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