Merge branch 'ff/tail'
[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 #undef  MAX
40 #define MAX(x,y) ((x) > (y) ? (x) : (y))
41
42 #if KERNEL_LINUX
43 /* No global variables */
44 /* #endif KERNEL_LINUX */
45
46 #elif HAVE_LIBKSTAT
47 static unsigned long long pagesize;
48 static kstat_t *ksp;
49 /* #endif HAVE_LIBKSTAT */
50
51 #elif defined(VM_SWAPUSAGE)
52 /* No global variables */
53 /* #endif defined(VM_SWAPUSAGE) */
54
55 #elif HAVE_LIBKVM
56 static kvm_t *kvm_obj = NULL;
57 int kvm_pagesize;
58 /* #endif HAVE_LIBKVM */
59
60 #elif HAVE_LIBSTATGRAB
61 /* No global variables */
62 /* #endif HAVE_LIBSTATGRAB */
63
64 #else
65 # error "No applicable input method."
66 #endif /* HAVE_LIBSTATGRAB */
67
68 static int swap_init (void)
69 {
70 #if KERNEL_LINUX
71         /* No init stuff */
72 /* #endif KERNEL_LINUX */
73
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 */
80
81 #elif defined(VM_SWAPUSAGE)
82         /* No init stuff */
83 /* #endif defined(VM_SWAPUSAGE) */
84
85 #elif HAVE_LIBKVM
86         if (kvm_obj != NULL)
87         {
88                 kvm_close (kvm_obj);
89                 kvm_obj = NULL;
90         }
91
92         kvm_pagesize = getpagesize ();
93
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 */
105
106 #elif HAVE_LIBSTATGRAB
107         /* No init stuff */
108 #endif /* HAVE_LIBSTATGRAB */
109
110         return (0);
111 }
112
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;
117
118         values[0].gauge = value;
119
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         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
126
127         plugin_dispatch_values ("swap", &vl);
128 } /* void swap_submit */
129
130 static int swap_read (void)
131 {
132 #if KERNEL_LINUX
133         FILE *fh;
134         char buffer[1024];
135         
136         char *fields[8];
137         int numfields;
138
139         unsigned long long swap_used   = 0LL;
140         unsigned long long swap_cached = 0LL;
141         unsigned long long swap_free   = 0LL;
142         unsigned long long swap_total  = 0LL;
143
144         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
145         {
146                 char errbuf[1024];
147                 WARNING ("memory: fopen: %s",
148                                 sstrerror (errno, errbuf, sizeof (errbuf)));
149                 return (-1);
150         }
151
152         while (fgets (buffer, 1024, fh) != NULL)
153         {
154                 unsigned long long *val = NULL;
155
156                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
157                         val = &swap_total;
158                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
159                         val = &swap_free;
160                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
161                         val = &swap_cached;
162                 else
163                         continue;
164
165                 numfields = strsplit (buffer, fields, 8);
166
167                 if (numfields < 2)
168                         continue;
169
170                 *val = atoll (fields[1]) * 1024LL;
171         }
172
173         if (fclose (fh))
174         {
175                 char errbuf[1024];
176                 WARNING ("memory: fclose: %s",
177                                 sstrerror (errno, errbuf, sizeof (errbuf)));
178         }
179
180         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
181                 return (-1);
182
183         swap_used = swap_total - (swap_free + swap_cached);
184
185         swap_submit ("used", swap_used);
186         swap_submit ("free", swap_free);
187         swap_submit ("cached", swap_cached);
188 /* #endif KERNEL_LINUX */
189
190 #elif HAVE_LIBKSTAT
191         unsigned long long swap_alloc;
192         unsigned long long swap_resv;
193         unsigned long long swap_avail;
194
195         struct anoninfo ai;
196
197         if (swapctl (SC_AINFO, &ai) == -1)
198         {
199                 char errbuf[1024];
200                 ERROR ("swap plugin: swapctl failed: %s",
201                                 sstrerror (errno, errbuf, sizeof (errbuf)));
202                 return (-1);
203         }
204
205         /*
206          * Calculations from:
207          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
208          * Also see:
209          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
210          * /usr/include/vm/anon.h
211          *
212          * In short, swap -s shows: allocated + reserved = used, available
213          *
214          * However, Solaris does not allow to allocated/reserved more than the
215          * available swap (physical memory + disk swap), so the pedant may
216          * prefer: allocated + unallocated = reserved, available
217          * 
218          * We map the above to: used + resv = n/a, free
219          *
220          * Does your brain hurt yet?  - Christophe Kalt
221          *
222          * Oh, and in case you wonder,
223          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
224          * can suffer from a 32bit overflow.
225          */
226         swap_alloc  = ai.ani_max - ai.ani_free;
227         swap_alloc *= pagesize;
228         swap_resv   = ai.ani_resv + ai.ani_free - ai.ani_max;
229         swap_resv  *= pagesize;
230         swap_avail  = ai.ani_max - ai.ani_resv;
231         swap_avail *= pagesize;
232
233         swap_submit ("used", swap_alloc);
234         swap_submit ("free", swap_avail);
235         swap_submit ("reserved", swap_resv);
236 /* #endif HAVE_LIBKSTAT */
237
238 #elif defined(VM_SWAPUSAGE)
239         int              mib[3];
240         size_t           mib_len;
241         struct xsw_usage sw_usage;
242         size_t           sw_usage_len;
243
244         mib_len = 2;
245         mib[0]  = CTL_VM;
246         mib[1]  = VM_SWAPUSAGE;
247
248         sw_usage_len = sizeof (struct xsw_usage);
249
250         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
251                 return (-1);
252
253         /* The returned values are bytes. */
254         swap_submit ("used", sw_usage.xsu_used);
255         swap_submit ("free", sw_usage.xsu_avail);
256 /* #endif VM_SWAPUSAGE */
257
258 #elif HAVE_LIBKVM
259         struct kvm_swap data_s;
260         int             status;
261
262         unsigned long long used;
263         unsigned long long free;
264         unsigned long long total;
265
266         if (kvm_obj == NULL)
267                 return (-1);
268
269         /* only one structure => only get the grand total, no details */
270         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
271         if (status == -1)
272                 return (-1);
273
274         total = (unsigned long long) data_s.ksw_total;
275         used  = (unsigned long long) data_s.ksw_used;
276
277         total *= (unsigned long long) kvm_pagesize;
278         used  *= (unsigned long long) kvm_pagesize;
279
280         free = total - used;
281
282         swap_submit ("used", used);
283         swap_submit ("free", free);
284 /* #endif HAVE_LIBKVM */
285
286 #elif HAVE_LIBSTATGRAB
287         sg_swap_stats *swap;
288
289         swap = sg_get_swap_stats ();
290
291         if (swap == NULL)
292                 return (-1);
293
294         swap_submit ("used", swap->used);
295         swap_submit ("free", swap->free);
296 #endif /* HAVE_LIBSTATGRAB */
297
298         return (0);
299 } /* int swap_read */
300
301 void module_register (void)
302 {
303         plugin_register_init ("swap", swap_init);
304         plugin_register_read ("swap", swap_read);
305 } /* void module_register */