Merge branch 'pull/collectd-4' into collectd-4
[collectd.git] / src / swap.c
1 /**
2  * collectd - src/swap.c
3  * Copyright (C) 2005-2007  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_SYS_PARAM_H
30 #  include <sys/param.h>
31 #endif
32 #if HAVE_SYS_SYSCTL_H
33 #  include <sys/sysctl.h>
34 #endif
35 #if HAVE_KVM_H
36 #  include <kvm.h>
37 #endif
38
39 #if KERNEL_LINUX || HAVE_LIBKSTAT || defined(VM_SWAPUSAGE) || HAVE_LIBKVM || HAVE_LIBSTATGRAB
40 # define SWAP_HAVE_READ 1
41 #else
42 # define SWAP_HAVE_READ 0
43 #endif
44
45 #undef  MAX
46 #define MAX(x,y) ((x) > (y) ? (x) : (y))
47
48 static data_source_t data_source[1] =
49 {
50         {"value", DS_TYPE_GAUGE, 0, 1099511627776.0}
51 };
52
53 static data_set_t data_set =
54 {
55         "swap", 1, data_source
56 };
57
58 #if SWAP_HAVE_READ
59 #if KERNEL_LINUX
60 /* No global variables */
61 /* #endif KERNEL_LINUX */
62
63 #elif HAVE_LIBKSTAT
64 static unsigned long long pagesize;
65 static kstat_t *ksp;
66 /* #endif HAVE_LIBKSTAT */
67
68 #elif defined(VM_SWAPUSAGE)
69 /* No global variables */
70 /* #endif defined(VM_SWAPUSAGE) */
71
72 #elif HAVE_LIBKVM
73 static kvm_t *kvm_obj = NULL;
74 int kvm_pagesize;
75 /* #endif HAVE_LIBKVM */
76
77 #elif HAVE_LIBSTATGRAB
78 /* No global variables */
79 #endif /* HAVE_LIBSTATGRAB */
80
81 static int swap_init (void)
82 {
83 #if KERNEL_LINUX
84         /* No init stuff */
85 /* #endif KERNEL_LINUX */
86
87 #elif HAVE_LIBKSTAT
88         /* getpagesize(3C) tells me this does not fail.. */
89         pagesize = (unsigned long long) getpagesize ();
90         if (get_kstat (&ksp, "unix", 0, "system_pages"))
91                 ksp = NULL;
92 /* #endif HAVE_LIBKSTAT */
93
94 #elif defined(VM_SWAPUSAGE)
95         /* No init stuff */
96 /* #endif defined(VM_SWAPUSAGE) */
97
98 #elif HAVE_LIBKVM
99         if (kvm_obj != NULL)
100         {
101                 kvm_close (kvm_obj);
102                 kvm_obj = NULL;
103         }
104
105         kvm_pagesize = getpagesize ();
106
107         if ((kvm_obj = kvm_open (NULL, /* execfile */
108                                         NULL, /* corefile */
109                                         NULL, /* swapfile */
110                                         O_RDONLY, /* flags */
111                                         NULL)) /* errstr */
112                         == NULL)
113         {
114                 ERROR ("swap plugin: kvm_open failed.");
115                 return (-1);
116         }
117 /* #endif HAVE_LIBKVM */
118
119 #elif HAVE_LIBSTATGRAB
120         /* No init stuff */
121 #endif /* HAVE_LIBSTATGRAB */
122
123         return (0);
124 }
125
126 static void swap_submit (const char *type_instance, double value)
127 {
128         value_t values[1];
129         value_list_t vl = VALUE_LIST_INIT;
130
131         values[0].gauge = value;
132
133         vl.values = values;
134         vl.values_len = 1;
135         vl.time = time (NULL);
136         strcpy (vl.host, hostname_g);
137         strcpy (vl.plugin, "swap");
138         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
139
140         plugin_dispatch_values ("swap", &vl);
141 } /* void swap_submit */
142
143 static int swap_read (void)
144 {
145 #if KERNEL_LINUX
146         FILE *fh;
147         char buffer[1024];
148         
149         char *fields[8];
150         int numfields;
151
152         unsigned long long swap_used   = 0LL;
153         unsigned long long swap_cached = 0LL;
154         unsigned long long swap_free   = 0LL;
155         unsigned long long swap_total  = 0LL;
156
157         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
158         {
159                 char errbuf[1024];
160                 WARNING ("memory: fopen: %s",
161                                 sstrerror (errno, errbuf, sizeof (errbuf)));
162                 return (-1);
163         }
164
165         while (fgets (buffer, 1024, fh) != NULL)
166         {
167                 unsigned long long *val = NULL;
168
169                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
170                         val = &swap_total;
171                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
172                         val = &swap_free;
173                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
174                         val = &swap_cached;
175                 else
176                         continue;
177
178                 numfields = strsplit (buffer, fields, 8);
179
180                 if (numfields < 2)
181                         continue;
182
183                 *val = atoll (fields[1]) * 1024LL;
184         }
185
186         if (fclose (fh))
187         {
188                 char errbuf[1024];
189                 WARNING ("memory: fclose: %s",
190                                 sstrerror (errno, errbuf, sizeof (errbuf)));
191         }
192
193         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
194                 return (-1);
195
196         swap_used = swap_total - (swap_free + swap_cached);
197
198         swap_submit ("used", swap_used);
199         swap_submit ("free", swap_free);
200         swap_submit ("cached", swap_cached);
201 /* #endif KERNEL_LINUX */
202
203 #elif HAVE_LIBKSTAT
204         unsigned long long swap_alloc;
205         unsigned long long swap_resv;
206         unsigned long long swap_avail;
207
208         struct anoninfo ai;
209
210         if (swapctl (SC_AINFO, &ai) == -1)
211         {
212                 char errbuf[1024];
213                 ERROR ("swap plugin: swapctl failed: %s",
214                                 sstrerror (errno, errbuf, sizeof (errbuf)));
215                 return (-1);
216         }
217
218         /*
219          * Calculations from:
220          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
221          * Also see:
222          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
223          * /usr/include/vm/anon.h
224          *
225          * In short, swap -s shows: allocated + reserved = used, available
226          *
227          * However, Solaris does not allow to allocated/reserved more than the
228          * available swap (physical memory + disk swap), so the pedant may
229          * prefer: allocated + unallocated = reserved, available
230          * 
231          * We map the above to: used + resv = n/a, free
232          *
233          * Does your brain hurt yet?  - Christophe Kalt
234          *
235          * Oh, and in case you wonder,
236          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
237          * can suffer from a 32bit overflow.
238          */
239         swap_alloc  = ai.ani_max - ai.ani_free;
240         swap_alloc *= pagesize;
241         swap_resv   = ai.ani_resv + ai.ani_free - ai.ani_max;
242         swap_resv  *= pagesize;
243         swap_avail  = ai.ani_max - ai.ani_resv;
244         swap_avail *= pagesize;
245
246         swap_submit ("used", swap_alloc);
247         swap_submit ("free", swap_avail);
248         swap_submit ("reserved", swap_resv - swap_alloc);
249 /* #endif HAVE_LIBKSTAT */
250
251 #elif defined(VM_SWAPUSAGE)
252         int              mib[3];
253         size_t           mib_len;
254         struct xsw_usage sw_usage;
255         size_t           sw_usage_len;
256
257         mib_len = 2;
258         mib[0]  = CTL_VM;
259         mib[1]  = VM_SWAPUSAGE;
260
261         sw_usage_len = sizeof (struct xsw_usage);
262
263         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
264                 return (-1);
265
266         /* The returned values are bytes. */
267         swap_submit ("used", sw_usage.xsu_used);
268         swap_submit ("free", sw_usage.xsu_avail);
269 /* #endif VM_SWAPUSAGE */
270
271 #elif HAVE_LIBKVM
272         struct kvm_swap data_s;
273         int             status;
274
275         unsigned long long used;
276         unsigned long long free;
277         unsigned long long total;
278
279         if (kvm_obj == NULL)
280                 return (-1);
281
282         /* only one structure => only get the grand total, no details */
283         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
284         if (status == -1)
285                 return (-1);
286
287         total = (unsigned long long) data_s.ksw_total;
288         used  = (unsigned long long) data_s.ksw_used;
289
290         total *= (unsigned long long) kvm_pagesize;
291         used  *= (unsigned long long) kvm_pagesize;
292
293         free = total - used;
294
295         swap_submit ("used", used);
296         swap_submit ("free", free);
297 /* #endif HAVE_LIBKVM */
298
299 #elif HAVE_LIBSTATGRAB
300         sg_swap_stats *swap;
301
302         swap = sg_get_swap_stats ();
303
304         if (swap == NULL)
305                 return (-1);
306
307         swap_submit ("used", swap->used);
308         swap_submit ("free", swap->free);
309 #endif /* HAVE_LIBSTATGRAB */
310
311         return (0);
312 } /* int swap_read */
313 #endif /* SWAP_HAVE_READ */
314
315 void module_register (modreg_e load)
316 {
317         if (load & MR_DATASETS)
318                 plugin_register_data_set (&data_set);
319
320 #if SWAP_HAVE_READ
321         if (load & MR_READ)
322         {
323                 plugin_register_init ("swap", swap_init);
324                 plugin_register_read ("swap", swap_read);
325         }
326 #endif /* SWAP_HAVE_READ */
327 } /* void module_register */