1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005-2012 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 static perfstat_memory_total_t pmemory;
101 /*# endif HAVE_PERFSTAT */
103 #else
104 # error "No applicable input method."
105 #endif /* HAVE_LIBSTATGRAB */
107 static const char *config_keys[] =
108 {
109 "ReportBytes",
110 "ReportByDevice"
111 };
112 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
114 static int swap_config (const char *key, const char *value) /* {{{ */
115 {
116 if (strcasecmp ("ReportBytes", key) == 0)
117 {
118 #if KERNEL_LINUX
119 report_bytes = IS_TRUE (value) ? 1 : 0;
120 #else
121 WARNING ("swap plugin: The \"ReportBytes\" option is only "
122 "valid under Linux. "
123 "The option is going to be ignored.");
124 #endif
125 }
126 else if (strcasecmp ("ReportByDevice", key) == 0)
127 {
128 #if SWAP_HAVE_REPORT_BY_DEVICE
129 if (IS_TRUE (value))
130 report_by_device = 1;
131 else
132 report_by_device = 0;
133 #else
134 WARNING ("swap plugin: The \"ReportByDevice\" option is not "
135 "supported on this platform. "
136 "The option is going to be ignored.");
137 #endif /* SWAP_HAVE_REPORT_BY_DEVICE */
138 }
139 else
140 {
141 return (-1);
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 if (kvm_obj != NULL)
164 {
165 kvm_close (kvm_obj);
166 kvm_obj = NULL;
167 }
169 kvm_pagesize = getpagesize ();
171 if ((kvm_obj = kvm_open (NULL, /* execfile */
172 NULL, /* corefile */
173 NULL, /* swapfile */
174 O_RDONLY, /* flags */
175 NULL)) /* errstr */
176 == NULL)
177 {
178 ERROR ("swap plugin: kvm_open failed.");
179 return (-1);
180 }
181 /* #endif HAVE_LIBKVM_GETSWAPINFO */
183 #elif HAVE_LIBSTATGRAB
184 /* No init stuff */
185 /* #endif HAVE_LIBSTATGRAB */
187 #elif HAVE_PERFSTAT
188 pagesize = getpagesize();
189 #endif /* HAVE_PERFSTAT */
191 return (0);
192 } /* }}} int swap_init */
194 static void swap_submit (const char *plugin_instance, /* {{{ */
195 const char *type, const char *type_instance,
196 value_t value)
197 {
198 value_list_t vl = VALUE_LIST_INIT;
200 assert (type != NULL);
202 vl.values = &value;
203 vl.values_len = 1;
204 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
205 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
206 if (plugin_instance != NULL)
207 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
208 sstrncpy (vl.type, type, sizeof (vl.type));
209 if (type_instance != NULL)
210 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
212 plugin_dispatch_values (&vl);
213 } /* }}} void swap_submit_inst */
215 static void swap_submit_gauge (const char *plugin_instance, /* {{{ */
216 const char *type_instance, gauge_t value)
217 {
218 value_t v;
220 v.gauge = value;
221 swap_submit (plugin_instance, "swap", type_instance, v);
222 } /* }}} void swap_submit_gauge */
224 #if KERNEL_LINUX || HAVE_PERFSTAT
225 static void swap_submit_derive (const char *plugin_instance, /* {{{ */
226 const char *type_instance, derive_t value)
227 {
228 value_t v;
230 v.derive = value;
231 swap_submit (plugin_instance, "swap_io", type_instance, v);
232 } /* }}} void swap_submit_derive */
233 #endif
235 #if KERNEL_LINUX
236 static int swap_read_separate (void) /* {{{ */
237 {
238 FILE *fh;
239 char buffer[1024];
241 fh = fopen ("/proc/swaps", "r");
242 if (fh == NULL)
243 {
244 char errbuf[1024];
245 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
246 sstrerror (errno, errbuf, sizeof (errbuf)));
247 return (-1);
248 }
250 while (fgets (buffer, sizeof (buffer), fh) != NULL)
251 {
252 char *fields[8];
253 int numfields;
254 char *endptr;
256 char path[PATH_MAX];
257 gauge_t size;
258 gauge_t used;
259 gauge_t free;
261 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
262 if (numfields != 5)
263 continue;
265 sstrncpy (path, fields[0], sizeof (path));
266 escape_slashes (path, sizeof (path));
268 errno = 0;
269 endptr = NULL;
270 size = strtod (fields[2], &endptr);
271 if ((endptr == fields[2]) || (errno != 0))
272 continue;
274 errno = 0;
275 endptr = NULL;
276 used = strtod (fields[3], &endptr);
277 if ((endptr == fields[3]) || (errno != 0))
278 continue;
280 if (size < used)
281 continue;
283 free = size - used;
285 swap_submit_gauge (path, "used", used);
286 swap_submit_gauge (path, "free", free);
287 }
289 fclose (fh);
291 return (0);
292 } /* }}} int swap_read_separate */
294 static int swap_read_combined (void) /* {{{ */
295 {
296 FILE *fh;
297 char buffer[1024];
299 uint8_t have_data = 0;
300 gauge_t swap_used = 0.0;
301 gauge_t swap_cached = 0.0;
302 gauge_t swap_free = 0.0;
303 gauge_t swap_total = 0.0;
305 fh = fopen ("/proc/meminfo", "r");
306 if (fh == NULL)
307 {
308 char errbuf[1024];
309 WARNING ("swap plugin: fopen (/proc/meminfo) failed: %s",
310 sstrerror (errno, errbuf, sizeof (errbuf)));
311 return (-1);
312 }
314 while (fgets (buffer, sizeof (buffer), fh) != NULL)
315 {
316 char *fields[8];
317 int numfields;
319 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
320 if (numfields < 2)
321 continue;
323 if (strcasecmp (fields[0], "SwapTotal:") == 0)
324 {
325 swap_total = strtod (fields[1], /* endptr = */ NULL);
326 have_data |= 0x01;
327 }
328 else if (strcasecmp (fields[0], "SwapFree:") == 0)
329 {
330 swap_free = strtod (fields[1], /* endptr = */ NULL);
331 have_data |= 0x02;
332 }
333 else if (strcasecmp (fields[0], "SwapCached:") == 0)
334 {
335 swap_cached = strtod (fields[1], /* endptr = */ NULL);
336 have_data |= 0x04;
337 }
338 }
340 fclose (fh);
342 if ((have_data & 0x03) != 0x03)
343 return (ENOENT);
345 if (isnan (swap_total)
346 || (swap_total <= 0.0)
347 || ((swap_free + swap_cached) > swap_total))
348 return (EINVAL);
350 swap_used = swap_total - (swap_free + swap_cached);
352 swap_submit_gauge (NULL, "used", 1024.0 * swap_used);
353 swap_submit_gauge (NULL, "free", 1024.0 * swap_free);
354 if (have_data & 0x04)
355 swap_submit_gauge (NULL, "cached", 1024.0 * swap_cached);
357 return (0);
358 } /* }}} int swap_read_combined */
360 static int swap_read_io (void) /* {{{ */
361 {
362 FILE *fh;
363 char buffer[1024];
365 _Bool old_kernel = 0;
367 uint8_t have_data = 0;
368 derive_t swap_in = 0;
369 derive_t swap_out = 0;
371 fh = fopen ("/proc/vmstat", "r");
372 if (fh == NULL)
373 {
374 /* /proc/vmstat does not exist in kernels <2.6 */
375 fh = fopen ("/proc/stat", "r");
376 if (fh == NULL)
377 {
378 char errbuf[1024];
379 WARNING ("swap: fopen: %s",
380 sstrerror (errno, errbuf, sizeof (errbuf)));
381 return (-1);
382 }
383 else
384 old_kernel = 1;
385 }
387 while (fgets (buffer, sizeof (buffer), fh) != NULL)
388 {
389 char *fields[8];
390 int numfields;
392 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
394 if (!old_kernel)
395 {
396 if (numfields != 2)
397 continue;
399 if (strcasecmp ("pswpin", fields[0]) == 0)
400 {
401 strtoderive (fields[1], &swap_in);
402 have_data |= 0x01;
403 }
404 else if (strcasecmp ("pswpout", fields[0]) == 0)
405 {
406 strtoderive (fields[1], &swap_out);
407 have_data |= 0x02;
408 }
409 }
410 else /* if (old_kernel) */
411 {
412 if (numfields != 3)
413 continue;
415 if (strcasecmp ("page", fields[0]) == 0)
416 {
417 strtoderive (fields[1], &swap_in);
418 strtoderive (fields[2], &swap_out);
419 }
420 }
421 } /* while (fgets) */
423 fclose (fh);
425 if (have_data != 0x03)
426 return (ENOENT);
428 if (report_bytes)
429 {
430 swap_in = swap_in * pagesize;
431 swap_out = swap_out * pagesize;
432 }
434 swap_submit_derive (NULL, "in", swap_in);
435 swap_submit_derive (NULL, "out", swap_out);
437 return (0);
438 } /* }}} int swap_read_io */
440 static int swap_read (void) /* {{{ */
441 {
442 if (report_by_device)
443 swap_read_separate ();
444 else
445 swap_read_combined ();
447 swap_read_io ();
449 return (0);
450 } /* }}} int swap_read */
451 /* #endif KERNEL_LINUX */
453 /*
454 * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
455 * and kstat. The former reads physical space used on a device, the latter
456 * reports the view from the virtual memory system. It was decided that the
457 * kstat-based information should be moved to the "vmem" plugin, but nobody
458 * with enough Solaris experience was available at that time to do this. The
459 * code below is still there for your reference but it won't be activated in
460 * *this* plugin again. --octo
461 */
462 #elif 0 && HAVE_LIBKSTAT
463 /* kstat-based read function */
464 static int swap_read_kstat (void) /* {{{ */
465 {
466 derive_t swap_alloc;
467 derive_t swap_resv;
468 derive_t swap_avail;
470 struct anoninfo ai;
472 if (swapctl (SC_AINFO, &ai) == -1)
473 {
474 char errbuf[1024];
475 ERROR ("swap plugin: swapctl failed: %s",
476 sstrerror (errno, errbuf, sizeof (errbuf)));
477 return (-1);
478 }
480 /*
481 * Calculations from:
482 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
483 * Also see:
484 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
485 * /usr/include/vm/anon.h
486 *
487 * In short, swap -s shows: allocated + reserved = used, available
488 *
489 * However, Solaris does not allow to allocated/reserved more than the
490 * available swap (physical memory + disk swap), so the pedant may
491 * prefer: allocated + unallocated = reserved, available
492 *
493 * We map the above to: used + resv = n/a, free
494 *
495 * Does your brain hurt yet? - Christophe Kalt
496 *
497 * Oh, and in case you wonder,
498 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
499 * can suffer from a 32bit overflow.
500 */
501 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
502 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
503 * pagesize);
504 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
506 swap_submit_gauge (NULL, "used", swap_alloc);
507 swap_submit_gauge (NULL, "free", swap_avail);
508 swap_submit_gauge (NULL, "reserved", swap_resv);
510 return (0);
511 } /* }}} int swap_read_kstat */
512 /* #endif 0 && HAVE_LIBKSTAT */
514 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
515 /* swapctl-based read function */
516 static int swap_read (void) /* {{{ */
517 {
518 swaptbl_t *s;
519 char *s_paths;
520 int swap_num;
521 int status;
522 int i;
524 derive_t avail = 0;
525 derive_t total = 0;
527 swap_num = swapctl (SC_GETNSWP, NULL);
528 if (swap_num < 0)
529 {
530 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
531 swap_num);
532 return (-1);
533 }
534 else if (swap_num == 0)
535 return (0);
537 /* Allocate and initialize the swaptbl_t structure */
538 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
539 if (s == NULL)
540 {
541 ERROR ("swap plugin: smalloc failed.");
542 return (-1);
543 }
545 /* Memory to store the path names. We only use these paths when the
546 * separate option has been configured, but it's easier to just
547 * allocate enough memory in any case. */
548 s_paths = calloc (swap_num, PATH_MAX);
549 if (s_paths == NULL)
550 {
551 ERROR ("swap plugin: malloc failed.");
552 sfree (s);
553 return (-1);
554 }
555 for (i = 0; i < swap_num; i++)
556 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
557 s->swt_n = swap_num;
559 status = swapctl (SC_LIST, s);
560 if (status < 0)
561 {
562 char errbuf[1024];
563 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
564 sstrerror (errno, errbuf, sizeof (errbuf)));
565 sfree (s_paths);
566 sfree (s);
567 return (-1);
568 }
569 else if (swap_num < status)
570 {
571 /* more elements returned than requested */
572 ERROR ("swap plugin: I allocated memory for %i structure%s, "
573 "but swapctl(2) claims to have returned %i. "
574 "I'm confused and will give up.",
575 swap_num, (swap_num == 1) ? "" : "s",
576 status);
577 sfree (s_paths);
578 sfree (s);
579 return (-1);
580 }
581 else if (swap_num > status)
582 /* less elements returned than requested */
583 swap_num = status;
585 for (i = 0; i < swap_num; i++)
586 {
587 char path[PATH_MAX];
588 derive_t this_total;
589 derive_t this_avail;
591 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
592 continue;
594 this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
595 this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize;
597 /* Shortcut for the "combined" setting (default) */
598 if (!report_by_device)
599 {
600 avail += this_avail;
601 total += this_total;
602 continue;
603 }
605 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
606 escape_slashes (path, sizeof (path));
608 swap_submit_gauge (path, "used", (gauge_t) (this_total - this_avail));
609 swap_submit_gauge (path, "free", (gauge_t) this_avail);
610 } /* for (swap_num) */
612 if (total < avail)
613 {
614 ERROR ("swap plugin: Total swap space (%"PRIi64") "
615 "is less than free swap space (%"PRIi64").",
616 total, avail);
617 sfree (s_paths);
618 sfree (s);
619 return (-1);
620 }
622 /* If the "separate" option was specified (report_by_device == 2), all
623 * values have already been dispatched from within the loop. */
624 if (!report_by_device)
625 {
626 swap_submit_gauge (NULL, "used", (gauge_t) (total - avail));
627 swap_submit_gauge (NULL, "free", (gauge_t) avail);
628 }
630 sfree (s_paths);
631 sfree (s);
632 return (0);
633 } /* }}} int swap_read */
634 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
636 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
637 static int swap_read (void) /* {{{ */
638 {
639 struct swapent *swap_entries;
640 int swap_num;
641 int status;
642 int i;
644 derive_t used = 0;
645 derive_t total = 0;
647 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
648 if (swap_num < 0)
649 {
650 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
651 swap_num);
652 return (-1);
653 }
654 else if (swap_num == 0)
655 return (0);
657 swap_entries = calloc (swap_num, sizeof (*swap_entries));
658 if (swap_entries == NULL)
659 {
660 ERROR ("swap plugin: calloc failed.");
661 return (-1);
662 }
664 status = swapctl (SWAP_STATS, swap_entries, swap_num);
665 if (status != swap_num)
666 {
667 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
668 status);
669 sfree (swap_entries);
670 return (-1);
671 }
673 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
674 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
675 #else
676 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
677 #endif
679 for (i = 0; i < swap_num; i++)
680 {
681 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
682 continue;
684 used += ((derive_t) swap_entries[i].se_inuse)
685 * C_SWAP_BLOCK_SIZE;
686 total += ((derive_t) swap_entries[i].se_nblks)
687 * C_SWAP_BLOCK_SIZE;
688 }
690 if (total < used)
691 {
692 ERROR ("swap plugin: Total swap space (%"PRIu64") "
693 "is less than used swap space (%"PRIu64").",
694 total, used);
695 return (-1);
696 }
698 swap_submit_gauge (NULL, "used", (gauge_t) used);
699 swap_submit_gauge (NULL, "free", (gauge_t) (total - used));
701 sfree (swap_entries);
703 return (0);
704 } /* }}} int swap_read */
705 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
707 #elif defined(VM_SWAPUSAGE)
708 static int swap_read (void) /* {{{ */
709 {
710 int mib[3];
711 size_t mib_len;
712 struct xsw_usage sw_usage;
713 size_t sw_usage_len;
715 mib_len = 2;
716 mib[0] = CTL_VM;
717 mib[1] = VM_SWAPUSAGE;
719 sw_usage_len = sizeof (struct xsw_usage);
721 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
722 return (-1);
724 /* The returned values are bytes. */
725 swap_submit_gauge (NULL, "used", (gauge_t) sw_usage.xsu_used);
726 swap_submit_gauge (NULL, "free", (gauge_t) sw_usage.xsu_avail);
728 return (0);
729 } /* }}} int swap_read */
730 /* #endif VM_SWAPUSAGE */
732 #elif HAVE_LIBKVM_GETSWAPINFO
733 static int swap_read (void) /* {{{ */
734 {
735 struct kvm_swap data_s;
736 int status;
738 derive_t used;
739 derive_t free;
740 derive_t total;
742 if (kvm_obj == NULL)
743 return (-1);
745 /* only one structure => only get the grand total, no details */
746 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
747 if (status == -1)
748 return (-1);
750 total = (derive_t) data_s.ksw_total;
751 used = (derive_t) data_s.ksw_used;
753 total *= (derive_t) kvm_pagesize;
754 used *= (derive_t) kvm_pagesize;
756 free = total - used;
758 swap_submit_gauge (NULL, "used", (gauge_t) used);
759 swap_submit_gauge (NULL, "free", (gauge_t) free);
761 return (0);
762 } /* }}} int swap_read */
763 /* #endif HAVE_LIBKVM_GETSWAPINFO */
765 #elif HAVE_LIBSTATGRAB
766 static int swap_read (void) /* {{{ */
767 {
768 sg_swap_stats *swap;
770 swap = sg_get_swap_stats ();
772 if (swap == NULL)
773 return (-1);
775 swap_submit_gauge (NULL, "used", (gauge_t) swap->used);
776 swap_submit_gauge (NULL, "free", (gauge_t) swap->free);
778 return (0);
779 } /* }}} int swap_read */
780 /* #endif HAVE_LIBSTATGRAB */
782 #elif HAVE_PERFSTAT
783 static int swap_read (void) /* {{{ */
784 {
785 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
786 {
787 char errbuf[1024];
788 WARNING ("memory plugin: perfstat_memory_total failed: %s",
789 sstrerror (errno, errbuf, sizeof (errbuf)));
790 return (-1);
791 }
793 swap_submit_gauge (NULL, "used", (gauge_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize);
794 swap_submit_gauge (NULL, "free", (gauge_t) pmemory.pgsp_free * pagesize );
795 swap_submit_gauge (NULL, "reserved", (gauge_t) pmemory.pgsp_rsvd * pagesize);
796 swap_submit_derive (NULL, "in", (derive_t) pmemory.pgspins * pagesize);
797 swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
799 return (0);
800 } /* }}} int swap_read */
801 #endif /* HAVE_PERFSTAT */
803 void module_register (void)
804 {
805 plugin_register_config ("swap", swap_config,
806 config_keys, config_keys_num);
807 plugin_register_init ("swap", swap_init);
808 plugin_register_read ("swap", swap_read);
809 } /* void module_register */
811 /* vim: set fdm=marker : */