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