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