swap plugin: Add the "ReportBytes" option.
[collectd.git] / src / swap.c
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  **/
26
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
36
37 #include "collectd.h"
38 #include "common.h"
39 #include "plugin.h"
40
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
59
60 #if HAVE_STATGRAB_H
61 # include <statgrab.h>
62 #endif
63
64 #if HAVE_PERFSTAT
65 # include <sys/protosw.h>
66 # include <libperfstat.h>
67 #endif
68
69 #undef  MAX
70 #define MAX(x,y) ((x) > (y) ? (x) : (y))
71
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 */
78
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 */
84
85 #elif defined(VM_SWAPUSAGE)
86 /* No global variables */
87 /* #endif defined(VM_SWAPUSAGE) */
88
89 #elif HAVE_LIBKVM_GETSWAPINFO
90 static kvm_t *kvm_obj = NULL;
91 int kvm_pagesize;
92 /* #endif HAVE_LIBKVM_GETSWAPINFO */
93
94 #elif HAVE_LIBSTATGRAB
95 /* No global variables */
96 /* #endif HAVE_LIBSTATGRAB */
97
98 #elif HAVE_PERFSTAT
99 static int pagesize;
100 static perfstat_memory_total_t pmemory;
101 /*# endif HAVE_PERFSTAT */
102
103 #else
104 # error "No applicable input method."
105 #endif /* HAVE_LIBSTATGRAB */
106
107 static const char *config_keys[] =
108 {
109         "ReportBytes",
110         "ReportByDevice"
111 };
112 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
113
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         }
143
144         return (0);
145 } /* }}} int swap_config */
146
147 static int swap_init (void) /* {{{ */
148 {
149 #if KERNEL_LINUX
150         pagesize = (derive_t) sysconf (_SC_PAGESIZE);
151 /* #endif KERNEL_LINUX */
152
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 */
157
158 #elif defined(VM_SWAPUSAGE)
159         /* No init stuff */
160 /* #endif defined(VM_SWAPUSAGE) */
161
162 #elif HAVE_LIBKVM_GETSWAPINFO
163         if (kvm_obj != NULL)
164         {
165                 kvm_close (kvm_obj);
166                 kvm_obj = NULL;
167         }
168
169         kvm_pagesize = getpagesize ();
170
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 */
182
183 #elif HAVE_LIBSTATGRAB
184         /* No init stuff */
185 /* #endif HAVE_LIBSTATGRAB */
186
187 #elif HAVE_PERFSTAT
188         pagesize = getpagesize();
189 #endif /* HAVE_PERFSTAT */
190
191         return (0);
192 } /* }}} int swap_init */
193
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;
199
200         assert (type != NULL);
201
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));
211
212         plugin_dispatch_values (&vl);
213 } /* }}} void swap_submit_inst */
214
215 static void swap_submit_gauge (const char *plugin_instance, /* {{{ */
216                 const char *type_instance, gauge_t value)
217 {
218         value_t v;
219
220         v.gauge = value;
221         swap_submit (plugin_instance, "swap", type_instance, v);
222 } /* }}} void swap_submit_gauge */
223
224 #if KERNEL_LINUX
225 static void swap_submit_derive (const char *plugin_instance, /* {{{ */
226                 const char *type_instance, derive_t value)
227 {
228         value_t v;
229
230         v.derive = value;
231         swap_submit (plugin_instance, "swap_io", type_instance, v);
232 } /* }}} void swap_submit_derive */
233
234 static int swap_read_separate (void) /* {{{ */
235 {
236         FILE *fh;
237         char buffer[1024];
238
239         fh = fopen ("/proc/swaps", "r");
240         if (fh == NULL)
241         {
242                 char errbuf[1024];
243                 WARNING ("swap plugin: fopen (/proc/swaps) failed: %s",
244                                 sstrerror (errno, errbuf, sizeof (errbuf)));
245                 return (-1);
246         }
247
248         while (fgets (buffer, sizeof (buffer), fh) != NULL)
249         {
250                 char *fields[8];
251                 int numfields;
252                 char *endptr;
253
254                 char path[PATH_MAX];
255                 gauge_t size;
256                 gauge_t used;
257                 gauge_t free;
258
259                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
260                 if (numfields != 5)
261                         continue;
262
263                 sstrncpy (path, fields[0], sizeof (path));
264                 escape_slashes (path, sizeof (path));
265
266                 errno = 0;
267                 endptr = NULL;
268                 size = strtod (fields[2], &endptr);
269                 if ((endptr == fields[2]) || (errno != 0))
270                         continue;
271
272                 errno = 0;
273                 endptr = NULL;
274                 used = strtod (fields[3], &endptr);
275                 if ((endptr == fields[3]) || (errno != 0))
276                         continue;
277
278                 if (size < used)
279                         continue;
280
281                 free = size - used;
282
283                 swap_submit_gauge (path, "used", used);
284                 swap_submit_gauge (path, "free", free);
285         }
286
287         fclose (fh);
288
289         return (0);
290 } /* }}} int swap_read_separate */
291
292 static int swap_read_combined (void) /* {{{ */
293 {
294         FILE *fh;
295         char buffer[1024];
296
297         uint8_t have_data = 0;
298         gauge_t swap_used   = 0.0;
299         gauge_t swap_cached = 0.0;
300         gauge_t swap_free   = 0.0;
301         gauge_t swap_total  = 0.0;
302
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         }
311
312         while (fgets (buffer, sizeof (buffer), fh) != NULL)
313         {
314                 char *fields[8];
315                 int numfields;
316
317                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
318                 if (numfields < 2)
319                         continue;
320
321                 if (strcasecmp (fields[0], "SwapTotal:") == 0)
322                 {
323                         swap_total = strtod (fields[1], /* endptr = */ NULL);
324                         have_data |= 0x01;
325                 }
326                 else if (strcasecmp (fields[0], "SwapFree:") == 0)
327                 {
328                         swap_free = strtod (fields[1], /* endptr = */ NULL);
329                         have_data |= 0x02;
330                 }
331                 else if (strcasecmp (fields[0], "SwapCached:") == 0)
332                 {
333                         swap_cached = strtod (fields[1], /* endptr = */ NULL);
334                         have_data |= 0x04;
335                 }
336         }
337
338         fclose (fh);
339
340         if (have_data != 0x07)
341                 return (ENOENT);
342
343         if (isnan (swap_total)
344                         || (swap_total <= 0.0)
345                         || ((swap_free + swap_cached) > swap_total))
346                 return (EINVAL);
347
348         swap_used = swap_total - (swap_free + swap_cached);
349
350         swap_submit_gauge (NULL, "used",   1024.0 * swap_used);
351         swap_submit_gauge (NULL, "free",   1024.0 * swap_free);
352         swap_submit_gauge (NULL, "cached", 1024.0 * swap_cached);
353
354         return (0);
355 } /* }}} int swap_read_combined */
356
357 static int swap_read_io (void) /* {{{ */
358 {
359         FILE *fh;
360         char buffer[1024];
361
362         _Bool old_kernel = 0;
363
364         uint8_t have_data = 0;
365         derive_t swap_in  = 0;
366         derive_t swap_out = 0;
367
368         fh = fopen ("/proc/vmstat", "r");
369         if (fh == NULL)
370         {
371                 /* /proc/vmstat does not exist in kernels <2.6 */
372                 fh = fopen ("/proc/stat", "r");
373                 if (fh == NULL)
374                 {
375                         char errbuf[1024];
376                         WARNING ("swap: fopen: %s",
377                                         sstrerror (errno, errbuf, sizeof (errbuf)));
378                         return (-1);
379                 }
380                 else
381                         old_kernel = 1;
382         }
383
384         while (fgets (buffer, sizeof (buffer), fh) != NULL)
385         {
386                 char *fields[8];
387                 int numfields;
388
389                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
390
391                 if (!old_kernel)
392                 {
393                         if (numfields != 2)
394                                 continue;
395
396                         if (strcasecmp ("pswpin", fields[0]) == 0)
397                         {
398                                 strtoderive (fields[1], &swap_in);
399                                 have_data |= 0x01;
400                         }
401                         else if (strcasecmp ("pswpout", fields[0]) == 0)
402                         {
403                                 strtoderive (fields[1], &swap_out);
404                                 have_data |= 0x02;
405                         }
406                 }
407                 else /* if (old_kernel) */
408                 {
409                         if (numfields != 3)
410                                 continue;
411
412                         if (strcasecmp ("page", fields[0]) == 0)
413                         {
414                                 strtoderive (fields[1], &swap_in);
415                                 strtoderive (fields[2], &swap_out);
416                         }
417                 }
418         } /* while (fgets) */
419
420         fclose (fh);
421
422         if (have_data != 0x03)
423                 return (ENOENT);
424
425         if (report_bytes)
426         {
427                 swap_in = swap_in * pagesize;
428                 swap_out = swap_out * pagesize;
429         }
430
431         swap_submit_derive (NULL, "in",  swap_in);
432         swap_submit_derive (NULL, "out", swap_out);
433
434         return (0);
435 } /* }}} int swap_read_io */
436
437 static int swap_read (void) /* {{{ */
438 {
439         if (report_by_device)
440                 swap_read_separate ();
441         else
442                 swap_read_combined ();
443
444         swap_read_io ();
445
446         return (0);
447 } /* }}} int swap_read */
448 /* #endif KERNEL_LINUX */
449
450 /*
451  * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
452  * and kstat. The former reads physical space used on a device, the latter
453  * reports the view from the virtual memory system. It was decided that the
454  * kstat-based information should be moved to the "vmem" plugin, but nobody
455  * with enough Solaris experience was available at that time to do this. The
456  * code below is still there for your reference but it won't be activated in
457  * *this* plugin again. --octo
458  */
459 #elif 0 && HAVE_LIBKSTAT
460 /* kstat-based read function */
461 static int swap_read_kstat (void) /* {{{ */
462 {
463         derive_t swap_alloc;
464         derive_t swap_resv;
465         derive_t swap_avail;
466
467         struct anoninfo ai;
468
469         if (swapctl (SC_AINFO, &ai) == -1)
470         {
471                 char errbuf[1024];
472                 ERROR ("swap plugin: swapctl failed: %s",
473                                 sstrerror (errno, errbuf, sizeof (errbuf)));
474                 return (-1);
475         }
476
477         /*
478          * Calculations from:
479          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
480          * Also see:
481          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
482          * /usr/include/vm/anon.h
483          *
484          * In short, swap -s shows: allocated + reserved = used, available
485          *
486          * However, Solaris does not allow to allocated/reserved more than the
487          * available swap (physical memory + disk swap), so the pedant may
488          * prefer: allocated + unallocated = reserved, available
489          *
490          * We map the above to: used + resv = n/a, free
491          *
492          * Does your brain hurt yet?  - Christophe Kalt
493          *
494          * Oh, and in case you wonder,
495          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
496          * can suffer from a 32bit overflow.
497          */
498         swap_alloc  = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
499         swap_resv   = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
500                         * pagesize);
501         swap_avail  = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
502
503         swap_submit_gauge (NULL, "used", swap_alloc);
504         swap_submit_gauge (NULL, "free", swap_avail);
505         swap_submit_gauge (NULL, "reserved", swap_resv);
506
507         return (0);
508 } /* }}} int swap_read_kstat */
509 /* #endif 0 && HAVE_LIBKSTAT */
510
511 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
512 /* swapctl-based read function */
513 static int swap_read (void) /* {{{ */
514 {
515         swaptbl_t *s;
516         char *s_paths;
517         int swap_num;
518         int status;
519         int i;
520
521         derive_t avail = 0;
522         derive_t total = 0;
523
524         swap_num = swapctl (SC_GETNSWP, NULL);
525         if (swap_num < 0)
526         {
527                 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
528                                 swap_num);
529                 return (-1);
530         }
531         else if (swap_num == 0)
532                 return (0);
533
534         /* Allocate and initialize the swaptbl_t structure */
535         s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
536         if (s == NULL)
537         {
538                 ERROR ("swap plugin: smalloc failed.");
539                 return (-1);
540         }
541
542         /* Memory to store the path names. We only use these paths when the
543          * separate option has been configured, but it's easier to just
544          * allocate enough memory in any case. */
545         s_paths = calloc (swap_num, PATH_MAX);
546         if (s_paths == NULL)
547         {
548                 ERROR ("swap plugin: malloc failed.");
549                 sfree (s);
550                 return (-1);
551         }
552         for (i = 0; i < swap_num; i++)
553                 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
554         s->swt_n = swap_num;
555
556         status = swapctl (SC_LIST, s);
557         if (status < 0)
558         {
559                 char errbuf[1024];
560                 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
561                                 sstrerror (errno, errbuf, sizeof (errbuf)));
562                 sfree (s_paths);
563                 sfree (s);
564                 return (-1);
565         }
566         else if (swap_num < status)
567         {
568                 /* more elements returned than requested */
569                 ERROR ("swap plugin: I allocated memory for %i structure%s, "
570                                 "but swapctl(2) claims to have returned %i. "
571                                 "I'm confused and will give up.",
572                                 swap_num, (swap_num == 1) ? "" : "s",
573                                 status);
574                 sfree (s_paths);
575                 sfree (s);
576                 return (-1);
577         }
578         else if (swap_num > status)
579                 /* less elements returned than requested */
580                 swap_num = status;
581
582         for (i = 0; i < swap_num; i++)
583         {
584                 char path[PATH_MAX];
585                 derive_t this_total;
586                 derive_t this_avail;
587
588                 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
589                         continue;
590
591                 this_total = ((derive_t) s->swt_ent[i].ste_pages) * pagesize;
592                 this_avail = ((derive_t) s->swt_ent[i].ste_free)  * pagesize;
593
594                 /* Shortcut for the "combined" setting (default) */
595                 if (!report_by_device)
596                 {
597                         avail += this_avail;
598                         total += this_total;
599                         continue;
600                 }
601
602                 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
603                 escape_slashes (path, sizeof (path));
604
605                 swap_submit_gauge (path, "used", (gauge_t) (this_total - this_avail));
606                 swap_submit_gauge (path, "free", (gauge_t) this_avail);
607         } /* for (swap_num) */
608
609         if (total < avail)
610         {
611                 ERROR ("swap plugin: Total swap space (%"PRIi64") "
612                                 "is less than free swap space (%"PRIi64").",
613                                 total, avail);
614                 sfree (s_paths);
615                 sfree (s);
616                 return (-1);
617         }
618
619         /* If the "separate" option was specified (report_by_device == 2), all
620          * values have already been dispatched from within the loop. */
621         if (!report_by_device)
622         {
623                 swap_submit_gauge (NULL, "used", (gauge_t) (total - avail));
624                 swap_submit_gauge (NULL, "free", (gauge_t) avail);
625         }
626
627         sfree (s_paths);
628         sfree (s);
629         return (0);
630 } /* }}} int swap_read */
631 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
632
633 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
634 static int swap_read (void) /* {{{ */
635 {
636         struct swapent *swap_entries;
637         int swap_num;
638         int status;
639         int i;
640
641         derive_t used  = 0;
642         derive_t total = 0;
643
644         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
645         if (swap_num < 0)
646         {
647                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
648                                 swap_num);
649                 return (-1);
650         }
651         else if (swap_num == 0)
652                 return (0);
653
654         swap_entries = calloc (swap_num, sizeof (*swap_entries));
655         if (swap_entries == NULL)
656         {
657                 ERROR ("swap plugin: calloc failed.");
658                 return (-1);
659         }
660
661         status = swapctl (SWAP_STATS, swap_entries, swap_num);
662         if (status != swap_num)
663         {
664                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
665                                 status);
666                 sfree (swap_entries);
667                 return (-1);
668         }
669
670 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
671 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
672 #else
673 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
674 #endif
675
676         for (i = 0; i < swap_num; i++)
677         {
678                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
679                         continue;
680
681                 used  += ((derive_t) swap_entries[i].se_inuse)
682                         * C_SWAP_BLOCK_SIZE;
683                 total += ((derive_t) swap_entries[i].se_nblks)
684                         * C_SWAP_BLOCK_SIZE;
685         }
686
687         if (total < used)
688         {
689                 ERROR ("swap plugin: Total swap space (%"PRIu64") "
690                                 "is less than used swap space (%"PRIu64").",
691                                 total, used);
692                 return (-1);
693         }
694
695         swap_submit_gauge (NULL, "used", (gauge_t) used);
696         swap_submit_gauge (NULL, "free", (gauge_t) (total - used));
697
698         sfree (swap_entries);
699
700         return (0);
701 } /* }}} int swap_read */
702 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
703
704 #elif defined(VM_SWAPUSAGE)
705 static int swap_read (void) /* {{{ */
706 {
707         int              mib[3];
708         size_t           mib_len;
709         struct xsw_usage sw_usage;
710         size_t           sw_usage_len;
711
712         mib_len = 2;
713         mib[0]  = CTL_VM;
714         mib[1]  = VM_SWAPUSAGE;
715
716         sw_usage_len = sizeof (struct xsw_usage);
717
718         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
719                 return (-1);
720
721         /* The returned values are bytes. */
722         swap_submit_gauge (NULL, "used", (gauge_t) sw_usage.xsu_used);
723         swap_submit_gauge (NULL, "free", (gauge_t) sw_usage.xsu_avail);
724
725         return (0);
726 } /* }}} int swap_read */
727 /* #endif VM_SWAPUSAGE */
728
729 #elif HAVE_LIBKVM_GETSWAPINFO
730 static int swap_read (void) /* {{{ */
731 {
732         struct kvm_swap data_s;
733         int             status;
734
735         derive_t used;
736         derive_t free;
737         derive_t total;
738
739         if (kvm_obj == NULL)
740                 return (-1);
741
742         /* only one structure => only get the grand total, no details */
743         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
744         if (status == -1)
745                 return (-1);
746
747         total = (derive_t) data_s.ksw_total;
748         used  = (derive_t) data_s.ksw_used;
749
750         total *= (derive_t) kvm_pagesize;
751         used  *= (derive_t) kvm_pagesize;
752
753         free = total - used;
754
755         swap_submit_gauge (NULL, "used", (gauge_t) used);
756         swap_submit_gauge (NULL, "free", (gauge_t) free);
757
758         return (0);
759 } /* }}} int swap_read */
760 /* #endif HAVE_LIBKVM_GETSWAPINFO */
761
762 #elif HAVE_LIBSTATGRAB
763 static int swap_read (void) /* {{{ */
764 {
765         sg_swap_stats *swap;
766
767         swap = sg_get_swap_stats ();
768
769         if (swap == NULL)
770                 return (-1);
771
772         swap_submit_gauge (NULL, "used", (gauge_t) swap->used);
773         swap_submit_gauge (NULL, "free", (gauge_t) swap->free);
774
775         return (0);
776 } /* }}} int swap_read */
777 /* #endif  HAVE_LIBSTATGRAB */
778
779 #elif HAVE_PERFSTAT
780 static int swap_read (void) /* {{{ */
781 {
782         if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
783         {
784                 char errbuf[1024];
785                 WARNING ("memory plugin: perfstat_memory_total failed: %s",
786                         sstrerror (errno, errbuf, sizeof (errbuf)));
787                 return (-1);
788         }
789         swap_submit_gauge (NULL, "used", (gauge_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize);
790         swap_submit_gauge (NULL, "free", (gauge_t) pmemory.pgsp_free * pagesize );
791
792         return (0);
793 } /* }}} int swap_read */
794 #endif /* HAVE_PERFSTAT */
795
796 void module_register (void)
797 {
798         plugin_register_config ("swap", swap_config,
799                         config_keys, config_keys_num);
800         plugin_register_init ("swap", swap_init);
801         plugin_register_read ("swap", swap_read);
802 } /* void module_register */
803
804 /* vim: set fdm=marker : */