Code

swap plugin: Add the "ReportBytes" option.
[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  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; only version 2 of the License is applicable.
10  *
11  * This program is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License along
17  * with this program; if not, write to the Free Software Foundation, Inc.,
18  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19  *
20  * Authors:
21  *   Florian octo Forster <octo at verplant.org>
22  *   Manuel Sanmartin
23  **/
25 #if HAVE_CONFIG_H
26 # include "config.h"
27 # undef HAVE_CONFIG_H
28 #endif
29 /* avoid swap.h error "Cannot use swapctl in the large files compilation environment" */
30 #if HAVE_SYS_SWAP_H && !defined(_LP64) && _FILE_OFFSET_BITS == 64
31 #  undef _FILE_OFFSET_BITS
32 #  undef _LARGEFILE64_SOURCE
33 #endif
35 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
39 #if HAVE_SYS_SWAP_H
40 # include <sys/swap.h>
41 #endif
42 #if HAVE_VM_ANON_H
43 # include <vm/anon.h>
44 #endif
45 #if HAVE_SYS_PARAM_H
46 #  include <sys/param.h>
47 #endif
48 #if HAVE_SYS_SYSCTL_H
49 #  include <sys/sysctl.h>
50 #endif
51 #if HAVE_SYS_DKSTAT_H
52 #  include <sys/dkstat.h>
53 #endif
54 #if HAVE_KVM_H
55 #  include <kvm.h>
56 #endif
58 #if HAVE_STATGRAB_H
59 # include <statgrab.h>
60 #endif
62 #if HAVE_PERFSTAT
63 # include <sys/protosw.h>
64 # include <libperfstat.h>
65 #endif
67 #undef  MAX
68 #define MAX(x,y) ((x) > (y) ? (x) : (y))
70 #if KERNEL_LINUX
71 static derive_t pagesize;
72 static _Bool report_bytes = 0;
73 /* #endif KERNEL_LINUX */
75 #elif HAVE_LIBKSTAT
76 static derive_t pagesize;
77 static kstat_t *ksp;
78 /* #endif HAVE_LIBKSTAT */
80 #elif HAVE_SWAPCTL
81 /* No global variables */
82 /* #endif HAVE_SWAPCTL */
84 #elif defined(VM_SWAPUSAGE)
85 /* No global variables */
86 /* #endif defined(VM_SWAPUSAGE) */
88 #elif HAVE_LIBKVM_GETSWAPINFO
89 static kvm_t *kvm_obj = NULL;
90 int kvm_pagesize;
91 /* #endif HAVE_LIBKVM_GETSWAPINFO */
93 #elif HAVE_LIBSTATGRAB
94 /* No global variables */
95 /* #endif HAVE_LIBSTATGRAB */
97 #elif HAVE_PERFSTAT
98 static int pagesize;
99 static perfstat_memory_total_t pmemory;
100 /*# endif HAVE_PERFSTAT */
102 #else
103 # error "No applicable input method."
104 #endif /* HAVE_LIBSTATGRAB */
106 static const char *config_keys[] =
108         "ReportBytes"
109 };
110 static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
112 static int swap_init (void)
114 #if KERNEL_LINUX
115         pagesize = (derive_t) sysconf (_SC_PAGESIZE);
116 /* #endif KERNEL_LINUX */
118 #elif HAVE_LIBKSTAT
119         /* getpagesize(3C) tells me this does not fail.. */
120         pagesize = (derive_t) getpagesize ();
121         if (get_kstat (&ksp, "unix", 0, "system_pages"))
122                 ksp = NULL;
123 /* #endif HAVE_LIBKSTAT */
125 #elif HAVE_SWAPCTL
126         /* No init stuff */
127 /* #endif HAVE_SWAPCTL */
129 #elif defined(VM_SWAPUSAGE)
130         /* No init stuff */
131 /* #endif defined(VM_SWAPUSAGE) */
133 #elif HAVE_LIBKVM_GETSWAPINFO
134         if (kvm_obj != NULL)
135         {
136                 kvm_close (kvm_obj);
137                 kvm_obj = NULL;
138         }
140         kvm_pagesize = getpagesize ();
142         if ((kvm_obj = kvm_open (NULL, /* execfile */
143                                         NULL, /* corefile */
144                                         NULL, /* swapfile */
145                                         O_RDONLY, /* flags */
146                                         NULL)) /* errstr */
147                         == NULL)
148         {
149                 ERROR ("swap plugin: kvm_open failed.");
150                 return (-1);
151         }
152 /* #endif HAVE_LIBKVM_GETSWAPINFO */
154 #elif HAVE_LIBSTATGRAB
155         /* No init stuff */
156 /* #endif HAVE_LIBSTATGRAB */
158 #elif HAVE_PERFSTAT
159         pagesize = getpagesize();
160 #endif /* HAVE_PERFSTAT */
162         return (0);
165 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
167         value_t values[1];
168         value_list_t vl = VALUE_LIST_INIT;
170         switch (type)
171         {
172                 case DS_TYPE_GAUGE:
173                         values[0].gauge = (gauge_t) value;
174                         sstrncpy (vl.type, "swap", sizeof (vl.type));
175                         break;
176                 case DS_TYPE_DERIVE:
177                         values[0].derive = value;
178                         sstrncpy (vl.type, "swap_io", sizeof (vl.type));
179                         break;
180                 default:
181                         ERROR ("swap plugin: swap_submit called with wrong"
182                                 " type");
183         }
185         vl.values = values;
186         vl.values_len = 1;
187         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
188         sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
189         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
191         plugin_dispatch_values (&vl);
192 } /* void swap_submit */
194 static int swap_read (void)
196 #if KERNEL_LINUX
197         FILE *fh;
198         char buffer[1024];
200         char *fields[8];
201         int numfields;
203         _Bool old_kernel=0;
205         derive_t swap_used   = 0;
206         derive_t swap_cached = 0;
207         derive_t swap_free   = 0;
208         derive_t swap_total  = 0;
209         derive_t swap_in     = 0;
210         derive_t swap_out    = 0;
212         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
213         {
214                 char errbuf[1024];
215                 WARNING ("memory: fopen: %s",
216                                 sstrerror (errno, errbuf, sizeof (errbuf)));
217                 return (-1);
218         }
220         while (fgets (buffer, sizeof (buffer), fh) != NULL)
221         {
222                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
223                 if (numfields < 2)
224                         continue;
226                 if (strcasecmp (fields[0], "SwapTotal:") == 0)
227                         strtoderive (fields[1], &swap_total);
228                 else if (strcasecmp (fields[0], "SwapFree:") == 0)
229                         strtoderive (fields[1], &swap_free);
230                 else if (strcasecmp (fields[0], "SwapCached:") == 0)
231                         strtoderive (fields[1], &swap_cached);
232         }
234         if (fclose (fh))
235         {
236                 char errbuf[1024];
237                 WARNING ("memory: fclose: %s",
238                                 sstrerror (errno, errbuf, sizeof (errbuf)));
239         }
241         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
242                 return (-1);
244         swap_used = swap_total - (swap_free + swap_cached);
246         if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
247         {
248                 // /proc/vmstat does not exist in kernels <2.6
249                 if ((fh = fopen ("/proc/stat", "r")) == NULL )
250                 {
251                         char errbuf[1024];
252                         WARNING ("swap: fopen: %s",
253                                         sstrerror (errno, errbuf, sizeof (errbuf)));
254                         return (-1);
255                 }
256                 else
257                         old_kernel = 1;
258         }
260         while (fgets (buffer, sizeof (buffer), fh) != NULL)
261         {
262                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
264                 if (!old_kernel)
265                 {
266                         if (numfields != 2)
267                                 continue;
269                         if (strcasecmp ("pswpin", fields[0]) == 0)
270                                 strtoderive (fields[1], &swap_in);
271                         else if (strcasecmp ("pswpout", fields[0]) == 0)
272                                 strtoderive (fields[1], &swap_out);
273                 }
274                 else /* if (old_kernel) */
275                 {
276                         if (numfields != 3)
277                                 continue;
279                         if (strcasecmp ("page", fields[0]) == 0)
280                         {
281                                 strtoderive (fields[1], &swap_in);
282                                 strtoderive (fields[2], &swap_out);
283                         }
284                 }
285         } /* while (fgets) */
287         if (fclose (fh))
288         {
289                 char errbuf[1024];
290                 WARNING ("swap: fclose: %s",
291                                 sstrerror (errno, errbuf, sizeof (errbuf)));
292         }
294         if (report_bytes)
295         {
296                 swap_in  *= pagesize;
297                 swap_out *= pagesize;
298         }
300         swap_submit ("used",   1024 * swap_used,   DS_TYPE_GAUGE);
301         swap_submit ("free",   1024 * swap_free,   DS_TYPE_GAUGE);
302         swap_submit ("cached", 1024 * swap_cached, DS_TYPE_GAUGE);
303         swap_submit ("in",  swap_in,  DS_TYPE_DERIVE);
304         swap_submit ("out", swap_out, DS_TYPE_DERIVE);
305 /* #endif KERNEL_LINUX */
307 #elif HAVE_LIBKSTAT
308         derive_t swap_alloc;
309         derive_t swap_resv;
310         derive_t swap_avail;
312         struct anoninfo ai;
314         if (swapctl (SC_AINFO, &ai) == -1)
315         {
316                 char errbuf[1024];
317                 ERROR ("swap plugin: swapctl failed: %s",
318                                 sstrerror (errno, errbuf, sizeof (errbuf)));
319                 return (-1);
320         }
322         /*
323          * Calculations from:
324          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
325          * Also see:
326          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
327          * /usr/include/vm/anon.h
328          *
329          * In short, swap -s shows: allocated + reserved = used, available
330          *
331          * However, Solaris does not allow to allocated/reserved more than the
332          * available swap (physical memory + disk swap), so the pedant may
333          * prefer: allocated + unallocated = reserved, available
334          *
335          * We map the above to: used + resv = n/a, free
336          *
337          * Does your brain hurt yet?  - Christophe Kalt
338          *
339          * Oh, and in case you wonder,
340          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
341          * can suffer from a 32bit overflow.
342          */
343         swap_alloc  = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
344         swap_resv   = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
345                         * pagesize);
346         swap_avail  = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
348         swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
349         swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
350         swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
351 /* #endif HAVE_LIBKSTAT */
353 #elif HAVE_SWAPCTL
354         struct swapent *swap_entries;
355         int swap_num;
356         int status;
357         int i;
359         derive_t used  = 0;
360         derive_t total = 0;
362         /*
363          * XXX: This is the syntax for the *BSD `swapctl', which has the
364          * following prototype:
365          *   swapctl (int cmd, void *arg, int misc);
366          *
367          * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
368          * too, but with the following prototype:
369          *   swapctl (int cmd, void *arg);
370          *
371          * Solaris is usually handled in the KSTAT case above. For other UNIXes
372          * a separate case for the other version of `swapctl' may be necessary.
373          */
374         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
375         if (swap_num < 0)
376         {
377                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
378                                 swap_num);
379                 return (-1);
380         }
381         else if (swap_num == 0)
382                 return (0);
384         swap_entries = calloc (swap_num, sizeof (*swap_entries));
385         if (swap_entries == NULL)
386         {
387                 ERROR ("swap plugin: calloc failed.");
388                 return (-1);
389         }
391         status = swapctl (SWAP_STATS, swap_entries, swap_num);
392         if (status != swap_num)
393         {
394                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
395                                 status);
396                 sfree (swap_entries);
397                 return (-1);
398         }
400 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
401 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
402 #else
403 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
404 #endif
406         for (i = 0; i < swap_num; i++)
407         {
408                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
409                         continue;
411                 used  += ((derive_t) swap_entries[i].se_inuse)
412                         * C_SWAP_BLOCK_SIZE;
413                 total += ((derive_t) swap_entries[i].se_nblks)
414                         * C_SWAP_BLOCK_SIZE;
415         }
417         if (total < used)
418         {
419                 ERROR ("swap plugin: Total swap space (%"PRIu64") "
420                                 "is less than used swap space (%"PRIu64").",
421                                 total, used);
422                 return (-1);
423         }
425         swap_submit ("used", used, DS_TYPE_GAUGE);
426         swap_submit ("free", total - used, DS_TYPE_GAUGE);
428         sfree (swap_entries);
429 /* #endif HAVE_SWAPCTL */
431 #elif defined(VM_SWAPUSAGE)
432         int              mib[3];
433         size_t           mib_len;
434         struct xsw_usage sw_usage;
435         size_t           sw_usage_len;
437         mib_len = 2;
438         mib[0]  = CTL_VM;
439         mib[1]  = VM_SWAPUSAGE;
441         sw_usage_len = sizeof (struct xsw_usage);
443         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
444                 return (-1);
446         /* The returned values are bytes. */
447         swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
448         swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
449 /* #endif VM_SWAPUSAGE */
451 #elif HAVE_LIBKVM_GETSWAPINFO
452         struct kvm_swap data_s;
453         int             status;
455         derive_t used;
456         derive_t free;
457         derive_t total;
459         if (kvm_obj == NULL)
460                 return (-1);
462         /* only one structure => only get the grand total, no details */
463         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
464         if (status == -1)
465                 return (-1);
467         total = (derive_t) data_s.ksw_total;
468         used  = (derive_t) data_s.ksw_used;
470         total *= (derive_t) kvm_pagesize;
471         used  *= (derive_t) kvm_pagesize;
473         free = total - used;
475         swap_submit ("used", used, DS_TYPE_GAUGE);
476         swap_submit ("free", free, DS_TYPE_GAUGE);
477 /* #endif HAVE_LIBKVM_GETSWAPINFO */
479 #elif HAVE_LIBSTATGRAB
480         sg_swap_stats *swap;
482         swap = sg_get_swap_stats ();
484         if (swap == NULL)
485                 return (-1);
487         swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
488         swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
489 /* #endif  HAVE_LIBSTATGRAB */
491 #elif HAVE_PERFSTAT
492         if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
493         {
494                 char errbuf[1024];
495                 WARNING ("memory plugin: perfstat_memory_total failed: %s",
496                         sstrerror (errno, errbuf, sizeof (errbuf)));
497                 return (-1);
498         }
499         swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
500         swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
501 #endif /* HAVE_PERFSTAT */
503         return (0);
504 } /* int swap_read */
506 static int swap_config (const char *key, const char *value)
508         if (strcasecmp ("ReportBytes", key) == 0)
509         {
510 #if KERNEL_LINUX
511                 report_bytes = IS_TRUE (value) ? 1 : 0;
512 #else
513                 WARNING ("swap plugin: The \"ReportBytes\" option is only "
514                                 "valid under Linux. The option is going to "
515                                 "be ignored.");
516 #endif
517         }
518         else
519         {
520                 return (-1);
521         }
523         return (0);
524 } /* int swap_config */
526 void module_register (void)
528         plugin_register_config ("swap", swap_config,
529                         config_keys, config_keys_num);
530         plugin_register_init ("swap", swap_init);
531         plugin_register_read ("swap", swap_read);
532 } /* void module_register */