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 char errbuf[_POSIX2_LINE_MAX];
162 if (kvm_obj != NULL)
163 {
164 kvm_close (kvm_obj);
165 kvm_obj = NULL;
166 }
168 kvm_pagesize = getpagesize ();
170 kvm_obj = kvm_openfiles (NULL, "/dev/null", NULL, O_RDONLY, errbuf);
172 if (kvm_obj == NULL)
173 {
174 ERROR ("swap plugin: kvm_openfiles failed, %s", errbuf);
175 return (-1);
176 }
177 /* #endif HAVE_LIBKVM_GETSWAPINFO */
179 #elif HAVE_LIBSTATGRAB
180 /* No init stuff */
181 /* #endif HAVE_LIBSTATGRAB */
183 #elif HAVE_PERFSTAT
184 pagesize = getpagesize();
185 #endif /* HAVE_PERFSTAT */
187 return (0);
188 } /* }}} int swap_init */
190 static void swap_submit_usage (char const *plugin_instance, /* {{{ */
191 gauge_t used, gauge_t free,
192 char const *other_name, gauge_t other_value)
193 {
194 value_t v[1];
195 value_list_t vl = VALUE_LIST_INIT;
197 vl.values = v;
198 vl.values_len = STATIC_ARRAY_SIZE (v);
199 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
200 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
201 if (plugin_instance != NULL)
202 sstrncpy (vl.plugin_instance, plugin_instance,
203 sizeof (vl.plugin_instance));
204 sstrncpy (vl.type, "swap", sizeof (vl.type));
206 if (values_absolute)
207 plugin_dispatch_multivalue (&vl, 0, DS_TYPE_GAUGE,
208 "used", used, "free", free,
209 other_name, other_value, NULL);
210 if (values_percentage)
211 plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE,
212 "used", used, "free", free,
213 other_name, other_value, NULL);
214 } /* }}} void swap_submit_usage */
216 #if KERNEL_LINUX || HAVE_PERFSTAT
217 __attribute__((nonnull(1)))
218 static void swap_submit_derive (char const *type_instance, /* {{{ */
219 derive_t value)
220 {
221 value_list_t vl = VALUE_LIST_INIT;
222 value_t v[1];
224 v[0].derive = value;
226 vl.values = v;
227 vl.values_len = STATIC_ARRAY_SIZE (v);
228 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
229 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
230 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
231 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
233 plugin_dispatch_values (&vl);
234 } /* }}} void swap_submit_derive */
235 #endif
237 #if KERNEL_LINUX
238 static int swap_read_separate (void) /* {{{ */
239 {
240 FILE *fh;
241 char buffer[1024];
243 fh = fopen ("/proc/swaps", "r");
244 if (fh == NULL)
245 {
246 char errbuf[1024];
247 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
248 sstrerror (errno, errbuf, sizeof (errbuf)));
249 return (-1);
250 }
252 while (fgets (buffer, sizeof (buffer), fh) != NULL)
253 {
254 char *fields[8];
255 int numfields;
256 char *endptr;
258 char path[PATH_MAX];
259 gauge_t total;
260 gauge_t used;
262 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
263 if (numfields != 5)
264 continue;
266 sstrncpy (path, fields[0], sizeof (path));
267 escape_slashes (path, sizeof (path));
269 errno = 0;
270 endptr = NULL;
271 total = strtod (fields[2], &endptr);
272 if ((endptr == fields[2]) || (errno != 0))
273 continue;
275 errno = 0;
276 endptr = NULL;
277 used = strtod (fields[3], &endptr);
278 if ((endptr == fields[3]) || (errno != 0))
279 continue;
281 if (total < used)
282 continue;
284 swap_submit_usage (path, used * 1024.0, (total - used) * 1024.0,
285 NULL, NAN);
286 }
288 fclose (fh);
290 return (0);
291 } /* }}} int swap_read_separate */
293 static int swap_read_combined (void) /* {{{ */
294 {
295 FILE *fh;
296 char buffer[1024];
298 gauge_t swap_used = NAN;
299 gauge_t swap_cached = NAN;
300 gauge_t swap_free = NAN;
301 gauge_t swap_total = NAN;
303 fh = fopen ("/proc/meminfo", "r");
304 if (fh == NULL)
305 {
306 char errbuf[1024];
307 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
308 sstrerror (errno, errbuf, sizeof (errbuf)));
309 return (-1);
310 }
312 while (fgets (buffer, sizeof (buffer), fh) != NULL)
313 {
314 char *fields[8];
315 int numfields;
317 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
318 if (numfields < 2)
319 continue;
321 if (strcasecmp (fields[0], "SwapTotal:") == 0)
322 strtogauge (fields[1], &swap_total);
323 else if (strcasecmp (fields[0], "SwapFree:") == 0)
324 strtogauge (fields[1], &swap_free);
325 else if (strcasecmp (fields[0], "SwapCached:") == 0)
326 strtogauge (fields[1], &swap_cached);
327 }
329 fclose (fh);
331 if (isnan (swap_total) || isnan (swap_free))
332 return (ENOENT);
334 /* Some systems, OpenVZ for example, don't provide SwapCached. */
335 if (isnan (swap_cached))
336 swap_used = swap_total - swap_free;
337 else
338 swap_used = swap_total - (swap_free + swap_cached);
339 assert (!isnan (swap_used));
341 if (swap_used < 0.0)
342 return (EINVAL);
344 swap_submit_usage (NULL, swap_used * 1024.0, swap_free * 1024.0,
345 isnan (swap_cached) ? NULL : "cached",
346 isnan (swap_cached) ? NAN : swap_cached * 1024.0);
347 return (0);
348 } /* }}} int swap_read_combined */
350 static int swap_read_io (void) /* {{{ */
351 {
352 FILE *fh;
353 char buffer[1024];
355 _Bool old_kernel = 0;
357 uint8_t have_data = 0;
358 derive_t swap_in = 0;
359 derive_t swap_out = 0;
361 fh = fopen ("/proc/vmstat", "r");
362 if (fh == NULL)
363 {
364 /* /proc/vmstat does not exist in kernels <2.6 */
365 fh = fopen ("/proc/stat", "r");
366 if (fh == NULL)
367 {
368 char errbuf[1024];
369 WARNING ("swap: fopen: %s",
370 sstrerror (errno, errbuf, sizeof (errbuf)));
371 return (-1);
372 }
373 else
374 old_kernel = 1;
375 }
377 while (fgets (buffer, sizeof (buffer), fh) != NULL)
378 {
379 char *fields[8];
380 int numfields;
382 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
384 if (!old_kernel)
385 {
386 if (numfields != 2)
387 continue;
389 if (strcasecmp ("pswpin", fields[0]) == 0)
390 {
391 strtoderive (fields[1], &swap_in);
392 have_data |= 0x01;
393 }
394 else if (strcasecmp ("pswpout", fields[0]) == 0)
395 {
396 strtoderive (fields[1], &swap_out);
397 have_data |= 0x02;
398 }
399 }
400 else /* if (old_kernel) */
401 {
402 if (numfields != 3)
403 continue;
405 if (strcasecmp ("page", fields[0]) == 0)
406 {
407 strtoderive (fields[1], &swap_in);
408 strtoderive (fields[2], &swap_out);
409 }
410 }
411 } /* while (fgets) */
413 fclose (fh);
415 if (have_data != 0x03)
416 return (ENOENT);
418 if (report_bytes)
419 {
420 swap_in = swap_in * pagesize;
421 swap_out = swap_out * pagesize;
422 }
424 swap_submit_derive ("in", swap_in);
425 swap_submit_derive ("out", swap_out);
427 return (0);
428 } /* }}} int swap_read_io */
430 static int swap_read (void) /* {{{ */
431 {
432 if (report_by_device)
433 swap_read_separate ();
434 else
435 swap_read_combined ();
437 swap_read_io ();
439 return (0);
440 } /* }}} int swap_read */
441 /* #endif KERNEL_LINUX */
443 /*
444 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
445 * and kstat. The former reads physical space used on a device, the latter
446 * reports the view from the virtual memory system. It was decided that the
447 * kstat-based information should be moved to the "vmem" plugin, but nobody
448 * with enough Solaris experience was available at that time to do this. The
449 * code below is still there for your reference but it won't be activated in
450 * *this* plugin again. --octo
451 */
452 #elif 0 && HAVE_LIBKSTAT
453 /* kstat-based read function */
454 static int swap_read_kstat (void) /* {{{ */
455 {
456 gauge_t swap_alloc;
457 gauge_t swap_resv;
458 gauge_t swap_avail;
460 struct anoninfo ai;
462 if (swapctl (SC_AINFO, &ai) == -1)
463 {
464 char errbuf[1024];
465 ERROR ("swap plugin: swapctl failed: %s",
466 sstrerror (errno, errbuf, sizeof (errbuf)));
467 return (-1);
468 }
470 /*
471 * Calculations from:
472 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
473 * Also see:
474 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
475 * /usr/include/vm/anon.h
476 *
477 * In short, swap -s shows: allocated + reserved = used, available
478 *
479 * However, Solaris does not allow to allocated/reserved more than the
480 * available swap (physical memory + disk swap), so the pedant may
481 * prefer: allocated + unallocated = reserved, available
482 *
483 * We map the above to: used + resv = n/a, free
484 *
485 * Does your brain hurt yet? - Christophe Kalt
486 *
487 * Oh, and in case you wonder,
488 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
489 * can suffer from a 32bit overflow.
490 */
491 swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
492 swap_resv = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
493 swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
495 swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
496 return (0);
497 } /* }}} int swap_read_kstat */
498 /* #endif 0 && HAVE_LIBKSTAT */
500 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
501 /* swapctl-based read function */
502 static int swap_read (void) /* {{{ */
503 {
504 swaptbl_t *s;
505 char *s_paths;
506 int swap_num;
507 int status;
508 int i;
510 gauge_t avail = 0;
511 gauge_t total = 0;
513 swap_num = swapctl (SC_GETNSWP, NULL);
514 if (swap_num < 0)
515 {
516 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
517 swap_num);
518 return (-1);
519 }
520 else if (swap_num == 0)
521 return (0);
523 /* Allocate and initialize the swaptbl_t structure */
524 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
525 if (s == NULL)
526 {
527 ERROR ("swap plugin: smalloc failed.");
528 return (-1);
529 }
531 /* Memory to store the path names. We only use these paths when the
532 * separate option has been configured, but it's easier to just
533 * allocate enough memory in any case. */
534 s_paths = calloc (swap_num, PATH_MAX);
535 if (s_paths == NULL)
536 {
537 ERROR ("swap plugin: malloc failed.");
538 sfree (s);
539 return (-1);
540 }
541 for (i = 0; i < swap_num; i++)
542 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
543 s->swt_n = swap_num;
545 status = swapctl (SC_LIST, s);
546 if (status < 0)
547 {
548 char errbuf[1024];
549 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
550 sstrerror (errno, errbuf, sizeof (errbuf)));
551 sfree (s_paths);
552 sfree (s);
553 return (-1);
554 }
555 else if (swap_num < status)
556 {
557 /* more elements returned than requested */
558 ERROR ("swap plugin: I allocated memory for %i structure%s, "
559 "but swapctl(2) claims to have returned %i. "
560 "I'm confused and will give up.",
561 swap_num, (swap_num == 1) ? "" : "s",
562 status);
563 sfree (s_paths);
564 sfree (s);
565 return (-1);
566 }
567 else if (swap_num > status)
568 /* less elements returned than requested */
569 swap_num = status;
571 for (i = 0; i < swap_num; i++)
572 {
573 char path[PATH_MAX];
574 gauge_t this_total;
575 gauge_t this_avail;
577 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
578 continue;
580 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
581 this_avail = (gauge_t) (s->swt_ent[i].ste_free * pagesize);
583 /* Shortcut for the "combined" setting (default) */
584 if (!report_by_device)
585 {
586 avail += this_avail;
587 total += this_total;
588 continue;
589 }
591 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
592 escape_slashes (path, sizeof (path));
594 swap_submit_usage (path, this_total - this_avail, this_avail,
595 NULL, NAN);
596 } /* for (swap_num) */
598 if (total < avail)
599 {
600 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
601 total, avail);
602 sfree (s_paths);
603 sfree (s);
604 return (-1);
605 }
607 /* If the "separate" option was specified (report_by_device == 1), all
608 * values have already been dispatched from within the loop. */
609 if (!report_by_device)
610 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
612 sfree (s_paths);
613 sfree (s);
614 return (0);
615 } /* }}} int swap_read */
616 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
618 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
619 static int swap_read (void) /* {{{ */
620 {
621 struct swapent *swap_entries;
622 int swap_num;
623 int status;
624 int i;
626 gauge_t used = 0;
627 gauge_t total = 0;
629 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
630 if (swap_num < 0)
631 {
632 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
633 swap_num);
634 return (-1);
635 }
636 else if (swap_num == 0)
637 return (0);
639 swap_entries = calloc (swap_num, sizeof (*swap_entries));
640 if (swap_entries == NULL)
641 {
642 ERROR ("swap plugin: calloc failed.");
643 return (-1);
644 }
646 status = swapctl (SWAP_STATS, swap_entries, swap_num);
647 if (status != swap_num)
648 {
649 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
650 status);
651 sfree (swap_entries);
652 return (-1);
653 }
655 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
656 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
657 #else
658 # define C_SWAP_BLOCK_SIZE 512.0
659 #endif
661 /* TODO: Report per-device stats. The path name is available from
662 * swap_entries[i].se_path */
663 for (i = 0; i < swap_num; i++)
664 {
665 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
666 continue;
668 used += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
669 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
670 }
672 if (total < used)
673 {
674 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
675 total, used);
676 return (-1);
677 }
679 swap_submit_usage (NULL, used, total - used, NULL, NAN);
681 sfree (swap_entries);
682 return (0);
683 } /* }}} int swap_read */
684 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
686 #elif defined(VM_SWAPUSAGE)
687 static int swap_read (void) /* {{{ */
688 {
689 int mib[3];
690 size_t mib_len;
691 struct xsw_usage sw_usage;
692 size_t sw_usage_len;
694 mib_len = 2;
695 mib[0] = CTL_VM;
696 mib[1] = VM_SWAPUSAGE;
698 sw_usage_len = sizeof (struct xsw_usage);
700 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
701 return (-1);
703 /* The returned values are bytes. */
704 swap_submit_usage (NULL,
705 (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
706 NULL, NAN);
708 return (0);
709 } /* }}} int swap_read */
710 /* #endif VM_SWAPUSAGE */
712 #elif HAVE_LIBKVM_GETSWAPINFO
713 static int swap_read (void) /* {{{ */
714 {
715 struct kvm_swap data_s;
716 int status;
718 gauge_t used;
719 gauge_t total;
721 if (kvm_obj == NULL)
722 return (-1);
724 /* only one structure => only get the grand total, no details */
725 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
726 if (status == -1)
727 return (-1);
729 total = (gauge_t) data_s.ksw_total;
730 used = (gauge_t) data_s.ksw_used;
732 total *= (gauge_t) kvm_pagesize;
733 used *= (gauge_t) kvm_pagesize;
735 swap_submit_usage (NULL, used, total - used, NULL, NAN);
737 return (0);
738 } /* }}} int swap_read */
739 /* #endif HAVE_LIBKVM_GETSWAPINFO */
741 #elif HAVE_LIBSTATGRAB
742 static int swap_read (void) /* {{{ */
743 {
744 sg_swap_stats *swap;
746 swap = sg_get_swap_stats ();
747 if (swap == NULL)
748 return (-1);
750 swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
751 NULL, NAN);
753 return (0);
754 } /* }}} int swap_read */
755 /* #endif HAVE_LIBSTATGRAB */
757 #elif HAVE_PERFSTAT
758 static int swap_read (void) /* {{{ */
759 {
760 perfstat_memory_total_t pmemory;
761 int status;
763 gauge_t total;
764 gauge_t free;
765 gauge_t reserved;
767 memset (&pmemory, 0, sizeof (pmemory));
768 status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
769 if (status < 0)
770 {
771 char errbuf[1024];
772 WARNING ("swap plugin: perfstat_memory_total failed: %s",
773 sstrerror (errno, errbuf, sizeof (errbuf)));
774 return (-1);
775 }
777 total = (gauge_t) (pmemory.pgsp_total * pagesize);
778 free = (gauge_t) (pmemory.pgsp_free * pagesize);
779 reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
781 swap_submit_usage (NULL, total - free, free, "reserved", reserved);
782 swap_submit_derive ("in", (derive_t) pmemory.pgspins * pagesize);
783 swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
785 return (0);
786 } /* }}} int swap_read */
787 #endif /* HAVE_PERFSTAT */
789 void module_register (void)
790 {
791 plugin_register_complex_config ("swap", swap_config);
792 plugin_register_init ("swap", swap_init);
793 plugin_register_read ("swap", swap_read);
794 } /* void module_register */
796 /* vim: set fdm=marker : */