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