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 **/
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_KVM_H
36 # include <kvm.h>
37 #endif
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
45 #undef MAX
46 #define MAX(x,y) ((x) > (y) ? (x) : (y))
48 static data_source_t data_source[1] =
49 {
50 {"value", DS_TYPE_GAUGE, 0, 1099511627776.0}
51 };
53 static data_set_t data_set =
54 {
55 "swap", 1, data_source
56 };
58 #if SWAP_HAVE_READ
59 #if KERNEL_LINUX
60 /* No global variables */
61 /* #endif KERNEL_LINUX */
63 #elif HAVE_LIBKSTAT
64 static unsigned long long pagesize;
65 static kstat_t *ksp;
66 /* #endif HAVE_LIBKSTAT */
68 #elif defined(VM_SWAPUSAGE)
69 /* No global variables */
70 /* #endif defined(VM_SWAPUSAGE) */
72 #elif HAVE_LIBKVM
73 static kvm_t *kvm_obj = NULL;
74 int kvm_pagesize;
75 /* #endif HAVE_LIBKVM */
77 #elif HAVE_LIBSTATGRAB
78 /* No global variables */
79 #endif /* HAVE_LIBSTATGRAB */
81 static int swap_init (void)
82 {
83 #if KERNEL_LINUX
84 /* No init stuff */
85 /* #endif KERNEL_LINUX */
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 */
94 #elif defined(VM_SWAPUSAGE)
95 /* No init stuff */
96 /* #endif defined(VM_SWAPUSAGE) */
98 #elif HAVE_LIBKVM
99 if (kvm_obj != NULL)
100 {
101 kvm_close (kvm_obj);
102 kvm_obj = NULL;
103 }
105 kvm_pagesize = getpagesize ();
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 syslog (LOG_ERR, "swap plugin: kvm_open failed.");
115 return (-1);
116 }
117 /* #endif HAVE_LIBKVM */
119 #elif HAVE_LIBSTATGRAB
120 /* No init stuff */
121 #endif /* HAVE_LIBSTATGRAB */
123 return (0);
124 }
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;
131 values[0].gauge = value;
133 vl.values = values;
134 vl.values_len = 1;
135 vl.time = time (NULL);
136 strcpy (vl.host, hostname);
137 strcpy (vl.plugin, "swap");
138 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
140 plugin_dispatch_values ("swap", &vl);
141 } /* void swap_submit */
143 static int swap_read (void)
144 {
145 #if KERNEL_LINUX
146 FILE *fh;
147 char buffer[1024];
149 char *fields[8];
150 int numfields;
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;
157 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
158 {
159 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
160 return (-1);
161 }
163 while (fgets (buffer, 1024, fh) != NULL)
164 {
165 unsigned long long *val = NULL;
167 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
168 val = &swap_total;
169 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
170 val = &swap_free;
171 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
172 val = &swap_cached;
173 else
174 continue;
176 numfields = strsplit (buffer, fields, 8);
178 if (numfields < 2)
179 continue;
181 *val = atoll (fields[1]) * 1024LL;
182 }
184 if (fclose (fh))
185 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
187 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
188 return (-1);
190 swap_used = swap_total - (swap_free + swap_cached);
192 swap_submit ("used", swap_used);
193 swap_submit ("free", swap_free);
194 swap_submit ("cached", swap_cached);
195 /* #endif KERNEL_LINUX */
197 #elif HAVE_LIBKSTAT
198 unsigned long long swap_alloc;
199 unsigned long long swap_resv;
200 unsigned long long swap_avail;
202 struct anoninfo ai;
204 if (swapctl (SC_AINFO, &ai) == -1)
205 {
206 syslog (LOG_ERR, "swap plugin: swapctl failed: %s",
207 strerror (errno));
208 return (-1);
209 }
211 /*
212 * Calculations from:
213 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
214 * Also see:
215 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
216 * /usr/include/vm/anon.h
217 *
218 * In short, swap -s shows: allocated + reserved = used, available
219 *
220 * However, Solaris does not allow to allocated/reserved more than the
221 * available swap (physical memory + disk swap), so the pedant may
222 * prefer: allocated + unallocated = reserved, available
223 *
224 * We map the above to: used + resv = n/a, free
225 *
226 * Does your brain hurt yet? - Christophe Kalt
227 *
228 * Oh, and in case you wonder,
229 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
230 * can suffer from a 32bit overflow.
231 */
232 swap_alloc = ai.ani_max - ai.ani_free;
233 swap_alloc *= pagesize;
234 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
235 swap_resv *= pagesize;
236 swap_avail = ai.ani_max - ai.ani_resv;
237 swap_avail *= pagesize;
239 swap_submit ("used", swap_alloc);
240 swap_submit ("free", swap_avail);
241 swap_submit ("reserved", swap_resv - swap_alloc);
242 /* #endif HAVE_LIBKSTAT */
244 #elif defined(VM_SWAPUSAGE)
245 int mib[3];
246 size_t mib_len;
247 struct xsw_usage sw_usage;
248 size_t sw_usage_len;
250 mib_len = 2;
251 mib[0] = CTL_VM;
252 mib[1] = VM_SWAPUSAGE;
254 sw_usage_len = sizeof (struct xsw_usage);
256 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
257 return (-1);
259 /* The returned values are bytes. */
260 swap_submit ("used", sw_usage.xsu_used);
261 swap_submit ("free", sw_usage.xsu_avail);
262 /* #endif VM_SWAPUSAGE */
264 #elif HAVE_LIBKVM
265 struct kvm_swap data_s;
266 int status;
268 unsigned long long used;
269 unsigned long long free;
270 unsigned long long total;
272 if (kvm_obj == NULL)
273 return (-1);
275 /* only one structure => only get the grand total, no details */
276 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
277 if (status == -1)
278 return (-1);
280 total = (unsigned long long) data_s.ksw_total;
281 used = (unsigned long long) data_s.ksw_used;
283 total *= (unsigned long long) kvm_pagesize;
284 used *= (unsigned long long) kvm_pagesize;
286 free = total - used;
288 swap_submit ("used", used);
289 swap_submit ("free", free);
290 /* #endif HAVE_LIBKVM */
292 #elif HAVE_LIBSTATGRAB
293 sg_swap_stats *swap;
295 swap = sg_get_swap_stats ();
297 if (swap == NULL)
298 return (-1);
300 swap_submit ("used", swap->used);
301 swap_submit ("free", swap->free);
302 #endif /* HAVE_LIBSTATGRAB */
304 return (0);
305 } /* int swap_read */
306 #endif /* SWAP_HAVE_READ */
308 void module_register (void)
309 {
310 plugin_register_data_set (&data_set);
312 #if SWAP_HAVE_READ
313 plugin_register_init ("swap", swap_init);
314 plugin_register_read ("swap", swap_read);
315 #endif /* SWAP_HAVE_READ */
316 }