1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005-2010 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 /* No global variables */
74 /* #endif KERNEL_LINUX */
76 #elif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS)
77 static derive_t pagesize;
78 # if HAVE_LIBKSTAT
79 static kstat_t *ksp;
80 # endif /* HAVE_LIBKSTAT */
82 # if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
83 static const char *config_keys[] =
84 {
85 # if HAVE_LIBKSTAT
86 "ReportVirtual",
87 # endif
88 "ReportPhysical"
89 };
90 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
92 # if HAVE_LIBKSTAT
93 static _Bool report_virtual = 0;
94 # endif
95 static int report_physical = 1;
96 # endif /* HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
97 /* #endif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS) */
99 #elif defined(VM_SWAPUSAGE)
100 /* No global variables */
101 /* #endif defined(VM_SWAPUSAGE) */
103 #elif HAVE_LIBKVM_GETSWAPINFO
104 static kvm_t *kvm_obj = NULL;
105 int kvm_pagesize;
106 /* #endif HAVE_LIBKVM_GETSWAPINFO */
108 #elif HAVE_LIBSTATGRAB
109 /* No global variables */
110 /* #endif HAVE_LIBSTATGRAB */
112 #elif HAVE_PERFSTAT
113 static int pagesize;
114 static perfstat_memory_total_t pmemory;
115 /*# endif HAVE_PERFSTAT */
117 #else
118 # error "No applicable input method."
119 #endif /* HAVE_LIBSTATGRAB */
121 static int swap_init (void)
122 {
123 #if KERNEL_LINUX
124 /* No init stuff */
125 /* #endif KERNEL_LINUX */
127 #elif HAVE_LIBKSTAT
128 /* getpagesize(3C) tells me this does not fail.. */
129 pagesize = (derive_t) getpagesize ();
130 if (get_kstat (&ksp, "unix", 0, "system_pages"))
131 ksp = NULL;
132 /* #endif HAVE_LIBKSTAT */
134 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
135 /* getpagesize(3C) tells me this does not fail.. */
136 pagesize = (derive_t) getpagesize ();
137 /* #endif HAVE_SWAPCTL */
139 #elif defined(VM_SWAPUSAGE)
140 /* No init stuff */
141 /* #endif defined(VM_SWAPUSAGE) */
143 #elif HAVE_LIBKVM_GETSWAPINFO
144 if (kvm_obj != NULL)
145 {
146 kvm_close (kvm_obj);
147 kvm_obj = NULL;
148 }
150 kvm_pagesize = getpagesize ();
152 if ((kvm_obj = kvm_open (NULL, /* execfile */
153 NULL, /* corefile */
154 NULL, /* swapfile */
155 O_RDONLY, /* flags */
156 NULL)) /* errstr */
157 == NULL)
158 {
159 ERROR ("swap plugin: kvm_open failed.");
160 return (-1);
161 }
162 /* #endif HAVE_LIBKVM_GETSWAPINFO */
164 #elif HAVE_LIBSTATGRAB
165 /* No init stuff */
166 /* #endif HAVE_LIBSTATGRAB */
168 #elif HAVE_PERFSTAT
169 pagesize = getpagesize();
170 #endif /* HAVE_PERFSTAT */
172 return (0);
173 }
175 static void swap_submit_inst (const char *plugin_instance, /* {{{ */
176 const char *type_instance, derive_t value, unsigned type)
177 {
178 value_t values[1];
179 value_list_t vl = VALUE_LIST_INIT;
181 switch (type)
182 {
183 case DS_TYPE_GAUGE:
184 values[0].gauge = (gauge_t) value;
185 sstrncpy (vl.type, "swap", sizeof (vl.type));
186 break;
187 case DS_TYPE_DERIVE:
188 values[0].derive = value;
189 sstrncpy (vl.type, "swap_io", sizeof (vl.type));
190 break;
191 default:
192 ERROR ("swap plugin: swap_submit called with wrong"
193 " type");
194 }
196 vl.values = values;
197 vl.values_len = 1;
198 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
199 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
200 if (plugin_instance != NULL)
201 sstrncpy (vl.plugin_instance, plugin_instance, sizeof (vl.plugin_instance));
202 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
204 plugin_dispatch_values (&vl);
205 } /* }}} void swap_submit_inst */
207 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
208 {
209 swap_submit_inst (/* plugin instance = */ NULL,
210 type_instance, value, type);
211 }
213 #if KERNEL_LINUX
214 static int swap_read (void) /* {{{ */
215 {
216 FILE *fh;
217 char buffer[1024];
219 char *fields[8];
220 int numfields;
222 _Bool old_kernel=0;
224 derive_t swap_used = 0;
225 derive_t swap_cached = 0;
226 derive_t swap_free = 0;
227 derive_t swap_total = 0;
228 derive_t swap_in = 0;
229 derive_t swap_out = 0;
231 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
232 {
233 char errbuf[1024];
234 WARNING ("memory: fopen: %s",
235 sstrerror (errno, errbuf, sizeof (errbuf)));
236 return (-1);
237 }
239 while (fgets (buffer, sizeof (buffer), fh) != NULL)
240 {
241 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
242 if (numfields < 2)
243 continue;
245 if (strcasecmp (fields[0], "SwapTotal:") == 0)
246 strtoderive (fields[1], &swap_total);
247 else if (strcasecmp (fields[0], "SwapFree:") == 0)
248 strtoderive (fields[1], &swap_free);
249 else if (strcasecmp (fields[0], "SwapCached:") == 0)
250 strtoderive (fields[1], &swap_cached);
251 }
253 if (fclose (fh))
254 {
255 char errbuf[1024];
256 WARNING ("memory: fclose: %s",
257 sstrerror (errno, errbuf, sizeof (errbuf)));
258 }
260 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
261 return (-1);
263 swap_used = swap_total - (swap_free + swap_cached);
265 if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
266 {
267 // /proc/vmstat does not exist in kernels <2.6
268 if ((fh = fopen ("/proc/stat", "r")) == NULL )
269 {
270 char errbuf[1024];
271 WARNING ("swap: fopen: %s",
272 sstrerror (errno, errbuf, sizeof (errbuf)));
273 return (-1);
274 }
275 else
276 old_kernel = 1;
277 }
279 while (fgets (buffer, sizeof (buffer), fh) != NULL)
280 {
281 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
283 if (!old_kernel)
284 {
285 if (numfields != 2)
286 continue;
288 if (strcasecmp ("pswpin", fields[0]) == 0)
289 strtoderive (fields[1], &swap_in);
290 else if (strcasecmp ("pswpout", fields[0]) == 0)
291 strtoderive (fields[1], &swap_out);
292 }
293 else /* if (old_kernel) */
294 {
295 if (numfields != 3)
296 continue;
298 if (strcasecmp ("page", fields[0]) == 0)
299 {
300 strtoderive (fields[1], &swap_in);
301 strtoderive (fields[2], &swap_out);
302 }
303 }
304 } /* while (fgets) */
306 if (fclose (fh))
307 {
308 char errbuf[1024];
309 WARNING ("swap: fclose: %s",
310 sstrerror (errno, errbuf, sizeof (errbuf)));
311 }
313 swap_submit ("used", 1024 * swap_used, DS_TYPE_GAUGE);
314 swap_submit ("free", 1024 * swap_free, DS_TYPE_GAUGE);
315 swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE);
316 swap_submit ("in", swap_in, DS_TYPE_DERIVE);
317 swap_submit ("out", swap_out, DS_TYPE_DERIVE);
319 return (0);
320 } /* }}} int swap_read */
321 /* #endif KERNEL_LINUX */
323 /* Sorry for the amount of preprocessor magic that follows. Under Solaris, two
324 * mechanisms can be used to read swap statistics, swapctl and kstat. The
325 * former reads physical space used on a device, the latter reports the view
326 * from the virtual memory system. The following magic tries to be as flexible
327 * as possible by allowing either mechanism to be missing and act correctly in
328 * the event that both are available, i.e. let the user decide what to do. */
329 #elif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS)
330 # if HAVE_LIBKSTAT
331 /* kstat-based read function */
332 static int swap_read_kstat (void) /* {{{ */
333 {
334 derive_t swap_alloc;
335 derive_t swap_resv;
336 derive_t swap_avail;
338 struct anoninfo ai;
340 if (swapctl (SC_AINFO, &ai) == -1)
341 {
342 char errbuf[1024];
343 ERROR ("swap plugin: swapctl failed: %s",
344 sstrerror (errno, errbuf, sizeof (errbuf)));
345 return (-1);
346 }
348 /*
349 * Calculations from:
350 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
351 * Also see:
352 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
353 * /usr/include/vm/anon.h
354 *
355 * In short, swap -s shows: allocated + reserved = used, available
356 *
357 * However, Solaris does not allow to allocated/reserved more than the
358 * available swap (physical memory + disk swap), so the pedant may
359 * prefer: allocated + unallocated = reserved, available
360 *
361 * We map the above to: used + resv = n/a, free
362 *
363 * Does your brain hurt yet? - Christophe Kalt
364 *
365 * Oh, and in case you wonder,
366 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
367 * can suffer from a 32bit overflow.
368 */
369 swap_alloc = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
370 swap_resv = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
371 * pagesize);
372 swap_avail = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
374 swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
375 swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
376 swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
378 return (0);
379 } /* }}} int swap_read_kstat */
380 # endif /* HAVE_LIBKSTAT */
382 # if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
383 /* swapctl-based read function */
384 static int swap_read_swapctl2 (void) /* {{{ */
385 {
386 swaptbl_t *s;
387 char *s_paths;
388 int swap_num;
389 int status;
390 int i;
392 derive_t avail = 0;
393 derive_t total = 0;
395 swap_num = swapctl (SC_GETNSWP, NULL);
396 if (swap_num < 0)
397 {
398 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
399 swap_num);
400 return (-1);
401 }
402 else if (swap_num == 0)
403 return (0);
405 /* Allocate and initialize the swaptbl_t structure */
406 s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
407 if (s == NULL)
408 {
409 ERROR ("swap plugin: smalloc failed.");
410 return (-1);
411 }
413 /* Memory to store the path names. We only use these paths when the
414 * separate option has been configured, but it's easier to just
415 * allocate enough memory in any case. */
416 s_paths = calloc (swap_num, PATH_MAX);
417 if (s_paths == NULL)
418 {
419 ERROR ("swap plugin: malloc failed.");
420 sfree (s);
421 return (-1);
422 }
423 for (i = 0; i < swap_num; i++)
424 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
425 s->swt_n = swap_num;
427 status = swapctl (SC_LIST, s);
428 if (status < 0)
429 {
430 char errbuf[1024];
431 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
432 sstrerror (errno, errbuf, sizeof (errbuf)));
433 sfree (s_paths);
434 sfree (s);
435 return (-1);
436 }
437 else if (swap_num < status)
438 {
439 /* more elements returned than requested */
440 ERROR ("swap plugin: I allocated memory for %i structure%s, "
441 "but swapctl(2) claims to have returned %i. "
442 "I'm confused and will give up.",
443 swap_num, (swap_num == 1) ? "" : "s",
444 status);
445 sfree (s_paths);
446 sfree (s);
447 return (-1);
448 }
449 else if (swap_num > status)
450 /* less elements returned than requested */
451 swap_num = status;
453 for (i = 0; i < swap_num; i++)
454 {
455 char path[PATH_MAX];
456 derive_t this_total;
457 derive_t this_avail;
459 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
460 continue;
462 this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
463 this_avail = ((derive_t) s->swt_ent[i].ste_free) * pagesize;
465 /* Shortcut for the "combined" setting (default) */
466 if (report_physical != 2)
467 {
468 avail += this_avail;
469 total += this_total;
470 continue;
471 }
473 /* Okay, using "/" as swap device would be super-weird, but
474 * we'll handle it anyway to cover all cases. */
475 if (strcmp ("/", s->swt_ent[i].ste_path) == 0)
476 sstrncpy (path, "root", sizeof (path));
477 else
478 {
479 int j;
481 s->swt_ent[i].ste_path[PATH_MAX - 1] = 0;
482 /* Don't copy the leading slash */
483 sstrncpy (path, &s->swt_ent[i].ste_path[1], sizeof (path));
484 /* Convert slashes to dashes, just like the "df" plugin. */
485 for (j = 0; path[j] != 0; j++)
486 if (path[j] == '/')
487 path[j] = '-';
488 }
490 swap_submit_inst (path, "used", this_total - this_avail, DS_TYPE_GAUGE);
491 swap_submit_inst (path, "free", this_avail, DS_TYPE_GAUGE);
492 } /* for (swap_num) */
494 if (total < avail)
495 {
496 ERROR ("swap plugin: Total swap space (%"PRIi64") "
497 "is less than free swap space (%"PRIi64").",
498 total, avail);
499 sfree (s_paths);
500 sfree (s);
501 return (-1);
502 }
504 /* If the "separate" option was specified (report_physical == 2), all
505 * values have already been dispatched from within the loop. */
506 if (report_physical != 2)
507 {
508 swap_submit ("used", total - avail, DS_TYPE_GAUGE);
509 swap_submit ("free", avail, DS_TYPE_GAUGE);
510 }
512 sfree (s_paths);
513 sfree (s);
514 return (0);
515 } /* }}} int swap_read_swapctl2 */
517 /* Configuration: Present when swapctl or both methods are available. */
518 static int swap_config (const char *key, const char *value) /* {{{ */
519 {
520 if (strcasecmp ("ReportPhysical", key) == 0)
521 {
522 if (strcasecmp ("combined", value) == 0)
523 report_physical = 1;
524 else if (strcasecmp ("separate", value) == 0)
525 report_physical = 2;
526 # if HAVE_LIBKSTAT
527 else if (IS_TRUE (value))
528 report_physical = 1;
529 else if (IS_FALSE (value))
530 report_physical = 0;
531 # endif
532 else
533 WARNING ("swap plugin: The value \"%s\" is not "
534 "recognized by the ReportPhysical option.",
535 value);
536 }
537 # if HAVE_LIBKSTAT
538 else if (strcasecmp ("ReportVirtual", key) == 0)
539 {
540 if (IS_TRUE (value))
541 report_physical = 1;
542 else if (IS_FALSE (value))
543 report_physical = 0;
544 else
545 WARNING ("swap plugin: The value \"%s\" is not "
546 "recognized by the ReportVirtual option.",
547 value);
548 }
549 # endif /* HAVE_LIBKSTAT */
550 else
551 {
552 return (-1);
553 }
555 return (0);
556 } /* }}} int swap_config */
557 # endif /* HAVE_SWAPCTL_TWO_ARGS */
559 /* If both methods are available, check the config variables to decide which
560 * function to call. Otherwise, add aliases for the functions so we can
561 * "swap_read" in "module_register". */
562 # if HAVE_LIBKSTAT && HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
563 static int swap_read (void) /* {{{ */
564 {
565 int status;
567 if (report_physical)
568 {
569 status = swap_read_swapctl2 ();
570 if (status != 0)
571 return (status);
572 }
574 if (report_virtual)
575 {
576 status = swap_read_kstat ();
577 if (status != 0)
578 return (status);
579 }
581 return (0);
582 } /* }}} int swap_read */
583 # elif HAVE_LIBKSTAT
584 # define swap_read swap_read_kstat
585 # else /* if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
586 # define swap_read swap_read_swapctl2
587 # endif
588 /* #endif HAVE_LIBKSTAT || (HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS) */
590 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
591 static int swap_read (void) /* {{{ */
592 {
593 struct swapent *swap_entries;
594 int swap_num;
595 int status;
596 int i;
598 derive_t used = 0;
599 derive_t total = 0;
601 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
602 if (swap_num < 0)
603 {
604 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
605 swap_num);
606 return (-1);
607 }
608 else if (swap_num == 0)
609 return (0);
611 swap_entries = calloc (swap_num, sizeof (*swap_entries));
612 if (swap_entries == NULL)
613 {
614 ERROR ("swap plugin: calloc failed.");
615 return (-1);
616 }
618 status = swapctl (SWAP_STATS, swap_entries, swap_num);
619 if (status != swap_num)
620 {
621 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
622 status);
623 sfree (swap_entries);
624 return (-1);
625 }
627 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
628 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
629 #else
630 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
631 #endif
633 for (i = 0; i < swap_num; i++)
634 {
635 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
636 continue;
638 used += ((derive_t) swap_entries[i].se_inuse)
639 * C_SWAP_BLOCK_SIZE;
640 total += ((derive_t) swap_entries[i].se_nblks)
641 * C_SWAP_BLOCK_SIZE;
642 }
644 if (total < used)
645 {
646 ERROR ("swap plugin: Total swap space (%"PRIu64") "
647 "is less than used swap space (%"PRIu64").",
648 total, used);
649 return (-1);
650 }
652 swap_submit ("used", used, DS_TYPE_GAUGE);
653 swap_submit ("free", total - used, DS_TYPE_GAUGE);
655 sfree (swap_entries);
657 return (0);
658 } /* }}} int swap_read */
659 /* #endif HAVE_SWAPCTL */
661 #elif defined(VM_SWAPUSAGE)
662 static int swap_read (void) /* {{{ */
663 {
664 int mib[3];
665 size_t mib_len;
666 struct xsw_usage sw_usage;
667 size_t sw_usage_len;
669 mib_len = 2;
670 mib[0] = CTL_VM;
671 mib[1] = VM_SWAPUSAGE;
673 sw_usage_len = sizeof (struct xsw_usage);
675 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
676 return (-1);
678 /* The returned values are bytes. */
679 swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
680 swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
682 return (0);
683 } /* }}} int swap_read */
684 /* #endif VM_SWAPUSAGE */
686 #elif HAVE_LIBKVM_GETSWAPINFO
687 static int swap_read (void) /* {{{ */
688 {
689 struct kvm_swap data_s;
690 int status;
692 derive_t used;
693 derive_t free;
694 derive_t total;
696 if (kvm_obj == NULL)
697 return (-1);
699 /* only one structure => only get the grand total, no details */
700 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
701 if (status == -1)
702 return (-1);
704 total = (derive_t) data_s.ksw_total;
705 used = (derive_t) data_s.ksw_used;
707 total *= (derive_t) kvm_pagesize;
708 used *= (derive_t) kvm_pagesize;
710 free = total - used;
712 swap_submit ("used", used, DS_TYPE_GAUGE);
713 swap_submit ("free", free, DS_TYPE_GAUGE);
715 return (0);
716 } /* }}} int swap_read */
717 /* #endif HAVE_LIBKVM_GETSWAPINFO */
719 #elif HAVE_LIBSTATGRAB
720 static int swap_read (void) /* {{{ */
721 {
722 sg_swap_stats *swap;
724 swap = sg_get_swap_stats ();
726 if (swap == NULL)
727 return (-1);
729 swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
730 swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
732 return (0);
733 } /* }}} int swap_read */
734 /* #endif HAVE_LIBSTATGRAB */
736 #elif HAVE_PERFSTAT
737 static int swap_read (void) /* {{{ */
738 {
739 if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
740 {
741 char errbuf[1024];
742 WARNING ("memory plugin: perfstat_memory_total failed: %s",
743 sstrerror (errno, errbuf, sizeof (errbuf)));
744 return (-1);
745 }
746 swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
747 swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
749 return (0);
750 } /* }}} int swap_read */
751 #endif /* HAVE_PERFSTAT */
753 void module_register (void)
754 {
755 #if HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
756 plugin_register_config ("swap", swap_config, config_keys, config_keys_num);
757 #endif
758 plugin_register_init ("swap", swap_init);
759 plugin_register_read ("swap", swap_read);
760 } /* void module_register */
762 /* vim: set fdm=marker : */