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