1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005-2009 Florian octo Forster
4 * Copyright (C) 2009 Stefan Völkel
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; only version 2 of the License is applicable.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18 *
19 * Authors:
20 * Florian octo Forster <octo at verplant.org>
21 **/
23 #if HAVE_CONFIG_H
24 # include "config.h"
25 # undef HAVE_CONFIG_H
26 #endif
27 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
28 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
29 # undef _FILE_OFFSET_BITS
30 # undef _LARGEFILE64_SOURCE
31 #endif
33 #include "collectd.h"
34 #include "common.h"
35 #include "plugin.h"
37 #if HAVE_SYS_SWAP_H
38 # include <sys/swap.h>
39 #endif
40 #if HAVE_VM_ANON_H
41 # include <vm/anon.h>
42 #endif
43 #if HAVE_SYS_PARAM_H
44 # include <sys/param.h>
45 #endif
46 #if HAVE_SYS_SYSCTL_H
47 # include <sys/sysctl.h>
48 #endif
49 #if HAVE_SYS_DKSTAT_H
50 # include <sys/dkstat.h>
51 #endif
52 #if HAVE_KVM_H
53 # include <kvm.h>
54 #endif
56 #if HAVE_STATGRAB_H
57 # include <statgrab.h>
58 #endif
60 #undef MAX
61 #define MAX(x,y) ((x) > (y) ? (x) : (y))
63 #if KERNEL_LINUX
64 /* No global variables */
65 /* #endif KERNEL_LINUX */
67 #elif HAVE_LIBKSTAT
68 static derive_t pagesize;
69 static kstat_t *ksp;
70 /* #endif HAVE_LIBKSTAT */
72 #elif HAVE_SWAPCTL
73 /* No global variables */
74 /* #endif HAVE_SWAPCTL */
76 #elif defined(VM_SWAPUSAGE)
77 /* No global variables */
78 /* #endif defined(VM_SWAPUSAGE) */
80 #elif HAVE_LIBKVM_GETSWAPINFO
81 static kvm_t *kvm_obj = NULL;
82 int kvm_pagesize;
83 /* #endif HAVE_LIBKVM_GETSWAPINFO */
85 #elif HAVE_LIBSTATGRAB
86 /* No global variables */
87 /* #endif HAVE_LIBSTATGRAB */
89 #else
90 # error "No applicable input method."
91 #endif /* HAVE_LIBSTATGRAB */
93 static int swap_init (void)
94 {
95 #if KERNEL_LINUX
96 /* No init stuff */
97 /* #endif KERNEL_LINUX */
99 #elif HAVE_LIBKSTAT
100 /* getpagesize(3C) tells me this does not fail.. */
101 pagesize = (derive_t) getpagesize ();
102 if (get_kstat (&ksp, "unix", 0, "system_pages"))
103 ksp = NULL;
104 /* #endif HAVE_LIBKSTAT */
106 #elif HAVE_SWAPCTL
107 /* No init stuff */
108 /* #endif HAVE_SWAPCTL */
110 #elif defined(VM_SWAPUSAGE)
111 /* No init stuff */
112 /* #endif defined(VM_SWAPUSAGE) */
114 #elif HAVE_LIBKVM_GETSWAPINFO
115 if (kvm_obj != NULL)
116 {
117 kvm_close (kvm_obj);
118 kvm_obj = NULL;
119 }
121 kvm_pagesize = getpagesize ();
123 if ((kvm_obj = kvm_open (NULL, /* execfile */
124 NULL, /* corefile */
125 NULL, /* swapfile */
126 O_RDONLY, /* flags */
127 NULL)) /* errstr */
128 == NULL)
129 {
130 ERROR ("swap plugin: kvm_open failed.");
131 return (-1);
132 }
133 /* #endif HAVE_LIBKVM_GETSWAPINFO */
135 #elif HAVE_LIBSTATGRAB
136 /* No init stuff */
137 #endif /* HAVE_LIBSTATGRAB */
139 return (0);
140 }
142 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
143 {
144 value_t values[1];
145 value_list_t vl = VALUE_LIST_INIT;
147 switch (type)
148 {
149 case DS_TYPE_GAUGE:
150 values[0].gauge = (gauge_t) value;
151 sstrncpy (vl.type, "swap", sizeof (vl.type));
152 break;
153 case DS_TYPE_DERIVE:
154 values[0].derive = value;
155 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
156 break;
157 default:
158 ERROR ("swap plugin: swap_submit called with wrong"
159 " type");
160 }
162 vl.values = values;
163 vl.values_len = 1;
164 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
165 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
166 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
168 plugin_dispatch_values (&vl);
169 } /* void swap_submit */
171 static int swap_read (void)
172 {
173 #if KERNEL_LINUX
174 FILE *fh;
175 char buffer[1024];
177 char *fields[8];
178 int numfields;
180 derive_t swap_used = 0;
181 derive_t swap_cached = 0;
182 derive_t swap_free = 0;
183 derive_t swap_total = 0;
184 derive_t swap_in = 0;
185 derive_t swap_out = 0;
187 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
188 {
189 char errbuf[1024];
190 WARNING ("memory: fopen: %s",
191 sstrerror (errno, errbuf, sizeof (errbuf)));
192 return (-1);
193 }
195 while (fgets (buffer, 1024, fh) != NULL)
196 {
197 derive_t *val = NULL;
199 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
200 val = &swap_total;
201 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
202 val = &swap_free;
203 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
204 val = &swap_cached;
205 else
206 continue;
208 numfields = strsplit (buffer, fields, 8);
210 if (numfields < 2)
211 continue;
213 *val = (derive_t) atoll (fields[1]) * 1024LL;
214 }
216 if (fclose (fh))
217 {
218 char errbuf[1024];
219 WARNING ("memory: fclose: %s",
220 sstrerror (errno, errbuf, sizeof (errbuf)));
221 }
223 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
224 return (-1);
226 swap_used = swap_total - (swap_free + swap_cached);
228 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
229 {
230 char errbuf[1024];
231 WARNING ("swap: fopen: %s",
232 sstrerror (errno, errbuf, sizeof (errbuf)));
233 return (-1);
234 }
236 while (fgets (buffer, 1024, fh) != NULL)
237 {
238 derive_t *val = NULL;
240 if (strncasecmp (buffer, "pswpin", 6) == 0)
241 val = &swap_in;
242 else if (strncasecmp (buffer, "pswpout", 7) == 0)
243 val = &swap_out;
244 else
245 continue;
247 numfields = strsplit (buffer, fields, 8);
249 if (numfields < 2)
250 continue;
252 *val = (derive_t) atoll (fields[1]);
253 }
255 if (fclose (fh))
256 {
257 char errbuf[1024];
258 WARNING ("swap: fclose: %s",
259 sstrerror (errno, errbuf, sizeof (errbuf)));
260 }
262 swap_submit ("used", swap_used, DS_TYPE_GAUGE);
263 swap_submit ("free", swap_free, DS_TYPE_GAUGE);
264 swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
265 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
266 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
268 /* #endif KERNEL_LINUX */
270 #elif HAVE_LIBKSTAT
271 derive_t swap_alloc;
272 derive_t swap_resv;
273 derive_t swap_avail;
275 struct anoninfo ai;
277 if (swapctl (SC_AINFO, &ai) == -1)
278 {
279 char errbuf[1024];
280 ERROR ("swap plugin: swapctl failed: %s",
281 sstrerror (errno, errbuf, sizeof (errbuf)));
282 return (-1);
283 }
285 /*
286 * Calculations from:
287 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
288 * Also see:
289 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
290 * /usr/include/vm/anon.h
291 *
292 * In short, swap -s shows: allocated + reserved = used, available
293 *
294 * However, Solaris does not allow to allocated/reserved more than the
295 * available swap (physical memory + disk swap), so the pedant may
296 * prefer: allocated + unallocated = reserved, available
297 *
298 * We map the above to: used + resv = n/a, free
299 *
300 * Does your brain hurt yet? - Christophe Kalt
301 *
302 * Oh, and in case you wonder,
303 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
304 * can suffer from a 32bit overflow.
305 */
306 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
307 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
308 * pagesize);
309 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
311 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
312 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
313 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
314 /* #endif HAVE_LIBKSTAT */
316 #elif HAVE_SWAPCTL
317 struct swapent *swap_entries;
318 int swap_num;
319 int status;
320 int i;
322 derive_t used = 0;
323 derive_t total = 0;
325 /*
326 * XXX: This is the syntax for the *BSD `swapctl', which has the
327 * following prototype:
328 * swapctl (int cmd, void *arg, int misc);
329 *
330 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
331 * too, but with the following prototype:
332 * swapctl (int cmd, void *arg);
333 *
334 * Solaris is usually handled in the KSTAT case above. For other UNIXes
335 * a separate case for the other version of `swapctl' may be necessary.
336 */
337 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
338 if (swap_num < 0)
339 {
340 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
341 swap_num);
342 return (-1);
343 }
344 else if (swap_num == 0)
345 return (0);
347 swap_entries = calloc (swap_num, sizeof (*swap_entries));
348 if (swap_entries == NULL)
349 {
350 ERROR ("swap plugin: calloc failed.");
351 return (-1);
352 }
354 status = swapctl (SWAP_STATS, swap_entries, swap_num);
355 if (status != swap_num)
356 {
357 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
358 status);
359 sfree (swap_entries);
360 return (-1);
361 }
363 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
364 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
365 #else
366 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
367 #endif
369 for (i = 0; i < swap_num; i++)
370 {
371 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
372 continue;
374 used += ((derive_t) swap_entries[i].se_inuse)
375 * C_SWAP_BLOCK_SIZE;
376 total += ((derive_t) swap_entries[i].se_nblks)
377 * C_SWAP_BLOCK_SIZE;
378 }
380 if (total < used)
381 {
382 ERROR ("swap plugin: Total swap space (%"PRIu64") "
383 "is less than used swap space (%"PRIu64").",
384 total, used);
385 return (-1);
386 }
388 swap_submit ("used", used, DS_TYPE_GAUGE);
389 swap_submit ("free", total - used, DS_TYPE_GAUGE);
391 sfree (swap_entries);
392 /* #endif HAVE_SWAPCTL */
394 #elif defined(VM_SWAPUSAGE)
395 int mib[3];
396 size_t mib_len;
397 struct xsw_usage sw_usage;
398 size_t sw_usage_len;
400 mib_len = 2;
401 mib[0] = CTL_VM;
402 mib[1] = VM_SWAPUSAGE;
404 sw_usage_len = sizeof (struct xsw_usage);
406 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
407 return (-1);
409 /* The returned values are bytes. */
410 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
411 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
412 /* #endif VM_SWAPUSAGE */
414 #elif HAVE_LIBKVM_GETSWAPINFO
415 struct kvm_swap data_s;
416 int status;
418 derive_t used;
419 derive_t free;
420 derive_t total;
422 if (kvm_obj == NULL)
423 return (-1);
425 /* only one structure => only get the grand total, no details */
426 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
427 if (status == -1)
428 return (-1);
430 total = (derive_t) data_s.ksw_total;
431 used = (derive_t) data_s.ksw_used;
433 total *= (derive_t) kvm_pagesize;
434 used *= (derive_t) kvm_pagesize;
436 free = total - used;
438 swap_submit ("used", used, DS_TYPE_GAUGE);
439 swap_submit ("free", free, DS_TYPE_GAUGE);
440 /* #endif HAVE_LIBKVM_GETSWAPINFO */
442 #elif HAVE_LIBSTATGRAB
443 sg_swap_stats *swap;
445 swap = sg_get_swap_stats ();
447 if (swap == NULL)
448 return (-1);
450 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
451 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
452 #endif /* HAVE_LIBSTATGRAB */
454 return (0);
455 } /* int swap_read */
457 void module_register (void)
458 {
459 plugin_register_init ("swap", swap_init);
460 plugin_register_read ("swap", swap_read);
461 } /* void module_register */