Merge branch 'collectd-4.5' into collectd-4.6
[collectd.git] / src / swap.c
1 /**
2  * collectd - src/swap.c
3  * Copyright (C) 2005-2009  Florian octo Forster
4  *
5  * This program is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License as published by the
7  * Free Software Foundation; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #if HAVE_SYS_SWAP_H
27 # include <sys/swap.h>
28 #endif
29 #if HAVE_VM_ANON_H
30 # include <vm/anon.h>
31 #endif
32 #if HAVE_SYS_PARAM_H
33 #  include <sys/param.h>
34 #endif
35 #if HAVE_SYS_SYSCTL_H
36 #  include <sys/sysctl.h>
37 #endif
38 #if HAVE_SYS_DKSTAT_H
39 #  include <sys/dkstat.h>
40 #endif
41 #if HAVE_KVM_H
42 #  include <kvm.h>
43 #endif
44
45 #if HAVE_STATGRAB_H
46 # include <statgrab.h>
47 #endif
48
49 #undef  MAX
50 #define MAX(x,y) ((x) > (y) ? (x) : (y))
51
52 #if KERNEL_LINUX
53 /* No global variables */
54 /* #endif KERNEL_LINUX */
55
56 #elif HAVE_LIBKSTAT
57 static unsigned long long pagesize;
58 static kstat_t *ksp;
59 /* #endif HAVE_LIBKSTAT */
60
61 #elif HAVE_SWAPCTL
62 /* No global variables */
63 /* #endif HAVE_SWAPCTL */
64
65 #elif defined(VM_SWAPUSAGE)
66 /* No global variables */
67 /* #endif defined(VM_SWAPUSAGE) */
68
69 #elif HAVE_LIBKVM_GETSWAPINFO
70 static kvm_t *kvm_obj = NULL;
71 int kvm_pagesize;
72 /* #endif HAVE_LIBKVM_GETSWAPINFO */
73
74 #elif HAVE_LIBSTATGRAB
75 /* No global variables */
76 /* #endif HAVE_LIBSTATGRAB */
77
78 #else
79 # error "No applicable input method."
80 #endif /* HAVE_LIBSTATGRAB */
81
82 static int swap_init (void)
83 {
84 #if KERNEL_LINUX
85         /* No init stuff */
86 /* #endif KERNEL_LINUX */
87
88 #elif HAVE_LIBKSTAT
89         /* getpagesize(3C) tells me this does not fail.. */
90         pagesize = (unsigned long long) getpagesize ();
91         if (get_kstat (&ksp, "unix", 0, "system_pages"))
92                 ksp = NULL;
93 /* #endif HAVE_LIBKSTAT */
94
95 #elif HAVE_SWAPCTL
96         /* No init stuff */
97 /* #endif HAVE_SWAPCTL */
98
99 #elif defined(VM_SWAPUSAGE)
100         /* No init stuff */
101 /* #endif defined(VM_SWAPUSAGE) */
102
103 #elif HAVE_LIBKVM_GETSWAPINFO
104         if (kvm_obj != NULL)
105         {
106                 kvm_close (kvm_obj);
107                 kvm_obj = NULL;
108         }
109
110         kvm_pagesize = getpagesize ();
111
112         if ((kvm_obj = kvm_open (NULL, /* execfile */
113                                         NULL, /* corefile */
114                                         NULL, /* swapfile */
115                                         O_RDONLY, /* flags */
116                                         NULL)) /* errstr */
117                         == NULL)
118         {
119                 ERROR ("swap plugin: kvm_open failed.");
120                 return (-1);
121         }
122 /* #endif HAVE_LIBKVM_GETSWAPINFO */
123
124 #elif HAVE_LIBSTATGRAB
125         /* No init stuff */
126 #endif /* HAVE_LIBSTATGRAB */
127
128         return (0);
129 }
130
131 static void swap_submit (const char *type_instance, double value)
132 {
133         value_t values[1];
134         value_list_t vl = VALUE_LIST_INIT;
135
136         values[0].gauge = value;
137
138         vl.values = values;
139         vl.values_len = 1;
140         sstrncpy (vl.host, hostname_g, sizeof (vl.host));
141         sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
142         sstrncpy (vl.type, "swap", sizeof (vl.type));
143         sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
144
145         plugin_dispatch_values (&vl);
146 } /* void swap_submit */
147
148 static int swap_read (void)
149 {
150 #if KERNEL_LINUX
151         FILE *fh;
152         char buffer[1024];
153         
154         char *fields[8];
155         int numfields;
156
157         unsigned long long swap_used   = 0LL;
158         unsigned long long swap_cached = 0LL;
159         unsigned long long swap_free   = 0LL;
160         unsigned long long swap_total  = 0LL;
161
162         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
163         {
164                 char errbuf[1024];
165                 WARNING ("memory: fopen: %s",
166                                 sstrerror (errno, errbuf, sizeof (errbuf)));
167                 return (-1);
168         }
169
170         while (fgets (buffer, 1024, fh) != NULL)
171         {
172                 unsigned long long *val = NULL;
173
174                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
175                         val = &swap_total;
176                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
177                         val = &swap_free;
178                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
179                         val = &swap_cached;
180                 else
181                         continue;
182
183                 numfields = strsplit (buffer, fields, 8);
184
185                 if (numfields < 2)
186                         continue;
187
188                 *val = atoll (fields[1]) * 1024LL;
189         }
190
191         if (fclose (fh))
192         {
193                 char errbuf[1024];
194                 WARNING ("memory: fclose: %s",
195                                 sstrerror (errno, errbuf, sizeof (errbuf)));
196         }
197
198         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
199                 return (-1);
200
201         swap_used = swap_total - (swap_free + swap_cached);
202
203         swap_submit ("used", swap_used);
204         swap_submit ("free", swap_free);
205         swap_submit ("cached", swap_cached);
206 /* #endif KERNEL_LINUX */
207
208 #elif HAVE_LIBKSTAT
209         unsigned long long swap_alloc;
210         unsigned long long swap_resv;
211         unsigned long long swap_avail;
212
213         struct anoninfo ai;
214
215         if (swapctl (SC_AINFO, &ai) == -1)
216         {
217                 char errbuf[1024];
218                 ERROR ("swap plugin: swapctl failed: %s",
219                                 sstrerror (errno, errbuf, sizeof (errbuf)));
220                 return (-1);
221         }
222
223         /*
224          * Calculations from:
225          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
226          * Also see:
227          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
228          * /usr/include/vm/anon.h
229          *
230          * In short, swap -s shows: allocated + reserved = used, available
231          *
232          * However, Solaris does not allow to allocated/reserved more than the
233          * available swap (physical memory + disk swap), so the pedant may
234          * prefer: allocated + unallocated = reserved, available
235          * 
236          * We map the above to: used + resv = n/a, free
237          *
238          * Does your brain hurt yet?  - Christophe Kalt
239          *
240          * Oh, and in case you wonder,
241          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
242          * can suffer from a 32bit overflow.
243          */
244         swap_alloc  = ai.ani_max - ai.ani_free;
245         swap_alloc *= pagesize;
246         swap_resv   = ai.ani_resv + ai.ani_free - ai.ani_max;
247         swap_resv  *= pagesize;
248         swap_avail  = ai.ani_max - ai.ani_resv;
249         swap_avail *= pagesize;
250
251         swap_submit ("used", swap_alloc);
252         swap_submit ("free", swap_avail);
253         swap_submit ("reserved", swap_resv);
254 /* #endif HAVE_LIBKSTAT */
255
256 #elif HAVE_SWAPCTL
257         struct swapent *swap_entries;
258         int swap_num;
259         int status;
260         int i;
261
262         uint64_t used  = 0;
263         uint64_t total = 0;
264
265         /*
266          * XXX: This is the syntax for the *BSD `swapctl', which has the
267          * following prototype:
268          *   swapctl (int cmd, void *arg, int misc);
269          *
270          * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
271          * too, but with the following prototype:
272          *   swapctl (int cmd, void *arg);
273          *
274          * Solaris is usually handled in the KSTAT case above. For other UNIXes
275          * a separate case for the other version of `swapctl' may be necessary.
276          */
277         swap_num = swapctl (SWAP_NSWAP, NULL, 0);
278         if (swap_num < 0)
279         {
280                 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
281                                 swap_num);
282                 return (-1);
283         }
284         else if (swap_num == 0)
285                 return (0);
286
287         swap_entries = calloc (swap_num, sizeof (*swap_entries));
288         if (swap_entries == NULL)
289         {
290                 ERROR ("swap plugin: calloc failed.");
291                 return (-1);
292         }
293
294         status = swapctl (SWAP_STATS, swap_entries, swap_num);
295         if (status != swap_num)
296         {
297                 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
298                                 status);
299                 sfree (swap_entries);
300                 return (-1);
301         }
302
303 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
304 # define C_SWAP_BLOCK_SIZE ((uint64_t) DEV_BSIZE)
305 #else
306 # define C_SWAP_BLOCK_SIZE ((uint64_t) 512)
307 #endif
308
309         for (i = 0; i < swap_num; i++)
310         {
311                 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
312                         continue;
313
314                 used  += ((uint64_t) swap_entries[i].se_inuse)
315                         * C_SWAP_BLOCK_SIZE;
316                 total += ((uint64_t) swap_entries[i].se_nblks)
317                         * C_SWAP_BLOCK_SIZE;
318         }
319
320         if (total < used)
321         {
322                 ERROR ("swap plugin: Total swap space (%"PRIu64") "
323                                 "is less than used swap space (%"PRIu64").",
324                                 total, used);
325                 return (-1);
326         }
327
328         swap_submit ("used", (gauge_t) used);
329         swap_submit ("free", (gauge_t) (total - used));
330
331         sfree (swap_entries);
332 /* #endif HAVE_SWAPCTL */
333
334 #elif defined(VM_SWAPUSAGE)
335         int              mib[3];
336         size_t           mib_len;
337         struct xsw_usage sw_usage;
338         size_t           sw_usage_len;
339
340         mib_len = 2;
341         mib[0]  = CTL_VM;
342         mib[1]  = VM_SWAPUSAGE;
343
344         sw_usage_len = sizeof (struct xsw_usage);
345
346         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
347                 return (-1);
348
349         /* The returned values are bytes. */
350         swap_submit ("used", sw_usage.xsu_used);
351         swap_submit ("free", sw_usage.xsu_avail);
352 /* #endif VM_SWAPUSAGE */
353
354 #elif HAVE_LIBKVM_GETSWAPINFO
355         struct kvm_swap data_s;
356         int             status;
357
358         unsigned long long used;
359         unsigned long long free;
360         unsigned long long total;
361
362         if (kvm_obj == NULL)
363                 return (-1);
364
365         /* only one structure => only get the grand total, no details */
366         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
367         if (status == -1)
368                 return (-1);
369
370         total = (unsigned long long) data_s.ksw_total;
371         used  = (unsigned long long) data_s.ksw_used;
372
373         total *= (unsigned long long) kvm_pagesize;
374         used  *= (unsigned long long) kvm_pagesize;
375
376         free = total - used;
377
378         swap_submit ("used", used);
379         swap_submit ("free", free);
380 /* #endif HAVE_LIBKVM_GETSWAPINFO */
381
382 #elif HAVE_LIBSTATGRAB
383         sg_swap_stats *swap;
384
385         swap = sg_get_swap_stats ();
386
387         if (swap == NULL)
388                 return (-1);
389
390         swap_submit ("used", swap->used);
391         swap_submit ("free", swap->free);
392 #endif /* HAVE_LIBSTATGRAB */
393
394         return (0);
395 } /* int swap_read */
396
397 void module_register (void)
398 {
399         plugin_register_init ("swap", swap_init);
400         plugin_register_read ("swap", swap_read);
401 } /* void module_register */