swap plugin: Remove trailing whitespace.
[collectd.git] / src / swap.c
1 /**
2  * collectd - src/swap.c
3  * Copyright (C) 2005-2009  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  **/
24
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
34
35 #include "collectd.h"
36 #include "common.h"
37 #include "plugin.h"
38
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
57
58 #if HAVE_STATGRAB_H
59 # include <statgrab.h>
60 #endif
61
62 #if HAVE_PERFSTAT
63 # include <sys/protosw.h>
64 # include <libperfstat.h>
65 #endif
66
67 #undef  MAX
68 #define MAX(x,y) ((x) > (y) ? (x) : (y))
69
70 #if KERNEL_LINUX
71 /* No global variables */
72 /* #endif KERNEL_LINUX */
73
74 #elif HAVE_LIBKSTAT
75 static derive_t pagesize;
76 static kstat_t *ksp;
77 /* #endif HAVE_LIBKSTAT */
78
79 #elif HAVE_SWAPCTL
80 /* No global variables */
81 /* #endif HAVE_SWAPCTL */
82
83 #elif defined(VM_SWAPUSAGE)
84 /* No global variables */
85 /* #endif defined(VM_SWAPUSAGE) */
86
87 #elif HAVE_LIBKVM_GETSWAPINFO
88 static kvm_t *kvm_obj = NULL;
89 int kvm_pagesize;
90 /* #endif HAVE_LIBKVM_GETSWAPINFO */
91
92 #elif HAVE_LIBSTATGRAB
93 /* No global variables */
94 /* #endif HAVE_LIBSTATGRAB */
95
96 #elif HAVE_PERFSTAT
97 static int pagesize;
98 static perfstat_memory_total_t pmemory;
99 /*# endif HAVE_PERFSTAT */
100
101 #else
102 # error "No applicable input method."
103 #endif /* HAVE_LIBSTATGRAB */
104
105 static int swap_init (void)
106 {
107 #if KERNEL_LINUX
108         /* No init stuff */
109 /* #endif KERNEL_LINUX */
110
111 #elif HAVE_LIBKSTAT
112         /* getpagesize(3C) tells me this does not fail.. */
113         pagesize = (derive_t) getpagesize ();
114         if (get_kstat (&ksp, "unix", 0, "system_pages"))
115                 ksp = NULL;
116 /* #endif HAVE_LIBKSTAT */
117
118 #elif HAVE_SWAPCTL
119         /* No init stuff */
120 /* #endif HAVE_SWAPCTL */
121
122 #elif defined(VM_SWAPUSAGE)
123         /* No init stuff */
124 /* #endif defined(VM_SWAPUSAGE) */
125
126 #elif HAVE_LIBKVM_GETSWAPINFO
127         if (kvm_obj != NULL)
128         {
129                 kvm_close (kvm_obj);
130                 kvm_obj = NULL;
131         }
132
133         kvm_pagesize = getpagesize ();
134
135         if ((kvm_obj = kvm_open (NULL, /* execfile */
136                                         NULL, /* corefile */
137                                         NULL, /* swapfile */
138                                         O_RDONLY, /* flags */
139                                         NULL)) /* errstr */
140                         == NULL)
141         {
142                 ERROR ("swap plugin: kvm_open failed.");
143                 return (-1);
144         }
145 /* #endif HAVE_LIBKVM_GETSWAPINFO */
146
147 #elif HAVE_LIBSTATGRAB
148         /* No init stuff */
149 /* #endif HAVE_LIBSTATGRAB */
150
151 #elif HAVE_PERFSTAT
152         pagesize = getpagesize();
153 #endif /* HAVE_PERFSTAT */
154
155         return (0);
156 }
157
158 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
159 {
160         value_t values[1];
161         value_list_t vl = VALUE_LIST_INIT;
162
163         switch (type)
164         {
165                 case DS_TYPE_GAUGE:
166                         values[0].gauge = (gauge_t) value;
167                         sstrncpy (vl.type, "swap", sizeof (vl.type));
168                         break;
169                 case DS_TYPE_DERIVE:
170                         values[0].derive = value;
171                         sstrncpy (vl.type, "swap_io", sizeof (vl.type));
172                         break;
173                 default:
174                         ERROR ("swap plugin: swap_submit called with wrong"
175                                 " type");
176         }
177
178         vl.values = values;
179         vl.values_len = 1;
180         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
181         sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
182         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
183
184         plugin_dispatch_values (&vl);
185 } /* void swap_submit */
186
187 static int swap_read (void)
188 {
189 #if KERNEL_LINUX
190         FILE *fh;
191         char buffer[1024];
192
193         char *fields[8];
194         int numfields;
195
196         unsigned int old_kernel=0;
197
198         derive_t swap_used   = 0;
199         derive_t swap_cached = 0;
200         derive_t swap_free   = 0;
201         derive_t swap_total  = 0;
202         derive_t swap_in     = 0;
203         derive_t swap_out    = 0;
204
205         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
206         {
207                 char errbuf[1024];
208                 WARNING ("memory: fopen: %s",
209                                 sstrerror (errno, errbuf, sizeof (errbuf)));
210                 return (-1);
211         }
212
213         while (fgets (buffer, 1024, fh) != NULL)
214         {
215                 derive_t *val = NULL;
216
217                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
218                         val = &swap_total;
219                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
220                         val = &swap_free;
221                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
222                         val = &swap_cached;
223                 else
224                         continue;
225
226                 numfields = strsplit (buffer, fields, 8);
227
228                 if (numfields < 2)
229                         continue;
230
231                 *val = (derive_t) atoll (fields[1]) * 1024LL;
232         }
233
234         if (fclose (fh))
235         {
236                 char errbuf[1024];
237                 WARNING ("memory: fclose: %s",
238                                 sstrerror (errno, errbuf, sizeof (errbuf)));
239         }
240
241         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
242                 return (-1);
243
244         swap_used = swap_total - (swap_free + swap_cached);
245
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         }
259
260         if ( old_kernel )
261                 while (fgets (buffer, 1024, fh) != NULL)
262                 {
263                         if (strncasecmp (buffer, "page",4) == 0)
264                         {
265                                 numfields = strsplit(buffer,fields,3);
266                                 if ( numfields < 3 )
267                                         continue;
268                                 swap_in  = (derive_t) atoll(fields[1]);
269                                 swap_out = (derive_t) atoll(fields[2]);
270                         }
271                 }
272         else
273                 while (fgets (buffer, 1024, fh) != NULL)
274                 {
275                         derive_t *val = NULL;
276                         if (strncasecmp (buffer, "pswpin", 6) == 0)
277                                 val = &swap_in;
278                         else if (strncasecmp (buffer, "pswpout", 7) == 0)
279                                 val = &swap_out;
280                         else
281                                 continue;
282
283                         numfields = strsplit (buffer, fields, 8);
284
285                         if (numfields < 2)
286                                 continue;
287
288                         *val = (derive_t) atoll (fields[1]);
289                 }
290
291         if (fclose (fh))
292         {
293                 char errbuf[1024];
294                 WARNING ("swap: fclose: %s",
295                                 sstrerror (errno, errbuf, sizeof (errbuf)));
296         }
297
298         swap_submit ("used", swap_used, DS_TYPE_GAUGE);
299         swap_submit ("free", swap_free, DS_TYPE_GAUGE);
300         swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
301         swap_submit ("in", swap_in, DS_TYPE_DERIVE);
302         swap_submit ("out", swap_out, DS_TYPE_DERIVE);
303
304 /* #endif KERNEL_LINUX */
305
306 #elif HAVE_LIBKSTAT
307         derive_t swap_alloc;
308         derive_t swap_resv;
309         derive_t swap_avail;
310
311         struct anoninfo ai;
312
313         if (swapctl (SC_AINFO, &ai) == -1)
314         {
315                 char errbuf[1024];
316                 ERROR ("swap plugin: swapctl failed: %s",
317                                 sstrerror (errno, errbuf, sizeof (errbuf)));
318                 return (-1);
319         }
320
321         /*
322          * Calculations from:
323          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
324          * Also see:
325          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
326          * /usr/include/vm/anon.h
327          *
328          * In short, swap -s shows: allocated + reserved = used, available
329          *
330          * However, Solaris does not allow to allocated/reserved more than the
331          * available swap (physical memory + disk swap), so the pedant may
332          * prefer: allocated + unallocated = reserved, available
333          *
334          * We map the above to: used + resv = n/a, free
335          *
336          * Does your brain hurt yet?  - Christophe Kalt
337          *
338          * Oh, and in case you wonder,
339          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
340          * can suffer from a 32bit overflow.
341          */
342         swap_alloc  = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
343         swap_resv   = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
344                         * pagesize);
345         swap_avail  = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
346
347         swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
348         swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
349         swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
350 /* #endif HAVE_LIBKSTAT */
351
352 #elif HAVE_SWAPCTL
353         struct swapent *swap_entries;
354         int swap_num;
355         int status;
356         int i;
357
358         derive_t used  = 0;
359         derive_t total = 0;
360
361         /*
362          * XXX: This is the syntax for the *BSD `swapctl', which has the
363          * following prototype:
364          *   swapctl (int cmd, void *arg, int misc);
365          *
366          * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
367          * too, but with the following prototype:
368          *   swapctl (int cmd, void *arg);
369          *
370          * Solaris is usually handled in the KSTAT case above. For other UNIXes
371          * a separate case for the other version of `swapctl' may be necessary.
372          */
373         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
374         if (swap_num < 0)
375         {
376                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
377                                 swap_num);
378                 return (-1);
379         }
380         else if (swap_num == 0)
381                 return (0);
382
383         swap_entries = calloc (swap_num, sizeof (*swap_entries));
384         if (swap_entries == NULL)
385         {
386                 ERROR ("swap plugin: calloc failed.");
387                 return (-1);
388         }
389
390         status = swapctl (SWAP_STATS, swap_entries, swap_num);
391         if (status != swap_num)
392         {
393                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
394                                 status);
395                 sfree (swap_entries);
396                 return (-1);
397         }
398
399 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
400 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
401 #else
402 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
403 #endif
404
405         for (i = 0; i < swap_num; i++)
406         {
407                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
408                         continue;
409
410                 used  += ((derive_t) swap_entries[i].se_inuse)
411                         * C_SWAP_BLOCK_SIZE;
412                 total += ((derive_t) swap_entries[i].se_nblks)
413                         * C_SWAP_BLOCK_SIZE;
414         }
415
416         if (total < used)
417         {
418                 ERROR ("swap plugin: Total swap space (%"PRIu64") "
419                                 "is less than used swap space (%"PRIu64").",
420                                 total, used);
421                 return (-1);
422         }
423
424         swap_submit ("used", used, DS_TYPE_GAUGE);
425         swap_submit ("free", total - used, DS_TYPE_GAUGE);
426
427         sfree (swap_entries);
428 /* #endif HAVE_SWAPCTL */
429
430 #elif defined(VM_SWAPUSAGE)
431         int              mib[3];
432         size_t           mib_len;
433         struct xsw_usage sw_usage;
434         size_t           sw_usage_len;
435
436         mib_len = 2;
437         mib[0]  = CTL_VM;
438         mib[1]  = VM_SWAPUSAGE;
439
440         sw_usage_len = sizeof (struct xsw_usage);
441
442         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
443                 return (-1);
444
445         /* The returned values are bytes. */
446         swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
447         swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
448 /* #endif VM_SWAPUSAGE */
449
450 #elif HAVE_LIBKVM_GETSWAPINFO
451         struct kvm_swap data_s;
452         int             status;
453
454         derive_t used;
455         derive_t free;
456         derive_t total;
457
458         if (kvm_obj == NULL)
459                 return (-1);
460
461         /* only one structure => only get the grand total, no details */
462         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
463         if (status == -1)
464                 return (-1);
465
466         total = (derive_t) data_s.ksw_total;
467         used  = (derive_t) data_s.ksw_used;
468
469         total *= (derive_t) kvm_pagesize;
470         used  *= (derive_t) kvm_pagesize;
471
472         free = total - used;
473
474         swap_submit ("used", used, DS_TYPE_GAUGE);
475         swap_submit ("free", free, DS_TYPE_GAUGE);
476 /* #endif HAVE_LIBKVM_GETSWAPINFO */
477
478 #elif HAVE_LIBSTATGRAB
479         sg_swap_stats *swap;
480
481         swap = sg_get_swap_stats ();
482
483         if (swap == NULL)
484                 return (-1);
485
486         swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
487         swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
488 /* #endif  HAVE_LIBSTATGRAB */
489
490 #elif HAVE_PERFSTAT
491         if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
492         {
493                 char errbuf[1024];
494                 WARNING ("memory plugin: perfstat_memory_total failed: %s",
495                         sstrerror (errno, errbuf, sizeof (errbuf)));
496                 return (-1);
497         }
498         swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
499         swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
500 #endif /* HAVE_PERFSTAT */
501
502         return (0);
503 } /* int swap_read */
504
505 void module_register (void)
506 {
507         plugin_register_init ("swap", swap_init);
508         plugin_register_read ("swap", swap_read);
509 } /* void module_register */