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 #undef MAX
40 #define MAX(x,y) ((x) > (y) ? (x) : (y))
42 #if KERNEL_LINUX
43 /* No global variables */
44 /* #endif KERNEL_LINUX */
46 #elif HAVE_LIBKSTAT
47 static unsigned long long pagesize;
48 static kstat_t *ksp;
49 /* #endif HAVE_LIBKSTAT */
51 #elif defined(VM_SWAPUSAGE)
52 /* No global variables */
53 /* #endif defined(VM_SWAPUSAGE) */
55 #elif HAVE_LIBKVM
56 static kvm_t *kvm_obj = NULL;
57 int kvm_pagesize;
58 /* #endif HAVE_LIBKVM */
60 #elif HAVE_LIBSTATGRAB
61 /* No global variables */
62 /* #endif HAVE_LIBSTATGRAB */
64 #else
65 # error "No applicable input method."
66 #endif /* HAVE_LIBSTATGRAB */
68 static int swap_init (void)
69 {
70 #if KERNEL_LINUX
71 /* No init stuff */
72 /* #endif KERNEL_LINUX */
74 #elif HAVE_LIBKSTAT
75 /* getpagesize(3C) tells me this does not fail.. */
76 pagesize = (unsigned long long) getpagesize ();
77 if (get_kstat (&ksp, "unix", 0, "system_pages"))
78 ksp = NULL;
79 /* #endif HAVE_LIBKSTAT */
81 #elif defined(VM_SWAPUSAGE)
82 /* No init stuff */
83 /* #endif defined(VM_SWAPUSAGE) */
85 #elif HAVE_LIBKVM
86 if (kvm_obj != NULL)
87 {
88 kvm_close (kvm_obj);
89 kvm_obj = NULL;
90 }
92 kvm_pagesize = getpagesize ();
94 if ((kvm_obj = kvm_open (NULL, /* execfile */
95 NULL, /* corefile */
96 NULL, /* swapfile */
97 O_RDONLY, /* flags */
98 NULL)) /* errstr */
99 == NULL)
100 {
101 ERROR ("swap plugin: kvm_open failed.");
102 return (-1);
103 }
104 /* #endif HAVE_LIBKVM */
106 #elif HAVE_LIBSTATGRAB
107 /* No init stuff */
108 #endif /* HAVE_LIBSTATGRAB */
110 return (0);
111 }
113 static void swap_submit (const char *type_instance, double value)
114 {
115 value_t values[1];
116 value_list_t vl = VALUE_LIST_INIT;
118 values[0].gauge = value;
120 vl.values = values;
121 vl.values_len = 1;
122 vl.time = time (NULL);
123 strcpy (vl.host, hostname_g);
124 strcpy (vl.plugin, "swap");
125 strcpy (vl.type, "swap");
126 strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
128 plugin_dispatch_values (&vl);
129 } /* void swap_submit */
131 static int swap_read (void)
132 {
133 #if KERNEL_LINUX
134 FILE *fh;
135 char buffer[1024];
137 char *fields[8];
138 int numfields;
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)
146 {
147 char errbuf[1024];
148 WARNING ("memory: fopen: %s",
149 sstrerror (errno, errbuf, sizeof (errbuf)));
150 return (-1);
151 }
153 while (fgets (buffer, 1024, fh) != NULL)
154 {
155 unsigned long long *val = NULL;
157 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
158 val = &swap_total;
159 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
160 val = &swap_free;
161 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
162 val = &swap_cached;
163 else
164 continue;
166 numfields = strsplit (buffer, fields, 8);
168 if (numfields < 2)
169 continue;
171 *val = atoll (fields[1]) * 1024LL;
172 }
174 if (fclose (fh))
175 {
176 char errbuf[1024];
177 WARNING ("memory: fclose: %s",
178 sstrerror (errno, errbuf, sizeof (errbuf)));
179 }
181 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
182 return (-1);
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 */
191 #elif HAVE_LIBKSTAT
192 unsigned long long swap_alloc;
193 unsigned long long swap_resv;
194 unsigned long long swap_avail;
196 struct anoninfo ai;
198 if (swapctl (SC_AINFO, &ai) == -1)
199 {
200 char errbuf[1024];
201 ERROR ("swap plugin: swapctl failed: %s",
202 sstrerror (errno, errbuf, sizeof (errbuf)));
203 return (-1);
204 }
206 /*
207 * Calculations from:
208 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
209 * Also see:
210 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
211 * /usr/include/vm/anon.h
212 *
213 * In short, swap -s shows: allocated + reserved = used, available
214 *
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
218 *
219 * We map the above to: used + resv = n/a, free
220 *
221 * Does your brain hurt yet? - Christophe Kalt
222 *
223 * Oh, and in case you wonder,
224 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
225 * can suffer from a 32bit overflow.
226 */
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)
240 int mib[3];
241 size_t mib_len;
242 struct xsw_usage sw_usage;
243 size_t sw_usage_len;
245 mib_len = 2;
246 mib[0] = CTL_VM;
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)
252 return (-1);
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 */
259 #elif HAVE_LIBKVM
260 struct kvm_swap data_s;
261 int status;
263 unsigned long long used;
264 unsigned long long free;
265 unsigned long long total;
267 if (kvm_obj == NULL)
268 return (-1);
270 /* only one structure => only get the grand total, no details */
271 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
272 if (status == -1)
273 return (-1);
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;
281 free = total - used;
283 swap_submit ("used", used);
284 swap_submit ("free", free);
285 /* #endif HAVE_LIBKVM */
287 #elif HAVE_LIBSTATGRAB
288 sg_swap_stats *swap;
290 swap = sg_get_swap_stats ();
292 if (swap == NULL)
293 return (-1);
295 swap_submit ("used", swap->used);
296 swap_submit ("free", swap->free);
297 #endif /* HAVE_LIBSTATGRAB */
299 return (0);
300 } /* int swap_read */
302 void module_register (void)
303 {
304 plugin_register_init ("swap", swap_init);
305 plugin_register_read ("swap", swap_read);
306 } /* void module_register */