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