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 HAVE_STATGRAB_H
40 # include <statgrab.h>
41 #endif
43 #undef MAX
44 #define MAX(x,y) ((x) > (y) ? (x) : (y))
46 #if KERNEL_LINUX
47 /* No global variables */
48 /* #endif KERNEL_LINUX */
50 #elif HAVE_LIBKSTAT
51 static unsigned long long pagesize;
52 static kstat_t *ksp;
53 /* #endif HAVE_LIBKSTAT */
55 #elif defined(VM_SWAPUSAGE)
56 /* No global variables */
57 /* #endif defined(VM_SWAPUSAGE) */
59 #elif HAVE_LIBKVM_GETSWAPINFO
60 static kvm_t *kvm_obj = NULL;
61 int kvm_pagesize;
62 /* #endif HAVE_LIBKVM_GETSWAPINFO */
64 #elif HAVE_LIBSTATGRAB
65 /* No global variables */
66 /* #endif HAVE_LIBSTATGRAB */
68 #else
69 # error "No applicable input method."
70 #endif /* HAVE_LIBSTATGRAB */
72 static int swap_init (void)
73 {
74 #if KERNEL_LINUX
75 /* No init stuff */
76 /* #endif KERNEL_LINUX */
78 #elif HAVE_LIBKSTAT
79 /* getpagesize(3C) tells me this does not fail.. */
80 pagesize = (unsigned long long) getpagesize ();
81 if (get_kstat (&ksp, "unix", 0, "system_pages"))
82 ksp = NULL;
83 /* #endif HAVE_LIBKSTAT */
85 #elif defined(VM_SWAPUSAGE)
86 /* No init stuff */
87 /* #endif defined(VM_SWAPUSAGE) */
89 #elif HAVE_LIBKVM_GETSWAPINFO
90 if (kvm_obj != NULL)
91 {
92 kvm_close (kvm_obj);
93 kvm_obj = NULL;
94 }
96 kvm_pagesize = getpagesize ();
98 if ((kvm_obj = kvm_open (NULL, /* execfile */
99 NULL, /* corefile */
100 NULL, /* swapfile */
101 O_RDONLY, /* flags */
102 NULL)) /* errstr */
103 == NULL)
104 {
105 ERROR ("swap plugin: kvm_open failed.");
106 return (-1);
107 }
108 /* #endif HAVE_LIBKVM_GETSWAPINFO */
110 #elif HAVE_LIBSTATGRAB
111 /* No init stuff */
112 #endif /* HAVE_LIBSTATGRAB */
114 return (0);
115 }
117 static void swap_submit (const char *type_instance, double value)
118 {
119 value_t values[1];
120 value_list_t vl = VALUE_LIST_INIT;
122 values[0].gauge = value;
124 vl.values = values;
125 vl.values_len = 1;
126 vl.time = time (NULL);
127 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
128 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
129 sstrncpy (vl.type, "swap", sizeof (vl.type));
130 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
132 plugin_dispatch_values (&vl);
133 } /* void swap_submit */
135 static int swap_read (void)
136 {
137 #if KERNEL_LINUX
138 FILE *fh;
139 char buffer[1024];
141 char *fields[8];
142 int numfields;
144 unsigned long long swap_used = 0LL;
145 unsigned long long swap_cached = 0LL;
146 unsigned long long swap_free = 0LL;
147 unsigned long long swap_total = 0LL;
149 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
150 {
151 char errbuf[1024];
152 WARNING ("memory: fopen: %s",
153 sstrerror (errno, errbuf, sizeof (errbuf)));
154 return (-1);
155 }
157 while (fgets (buffer, 1024, fh) != NULL)
158 {
159 unsigned long long *val = NULL;
161 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
162 val = &swap_total;
163 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
164 val = &swap_free;
165 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
166 val = &swap_cached;
167 else
168 continue;
170 numfields = strsplit (buffer, fields, 8);
172 if (numfields < 2)
173 continue;
175 *val = atoll (fields[1]) * 1024LL;
176 }
178 if (fclose (fh))
179 {
180 char errbuf[1024];
181 WARNING ("memory: fclose: %s",
182 sstrerror (errno, errbuf, sizeof (errbuf)));
183 }
185 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
186 return (-1);
188 swap_used = swap_total - (swap_free + swap_cached);
190 swap_submit ("used", swap_used);
191 swap_submit ("free", swap_free);
192 swap_submit ("cached", swap_cached);
193 /* #endif KERNEL_LINUX */
195 #elif HAVE_LIBKSTAT
196 unsigned long long swap_alloc;
197 unsigned long long swap_resv;
198 unsigned long long swap_avail;
200 struct anoninfo ai;
202 if (swapctl (SC_AINFO, &ai) == -1)
203 {
204 char errbuf[1024];
205 ERROR ("swap plugin: swapctl failed: %s",
206 sstrerror (errno, errbuf, sizeof (errbuf)));
207 return (-1);
208 }
210 /*
211 * Calculations from:
212 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
213 * Also see:
214 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
215 * /usr/include/vm/anon.h
216 *
217 * In short, swap -s shows: allocated + reserved = used, available
218 *
219 * However, Solaris does not allow to allocated/reserved more than the
220 * available swap (physical memory + disk swap), so the pedant may
221 * prefer: allocated + unallocated = reserved, available
222 *
223 * We map the above to: used + resv = n/a, free
224 *
225 * Does your brain hurt yet? - Christophe Kalt
226 *
227 * Oh, and in case you wonder,
228 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
229 * can suffer from a 32bit overflow.
230 */
231 swap_alloc = ai.ani_max - ai.ani_free;
232 swap_alloc *= pagesize;
233 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
234 swap_resv *= pagesize;
235 swap_avail = ai.ani_max - ai.ani_resv;
236 swap_avail *= pagesize;
238 swap_submit ("used", swap_alloc);
239 swap_submit ("free", swap_avail);
240 swap_submit ("reserved", swap_resv);
241 /* #endif HAVE_LIBKSTAT */
243 #elif defined(VM_SWAPUSAGE)
244 int mib[3];
245 size_t mib_len;
246 struct xsw_usage sw_usage;
247 size_t sw_usage_len;
249 mib_len = 2;
250 mib[0] = CTL_VM;
251 mib[1] = VM_SWAPUSAGE;
253 sw_usage_len = sizeof (struct xsw_usage);
255 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
256 return (-1);
258 /* The returned values are bytes. */
259 swap_submit ("used", sw_usage.xsu_used);
260 swap_submit ("free", sw_usage.xsu_avail);
261 /* #endif VM_SWAPUSAGE */
263 #elif HAVE_LIBKVM_GETSWAPINFO
264 struct kvm_swap data_s;
265 int status;
267 unsigned long long used;
268 unsigned long long free;
269 unsigned long long total;
271 if (kvm_obj == NULL)
272 return (-1);
274 /* only one structure => only get the grand total, no details */
275 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
276 if (status == -1)
277 return (-1);
279 total = (unsigned long long) data_s.ksw_total;
280 used = (unsigned long long) data_s.ksw_used;
282 total *= (unsigned long long) kvm_pagesize;
283 used *= (unsigned long long) kvm_pagesize;
285 free = total - used;
287 swap_submit ("used", used);
288 swap_submit ("free", free);
289 /* #endif HAVE_LIBKVM_GETSWAPINFO */
291 #elif HAVE_LIBSTATGRAB
292 sg_swap_stats *swap;
294 swap = sg_get_swap_stats ();
296 if (swap == NULL)
297 return (-1);
299 swap_submit ("used", swap->used);
300 swap_submit ("free", swap->free);
301 #endif /* HAVE_LIBSTATGRAB */
303 return (0);
304 } /* int swap_read */
306 void module_register (void)
307 {
308 plugin_register_init ("swap", swap_init);
309 plugin_register_read ("swap", swap_read);
310 } /* void module_register */