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 ERROR ("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_g);
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 char errbuf[1024];
160 WARNING ("memory: fopen: %s",
161 sstrerror (errno, errbuf, sizeof (errbuf)));
162 return (-1);
163 }
165 while (fgets (buffer, 1024, fh) != NULL)
166 {
167 unsigned long long *val = NULL;
169 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
170 val = &swap_total;
171 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
172 val = &swap_free;
173 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
174 val = &swap_cached;
175 else
176 continue;
178 numfields = strsplit (buffer, fields, 8);
180 if (numfields < 2)
181 continue;
183 *val = atoll (fields[1]) * 1024LL;
184 }
186 if (fclose (fh))
187 {
188 char errbuf[1024];
189 WARNING ("memory: fclose: %s",
190 sstrerror (errno, errbuf, sizeof (errbuf)));
191 }
193 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
194 return (-1);
196 swap_used = swap_total - (swap_free + swap_cached);
198 swap_submit ("used", swap_used);
199 swap_submit ("free", swap_free);
200 swap_submit ("cached", swap_cached);
201 /* #endif KERNEL_LINUX */
203 #elif HAVE_LIBKSTAT
204 unsigned long long swap_alloc;
205 unsigned long long swap_resv;
206 unsigned long long swap_avail;
208 struct anoninfo ai;
210 if (swapctl (SC_AINFO, &ai) == -1)
211 {
212 char errbuf[1024];
213 ERROR ("swap plugin: swapctl failed: %s",
214 sstrerror (errno, errbuf, sizeof (errbuf)));
215 return (-1);
216 }
218 /*
219 * Calculations from:
220 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
221 * Also see:
222 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
223 * /usr/include/vm/anon.h
224 *
225 * In short, swap -s shows: allocated + reserved = used, available
226 *
227 * However, Solaris does not allow to allocated/reserved more than the
228 * available swap (physical memory + disk swap), so the pedant may
229 * prefer: allocated + unallocated = reserved, available
230 *
231 * We map the above to: used + resv = n/a, free
232 *
233 * Does your brain hurt yet? - Christophe Kalt
234 *
235 * Oh, and in case you wonder,
236 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
237 * can suffer from a 32bit overflow.
238 */
239 swap_alloc = ai.ani_max - ai.ani_free;
240 swap_alloc *= pagesize;
241 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
242 swap_resv *= pagesize;
243 swap_avail = ai.ani_max - ai.ani_resv;
244 swap_avail *= pagesize;
246 swap_submit ("used", swap_alloc);
247 swap_submit ("free", swap_avail);
248 swap_submit ("reserved", swap_resv - swap_alloc);
249 /* #endif HAVE_LIBKSTAT */
251 #elif defined(VM_SWAPUSAGE)
252 int mib[3];
253 size_t mib_len;
254 struct xsw_usage sw_usage;
255 size_t sw_usage_len;
257 mib_len = 2;
258 mib[0] = CTL_VM;
259 mib[1] = VM_SWAPUSAGE;
261 sw_usage_len = sizeof (struct xsw_usage);
263 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
264 return (-1);
266 /* The returned values are bytes. */
267 swap_submit ("used", sw_usage.xsu_used);
268 swap_submit ("free", sw_usage.xsu_avail);
269 /* #endif VM_SWAPUSAGE */
271 #elif HAVE_LIBKVM
272 struct kvm_swap data_s;
273 int status;
275 unsigned long long used;
276 unsigned long long free;
277 unsigned long long total;
279 if (kvm_obj == NULL)
280 return (-1);
282 /* only one structure => only get the grand total, no details */
283 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
284 if (status == -1)
285 return (-1);
287 total = (unsigned long long) data_s.ksw_total;
288 used = (unsigned long long) data_s.ksw_used;
290 total *= (unsigned long long) kvm_pagesize;
291 used *= (unsigned long long) kvm_pagesize;
293 free = total - used;
295 swap_submit ("used", used);
296 swap_submit ("free", free);
297 /* #endif HAVE_LIBKVM */
299 #elif HAVE_LIBSTATGRAB
300 sg_swap_stats *swap;
302 swap = sg_get_swap_stats ();
304 if (swap == NULL)
305 return (-1);
307 swap_submit ("used", swap->used);
308 swap_submit ("free", swap->free);
309 #endif /* HAVE_LIBSTATGRAB */
311 return (0);
312 } /* int swap_read */
313 #endif /* SWAP_HAVE_READ */
315 void module_register (void)
316 {
317 plugin_register_data_set (&data_set);
319 #if SWAP_HAVE_READ
320 plugin_register_init ("swap", swap_init);
321 plugin_register_read ("swap", swap_read);
322 #endif /* SWAP_HAVE_READ */
323 }