b8b5f09830629e57dd34c80b739a38e0e7e6cee3
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 sstrncpy (vl.host, hostname_g, sizeof (vl.host));
127 sstrncpy (vl.plugin, "swap", sizeof (vl.plugin));
128 sstrncpy (vl.type, "swap", sizeof (vl.type));
129 sstrncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
131 plugin_dispatch_values (&vl);
132 } /* void swap_submit */
134 static int swap_read (void)
135 {
136 #if KERNEL_LINUX
137 FILE *fh;
138 char buffer[1024];
140 char *fields[8];
141 int numfields;
143 unsigned long long swap_used = 0LL;
144 unsigned long long swap_cached = 0LL;
145 unsigned long long swap_free = 0LL;
146 unsigned long long swap_total = 0LL;
148 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
149 {
150 char errbuf[1024];
151 WARNING ("memory: fopen: %s",
152 sstrerror (errno, errbuf, sizeof (errbuf)));
153 return (-1);
154 }
156 while (fgets (buffer, 1024, fh) != NULL)
157 {
158 unsigned long long *val = NULL;
160 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
161 val = &swap_total;
162 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
163 val = &swap_free;
164 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
165 val = &swap_cached;
166 else
167 continue;
169 numfields = strsplit (buffer, fields, 8);
171 if (numfields < 2)
172 continue;
174 *val = atoll (fields[1]) * 1024LL;
175 }
177 if (fclose (fh))
178 {
179 char errbuf[1024];
180 WARNING ("memory: fclose: %s",
181 sstrerror (errno, errbuf, sizeof (errbuf)));
182 }
184 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
185 return (-1);
187 swap_used = swap_total - (swap_free + swap_cached);
189 swap_submit ("used", swap_used);
190 swap_submit ("free", swap_free);
191 swap_submit ("cached", swap_cached);
192 /* #endif KERNEL_LINUX */
194 #elif HAVE_LIBKSTAT
195 unsigned long long swap_alloc;
196 unsigned long long swap_resv;
197 unsigned long long swap_avail;
199 struct anoninfo ai;
201 if (swapctl (SC_AINFO, &ai) == -1)
202 {
203 char errbuf[1024];
204 ERROR ("swap plugin: swapctl failed: %s",
205 sstrerror (errno, errbuf, sizeof (errbuf)));
206 return (-1);
207 }
209 /*
210 * Calculations from:
211 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
212 * Also see:
213 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
214 * /usr/include/vm/anon.h
215 *
216 * In short, swap -s shows: allocated + reserved = used, available
217 *
218 * However, Solaris does not allow to allocated/reserved more than the
219 * available swap (physical memory + disk swap), so the pedant may
220 * prefer: allocated + unallocated = reserved, available
221 *
222 * We map the above to: used + resv = n/a, free
223 *
224 * Does your brain hurt yet? - Christophe Kalt
225 *
226 * Oh, and in case you wonder,
227 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
228 * can suffer from a 32bit overflow.
229 */
230 swap_alloc = ai.ani_max - ai.ani_free;
231 swap_alloc *= pagesize;
232 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
233 swap_resv *= pagesize;
234 swap_avail = ai.ani_max - ai.ani_resv;
235 swap_avail *= pagesize;
237 swap_submit ("used", swap_alloc);
238 swap_submit ("free", swap_avail);
239 swap_submit ("reserved", swap_resv);
240 /* #endif HAVE_LIBKSTAT */
242 #elif defined(VM_SWAPUSAGE)
243 int mib[3];
244 size_t mib_len;
245 struct xsw_usage sw_usage;
246 size_t sw_usage_len;
248 mib_len = 2;
249 mib[0] = CTL_VM;
250 mib[1] = VM_SWAPUSAGE;
252 sw_usage_len = sizeof (struct xsw_usage);
254 if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
255 return (-1);
257 /* The returned values are bytes. */
258 swap_submit ("used", sw_usage.xsu_used);
259 swap_submit ("free", sw_usage.xsu_avail);
260 /* #endif VM_SWAPUSAGE */
262 #elif HAVE_LIBKVM_GETSWAPINFO
263 struct kvm_swap data_s;
264 int status;
266 unsigned long long used;
267 unsigned long long free;
268 unsigned long long total;
270 if (kvm_obj == NULL)
271 return (-1);
273 /* only one structure => only get the grand total, no details */
274 status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
275 if (status == -1)
276 return (-1);
278 total = (unsigned long long) data_s.ksw_total;
279 used = (unsigned long long) data_s.ksw_used;
281 total *= (unsigned long long) kvm_pagesize;
282 used *= (unsigned long long) kvm_pagesize;
284 free = total - used;
286 swap_submit ("used", used);
287 swap_submit ("free", free);
288 /* #endif HAVE_LIBKVM_GETSWAPINFO */
290 #elif HAVE_LIBSTATGRAB
291 sg_swap_stats *swap;
293 swap = sg_get_swap_stats ();
295 if (swap == NULL)
296 return (-1);
298 swap_submit ("used", swap->used);
299 swap_submit ("free", swap->free);
300 #endif /* HAVE_LIBSTATGRAB */
302 return (0);
303 } /* int swap_read */
305 void module_register (void)
306 {
307 plugin_register_init ("swap", swap_init);
308 plugin_register_read ("swap", swap_read);
309 } /* void module_register */