Merge branch 'ff/local'
[collectd.git] / src / swap.c
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  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
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
38
39 #if HAVE_STATGRAB_H
40 # include <statgrab.h>
41 #endif
42
43 #undef  MAX
44 #define MAX(x,y) ((x) > (y) ? (x) : (y))
45
46 #if KERNEL_LINUX
47 /* No global variables */
48 /* #endif KERNEL_LINUX */
49
50 #elif HAVE_LIBKSTAT
51 static unsigned long long pagesize;
52 static kstat_t *ksp;
53 /* #endif HAVE_LIBKSTAT */
54
55 #elif defined(VM_SWAPUSAGE)
56 /* No global variables */
57 /* #endif defined(VM_SWAPUSAGE) */
58
59 #elif HAVE_LIBKVM_GETSWAPINFO
60 static kvm_t *kvm_obj = NULL;
61 int kvm_pagesize;
62 /* #endif HAVE_LIBKVM_GETSWAPINFO */
63
64 #elif HAVE_LIBSTATGRAB
65 /* No global variables */
66 /* #endif HAVE_LIBSTATGRAB */
67
68 #else
69 # error "No applicable input method."
70 #endif /* HAVE_LIBSTATGRAB */
71
72 static int swap_init (void)
73 {
74 #if KERNEL_LINUX
75         /* No init stuff */
76 /* #endif KERNEL_LINUX */
77
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 */
84
85 #elif defined(VM_SWAPUSAGE)
86         /* No init stuff */
87 /* #endif defined(VM_SWAPUSAGE) */
88
89 #elif HAVE_LIBKVM_GETSWAPINFO
90         if (kvm_obj != NULL)
91         {
92                 kvm_close (kvm_obj);
93                 kvm_obj = NULL;
94         }
95
96         kvm_pagesize = getpagesize ();
97
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 */
109
110 #elif HAVE_LIBSTATGRAB
111         /* No init stuff */
112 #endif /* HAVE_LIBSTATGRAB */
113
114         return (0);
115 }
116
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;
121
122         values[0].gauge = value;
123
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));
130
131         plugin_dispatch_values (&vl);
132 } /* void swap_submit */
133
134 static int swap_read (void)
135 {
136 #if KERNEL_LINUX
137         FILE *fh;
138         char buffer[1024];
139         
140         char *fields[8];
141         int numfields;
142
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;
147
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         }
155
156         while (fgets (buffer, 1024, fh) != NULL)
157         {
158                 unsigned long long *val = NULL;
159
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;
168
169                 numfields = strsplit (buffer, fields, 8);
170
171                 if (numfields < 2)
172                         continue;
173
174                 *val = atoll (fields[1]) * 1024LL;
175         }
176
177         if (fclose (fh))
178         {
179                 char errbuf[1024];
180                 WARNING ("memory: fclose: %s",
181                                 sstrerror (errno, errbuf, sizeof (errbuf)));
182         }
183
184         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
185                 return (-1);
186
187         swap_used = swap_total - (swap_free + swap_cached);
188
189         swap_submit ("used", swap_used);
190         swap_submit ("free", swap_free);
191         swap_submit ("cached", swap_cached);
192 /* #endif KERNEL_LINUX */
193
194 #elif HAVE_LIBKSTAT
195         unsigned long long swap_alloc;
196         unsigned long long swap_resv;
197         unsigned long long swap_avail;
198
199         struct anoninfo ai;
200
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         }
208
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;
236
237         swap_submit ("used", swap_alloc);
238         swap_submit ("free", swap_avail);
239         swap_submit ("reserved", swap_resv);
240 /* #endif HAVE_LIBKSTAT */
241
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;
247
248         mib_len = 2;
249         mib[0]  = CTL_VM;
250         mib[1]  = VM_SWAPUSAGE;
251
252         sw_usage_len = sizeof (struct xsw_usage);
253
254         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
255                 return (-1);
256
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 */
261
262 #elif HAVE_LIBKVM_GETSWAPINFO
263         struct kvm_swap data_s;
264         int             status;
265
266         unsigned long long used;
267         unsigned long long free;
268         unsigned long long total;
269
270         if (kvm_obj == NULL)
271                 return (-1);
272
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);
277
278         total = (unsigned long long) data_s.ksw_total;
279         used  = (unsigned long long) data_s.ksw_used;
280
281         total *= (unsigned long long) kvm_pagesize;
282         used  *= (unsigned long long) kvm_pagesize;
283
284         free = total - used;
285
286         swap_submit ("used", used);
287         swap_submit ("free", free);
288 /* #endif HAVE_LIBKVM_GETSWAPINFO */
289
290 #elif HAVE_LIBSTATGRAB
291         sg_swap_stats *swap;
292
293         swap = sg_get_swap_stats ();
294
295         if (swap == NULL)
296                 return (-1);
297
298         swap_submit ("used", swap->used);
299         swap_submit ("free", swap->free);
300 #endif /* HAVE_LIBSTATGRAB */
301
302         return (0);
303 } /* int swap_read */
304
305 void module_register (void)
306 {
307         plugin_register_init ("swap", swap_init);
308         plugin_register_read ("swap", swap_read);
309 } /* void module_register */