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"
39 #include "common.h"
40 #include "plugin.h"
42 #if HAVE_SYS_SWAP_H
43 # include <sys/swap.h>
44 #endif
45 #if HAVE_VM_ANON_H
46 # include <vm/anon.h>
47 #endif
48 #if HAVE_SYS_PARAM_H
49 # include <sys/param.h>
50 #endif
51 #if HAVE_SYS_SYSCTL_H
52 # include <sys/sysctl.h>
53 #endif
54 #if HAVE_SYS_DKSTAT_H
55 # include <sys/dkstat.h>
56 #endif
57 #if HAVE_KVM_H
58 # include <kvm.h>
59 #endif
61 #if HAVE_STATGRAB_H
62 # include <statgrab.h>
63 #endif
65 #if HAVE_PERFSTAT
66 # include <sys/protosw.h>
67 # include <libperfstat.h>
68 #endif
70 #undef MAX
71 #define MAX(x,y) ((x) > (y) ? (x) : (y))
73 #if KERNEL_LINUX
74 # define SWAP_HAVE_REPORT_BY_DEVICE 1
75 static derive_t pagesize;
76 static _Bool report_bytes = 0;
77 static _Bool report_by_device = 0;
78 /* #endif KERNEL_LINUX */
80 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
81 # define SWAP_HAVE_REPORT_BY_DEVICE 1
82 static derive_t pagesize;
83 static _Bool report_by_device = 0;
84 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
86 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
87 /* No global variables */
88 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
90 #elif defined(VM_SWAPUSAGE)
91 /* No global variables */
92 /* #endif defined(VM_SWAPUSAGE) */
94 #elif HAVE_LIBKVM_GETSWAPINFO
95 static kvm_t *kvm_obj = NULL;
96 int kvm_pagesize;
97 /* #endif HAVE_LIBKVM_GETSWAPINFO */
99 #elif HAVE_LIBSTATGRAB
100 /* No global variables */
101 /* #endif HAVE_LIBSTATGRAB */
103 #elif HAVE_PERFSTAT
104 static int pagesize;
105 /*# endif HAVE_PERFSTAT */
107 #else
108 # error "No applicable input method."
109 #endif /* HAVE_LIBSTATGRAB */
111 static _Bool values_absolute = 1;
112 static _Bool values_percentage = 0;
114 static int swap_config (oconfig_item_t *ci) /* {{{ */
115 {
116 for (int i = 0; i < ci->children_num; i++)
117 {
118 oconfig_item_t *child = ci->children + i;
119 if (strcasecmp ("ReportBytes", child->key) == 0)
120 #if KERNEL_LINUX
121 cf_util_get_boolean (child, &report_bytes);
122 #else
123 WARNING ("swap plugin: The \"ReportBytes\" option "
124 "is only valid under Linux. "
125 "The option is going to be ignored.");
126 #endif
127 else if (strcasecmp ("ReportByDevice", child->key) == 0)
128 #if SWAP_HAVE_REPORT_BY_DEVICE
129 cf_util_get_boolean (child, &report_by_device);
130 #else
131 WARNING ("swap plugin: The \"ReportByDevice\" option "
132 "is not supported on this platform. "
133 "The option is going to be ignored.");
134 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
135 else if (strcasecmp ("ValuesAbsolute", child->key) == 0)
136 cf_util_get_boolean (child, &values_absolute);
137 else if (strcasecmp ("ValuesPercentage", child->key) == 0)
138 cf_util_get_boolean (child, &values_percentage);
139 else
140 WARNING ("swap plugin: Unknown config option: \"%s\"",
141 child->key);
142 }
144 return (0);
145 } /* }}} int swap_config */
147 static int swap_init (void) /* {{{ */
148 {
149 #if KERNEL_LINUX
150 pagesize = (derive_t) sysconf (_SC_PAGESIZE);
151 /* #endif KERNEL_LINUX */
153 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
154 /* getpagesize(3C) tells me this does not fail.. */
155 pagesize = (derive_t) getpagesize ();
156 /* #endif HAVE_SWAPCTL */
158 #elif defined(VM_SWAPUSAGE)
159 /* No init stuff */
160 /* #endif defined(VM_SWAPUSAGE) */
162 #elif HAVE_LIBKVM_GETSWAPINFO
163 char errbuf[_POSIX2_LINE_MAX];
165 if (kvm_obj != NULL)
166 {
167 kvm_close (kvm_obj);
168 kvm_obj = NULL;
169 }
171 kvm_pagesize = getpagesize ();
173 kvm_obj = kvm_openfiles (NULL, "/dev/null", NULL, O_RDONLY, errbuf);
175 if (kvm_obj == NULL)
176 {
177 ERROR ("swap plugin: kvm_openfiles failed, %s", errbuf);
178 return (-1);
179 }
180 /* #endif HAVE_LIBKVM_GETSWAPINFO */
182 #elif HAVE_LIBSTATGRAB
183 /* No init stuff */
184 /* #endif HAVE_LIBSTATGRAB */
186 #elif HAVE_PERFSTAT
187 pagesize = getpagesize();
188 #endif /* HAVE_PERFSTAT */
190 return (0);
191 } /* }}} int swap_init */
193 static void swap_submit_usage (char const *plugin_instance, /* {{{ */
194 gauge_t used, gauge_t free,
195 char const *other_name, gauge_t other_value)
196 {
197 value_list_t vl = VALUE_LIST_INIT;
199 vl.values = &(value_t) { .gauge = NAN };
200 vl.values_len = 1;
201 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
202 if (plugin_instance != NULL)
203 sstrncpy (vl.plugin_instance, plugin_instance,
204 sizeof (vl.plugin_instance));
205 sstrncpy (vl.type, "swap", sizeof (vl.type));
207 if (values_absolute)
208 plugin_dispatch_multivalue (&vl, 0, DS_TYPE_GAUGE,
209 "used", used, "free", free,
210 other_name, other_value, NULL);
211 if (values_percentage)
212 plugin_dispatch_multivalue (&vl, 1, DS_TYPE_GAUGE,
213 "used", used, "free", free,
214 other_name, other_value, NULL);
215 } /* }}} void swap_submit_usage */
217 #if KERNEL_LINUX || HAVE_PERFSTAT
218 __attribute__((nonnull(1)))
219 static void swap_submit_derive (char const *type_instance, /* {{{ */
220 derive_t value)
221 {
222 value_list_t vl = VALUE_LIST_INIT;
224 vl.values = &(value_t) { .derive = value };
225 vl.values_len = 1;
226 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
227 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
228 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
230 plugin_dispatch_values (&vl);
231 } /* }}} void swap_submit_derive */
232 #endif
234 #if KERNEL_LINUX
235 static int swap_read_separate (void) /* {{{ */
236 {
237 FILE *fh;
238 char buffer[1024];
240 fh = fopen ("/proc/swaps", "r");
241 if (fh == NULL)
242 {
243 char errbuf[1024];
244 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
245 sstrerror (errno, errbuf, sizeof (errbuf)));
246 return (-1);
247 }
249 while (fgets (buffer, sizeof (buffer), fh) != NULL)
250 {
251 char *fields[8];
252 int numfields;
253 char *endptr;
255 char path[PATH_MAX];
256 gauge_t total;
257 gauge_t used;
259 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
260 if (numfields != 5)
261 continue;
263 sstrncpy (path, fields[0], sizeof (path));
264 escape_slashes (path, sizeof (path));
266 errno = 0;
267 endptr = NULL;
268 total = strtod (fields[2], &endptr);
269 if ((endptr == fields[2]) || (errno != 0))
270 continue;
272 errno = 0;
273 endptr = NULL;
274 used = strtod (fields[3], &endptr);
275 if ((endptr == fields[3]) || (errno != 0))
276 continue;
278 if (total < used)
279 continue;
281 swap_submit_usage (path, used * 1024.0, (total - used) * 1024.0,
282 NULL, NAN);
283 }
285 fclose (fh);
287 return (0);
288 } /* }}} int swap_read_separate */
290 static int swap_read_combined (void) /* {{{ */
291 {
292 FILE *fh;
293 char buffer[1024];
295 gauge_t swap_used = NAN;
296 gauge_t swap_cached = NAN;
297 gauge_t swap_free = NAN;
298 gauge_t swap_total = NAN;
300 fh = fopen ("/proc/meminfo", "r");
301 if (fh == NULL)
302 {
303 char errbuf[1024];
304 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
305 sstrerror (errno, errbuf, sizeof (errbuf)));
306 return (-1);
307 }
309 while (fgets (buffer, sizeof (buffer), fh) != NULL)
310 {
311 char *fields[8];
312 int numfields;
314 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
315 if (numfields < 2)
316 continue;
318 if (strcasecmp (fields[0], "SwapTotal:") == 0)
319 strtogauge (fields[1], &swap_total);
320 else if (strcasecmp (fields[0], "SwapFree:") == 0)
321 strtogauge (fields[1], &swap_free);
322 else if (strcasecmp (fields[0], "SwapCached:") == 0)
323 strtogauge (fields[1], &swap_cached);
324 }
326 fclose (fh);
328 if (isnan (swap_total) || isnan (swap_free))
329 return (ENOENT);
331 /* Some systems, OpenVZ for example, don't provide SwapCached. */
332 if (isnan (swap_cached))
333 swap_used = swap_total - swap_free;
334 else
335 swap_used = swap_total - (swap_free + swap_cached);
336 assert (!isnan (swap_used));
338 if (swap_used < 0.0)
339 return (EINVAL);
341 swap_submit_usage (NULL, swap_used * 1024.0, swap_free * 1024.0,
342 isnan (swap_cached) ? NULL : "cached",
343 isnan (swap_cached) ? NAN : swap_cached * 1024.0);
344 return (0);
345 } /* }}} int swap_read_combined */
347 static int swap_read_io (void) /* {{{ */
348 {
349 FILE *fh;
350 char buffer[1024];
352 _Bool old_kernel = 0;
354 uint8_t have_data = 0;
355 derive_t swap_in = 0;
356 derive_t swap_out = 0;
358 fh = fopen ("/proc/vmstat", "r");
359 if (fh == NULL)
360 {
361 /* /proc/vmstat does not exist in kernels <2.6 */
362 fh = fopen ("/proc/stat", "r");
363 if (fh == NULL)
364 {
365 char errbuf[1024];
366 WARNING ("swap: fopen: %s",
367 sstrerror (errno, errbuf, sizeof (errbuf)));
368 return (-1);
369 }
370 else
371 old_kernel = 1;
372 }
374 while (fgets (buffer, sizeof (buffer), fh) != NULL)
375 {
376 char *fields[8];
377 int numfields;
379 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
381 if (!old_kernel)
382 {
383 if (numfields != 2)
384 continue;
386 if (strcasecmp ("pswpin", fields[0]) == 0)
387 {
388 strtoderive (fields[1], &swap_in);
389 have_data |= 0x01;
390 }
391 else if (strcasecmp ("pswpout", fields[0]) == 0)
392 {
393 strtoderive (fields[1], &swap_out);
394 have_data |= 0x02;
395 }
396 }
397 else /* if (old_kernel) */
398 {
399 if (numfields != 3)
400 continue;
402 if (strcasecmp ("page", fields[0]) == 0)
403 {
404 strtoderive (fields[1], &swap_in);
405 strtoderive (fields[2], &swap_out);
406 }
407 }
408 } /* while (fgets) */
410 fclose (fh);
412 if (have_data != 0x03)
413 return (ENOENT);
415 if (report_bytes)
416 {
417 swap_in = swap_in * pagesize;
418 swap_out = swap_out * pagesize;
419 }
421 swap_submit_derive ("in", swap_in);
422 swap_submit_derive ("out", swap_out);
424 return (0);
425 } /* }}} int swap_read_io */
427 static int swap_read (void) /* {{{ */
428 {
429 if (report_by_device)
430 swap_read_separate ();
431 else
432 swap_read_combined ();
434 swap_read_io ();
436 return (0);
437 } /* }}} int swap_read */
438 /* #endif KERNEL_LINUX */
440 /*
441 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
442 * and kstat. The former reads physical space used on a device, the latter
443 * reports the view from the virtual memory system. It was decided that the
444 * kstat-based information should be moved to the "vmem" plugin, but nobody
445 * with enough Solaris experience was available at that time to do this. The
446 * code below is still there for your reference but it won't be activated in
447 * *this* plugin again. --octo
448 */
449 #elif 0 && HAVE_LIBKSTAT
450 /* kstat-based read function */
451 static int swap_read_kstat (void) /* {{{ */
452 {
453 gauge_t swap_alloc;
454 gauge_t swap_resv;
455 gauge_t swap_avail;
457 struct anoninfo ai;
459 if (swapctl (SC_AINFO, &ai) == -1)
460 {
461 char errbuf[1024];
462 ERROR ("swap plugin: swapctl failed: %s",
463 sstrerror (errno, errbuf, sizeof (errbuf)));
464 return (-1);
465 }
467 /*
468 * Calculations from:
469 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
470 * Also see:
471 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
472 * /usr/include/vm/anon.h
473 *
474 * In short, swap -s shows: allocated + reserved = used, available
475 *
476 * However, Solaris does not allow to allocated/reserved more than the
477 * available swap (physical memory + disk swap), so the pedant may
478 * prefer: allocated + unallocated = reserved, available
479 *
480 * We map the above to: used + resv = n/a, free
481 *
482 * Does your brain hurt yet? - Christophe Kalt
483 *
484 * Oh, and in case you wonder,
485 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
486 * can suffer from a 32bit overflow.
487 */
488 swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
489 swap_resv = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
490 swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
492 swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
493 return (0);
494 } /* }}} int swap_read_kstat */
495 /* #endif 0 && HAVE_LIBKSTAT */
497 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
498 /* swapctl-based read function */
499 static int swap_read (void) /* {{{ */
500 {
501 swaptbl_t *s;
502 char *s_paths;
503 int swap_num;
504 int status;
506 gauge_t avail = 0;
507 gauge_t total = 0;
509 swap_num = swapctl (SC_GETNSWP, NULL);
510 if (swap_num < 0)
511 {
512 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
513 swap_num);
514 return (-1);
515 }
516 else if (swap_num == 0)
517 return (0);
519 /* Allocate and initialize the swaptbl_t structure */
520 s = malloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
521 if (s == NULL)
522 {
523 ERROR ("swap plugin: malloc failed.");
524 return (-1);
525 }
527 /* Memory to store the path names. We only use these paths when the
528 * separate option has been configured, but it's easier to just
529 * allocate enough memory in any case. */
530 s_paths = calloc (swap_num, PATH_MAX);
531 if (s_paths == NULL)
532 {
533 ERROR ("swap plugin: calloc failed.");
534 sfree (s);
535 return (-1);
536 }
537 for (int i = 0; i < swap_num; i++)
538 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
539 s->swt_n = swap_num;
541 status = swapctl (SC_LIST, s);
542 if (status < 0)
543 {
544 char errbuf[1024];
545 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
546 sstrerror (errno, errbuf, sizeof (errbuf)));
547 sfree (s_paths);
548 sfree (s);
549 return (-1);
550 }
551 else if (swap_num < status)
552 {
553 /* more elements returned than requested */
554 ERROR ("swap plugin: I allocated memory for %i structure%s, "
555 "but swapctl(2) claims to have returned %i. "
556 "I'm confused and will give up.",
557 swap_num, (swap_num == 1) ? "" : "s",
558 status);
559 sfree (s_paths);
560 sfree (s);
561 return (-1);
562 }
563 else if (swap_num > status)
564 /* less elements returned than requested */
565 swap_num = status;
567 for (int i = 0; i < swap_num; i++)
568 {
569 char path[PATH_MAX];
570 gauge_t this_total;
571 gauge_t this_avail;
573 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
574 continue;
576 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
577 this_avail = (gauge_t) (s->swt_ent[i].ste_free * pagesize);
579 /* Shortcut for the "combined" setting (default) */
580 if (!report_by_device)
581 {
582 avail += this_avail;
583 total += this_total;
584 continue;
585 }
587 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
588 escape_slashes (path, sizeof (path));
590 swap_submit_usage (path, this_total - this_avail, this_avail,
591 NULL, NAN);
592 } /* for (swap_num) */
594 if (total < avail)
595 {
596 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
597 total, avail);
598 sfree (s_paths);
599 sfree (s);
600 return (-1);
601 }
603 /* If the "separate" option was specified (report_by_device == 1), all
604 * values have already been dispatched from within the loop. */
605 if (!report_by_device)
606 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
608 sfree (s_paths);
609 sfree (s);
610 return (0);
611 } /* }}} int swap_read */
612 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
614 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
615 static int swap_read (void) /* {{{ */
616 {
617 struct swapent *swap_entries;
618 int swap_num;
619 int status;
621 gauge_t used = 0;
622 gauge_t total = 0;
624 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
625 if (swap_num < 0)
626 {
627 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
628 swap_num);
629 return (-1);
630 }
631 else if (swap_num == 0)
632 return (0);
634 swap_entries = calloc (swap_num, sizeof (*swap_entries));
635 if (swap_entries == NULL)
636 {
637 ERROR ("swap plugin: calloc failed.");
638 return (-1);
639 }
641 status = swapctl (SWAP_STATS, swap_entries, swap_num);
642 if (status != swap_num)
643 {
644 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
645 status);
646 sfree (swap_entries);
647 return (-1);
648 }
650 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
651 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
652 #else
653 # define C_SWAP_BLOCK_SIZE 512.0
654 #endif
656 /* TODO: Report per-device stats. The path name is available from
657 * swap_entries[i].se_path */
658 for (int i = 0; i < swap_num; i++)
659 {
660 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
661 continue;
663 used += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
664 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
665 }
667 if (total < used)
668 {
669 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
670 total, used);
671 sfree (swap_entries);
672 return (-1);
673 }
675 swap_submit_usage (NULL, used, total - used, NULL, NAN);
677 sfree (swap_entries);
678 return (0);
679 } /* }}} int swap_read */
680 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
682 #elif defined(VM_SWAPUSAGE)
683 static int swap_read (void) /* {{{ */
684 {
685 int mib[3];
686 size_t mib_len;
687 struct xsw_usage sw_usage;
688 size_t sw_usage_len;
690 mib_len = 2;
691 mib[0] = CTL_VM;
692 mib[1] = VM_SWAPUSAGE;
694 sw_usage_len = sizeof (struct xsw_usage);
696 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
697 return (-1);
699 /* The returned values are bytes. */
700 swap_submit_usage (NULL,
701 (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
702 NULL, NAN);
704 return (0);
705 } /* }}} int swap_read */
706 /* #endif VM_SWAPUSAGE */
708 #elif HAVE_LIBKVM_GETSWAPINFO
709 static int swap_read (void) /* {{{ */
710 {
711 struct kvm_swap data_s;
712 int status;
714 gauge_t used;
715 gauge_t total;
717 if (kvm_obj == NULL)
718 return (-1);
720 /* only one structure => only get the grand total, no details */
721 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
722 if (status == -1)
723 return (-1);
725 total = (gauge_t) data_s.ksw_total;
726 used = (gauge_t) data_s.ksw_used;
728 total *= (gauge_t) kvm_pagesize;
729 used *= (gauge_t) kvm_pagesize;
731 swap_submit_usage (NULL, used, total - used, NULL, NAN);
733 return (0);
734 } /* }}} int swap_read */
735 /* #endif HAVE_LIBKVM_GETSWAPINFO */
737 #elif HAVE_LIBSTATGRAB
738 static int swap_read (void) /* {{{ */
739 {
740 sg_swap_stats *swap;
742 swap = sg_get_swap_stats ();
743 if (swap == NULL)
744 return (-1);
746 swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
747 NULL, NAN);
749 return (0);
750 } /* }}} int swap_read */
751 /* #endif HAVE_LIBSTATGRAB */
753 #elif HAVE_PERFSTAT
754 static int swap_read (void) /* {{{ */
755 {
756 perfstat_memory_total_t pmemory = { 0 };
757 int status;
759 gauge_t total;
760 gauge_t free;
761 gauge_t reserved;
763 status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
764 if (status < 0)
765 {
766 char errbuf[1024];
767 WARNING ("swap plugin: perfstat_memory_total failed: %s",
768 sstrerror (errno, errbuf, sizeof (errbuf)));
769 return (-1);
770 }
772 total = (gauge_t) (pmemory.pgsp_total * pagesize);
773 free = (gauge_t) (pmemory.pgsp_free * pagesize);
774 reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
776 swap_submit_usage (NULL, total - free, free, "reserved", reserved);
777 swap_submit_derive ("in", (derive_t) pmemory.pgspins * pagesize);
778 swap_submit_derive ("out", (derive_t) pmemory.pgspouts * pagesize);
780 return (0);
781 } /* }}} int swap_read */
782 #endif /* HAVE_PERFSTAT */
784 void module_register (void)
785 {
786 plugin_register_complex_config ("swap", swap_config);
787 plugin_register_init ("swap", swap_init);
788 plugin_register_read ("swap", swap_read);
789 } /* void module_register */
791 /* vim: set fdm=marker : */