91f6a19f4c4b2d0e3824061b3b153b4a09e3e2a3
1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005-2014 Florian octo Forster
4 * Copyright (C) 2009 Stefan Völkel
5 * Copyright (C) 2009 Manuel Sanmartin
6 * Copyright (C) 2010 Aurélien Reynaud
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by the
10 * Free Software Foundation; only version 2 of the License is applicable.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 *
21 * Authors:
22 * Florian octo Forster <octo at collectd.org>
23 * Manuel Sanmartin
24 * Aurélien Reynaud <collectd at wattapower.net>
25 **/
27 #if HAVE_CONFIG_H
28 # include "config.h"
29 # undef HAVE_CONFIG_H
30 #endif
31 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
32 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
33 # undef _FILE_OFFSET_BITS
34 # undef _LARGEFILE64_SOURCE
35 #endif
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
41 #if HAVE_SYS_SWAP_H
42 # include <sys/swap.h>
43 #endif
44 #if HAVE_VM_ANON_H
45 # include <vm/anon.h>
46 #endif
47 #if HAVE_SYS_PARAM_H
48 # include <sys/param.h>
49 #endif
50 #if HAVE_SYS_SYSCTL_H
51 # include <sys/sysctl.h>
52 #endif
53 #if HAVE_SYS_DKSTAT_H
54 # include <sys/dkstat.h>
55 #endif
56 #if HAVE_KVM_H
57 # include <kvm.h>
58 #endif
60 #if HAVE_STATGRAB_H
61 # include <statgrab.h>
62 #endif
64 #if HAVE_PERFSTAT
65 # include <sys/protosw.h>
66 # include <libperfstat.h>
67 #endif
69 #undef MAX
70 #define MAX(x,y) ((x) > (y) ? (x) : (y))
72 #if KERNEL_LINUX
73 # define SWAP_HAVE_REPORT_BY_DEVICE 1
74 static derive_t pagesize;
75 static _Bool report_bytes = 0;
76 static _Bool report_by_device = 0;
77 /* #endif KERNEL_LINUX */
79 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
80 # define SWAP_HAVE_REPORT_BY_DEVICE 1
81 static derive_t pagesize;
82 static _Bool report_by_device = 0;
83 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
85 #elif defined(VM_SWAPUSAGE)
86 /* No global variables */
87 /* #endif defined(VM_SWAPUSAGE) */
89 #elif HAVE_LIBKVM_GETSWAPINFO
90 static kvm_t *kvm_obj = NULL;
91 int kvm_pagesize;
92 /* #endif HAVE_LIBKVM_GETSWAPINFO */
94 #elif HAVE_LIBSTATGRAB
95 /* No global variables */
96 /* #endif HAVE_LIBSTATGRAB */
98 #elif HAVE_PERFSTAT
99 static int pagesize;
100 /*# endif HAVE_PERFSTAT */
102 #else
103 # error "No applicable input method."
104 #endif /* HAVE_LIBSTATGRAB */
106 static _Bool values_absolute = 1;
107 static _Bool values_percentage = 0;
109 static int swap_config (oconfig_item_t *ci) /* {{{ */
110 {
111 int i;
113 for (i = 0; i < ci->children_num; i++)
114 {
115 oconfig_item_t *child = ci->children + i;
116 if (strcasecmp ("ReportBytes", child->key) == 0)
117 #if KERNEL_LINUX
118 cf_util_get_boolean (child, &report_bytes);
119 #else
120 WARNING ("swap plugin: The \"ReportBytes\" option "
121 "is only valid under Linux. "
122 "The option is going to be ignored.");
123 #endif
124 else if (strcasecmp ("ReportByDevice", child->key) == 0)
125 #if SWAP_HAVE_REPORT_BY_DEVICE
126 cf_util_get_boolean (child, &report_by_device);
127 #else
128 WARNING ("swap plugin: The \"ReportByDevice\" option "
129 "is not supported on this platform. "
130 "The option is going to be ignored.");
131 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
132 else if (strcasecmp ("ValuesAbsolute", child->key) == 0)
133 cf_util_get_boolean (child, &values_absolute);
134 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
135 cf_util_get_boolean (child, &values_percentage);
136 else
137 WARNING ("swap plugin: Unknown config option: \"%s\"",
138 child->key);
139 }
141 return (0);
142 } /* }}} int swap_config */
144 static int swap_init (void) /* {{{ */
145 {
146 #if KERNEL_LINUX
147 pagesize = (derive_t) sysconf (_SC_PAGESIZE);
148 /* #endif KERNEL_LINUX */
150 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
151 /* getpagesize(3C) tells me this does not fail.. */
152 pagesize = (derive_t) getpagesize ();
153 /* #endif HAVE_SWAPCTL */
155 #elif defined(VM_SWAPUSAGE)
156 /* No init stuff */
157 /* #endif defined(VM_SWAPUSAGE) */
159 #elif HAVE_LIBKVM_GETSWAPINFO
160 if (kvm_obj != NULL)
161 {
162 kvm_close (kvm_obj);
163 kvm_obj = NULL;
164 }
166 kvm_pagesize = getpagesize ();
168 if ((kvm_obj = kvm_open (NULL, /* execfile */
169 NULL, /* corefile */
170 NULL, /* swapfile */
171 O_RDONLY, /* flags */
172 NULL)) /* errstr */
173 == NULL)
174 {
175 ERROR ("swap plugin: kvm_open failed.");
176 return (-1);
177 }
178 /* #endif HAVE_LIBKVM_GETSWAPINFO */
180 #elif HAVE_LIBSTATGRAB
181 /* No init stuff */
182 /* #endif HAVE_LIBSTATGRAB */
184 #elif HAVE_PERFSTAT
185 pagesize = getpagesize();
186 #endif /* HAVE_PERFSTAT */
188 return (0);
189 } /* }}} int swap_init */
191 static void swap_submit (const char *plugin_instance, /* {{{ */
192 const char *type, const char *type_instance,
193 value_t value)
194 {
195 value_list_t vl = VALUE_LIST_INIT;
197 assert (type != NULL);
199 vl.values = &value;
200 vl.values_len = 1;
201 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
202 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
203 if (plugin_instance != NULL)
204 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
205 sstrncpy (vl.type, type, sizeof (vl.type));
206 if (type_instance != NULL)
207 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
209 plugin_dispatch_values (&vl);
210 } /* }}} void swap_submit_inst */
212 static void swap_submit_usage (char const *plugin_instance, /* {{{ */
213 gauge_t used, gauge_t free,
214 char const *other_name, gauge_t other_value)
215 {
216 value_t v[1];
217 value_list_t vl = VALUE_LIST_INIT;
219 vl.values = v;
220 vl.values_len = STATIC_ARRAY_SIZE (v);
221 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
222 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
223 if (plugin_instance != NULL)
224 sstrncpy (vl.plugin_instance, plugin_instance,
225 sizeof (vl.plugin_instance));
226 sstrncpy (vl.type, "swap", sizeof (vl.type));
228 if (values_absolute)
229 plugin_dispatch_multivalue (&vl, 0,
230 "used", used, "free", free,
231 other_name, other_value, NULL);
232 if (values_percentage)
233 plugin_dispatch_multivalue (&vl, 1,
234 "used", used, "free", free,
235 other_name, other_value, NULL);
236 } /* }}} void swap_submit_usage */
238 #if KERNEL_LINUX || HAVE_PERFSTAT
239 static void swap_submit_derive (const char *plugin_instance, /* {{{ */
240 const char *type_instance, derive_t value)
241 {
242 value_t v;
244 v.derive = value;
245 swap_submit (plugin_instance, "swap_io", type_instance, v);
246 } /* }}} void swap_submit_derive */
247 #endif
249 #if KERNEL_LINUX
250 static int swap_read_separate (void) /* {{{ */
251 {
252 FILE *fh;
253 char buffer[1024];
255 fh = fopen ("/proc/swaps", "r");
256 if (fh == NULL)
257 {
258 char errbuf[1024];
259 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
260 sstrerror (errno, errbuf, sizeof (errbuf)));
261 return (-1);
262 }
264 while (fgets (buffer, sizeof (buffer), fh) != NULL)
265 {
266 char *fields[8];
267 int numfields;
268 char *endptr;
270 char path[PATH_MAX];
271 gauge_t total;
272 gauge_t used;
274 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
275 if (numfields != 5)
276 continue;
278 sstrncpy (path, fields[0], sizeof (path));
279 escape_slashes (path, sizeof (path));
281 errno = 0;
282 endptr = NULL;
283 total = strtod (fields[2], &endptr);
284 if ((endptr == fields[2]) || (errno != 0))
285 continue;
287 errno = 0;
288 endptr = NULL;
289 used = strtod (fields[3], &endptr);
290 if ((endptr == fields[3]) || (errno != 0))
291 continue;
293 if (total < used)
294 continue;
296 swap_submit_usage (path, used, total - used, NULL, NAN);
297 }
299 fclose (fh);
301 return (0);
302 } /* }}} int swap_read_separate */
304 static int swap_read_combined (void) /* {{{ */
305 {
306 FILE *fh;
307 char buffer[1024];
309 uint8_t have_data = 0;
310 gauge_t swap_used = 0.0;
311 gauge_t swap_cached = 0.0;
312 gauge_t swap_free = 0.0;
313 gauge_t swap_total = 0.0;
315 fh = fopen ("/proc/meminfo", "r");
316 if (fh == NULL)
317 {
318 char errbuf[1024];
319 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
320 sstrerror (errno, errbuf, sizeof (errbuf)));
321 return (-1);
322 }
324 while (fgets (buffer, sizeof (buffer), fh) != NULL)
325 {
326 char *fields[8];
327 int numfields;
329 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
330 if (numfields < 2)
331 continue;
333 if (strcasecmp (fields[0], "SwapTotal:") == 0)
334 {
335 swap_total = strtod (fields[1], /* endptr = */ NULL);
336 have_data |= 0x01;
337 }
338 else if (strcasecmp (fields[0], "SwapFree:") == 0)
339 {
340 swap_free = strtod (fields[1], /* endptr = */ NULL);
341 have_data |= 0x02;
342 }
343 else if (strcasecmp (fields[0], "SwapCached:") == 0)
344 {
345 swap_cached = strtod (fields[1], /* endptr = */ NULL);
346 have_data |= 0x04;
347 }
348 }
350 fclose (fh);
352 if (have_data != 0x07)
353 return (ENOENT);
355 if (isnan (swap_total)
356 || (swap_total <= 0.0)
357 || ((swap_free + swap_cached) > swap_total))
358 return (EINVAL);
360 swap_used = swap_total - (swap_free + swap_cached);
362 swap_submit_usage (NULL, swap_used, swap_free, "cached", swap_cached);
363 return (0);
364 } /* }}} int swap_read_combined */
366 static int swap_read_io (void) /* {{{ */
367 {
368 FILE *fh;
369 char buffer[1024];
371 _Bool old_kernel = 0;
373 uint8_t have_data = 0;
374 derive_t swap_in = 0;
375 derive_t swap_out = 0;
377 fh = fopen ("/proc/vmstat", "r");
378 if (fh == NULL)
379 {
380 /* /proc/vmstat does not exist in kernels <2.6 */
381 fh = fopen ("/proc/stat", "r");
382 if (fh == NULL)
383 {
384 char errbuf[1024];
385 WARNING ("swap: fopen: %s",
386 sstrerror (errno, errbuf, sizeof (errbuf)));
387 return (-1);
388 }
389 else
390 old_kernel = 1;
391 }
393 while (fgets (buffer, sizeof (buffer), fh) != NULL)
394 {
395 char *fields[8];
396 int numfields;
398 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
400 if (!old_kernel)
401 {
402 if (numfields != 2)
403 continue;
405 if (strcasecmp ("pswpin", fields[0]) == 0)
406 {
407 strtoderive (fields[1], &swap_in);
408 have_data |= 0x01;
409 }
410 else if (strcasecmp ("pswpout", fields[0]) == 0)
411 {
412 strtoderive (fields[1], &swap_out);
413 have_data |= 0x02;
414 }
415 }
416 else /* if (old_kernel) */
417 {
418 if (numfields != 3)
419 continue;
421 if (strcasecmp ("page", fields[0]) == 0)
422 {
423 strtoderive (fields[1], &swap_in);
424 strtoderive (fields[2], &swap_out);
425 }
426 }
427 } /* while (fgets) */
429 fclose (fh);
431 if (have_data != 0x03)
432 return (ENOENT);
434 if (report_bytes)
435 {
436 swap_in = swap_in * pagesize;
437 swap_out = swap_out * pagesize;
438 }
440 swap_submit_derive (NULL, "in", swap_in);
441 swap_submit_derive (NULL, "out", swap_out);
443 return (0);
444 } /* }}} int swap_read_io */
446 static int swap_read (void) /* {{{ */
447 {
448 if (report_by_device)
449 swap_read_separate ();
450 else
451 swap_read_combined ();
453 swap_read_io ();
455 return (0);
456 } /* }}} int swap_read */
457 /* #endif KERNEL_LINUX */
459 /*
460 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
461 * and kstat. The former reads physical space used on a device, the latter
462 * reports the view from the virtual memory system. It was decided that the
463 * kstat-based information should be moved to the "vmem" plugin, but nobody
464 * with enough Solaris experience was available at that time to do this. The
465 * code below is still there for your reference but it won't be activated in
466 * *this* plugin again. --octo
467 */
468 #elif 0 && HAVE_LIBKSTAT
469 /* kstat-based read function */
470 static int swap_read_kstat (void) /* {{{ */
471 {
472 gauge_t swap_alloc;
473 gauge_t swap_resv;
474 gauge_t swap_avail;
476 struct anoninfo ai;
478 if (swapctl (SC_AINFO, &ai) == -1)
479 {
480 char errbuf[1024];
481 ERROR ("swap plugin: swapctl failed: %s",
482 sstrerror (errno, errbuf, sizeof (errbuf)));
483 return (-1);
484 }
486 /*
487 * Calculations from:
488 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
489 * Also see:
490 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
491 * /usr/include/vm/anon.h
492 *
493 * In short, swap -s shows: allocated + reserved = used, available
494 *
495 * However, Solaris does not allow to allocated/reserved more than the
496 * available swap (physical memory + disk swap), so the pedant may
497 * prefer: allocated + unallocated = reserved, available
498 *
499 * We map the above to: used + resv = n/a, free
500 *
501 * Does your brain hurt yet? - Christophe Kalt
502 *
503 * Oh, and in case you wonder,
504 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
505 * can suffer from a 32bit overflow.
506 */
507 swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
508 swap_resv = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
509 swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
511 swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
512 return (0);
513 } /* }}} int swap_read_kstat */
514 /* #endif 0 && HAVE_LIBKSTAT */
516 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
517 /* swapctl-based read function */
518 static int swap_read (void) /* {{{ */
519 {
520 swaptbl_t *s;
521 char *s_paths;
522 int swap_num;
523 int status;
524 int i;
526 gauge_t avail = 0;
527 gauge_t total = 0;
529 swap_num = swapctl (SC_GETNSWP, NULL);
530 if (swap_num < 0)
531 {
532 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
533 swap_num);
534 return (-1);
535 }
536 else if (swap_num == 0)
537 return (0);
539 /* Allocate and initialize the swaptbl_t structure */
540 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
541 if (s == NULL)
542 {
543 ERROR ("swap plugin: smalloc failed.");
544 return (-1);
545 }
547 /* Memory to store the path names. We only use these paths when the
548 * separate option has been configured, but it's easier to just
549 * allocate enough memory in any case. */
550 s_paths = calloc (swap_num, PATH_MAX);
551 if (s_paths == NULL)
552 {
553 ERROR ("swap plugin: malloc failed.");
554 sfree (s);
555 return (-1);
556 }
557 for (i = 0; i < swap_num; i++)
558 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
559 s->swt_n = swap_num;
561 status = swapctl (SC_LIST, s);
562 if (status < 0)
563 {
564 char errbuf[1024];
565 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
566 sstrerror (errno, errbuf, sizeof (errbuf)));
567 sfree (s_paths);
568 sfree (s);
569 return (-1);
570 }
571 else if (swap_num < status)
572 {
573 /* more elements returned than requested */
574 ERROR ("swap plugin: I allocated memory for %i structure%s, "
575 "but swapctl(2) claims to have returned %i. "
576 "I'm confused and will give up.",
577 swap_num, (swap_num == 1) ? "" : "s",
578 status);
579 sfree (s_paths);
580 sfree (s);
581 return (-1);
582 }
583 else if (swap_num > status)
584 /* less elements returned than requested */
585 swap_num = status;
587 for (i = 0; i < swap_num; i++)
588 {
589 char path[PATH_MAX];
590 gauge_t this_total;
591 gauge_t this_avail;
593 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
594 continue;
596 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
597 this_avail = (gauge_t) (s->swt_ent[i].ste_free * pagesize);
599 /* Shortcut for the "combined" setting (default) */
600 if (!report_by_device)
601 {
602 avail += this_avail;
603 total += this_total;
604 continue;
605 }
607 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
608 escape_slashes (path, sizeof (path));
610 swap_submit_usage (path, this_total - this_avail, this_avail,
611 NULL, NAN);
612 } /* for (swap_num) */
614 if (total < avail)
615 {
616 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
617 total, avail);
618 sfree (s_paths);
619 sfree (s);
620 return (-1);
621 }
623 /* If the "separate" option was specified (report_by_device == 1), all
624 * values have already been dispatched from within the loop. */
625 if (!report_by_device)
626 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
628 sfree (s_paths);
629 sfree (s);
630 return (0);
631 } /* }}} int swap_read */
632 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
634 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
635 static int swap_read (void) /* {{{ */
636 {
637 struct swapent *swap_entries;
638 int swap_num;
639 int status;
640 int i;
642 gauge_t used = 0;
643 gauge_t total = 0;
645 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
646 if (swap_num < 0)
647 {
648 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
649 swap_num);
650 return (-1);
651 }
652 else if (swap_num == 0)
653 return (0);
655 swap_entries = calloc (swap_num, sizeof (*swap_entries));
656 if (swap_entries == NULL)
657 {
658 ERROR ("swap plugin: calloc failed.");
659 return (-1);
660 }
662 status = swapctl (SWAP_STATS, swap_entries, swap_num);
663 if (status != swap_num)
664 {
665 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
666 status);
667 sfree (swap_entries);
668 return (-1);
669 }
671 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
672 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
673 #else
674 # define C_SWAP_BLOCK_SIZE 512.0
675 #endif
677 /* TODO: Report per-device stats. The path name is available from
678 * swap_entries[i].se_path */
679 for (i = 0; i < swap_num; i++)
680 {
681 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
682 continue;
684 used += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
685 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
686 }
688 if (total < used)
689 {
690 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
691 total, used);
692 return (-1);
693 }
695 swap_submit_usage (NULL, used, total - used, NULL, NAN);
697 sfree (swap_entries);
698 return (0);
699 } /* }}} int swap_read */
700 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
702 #elif defined(VM_SWAPUSAGE)
703 static int swap_read (void) /* {{{ */
704 {
705 int mib[3];
706 size_t mib_len;
707 struct xsw_usage sw_usage;
708 size_t sw_usage_len;
710 mib_len = 2;
711 mib[0] = CTL_VM;
712 mib[1] = VM_SWAPUSAGE;
714 sw_usage_len = sizeof (struct xsw_usage);
716 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
717 return (-1);
719 /* The returned values are bytes. */
720 swap_submit_usage (NULL,
721 (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
722 NULL, NAN);
724 return (0);
725 } /* }}} int swap_read */
726 /* #endif VM_SWAPUSAGE */
728 #elif HAVE_LIBKVM_GETSWAPINFO
729 static int swap_read (void) /* {{{ */
730 {
731 struct kvm_swap data_s;
732 int status;
734 gauge_t used;
735 gauge_t total;
737 if (kvm_obj == NULL)
738 return (-1);
740 /* only one structure => only get the grand total, no details */
741 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
742 if (status == -1)
743 return (-1);
745 total = (gauge_t) data_s.ksw_total;
746 used = (gauge_t) data_s.ksw_used;
748 total *= (gauge_t) kvm_pagesize;
749 used *= (gauge_t) kvm_pagesize;
751 swap_submit_usage (NULL, used, total - used, NULL, NAN);
753 return (0);
754 } /* }}} int swap_read */
755 /* #endif HAVE_LIBKVM_GETSWAPINFO */
757 #elif HAVE_LIBSTATGRAB
758 static int swap_read (void) /* {{{ */
759 {
760 sg_swap_stats *swap;
762 swap = sg_get_swap_stats ();
763 if (swap == NULL)
764 return (-1);
766 swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
767 NULL, NAN);
769 return (0);
770 } /* }}} int swap_read */
771 /* #endif HAVE_LIBSTATGRAB */
773 #elif HAVE_PERFSTAT
774 static int swap_read (void) /* {{{ */
775 {
776 perfstat_memory_total_t pmemory;
777 int status;
779 gauge_t total;
780 gauge_t free;
781 gauge_t reserved;
783 memset (&pmemory, 0, sizeof (pmemory));
784 status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
785 if (status < 0)
786 {
787 char errbuf[1024];
788 WARNING ("swap plugin: perfstat_memory_total failed: %s",
789 sstrerror (errno, errbuf, sizeof (errbuf)));
790 return (-1);
791 }
793 total = (gauge_t) (pmemory.pgsp_total * pagesize);
794 free = (gauge_t) (pmemory.pgsp_free * pagesize);
795 reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
797 swap_submit_usage (NULL, total - free, free, "reserved", reserved);
798 swap_submit_derive (NULL, "in", (derive_t) pmemory.pgspins * pagesize);
799 swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
801 return (0);
802 } /* }}} int swap_read */
803 #endif /* HAVE_PERFSTAT */
805 void module_register (void)
806 {
807 plugin_register_complex_config ("swap", swap_config);
808 plugin_register_init ("swap", swap_init);
809 plugin_register_read ("swap", swap_read);
810 } /* void module_register */
812 /* vim: set fdm=marker : */