Add support for Solaris/HP-UX versions of swapctl in swap plugin
[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 && HAVE_SWAPCTL_TWO_ARGS
80 static derive_t pagesize;
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 && HAVE_SWAPCTL_TWO_ARGS
119         /* getpagesize(3C) tells me this does not fail.. */
120         pagesize = (derive_t) getpagesize ();
121 /* #endif HAVE_SWAPCTL */
122
123 #elif defined(VM_SWAPUSAGE)
124         /* No init stuff */
125 /* #endif defined(VM_SWAPUSAGE) */
126
127 #elif HAVE_LIBKVM_GETSWAPINFO
128         if (kvm_obj != NULL)
129         {
130                 kvm_close (kvm_obj);
131                 kvm_obj = NULL;
132         }
133
134         kvm_pagesize = getpagesize ();
135
136         if ((kvm_obj = kvm_open (NULL, /* execfile */
137                                         NULL, /* corefile */
138                                         NULL, /* swapfile */
139                                         O_RDONLY, /* flags */
140                                         NULL)) /* errstr */
141                         == NULL)
142         {
143                 ERROR ("swap plugin: kvm_open failed.");
144                 return (-1);
145         }
146 /* #endif HAVE_LIBKVM_GETSWAPINFO */
147
148 #elif HAVE_LIBSTATGRAB
149         /* No init stuff */
150 /* #endif HAVE_LIBSTATGRAB */
151
152 #elif HAVE_PERFSTAT
153         pagesize = getpagesize();
154 #endif /* HAVE_PERFSTAT */
155
156         return (0);
157 }
158
159 static void swap_submit (const char *type_instance, derive_t value, unsigned type)
160 {
161         value_t values[1];
162         value_list_t vl = VALUE_LIST_INIT;
163
164         switch (type)
165         {
166                 case DS_TYPE_GAUGE:
167                         values[0].gauge = (gauge_t) value;
168                         sstrncpy (vl.type, "swap", sizeof (vl.type));
169                         break;
170                 case DS_TYPE_DERIVE:
171                         values[0].derive = value;
172                         sstrncpy (vl.type, "swap_io", sizeof (vl.type));
173                         break;
174                 default:
175                         ERROR ("swap plugin: swap_submit called with wrong"
176                                 " type");
177         }
178
179         vl.values = values;
180         vl.values_len = 1;
181         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
182         sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
183         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
184
185         plugin_dispatch_values (&vl);
186 } /* void swap_submit */
187
188 static int swap_read (void)
189 {
190 #if KERNEL_LINUX
191         FILE *fh;
192         char buffer[1024];
193
194         char *fields[8];
195         int numfields;
196
197         _Bool old_kernel=0;
198
199         derive_t swap_used   = 0;
200         derive_t swap_cached = 0;
201         derive_t swap_free   = 0;
202         derive_t swap_total  = 0;
203         derive_t swap_in     = 0;
204         derive_t swap_out    = 0;
205
206         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
207         {
208                 char errbuf[1024];
209                 WARNING ("memory: fopen: %s",
210                                 sstrerror (errno, errbuf, sizeof (errbuf)));
211                 return (-1);
212         }
213
214         while (fgets (buffer, 1024, fh) != NULL)
215         {
216                 derive_t *val = NULL;
217
218                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
219                         val = &swap_total;
220                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
221                         val = &swap_free;
222                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
223                         val = &swap_cached;
224                 else
225                         continue;
226
227                 numfields = strsplit (buffer, fields, 8);
228
229                 if (numfields < 2)
230                         continue;
231
232                 *val = (derive_t) atoll (fields[1]) * 1024LL;
233         }
234
235         if (fclose (fh))
236         {
237                 char errbuf[1024];
238                 WARNING ("memory: fclose: %s",
239                                 sstrerror (errno, errbuf, sizeof (errbuf)));
240         }
241
242         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
243                 return (-1);
244
245         swap_used = swap_total - (swap_free + swap_cached);
246
247         if ((fh = fopen ("/proc/vmstat", "r")) == NULL)
248         {
249                 // /proc/vmstat does not exist in kernels <2.6
250                 if ((fh = fopen ("/proc/stat", "r")) == NULL )
251                 {
252                         char errbuf[1024];
253                         WARNING ("swap: fopen: %s",
254                                         sstrerror (errno, errbuf, sizeof (errbuf)));
255                         return (-1);
256                 }
257                 else
258                         old_kernel = 1;
259         }
260
261         while (fgets (buffer, 1024, fh) != NULL)
262         {
263                 numfields = strsplit (buffer, fields, STATIC_ARRAY_SIZE (fields));
264
265                 if (!old_kernel)
266                 {
267                         if (numfields != 2)
268                                 continue;
269
270                         if (strcasecmp ("pswpin", fields[0]) != 0)
271                                 strtoderive (fields[1], &swap_in);
272                         else if (strcasecmp ("pswpout", fields[0]) == 0)
273                                 strtoderive (fields[1], &swap_out);
274                 }
275                 else /* if (old_kernel) */
276                 {
277                         if (numfields != 3)
278                                 continue;
279
280                         if (strcasecmp ("page", fields[0]) == 0)
281                         {
282                                 strtoderive (fields[1], &swap_in);
283                                 strtoderive (fields[2], &swap_out);
284                         }
285                 }
286         } /* while (fgets) */
287
288         if (fclose (fh))
289         {
290                 char errbuf[1024];
291                 WARNING ("swap: fclose: %s",
292                                 sstrerror (errno, errbuf, sizeof (errbuf)));
293         }
294
295         swap_submit ("used", swap_used, DS_TYPE_GAUGE);
296         swap_submit ("free", swap_free, DS_TYPE_GAUGE);
297         swap_submit ("cached", swap_cached, DS_TYPE_GAUGE);
298         swap_submit ("in", swap_in, DS_TYPE_DERIVE);
299         swap_submit ("out", swap_out, DS_TYPE_DERIVE);
300 /* #endif KERNEL_LINUX */
301
302 #elif HAVE_LIBKSTAT
303         derive_t swap_alloc;
304         derive_t swap_resv;
305         derive_t swap_avail;
306
307         struct anoninfo ai;
308
309         if (swapctl (SC_AINFO, &ai) == -1)
310         {
311                 char errbuf[1024];
312                 ERROR ("swap plugin: swapctl failed: %s",
313                                 sstrerror (errno, errbuf, sizeof (errbuf)));
314                 return (-1);
315         }
316
317         /*
318          * Calculations from:
319          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
320          * Also see:
321          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
322          * /usr/include/vm/anon.h
323          *
324          * In short, swap -s shows: allocated + reserved = used, available
325          *
326          * However, Solaris does not allow to allocated/reserved more than the
327          * available swap (physical memory + disk swap), so the pedant may
328          * prefer: allocated + unallocated = reserved, available
329          *
330          * We map the above to: used + resv = n/a, free
331          *
332          * Does your brain hurt yet?  - Christophe Kalt
333          *
334          * Oh, and in case you wonder,
335          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
336          * can suffer from a 32bit overflow.
337          */
338         swap_alloc  = (derive_t) ((ai.ani_max - ai.ani_free) * pagesize);
339         swap_resv   = (derive_t) ((ai.ani_resv + ai.ani_free - ai.ani_max)
340                         * pagesize);
341         swap_avail  = (derive_t) ((ai.ani_max - ai.ani_resv) * pagesize);
342
343         swap_submit ("used", swap_alloc, DS_TYPE_GAUGE);
344         swap_submit ("free", swap_avail, DS_TYPE_GAUGE);
345         swap_submit ("reserved", swap_resv, DS_TYPE_GAUGE);
346 /* #endif HAVE_LIBKSTAT */
347
348 #elif HAVE_SWAPCTL
349  #if HAVE_SWAPCTL_TWO_ARGS
350         swaptbl_t *s;
351         char strtab[255];
352         int swap_num;
353         int status;
354         int i;
355
356         derive_t avail = 0;
357         derive_t total = 0;
358
359         swap_num = swapctl (SC_GETNSWP, NULL);
360         if (swap_num < 0)
361         {
362                 ERROR ("swap plugin: swapctl (SC_GETNSWP) failed with status %i.",
363                                 swap_num);
364                 return (-1);
365         }
366         else if (swap_num == 0)
367                 return (0);
368
369         s = (swaptbl_t *) smalloc (swap_num * sizeof (swapent_t) + sizeof (struct swaptable));
370         if (s == NULL)
371         {
372                 ERROR ("swap plugin: smalloc failed.");
373                 return (-1);
374         }
375         /* Initialize string pointers. We have them share the same buffer as we don't care
376          * about the device's name, only its size. This saves memory and alloc/free ops */
377         for (i = 0; i < (swap_num + 1); i++) {
378                 s->swt_ent[i].ste_path = strtab;
379         }
380         s->swt_n = swap_num + 1;
381         status = swapctl (SC_LIST, s);
382         if (status != swap_num)
383         {
384                 ERROR ("swap plugin: swapctl (SC_LIST) failed with status %i.",
385                                 status);
386                 sfree (s);
387                 return (-1);
388         }
389
390         for (i = 0; i < swap_num; i++)
391         {
392                 if ((s->swt_ent[i].ste_flags & ST_INDEL) != 0)
393                         continue;
394
395                 avail += ((derive_t) s->swt_ent[i].ste_free)
396                          * pagesize;
397                 total += ((derive_t) s->swt_ent[i].ste_pages)
398                          * pagesize;
399         }
400
401         if (total < avail)
402         {
403                 ERROR ("swap plugin: Total swap space (%"PRIu64") "
404                                 "is less than free swap space (%"PRIu64").",
405                                 total, avail);
406                 return (-1);
407         }
408
409         swap_submit ("used", total - avail, DS_TYPE_GAUGE);
410         swap_submit ("free", avail, DS_TYPE_GAUGE);
411
412         sfree (s);
413  /* #endif HAVE_SWAPCTL_TWO_ARGS */
414  #elif HAVE_SWAPCTL_THREE_ARGS
415
416         struct swapent *swap_entries;
417         int swap_num;
418         int status;
419         int i;
420
421         derive_t used  = 0;
422         derive_t total = 0;
423
424         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
425         if (swap_num < 0)
426         {
427                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
428                                 swap_num);
429                 return (-1);
430         }
431         else if (swap_num == 0)
432                 return (0);
433
434         swap_entries = calloc (swap_num, sizeof (*swap_entries));
435         if (swap_entries == NULL)
436         {
437                 ERROR ("swap plugin: calloc failed.");
438                 return (-1);
439         }
440
441         status = swapctl (SWAP_STATS, swap_entries, swap_num);
442         if (status != swap_num)
443         {
444                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
445                                 status);
446                 sfree (swap_entries);
447                 return (-1);
448         }
449
450 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
451 # define C_SWAP_BLOCK_SIZE ((derive_t) DEV_BSIZE)
452 #else
453 # define C_SWAP_BLOCK_SIZE ((derive_t) 512)
454 #endif
455
456         for (i = 0; i < swap_num; i++)
457         {
458                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
459                         continue;
460
461                 used  += ((derive_t) swap_entries[i].se_inuse)
462                         * C_SWAP_BLOCK_SIZE;
463                 total += ((derive_t) swap_entries[i].se_nblks)
464                         * C_SWAP_BLOCK_SIZE;
465         }
466
467         if (total < used)
468         {
469                 ERROR ("swap plugin: Total swap space (%"PRIu64") "
470                                 "is less than used swap space (%"PRIu64").",
471                                 total, used);
472                 return (-1);
473         }
474
475         swap_submit ("used", used, DS_TYPE_GAUGE);
476         swap_submit ("free", total - used, DS_TYPE_GAUGE);
477
478         sfree (swap_entries);
479  #endif /* HAVE_SWAPCTL_THREE_ARGS */
480 /* #endif HAVE_SWAPCTL */
481
482 #elif defined(VM_SWAPUSAGE)
483         int              mib[3];
484         size_t           mib_len;
485         struct xsw_usage sw_usage;
486         size_t           sw_usage_len;
487
488         mib_len = 2;
489         mib[0]  = CTL_VM;
490         mib[1]  = VM_SWAPUSAGE;
491
492         sw_usage_len = sizeof (struct xsw_usage);
493
494         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
495                 return (-1);
496
497         /* The returned values are bytes. */
498         swap_submit ("used", (derive_t) sw_usage.xsu_used, DS_TYPE_GAUGE);
499         swap_submit ("free", (derive_t) sw_usage.xsu_avail, DS_TYPE_GAUGE);
500 /* #endif VM_SWAPUSAGE */
501
502 #elif HAVE_LIBKVM_GETSWAPINFO
503         struct kvm_swap data_s;
504         int             status;
505
506         derive_t used;
507         derive_t free;
508         derive_t total;
509
510         if (kvm_obj == NULL)
511                 return (-1);
512
513         /* only one structure => only get the grand total, no details */
514         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
515         if (status == -1)
516                 return (-1);
517
518         total = (derive_t) data_s.ksw_total;
519         used  = (derive_t) data_s.ksw_used;
520
521         total *= (derive_t) kvm_pagesize;
522         used  *= (derive_t) kvm_pagesize;
523
524         free = total - used;
525
526         swap_submit ("used", used, DS_TYPE_GAUGE);
527         swap_submit ("free", free, DS_TYPE_GAUGE);
528 /* #endif HAVE_LIBKVM_GETSWAPINFO */
529
530 #elif HAVE_LIBSTATGRAB
531         sg_swap_stats *swap;
532
533         swap = sg_get_swap_stats ();
534
535         if (swap == NULL)
536                 return (-1);
537
538         swap_submit ("used", (derive_t) swap->used, DS_TYPE_GAUGE);
539         swap_submit ("free", (derive_t) swap->free, DS_TYPE_GAUGE);
540 /* #endif  HAVE_LIBSTATGRAB */
541
542 #elif HAVE_PERFSTAT
543         if(perfstat_memory_total(NULL, &pmemory, sizeof(perfstat_memory_total_t), 1) < 0)
544         {
545                 char errbuf[1024];
546                 WARNING ("memory plugin: perfstat_memory_total failed: %s",
547                         sstrerror (errno, errbuf, sizeof (errbuf)));
548                 return (-1);
549         }
550         swap_submit ("used", (derive_t) (pmemory.pgsp_total - pmemory.pgsp_free) * pagesize, DS_TYPE_GAUGE);
551         swap_submit ("free", (derive_t) pmemory.pgsp_free * pagesize , DS_TYPE_GAUGE);
552 #endif /* HAVE_PERFSTAT */
553
554         return (0);
555 } /* int swap_read */
556
557 void module_register (void)
558 {
559         plugin_register_init ("swap", swap_init);
560         plugin_register_read ("swap", swap_read);
561 } /* void module_register */