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