swap plugin: Remove left-over debugging message.
[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         swap_submit_usage (NULL, swap_used, swap_free, "cached", swap_cached);
388         return (0);
389 } /* }}} int swap_read_combined */
390
391 static int swap_read_io (void) /* {{{ */
392 {
393         FILE *fh;
394         char buffer[1024];
395
396         _Bool old_kernel = 0;
397
398         uint8_t have_data = 0;
399         derive_t swap_in  = 0;
400         derive_t swap_out = 0;
401
402         fh = fopen ("/proc/vmstat", "r");
403         if (fh == NULL)
404         {
405                 /* /proc/vmstat does not exist in kernels <2.6 */
406                 fh = fopen ("/proc/stat", "r");
407                 if (fh == NULL)
408                 {
409                         char errbuf[1024];
410                         WARNING ("swap: fopen: %s",
411                                         sstrerror (errno, errbuf, sizeof (errbuf)));
412                         return (-1);
413                 }
414                 else
415                         old_kernel = 1;
416         }
417
418         while (fgets (buffer, sizeof (buffer), fh) != NULL)
419         {
420                 char *fields[8];
421                 int numfields;
422
423                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
424
425                 if (!old_kernel)
426                 {
427                         if (numfields != 2)
428                                 continue;
429
430                         if (strcasecmp ("pswpin", fields[0]) == 0)
431                         {
432                                 strtoderive (fields[1], &swap_in);
433                                 have_data |= 0x01;
434                         }
435                         else if (strcasecmp ("pswpout", fields[0]) == 0)
436                         {
437                                 strtoderive (fields[1], &swap_out);
438                                 have_data |= 0x02;
439                         }
440                 }
441                 else /* if (old_kernel) */
442                 {
443                         if (numfields != 3)
444                                 continue;
445
446                         if (strcasecmp ("page", fields[0]) == 0)
447                         {
448                                 strtoderive (fields[1], &swap_in);
449                                 strtoderive (fields[2], &swap_out);
450                         }
451                 }
452         } /* while (fgets) */
453
454         fclose (fh);
455
456         if (have_data != 0x03)
457                 return (ENOENT);
458
459         if (report_bytes)
460         {
461                 swap_in = swap_in * pagesize;
462                 swap_out = swap_out * pagesize;
463         }
464
465         swap_submit_derive (NULL, "in",  swap_in);
466         swap_submit_derive (NULL, "out", swap_out);
467
468         return (0);
469 } /* }}} int swap_read_io */
470
471 static int swap_read (void) /* {{{ */
472 {
473         if (report_by_device)
474                 swap_read_separate ();
475         else
476                 swap_read_combined ();
477
478         swap_read_io ();
479
480         return (0);
481 } /* }}} int swap_read */
482 /* #endif KERNEL_LINUX */
483
484 /*
485  * Under Solaris, two mechanisms can be used to read swap statistics, swapctl
486  * and kstat. The former reads physical space used on a device, the latter
487  * reports the view from the virtual memory system. It was decided that the
488  * kstat-based information should be moved to the "vmem" plugin, but nobody
489  * with enough Solaris experience was available at that time to do this. The
490  * code below is still there for your reference but it won't be activated in
491  * *this* plugin again. --octo
492  */
493 #elif 0 && HAVE_LIBKSTAT
494 /* kstat-based read function */
495 static int swap_read_kstat (void) /* {{{ */
496 {
497         gauge_t swap_alloc;
498         gauge_t swap_resv;
499         gauge_t swap_avail;
500
501         struct anoninfo ai;
502
503         if (swapctl (SC_AINFO, &ai) == -1)
504         {
505                 char errbuf[1024];
506                 ERROR ("swap plugin: swapctl failed: %s",
507                                 sstrerror (errno, errbuf, sizeof (errbuf)));
508                 return (-1);
509         }
510
511         /*
512          * Calculations from:
513          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
514          * Also see:
515          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
516          * /usr/include/vm/anon.h
517          *
518          * In short, swap -s shows: allocated + reserved = used, available
519          *
520          * However, Solaris does not allow to allocated/reserved more than the
521          * available swap (physical memory + disk swap), so the pedant may
522          * prefer: allocated + unallocated = reserved, available
523          *
524          * We map the above to: used + resv = n/a, free
525          *
526          * Does your brain hurt yet?  - Christophe Kalt
527          *
528          * Oh, and in case you wonder,
529          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
530          * can suffer from a 32bit overflow.
531          */
532         swap_alloc = (gauge_t) ((ai.ani_max - ai.ani_free) * pagesize);
533         swap_resv  = (gauge_t) ((ai.ani_resv + ai.ani_free - ai.ani_max) * pagesize);
534         swap_avail = (gauge_t) ((ai.ani_max - ai.ani_resv) * pagesize);
535
536         swap_submit_usage (NULL, swap_alloc, swap_avail, "reserved", swap_resv);
537         return (0);
538 } /* }}} int swap_read_kstat */
539 /* #endif 0 && HAVE_LIBKSTAT */
540
541 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS
542 /* swapctl-based read function */
543 static int swap_read (void) /* {{{ */
544 {
545         swaptbl_t *s;
546         char *s_paths;
547         int swap_num;
548         int status;
549         int i;
550
551         gauge_t avail = 0;
552         gauge_t total = 0;
553
554         swap_num = swapctl (SC_GETNSWP, NULL);
555         if (swap_num < 0)
556         {
557                 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
558                                 swap_num);
559                 return (-1);
560         }
561         else if (swap_num == 0)
562                 return (0);
563
564         /* Allocate and initialize the swaptbl_t structure */
565         s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
566         if (s == NULL)
567         {
568                 ERROR ("swap plugin: smalloc failed.");
569                 return (-1);
570         }
571
572         /* Memory to store the path names. We only use these paths when the
573          * separate option has been configured, but it's easier to just
574          * allocate enough memory in any case. */
575         s_paths = calloc (swap_num, PATH_MAX);
576         if (s_paths == NULL)
577         {
578                 ERROR ("swap plugin: malloc failed.");
579                 sfree (s);
580                 return (-1);
581         }
582         for (i = 0; i < swap_num; i++)
583                 s->swt_ent[i].ste_path = s_paths + (i * PATH_MAX);
584         s->swt_n = swap_num;
585
586         status = swapctl (SC_LIST, s);
587         if (status < 0)
588         {
589                 char errbuf[1024];
590                 ERROR ("swap plugin: swapctl (SC_LIST) failed: %s",
591                                 sstrerror (errno, errbuf, sizeof (errbuf)));
592                 sfree (s_paths);
593                 sfree (s);
594                 return (-1);
595         }
596         else if (swap_num < status)
597         {
598                 /* more elements returned than requested */
599                 ERROR ("swap plugin: I allocated memory for %i structure%s, "
600                                 "but swapctl(2) claims to have returned %i. "
601                                 "I'm confused and will give up.",
602                                 swap_num, (swap_num == 1) ? "" : "s",
603                                 status);
604                 sfree (s_paths);
605                 sfree (s);
606                 return (-1);
607         }
608         else if (swap_num > status)
609                 /* less elements returned than requested */
610                 swap_num = status;
611
612         for (i = 0; i < swap_num; i++)
613         {
614                 char path[PATH_MAX];
615                 gauge_t this_total;
616                 gauge_t this_avail;
617
618                 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
619                         continue;
620
621                 this_total = (gauge_t) (s->swt_ent[i].ste_pages * pagesize);
622                 this_avail = (gauge_t) (s->swt_ent[i].ste_free  * pagesize);
623
624                 /* Shortcut for the "combined" setting (default) */
625                 if (!report_by_device)
626                 {
627                         avail += this_avail;
628                         total += this_total;
629                         continue;
630                 }
631
632                 sstrncpy (path, s->swt_ent[i].ste_path, sizeof (path));
633                 escape_slashes (path, sizeof (path));
634
635                 swap_submit_usage (path, this_total - this_avail, this_avail,
636                                 NULL, NAN);
637         } /* for (swap_num) */
638
639         if (total < avail)
640         {
641                 ERROR ("swap plugin: Total swap space (%g) is less than free swap space (%g).",
642                                 total, avail);
643                 sfree (s_paths);
644                 sfree (s);
645                 return (-1);
646         }
647
648         /* If the "separate" option was specified (report_by_device == 1), all
649          * values have already been dispatched from within the loop. */
650         if (!report_by_device)
651                 swap_submit_usage (NULL, total - avail, avail, NULL, NAN);
652
653         sfree (s_paths);
654         sfree (s);
655         return (0);
656 } /* }}} int swap_read */
657 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_TWO_ARGS */
658
659 #elif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS
660 static int swap_read (void) /* {{{ */
661 {
662         struct swapent *swap_entries;
663         int swap_num;
664         int status;
665         int i;
666
667         gauge_t used  = 0;
668         gauge_t total = 0;
669
670         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
671         if (swap_num < 0)
672         {
673                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
674                                 swap_num);
675                 return (-1);
676         }
677         else if (swap_num == 0)
678                 return (0);
679
680         swap_entries = calloc (swap_num, sizeof (*swap_entries));
681         if (swap_entries == NULL)
682         {
683                 ERROR ("swap plugin: calloc failed.");
684                 return (-1);
685         }
686
687         status = swapctl (SWAP_STATS, swap_entries, swap_num);
688         if (status != swap_num)
689         {
690                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
691                                 status);
692                 sfree (swap_entries);
693                 return (-1);
694         }
695
696 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
697 # define C_SWAP_BLOCK_SIZE ((gauge_t) DEV_BSIZE)
698 #else
699 # define C_SWAP_BLOCK_SIZE 512.0
700 #endif
701
702         /* TODO: Report per-device stats. The path name is available from
703          * swap_entries[i].se_path */
704         for (i = 0; i < swap_num; i++)
705         {
706                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
707                         continue;
708
709                 used  += ((gauge_t) swap_entries[i].se_inuse) * C_SWAP_BLOCK_SIZE;
710                 total += ((gauge_t) swap_entries[i].se_nblks) * C_SWAP_BLOCK_SIZE;
711         }
712
713         if (total < used)
714         {
715                 ERROR ("swap plugin: Total swap space (%g) is less than used swap space (%g).",
716                                 total, used);
717                 return (-1);
718         }
719
720         swap_submit_usage (NULL, used, total - used, NULL, NAN);
721
722         sfree (swap_entries);
723         return (0);
724 } /* }}} int swap_read */
725 /* #endif HAVE_SWAPCTL && HAVE_SWAPCTL_THREE_ARGS */
726
727 #elif defined(VM_SWAPUSAGE)
728 static int swap_read (void) /* {{{ */
729 {
730         int              mib[3];
731         size_t           mib_len;
732         struct xsw_usage sw_usage;
733         size_t           sw_usage_len;
734
735         mib_len = 2;
736         mib[0]  = CTL_VM;
737         mib[1]  = VM_SWAPUSAGE;
738
739         sw_usage_len = sizeof (struct xsw_usage);
740
741         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
742                 return (-1);
743
744         /* The returned values are bytes. */
745         swap_submit_usage (NULL,
746                         (gauge_t) sw_usage.xsu_used, (gauge_t) sw_usage.xsu_avail,
747                         NULL, NAN);
748
749         return (0);
750 } /* }}} int swap_read */
751 /* #endif VM_SWAPUSAGE */
752
753 #elif HAVE_LIBKVM_GETSWAPINFO
754 static int swap_read (void) /* {{{ */
755 {
756         struct kvm_swap data_s;
757         int             status;
758
759         gauge_t used;
760         gauge_t total;
761
762         if (kvm_obj == NULL)
763                 return (-1);
764
765         /* only one structure => only get the grand total, no details */
766         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
767         if (status == -1)
768                 return (-1);
769
770         total = (gauge_t) data_s.ksw_total;
771         used  = (gauge_t) data_s.ksw_used;
772
773         total *= (gauge_t) kvm_pagesize;
774         used  *= (gauge_t) kvm_pagesize;
775
776         swap_submit_usage (NULL, used, total - used, NULL, NAN);
777
778         return (0);
779 } /* }}} int swap_read */
780 /* #endif HAVE_LIBKVM_GETSWAPINFO */
781
782 #elif HAVE_LIBSTATGRAB
783 static int swap_read (void) /* {{{ */
784 {
785         sg_swap_stats *swap;
786
787         swap = sg_get_swap_stats ();
788         if (swap == NULL)
789                 return (-1);
790
791         swap_submit_usage (NULL, (gauge_t) swap->used, (gauge_t) swap->free,
792                         NULL, NAN);
793
794         return (0);
795 } /* }}} int swap_read */
796 /* #endif  HAVE_LIBSTATGRAB */
797
798 #elif HAVE_PERFSTAT
799 static int swap_read (void) /* {{{ */
800 {
801         perfstat_memory_total_t pmemory;
802         int status;
803
804         gauge_t total;
805         gauge_t free;
806         gauge_t reserved;
807
808         memset (&pmemory, 0, sizeof (pmemory));
809         status = perfstat_memory_total (NULL, &pmemory, sizeof(perfstat_memory_total_t), 1);
810         if (status < 0)
811         {
812                 char errbuf[1024];
813                 WARNING ("swap plugin: perfstat_memory_total failed: %s",
814                         sstrerror (errno, errbuf, sizeof (errbuf)));
815                 return (-1);
816         }
817
818         total    = (gauge_t) (pmemory.pgsp_total * pagesize);
819         free     = (gauge_t) (pmemory.pgsp_free * pagesize);
820         reserved = (gauge_t) (pmemory.pgsp_rsvd * pagesize);
821
822         swap_submit_usage (NULL, total - free, free, "reserved", reserved);
823         swap_submit_derive (NULL, "in",  (derive_t) pmemory.pgspins * pagesize);
824         swap_submit_derive (NULL, "out", (derive_t) pmemory.pgspouts * pagesize);
825
826         return (0);
827 } /* }}} int swap_read */
828 #endif /* HAVE_PERFSTAT */
829
830 void module_register (void)
831 {
832         plugin_register_config ("swap", swap_config,
833                         config_keys, config_keys_num);
834         plugin_register_init ("swap", swap_init);
835         plugin_register_read ("swap", swap_read);
836 } /* void module_register */
837
838 /* vim: set fdm=marker : */