2 * collectd - src/swap.c
3 * Copyright (C) 2005-2007 Florian octo Forster
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.
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.
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
19 * Florian octo Forster <octo at verplant.org>
27 # include <sys/swap.h>
30 # include <sys/param.h>
33 # include <sys/sysctl.h>
40 #define MAX(x,y) ((x) > (y) ? (x) : (y))
43 /* No global variables */
44 /* #endif KERNEL_LINUX */
47 static unsigned long long pagesize;
49 /* #endif HAVE_LIBKSTAT */
51 #elif defined(VM_SWAPUSAGE)
52 /* No global variables */
53 /* #endif defined(VM_SWAPUSAGE) */
56 static kvm_t *kvm_obj = NULL;
58 /* #endif HAVE_LIBKVM */
60 #elif HAVE_LIBSTATGRAB
61 /* No global variables */
62 /* #endif HAVE_LIBSTATGRAB */
65 # error "No applicable input method."
66 #endif /* HAVE_LIBSTATGRAB */
68 static int swap_init (void)
72 /* #endif KERNEL_LINUX */
75 /* getpagesize(3C) tells me this does not fail.. */
76 pagesize = (unsigned long long) getpagesize ();
77 if (get_kstat (&ksp, "unix", 0, "system_pages"))
79 /* #endif HAVE_LIBKSTAT */
81 #elif defined(VM_SWAPUSAGE)
83 /* #endif defined(VM_SWAPUSAGE) */
92 kvm_pagesize = getpagesize ();
94 if ((kvm_obj = kvm_open (NULL, /* execfile */
101 ERROR ("swap plugin: kvm_open failed.");
104 /* #endif HAVE_LIBKVM */
106 #elif HAVE_LIBSTATGRAB
108 #endif /* HAVE_LIBSTATGRAB */
113 static void swap_submit (const char *type_instance, double value)
116 value_list_t vl = VALUE_LIST_INIT;
118 values[0].gauge = value;
122 vl.time = time (NULL);
123 strcpy (vl.host, hostname_g);
124 strcpy (vl.plugin, "swap");
125 strcpy (vl.type, "swap");
126 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
128 plugin_dispatch_values (&vl);
129 } /* void swap_submit */
131 static int swap_read (void)
140 unsigned long long swap_used = 0LL;
141 unsigned long long swap_cached = 0LL;
142 unsigned long long swap_free = 0LL;
143 unsigned long long swap_total = 0LL;
145 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
148 WARNING ("memory: fopen: %s",
149 sstrerror (errno, errbuf, sizeof (errbuf)));
153 while (fgets (buffer, 1024, fh) != NULL)
155 unsigned long long *val = NULL;
157 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
159 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
161 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
166 numfields = strsplit (buffer, fields, 8);
171 *val = atoll (fields[1]) * 1024LL;
177 WARNING ("memory: fclose: %s",
178 sstrerror (errno, errbuf, sizeof (errbuf)));
181 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
184 swap_used = swap_total - (swap_free + swap_cached);
186 swap_submit ("used", swap_used);
187 swap_submit ("free", swap_free);
188 swap_submit ("cached", swap_cached);
189 /* #endif KERNEL_LINUX */
192 unsigned long long swap_alloc;
193 unsigned long long swap_resv;
194 unsigned long long swap_avail;
198 if (swapctl (SC_AINFO, &ai) == -1)
201 ERROR ("swap plugin: swapctl failed: %s",
202 sstrerror (errno, errbuf, sizeof (errbuf)));
208 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
210 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
211 * /usr/include/vm/anon.h
213 * In short, swap -s shows: allocated + reserved = used, available
215 * However, Solaris does not allow to allocated/reserved more than the
216 * available swap (physical memory + disk swap), so the pedant may
217 * prefer: allocated + unallocated = reserved, available
219 * We map the above to: used + resv = n/a, free
221 * Does your brain hurt yet? - Christophe Kalt
223 * Oh, and in case you wonder,
224 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
225 * can suffer from a 32bit overflow.
227 swap_alloc = ai.ani_max - ai.ani_free;
228 swap_alloc *= pagesize;
229 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
230 swap_resv *= pagesize;
231 swap_avail = ai.ani_max - ai.ani_resv;
232 swap_avail *= pagesize;
234 swap_submit ("used", swap_alloc);
235 swap_submit ("free", swap_avail);
236 swap_submit ("reserved", swap_resv);
237 /* #endif HAVE_LIBKSTAT */
239 #elif defined(VM_SWAPUSAGE)
242 struct xsw_usage sw_usage;
247 mib[1] = VM_SWAPUSAGE;
249 sw_usage_len = sizeof (struct xsw_usage);
251 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
254 /* The returned values are bytes. */
255 swap_submit ("used", sw_usage.xsu_used);
256 swap_submit ("free", sw_usage.xsu_avail);
257 /* #endif VM_SWAPUSAGE */
260 struct kvm_swap data_s;
263 unsigned long long used;
264 unsigned long long free;
265 unsigned long long total;
270 /* only one structure => only get the grand total, no details */
271 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
275 total = (unsigned long long) data_s.ksw_total;
276 used = (unsigned long long) data_s.ksw_used;
278 total *= (unsigned long long) kvm_pagesize;
279 used *= (unsigned long long) kvm_pagesize;
283 swap_submit ("used", used);
284 swap_submit ("free", free);
285 /* #endif HAVE_LIBKVM */
287 #elif HAVE_LIBSTATGRAB
290 swap = sg_get_swap_stats ();
295 swap_submit ("used", swap->used);
296 swap_submit ("free", swap->free);
297 #endif /* HAVE_LIBSTATGRAB */
300 } /* int swap_read */
302 void module_register (void)
304 plugin_register_init ("swap", swap_init);
305 plugin_register_read ("swap", swap_read);
306 } /* void module_register */