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