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