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 **/
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
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_SYS_DKSTAT_H
36 # include <sys/dkstat.h>
37 #endif
38 #if HAVE_KVM_H
39 # include <kvm.h>
40 #endif
42 #if HAVE_STATGRAB_H
43 # include <statgrab.h>
44 #endif
46 #undef MAX
47 #define MAX(x,y) ((x) > (y) ? (x) : (y))
49 #if KERNEL_LINUX
50 /* No global variables */
51 /* #endif KERNEL_LINUX */
53 #elif HAVE_LIBKSTAT
54 static unsigned long long pagesize;
55 static kstat_t *ksp;
56 /* #endif HAVE_LIBKSTAT */
58 #elif HAVE_SWAPCTL
59 /* No global variables */
60 /* #endif HAVE_SWAPCTL */
62 #elif defined(VM_SWAPUSAGE)
63 /* No global variables */
64 /* #endif defined(VM_SWAPUSAGE) */
66 #elif HAVE_LIBKVM_GETSWAPINFO
67 static kvm_t *kvm_obj = NULL;
68 int kvm_pagesize;
69 /* #endif HAVE_LIBKVM_GETSWAPINFO */
71 #elif HAVE_LIBSTATGRAB
72 /* No global variables */
73 /* #endif HAVE_LIBSTATGRAB */
75 #else
76 # error "No applicable input method."
77 #endif /* HAVE_LIBSTATGRAB */
79 static int swap_init (void)
80 {
81 #if KERNEL_LINUX
82 /* No init stuff */
83 /* #endif KERNEL_LINUX */
85 #elif HAVE_LIBKSTAT
86 /* getpagesize(3C) tells me this does not fail.. */
87 pagesize = (unsigned long long) getpagesize ();
88 if (get_kstat (&ksp, "unix", 0, "system_pages"))
89 ksp = NULL;
90 /* #endif HAVE_LIBKSTAT */
92 #elif HAVE_SWAPCTL
93 /* No init stuff */
94 /* #endif HAVE_SWAPCTL */
96 #elif defined(VM_SWAPUSAGE)
97 /* No init stuff */
98 /* #endif defined(VM_SWAPUSAGE) */
100 #elif HAVE_LIBKVM_GETSWAPINFO
101 if (kvm_obj != NULL)
102 {
103 kvm_close (kvm_obj);
104 kvm_obj = NULL;
105 }
107 kvm_pagesize = getpagesize ();
109 if ((kvm_obj = kvm_open (NULL, /* execfile */
110 NULL, /* corefile */
111 NULL, /* swapfile */
112 O_RDONLY, /* flags */
113 NULL)) /* errstr */
114 == NULL)
115 {
116 ERROR ("swap plugin: kvm_open failed.");
117 return (-1);
118 }
119 /* #endif HAVE_LIBKVM_GETSWAPINFO */
121 #elif HAVE_LIBSTATGRAB
122 /* No init stuff */
123 #endif /* HAVE_LIBSTATGRAB */
125 return (0);
126 }
128 static void swap_submit (const char *type_instance, double value)
129 {
130 value_t values[1];
131 value_list_t vl = VALUE_LIST_INIT;
133 values[0].gauge = value;
135 vl.values = values;
136 vl.values_len = 1;
137 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
138 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
139 sstrncpy (vl.type, "swap", sizeof (vl.type));
140 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
142 plugin_dispatch_values (&vl);
143 } /* void swap_submit */
145 static int swap_read (void)
146 {
147 #if KERNEL_LINUX
148 FILE *fh;
149 char buffer[1024];
151 char *fields[8];
152 int numfields;
154 unsigned long long swap_used = 0LL;
155 unsigned long long swap_cached = 0LL;
156 unsigned long long swap_free = 0LL;
157 unsigned long long swap_total = 0LL;
159 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
160 {
161 char errbuf[1024];
162 WARNING ("memory: fopen: %s",
163 sstrerror (errno, errbuf, sizeof (errbuf)));
164 return (-1);
165 }
167 while (fgets (buffer, 1024, fh) != NULL)
168 {
169 unsigned long long *val = NULL;
171 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
172 val = &swap_total;
173 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
174 val = &swap_free;
175 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
176 val = &swap_cached;
177 else
178 continue;
180 numfields = strsplit (buffer, fields, 8);
182 if (numfields < 2)
183 continue;
185 *val = atoll (fields[1]) * 1024LL;
186 }
188 if (fclose (fh))
189 {
190 char errbuf[1024];
191 WARNING ("memory: fclose: %s",
192 sstrerror (errno, errbuf, sizeof (errbuf)));
193 }
195 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
196 return (-1);
198 swap_used = swap_total - (swap_free + swap_cached);
200 swap_submit ("used", swap_used);
201 swap_submit ("free", swap_free);
202 swap_submit ("cached", swap_cached);
203 /* #endif KERNEL_LINUX */
205 #elif HAVE_LIBKSTAT
206 unsigned long long swap_alloc;
207 unsigned long long swap_resv;
208 unsigned long long swap_avail;
210 struct anoninfo ai;
212 if (swapctl (SC_AINFO, &ai) == -1)
213 {
214 char errbuf[1024];
215 ERROR ("swap plugin: swapctl failed: %s",
216 sstrerror (errno, errbuf, sizeof (errbuf)));
217 return (-1);
218 }
220 /*
221 * Calculations from:
222 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
223 * Also see:
224 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
225 * /usr/include/vm/anon.h
226 *
227 * In short, swap -s shows: allocated + reserved = used, available
228 *
229 * However, Solaris does not allow to allocated/reserved more than the
230 * available swap (physical memory + disk swap), so the pedant may
231 * prefer: allocated + unallocated = reserved, available
232 *
233 * We map the above to: used + resv = n/a, free
234 *
235 * Does your brain hurt yet? - Christophe Kalt
236 *
237 * Oh, and in case you wonder,
238 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
239 * can suffer from a 32bit overflow.
240 */
241 swap_alloc = ai.ani_max - ai.ani_free;
242 swap_alloc *= pagesize;
243 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
244 swap_resv *= pagesize;
245 swap_avail = ai.ani_max - ai.ani_resv;
246 swap_avail *= pagesize;
248 swap_submit ("used", swap_alloc);
249 swap_submit ("free", swap_avail);
250 swap_submit ("reserved", swap_resv);
251 /* #endif HAVE_LIBKSTAT */
253 #elif HAVE_SWAPCTL
254 struct swapent *swap_entries;
255 int swap_num;
256 int status;
257 int i;
259 uint64_t used = 0;
260 uint64_t total = 0;
262 /*
263 * XXX: This is the syntax for the *BSD `swapctl', which has the
264 * following prototype:
265 * swapctl (int cmd, void *arg, int misc);
266 *
267 * HP-UX and Solaris (and possibly other UNIXes) provide `swapctl',
268 * too, but with the following prototype:
269 * swapctl (int cmd, void *arg);
270 *
271 * Solaris is usually handled in the KSTAT case above. For other UNIXes
272 * a separate case for the other version of `swapctl' may be necessary.
273 */
274 swap_num = swapctl (SWAP_NSWAP, NULL, 0);
275 if (swap_num < 0)
276 {
277 ERROR ("swap plugin: swapctl (SWAP_NSWAP) failed with status %i.",
278 swap_num);
279 return (-1);
280 }
281 else if (swap_num == 0)
282 return (0);
284 swap_entries = calloc (swap_num, sizeof (*swap_entries));
285 if (swap_entries == NULL)
286 {
287 ERROR ("swap plugin: calloc failed.");
288 return (-1);
289 }
291 status = swapctl (SWAP_STATS, swap_entries, swap_num);
292 if (status != swap_num)
293 {
294 ERROR ("swap plugin: swapctl (SWAP_STATS) failed with status %i.",
295 status);
296 sfree (swap_entries);
297 return (-1);
298 }
300 #if defined(DEV_BSIZE) && (DEV_BSIZE > 0)
301 # define C_SWAP_BLOCK_SIZE ((uint64_t) DEV_BSIZE)
302 #else
303 # define C_SWAP_BLOCK_SIZE ((uint64_t) 512)
304 #endif
306 for (i = 0; i < swap_num; i++)
307 {
308 if ((swap_entries[i].se_flags & SWF_ENABLE) == 0)
309 continue;
311 used += ((uint64_t) swap_entries[i].se_inuse)
312 * C_SWAP_BLOCK_SIZE;
313 total += ((uint64_t) swap_entries[i].se_nblks)
314 * C_SWAP_BLOCK_SIZE;
315 }
317 if (total < used)
318 {
319 ERROR ("swap plugin: Total swap space (%"PRIu64") "
320 "is less than used swap space (%"PRIu64").",
321 total, used);
322 return (-1);
323 }
325 swap_submit ("used", (gauge_t) used);
326 swap_submit ("free", (gauge_t) (total - used));
328 sfree (swap_entries);
329 /* #endif HAVE_SWAPCTL */
331 #elif defined(VM_SWAPUSAGE)
332 int mib[3];
333 size_t mib_len;
334 struct xsw_usage sw_usage;
335 size_t sw_usage_len;
337 mib_len = 2;
338 mib[0] = CTL_VM;
339 mib[1] = VM_SWAPUSAGE;
341 sw_usage_len = sizeof (struct xsw_usage);
343 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
344 return (-1);
346 /* The returned values are bytes. */
347 swap_submit ("used", sw_usage.xsu_used);
348 swap_submit ("free", sw_usage.xsu_avail);
349 /* #endif VM_SWAPUSAGE */
351 #elif HAVE_LIBKVM_GETSWAPINFO
352 struct kvm_swap data_s;
353 int status;
355 unsigned long long used;
356 unsigned long long free;
357 unsigned long long total;
359 if (kvm_obj == NULL)
360 return (-1);
362 /* only one structure => only get the grand total, no details */
363 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
364 if (status == -1)
365 return (-1);
367 total = (unsigned long long) data_s.ksw_total;
368 used = (unsigned long long) data_s.ksw_used;
370 total *= (unsigned long long) kvm_pagesize;
371 used *= (unsigned long long) kvm_pagesize;
373 free = total - used;
375 swap_submit ("used", used);
376 swap_submit ("free", free);
377 /* #endif HAVE_LIBKVM_GETSWAPINFO */
379 #elif HAVE_LIBSTATGRAB
380 sg_swap_stats *swap;
382 swap = sg_get_swap_stats ();
384 if (swap == NULL)
385 return (-1);
387 swap_submit ("used", swap->used);
388 swap_submit ("free", swap->free);
389 #endif /* HAVE_LIBSTATGRAB */
391 return (0);
392 } /* int swap_read */
394 void module_register (void)
395 {
396 plugin_register_init ("swap", swap_init);
397 plugin_register_read ("swap", swap_read);
398 } /* void module_register */