2 * collectd - src/swap.c
3 * Copyright (C) 2005,2006 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; either version 2 of the License, or (at your
8 * option) any later version.
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
20 * Florian octo Forster <octo at verplant.org>
28 # include <sys/swap.h>
31 # include <sys/param.h>
34 # include <sys/sysctl.h>
40 #define MODULE_NAME "swap"
42 #if KERNEL_LINUX || HAVE_LIBKSTAT || defined(VM_SWAPUSAGE) || HAVE_LIBKVM || HAVE_LIBSTATGRAB
43 # define SWAP_HAVE_READ 1
45 # define SWAP_HAVE_READ 0
49 #define MAX(x,y) ((x) > (y) ? (x) : (y))
51 static char *swap_file = "swap.rrd";
53 /* 1099511627776 == 1TB ought to be enough for anyone ;) */
54 static char *ds_def[] =
56 "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
57 "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
58 "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
59 "DS:resv:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
62 static int ds_num = 4;
65 /* No global variables */
66 /* #endif KERNEL_LINUX */
69 static unsigned long long pagesize;
71 /* #endif HAVE_LIBKSTAT */
73 #elif defined(VM_SWAPUSAGE)
74 /* No global variables */
75 /* #endif defined(VM_SWAPUSAGE) */
78 static kvm_t *kvm_obj = NULL;
80 /* #endif HAVE_LIBKVM */
82 #elif HAVE_LIBSTATGRAB
83 /* No global variables */
84 #endif /* HAVE_LIBSTATGRAB */
86 static void swap_init (void)
90 /* #endif KERNEL_LINUX */
93 /* getpagesize(3C) tells me this does not fail.. */
94 pagesize = (unsigned long long) getpagesize ();
95 if (get_kstat (&ksp, "unix", 0, "system_pages"))
97 /* #endif HAVE_LIBKSTAT */
99 #elif defined(VM_SWAPUSAGE)
101 /* #endif defined(VM_SWAPUSAGE) */
110 kvm_pagesize = getpagesize ();
112 if ((kvm_obj = kvm_open (NULL, /* execfile */
115 O_RDONLY, /* flags */
119 syslog (LOG_ERR, "swap plugin: kvm_open failed.");
122 /* #endif HAVE_LIBKVM */
124 #elif HAVE_LIBSTATGRAB
126 #endif /* HAVE_LIBSTATGRAB */
131 static void swap_write (char *host, char *inst, char *val)
133 rrd_update_file (host, swap_file, val, ds_def, ds_num);
137 static void swap_submit (unsigned long long swap_used,
138 unsigned long long swap_free,
139 unsigned long long swap_cached,
140 unsigned long long swap_resv)
144 if (snprintf (buffer, 512, "%u:%llu:%llu:%llu:%llu", (unsigned int) curtime,
145 swap_used, swap_free, swap_cached, swap_resv) >= 512)
148 plugin_submit (MODULE_NAME, "-", buffer);
151 static void swap_read (void)
160 unsigned long long swap_used = 0LL;
161 unsigned long long swap_cached = 0LL;
162 unsigned long long swap_free = 0LL;
163 unsigned long long swap_total = 0LL;
165 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
167 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
171 while (fgets (buffer, 1024, fh) != NULL)
173 unsigned long long *val = NULL;
175 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
177 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
179 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
184 numfields = strsplit (buffer, fields, 8);
189 *val = atoll (fields[1]) * 1024LL;
193 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
195 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
198 swap_used = swap_total - (swap_free + swap_cached);
200 swap_submit (swap_used, swap_free, swap_cached, -1LL);
201 /* #endif KERNEL_LINUX */
204 unsigned long long swap_alloc;
205 unsigned long long swap_resv;
206 unsigned long long swap_avail;
210 if (swapctl (SC_AINFO, &ai) == -1)
212 syslog (LOG_ERR, "swap plugin: swapctl failed: %s",
219 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
221 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
222 * /usr/include/vm/anon.h
224 * In short, swap -s shows: allocated + reserved = used, available
226 * However, Solaris does not allow to allocated/reserved more than the
227 * available swap (physical memory + disk swap), so the pedant may
228 * prefer: allocated + unallocated = reserved, available
230 * We map the above to: used + resv = n/a, free
232 * Does your brain hurt yet? - Christophe Kalt
234 * Oh, and in case you wonder,
235 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
236 * can suffer from a 32bit overflow.
238 swap_alloc = ai.ani_max - ai.ani_free;
239 swap_alloc *= pagesize;
240 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
241 swap_resv *= pagesize;
242 swap_avail = ai.ani_max - ai.ani_resv;
243 swap_avail *= pagesize;
245 swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
246 /* #endif HAVE_LIBKSTAT */
248 #elif defined(VM_SWAPUSAGE)
251 struct xsw_usage sw_usage;
256 mib[1] = VM_SWAPUSAGE;
258 sw_usage_len = sizeof (struct xsw_usage);
260 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
263 /* The returned values are bytes. */
264 swap_submit (sw_usage.xsu_used, sw_usage.xsu_avail, -1LL, -1LL);
265 /* #endif VM_SWAPUSAGE */
268 struct kvm_swap data_s;
271 unsigned long long used;
272 unsigned long long free;
273 unsigned long long total;
278 /* only one structure => only get the grand total, no details */
279 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
283 total = (unsigned long long) data_s.ksw_total;
284 used = (unsigned long long) data_s.ksw_used;
286 total *= (unsigned long long) kvm_pagesize;
287 used *= (unsigned long long) kvm_pagesize;
291 swap_submit (used, free, -1LL, -1LL);
292 /* #endif HAVE_LIBKVM */
294 #elif HAVE_LIBSTATGRAB
297 if ((swap = sg_get_swap_stats ()) != NULL)
298 swap_submit (swap->used, swap->free, -1LL, -1LL);
299 #endif /* HAVE_LIBSTATGRAB */
302 # define swap_read NULL
303 #endif /* SWAP_HAVE_READ */
305 void module_register (void)
307 plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);