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