43275fc119540406960b2923ac58dee29c9bb1d2
[collectd.git] / src / swap.c
1 /**
2  * collectd - src/swap.c
3  * Copyright (C) 2005,2006  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; either version 2 of the License, or (at your
8  * option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License along
16  * with this program; if not, write to the Free Software Foundation, Inc.,
17  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
18  *
19  * Authors:
20  *   Florian octo Forster <octo at verplant.org>
21  **/
22
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
26
27 #if HAVE_SYS_SWAP_H
28 # include <sys/swap.h>
29 #endif
30 #if HAVE_SYS_PARAM_H
31 #  include <sys/param.h>
32 #endif
33 #if HAVE_SYS_SYSCTL_H
34 #  include <sys/sysctl.h>
35 #endif
36 #if HAVE_KVM_H
37 #  include <kvm.h>
38 #endif
39
40 #define MODULE_NAME "swap"
41
42 #if KERNEL_LINUX || HAVE_LIBKSTAT || defined(VM_SWAPUSAGE) || HAVE_LIBKVM || HAVE_LIBSTATGRAB
43 # define SWAP_HAVE_READ 1
44 #else
45 # define SWAP_HAVE_READ 0
46 #endif
47
48 #undef  MAX
49 #define MAX(x,y) ((x) > (y) ? (x) : (y))
50
51 static char *swap_file = "swap.rrd";
52
53 /* 1099511627776 == 1TB ought to be enough for anyone ;) */
54 static char *ds_def[] =
55 {
56         "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
57         "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
58         "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
59         "DS:resv:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
60         NULL
61 };
62 static int ds_num = 4;
63
64 #if KERNEL_LINUX
65 /* No global variables */
66 /* #endif KERNEL_LINUX */
67
68 #elif HAVE_LIBKSTAT
69 static unsigned long long pagesize;
70 static kstat_t *ksp;
71 /* #endif HAVE_LIBKSTAT */
72
73 #elif defined(VM_SWAPUSAGE)
74 /* No global variables */
75 /* #endif defined(VM_SWAPUSAGE) */
76
77 #elif HAVE_LIBKVM
78 static kvm_t *kvm_obj = NULL;
79 int kvm_pagesize;
80 /* #endif HAVE_LIBKVM */
81
82 #elif HAVE_LIBSTATGRAB
83 /* No global variables */
84 #endif /* HAVE_LIBSTATGRAB */
85
86 static void swap_init (void)
87 {
88 #if KERNEL_LINUX
89         /* No init stuff */
90 /* #endif KERNEL_LINUX */
91
92 #elif HAVE_LIBKSTAT
93         /* getpagesize(3C) tells me this does not fail.. */
94         pagesize = (unsigned long long) getpagesize ();
95         if (get_kstat (&ksp, "unix", 0, "system_pages"))
96                 ksp = NULL;
97 /* #endif HAVE_LIBKSTAT */
98
99 #elif defined(VM_SWAPUSAGE)
100         /* No init stuff */
101 /* #endif defined(VM_SWAPUSAGE) */
102
103 #elif HAVE_LIBKVM
104         if (kvm_obj != NULL)
105         {
106                 kvm_close (kvm_obj);
107                 kvm_obj = NULL;
108         }
109
110         kvm_pagesize = getpagesize ();
111
112         if ((kvm_obj = kvm_open (NULL, /* execfile */
113                                         NULL, /* corefile */
114                                         NULL, /* swapfile */
115                                         O_RDONLY, /* flags */
116                                         NULL)) /* errstr */
117                         == NULL)
118         {
119                 syslog (LOG_ERR, "swap plugin: kvm_open failed.");
120                 return;
121         }
122 /* #endif HAVE_LIBKVM */
123
124 #elif HAVE_LIBSTATGRAB
125         /* No init stuff */
126 #endif /* HAVE_LIBSTATGRAB */
127
128         return;
129 }
130
131 static void swap_write (char *host, char *inst, char *val)
132 {
133         rrd_update_file (host, swap_file, val, ds_def, ds_num);
134 }
135
136 #if SWAP_HAVE_READ
137 static void swap_submit (unsigned long long swap_used,
138                 unsigned long long swap_free,
139                 unsigned long long swap_cached,
140                 unsigned long long swap_resv)
141 {
142         char buffer[512];
143
144         if (snprintf (buffer, 512, "%u:%llu:%llu:%llu:%llu", (unsigned int) curtime,
145                                 swap_used, swap_free, swap_cached, swap_resv) >= 512)
146                 return;
147
148         plugin_submit (MODULE_NAME, "-", buffer);
149 }
150
151 static void swap_read (void)
152 {
153 #if KERNEL_LINUX
154         FILE *fh;
155         char buffer[1024];
156         
157         char *fields[8];
158         int numfields;
159
160         unsigned long long swap_used   = 0LL;
161         unsigned long long swap_cached = 0LL;
162         unsigned long long swap_free   = 0LL;
163         unsigned long long swap_total  = 0LL;
164
165         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
166         {
167                 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
168                 return;
169         }
170
171         while (fgets (buffer, 1024, fh) != NULL)
172         {
173                 unsigned long long *val = NULL;
174
175                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
176                         val = &swap_total;
177                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
178                         val = &swap_free;
179                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
180                         val = &swap_cached;
181                 else
182                         continue;
183
184                 numfields = strsplit (buffer, fields, 8);
185
186                 if (numfields < 2)
187                         continue;
188
189                 *val = atoll (fields[1]) * 1024LL;
190         }
191
192         if (fclose (fh))
193                 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
194
195         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
196                 return;
197
198         swap_used = swap_total - (swap_free + swap_cached);
199
200         swap_submit (swap_used, swap_free, swap_cached, -1LL);
201 /* #endif KERNEL_LINUX */
202
203 #elif HAVE_LIBKSTAT
204         unsigned long long swap_alloc;
205         unsigned long long swap_resv;
206         unsigned long long swap_avail;
207
208         struct anoninfo ai;
209
210         if (swapctl (SC_AINFO, &ai) == -1)
211         {
212                 syslog (LOG_ERR, "swap plugin: swapctl failed: %s",
213                                 strerror (errno));
214                 return;
215         }
216
217         /*
218          * Calculations from:
219          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
220          * Also see:
221          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
222          * /usr/include/vm/anon.h
223          *
224          * In short, swap -s shows: allocated + reserved = used, available
225          *
226          * However, Solaris does not allow to allocated/reserved more than the
227          * available swap (physical memory + disk swap), so the pedant may
228          * prefer: allocated + unallocated = reserved, available
229          * 
230          * We map the above to: used + resv = n/a, free
231          *
232          * Does your brain hurt yet?  - Christophe Kalt
233          *
234          * Oh, and in case you wonder,
235          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
236          * can suffer from a 32bit overflow.
237          */
238         swap_alloc  = ai.ani_max - ai.ani_free;
239         swap_alloc *= pagesize;
240         swap_resv   = ai.ani_resv + ai.ani_free - ai.ani_max;
241         swap_resv  *= pagesize;
242         swap_avail  = ai.ani_max - ai.ani_resv;
243         swap_avail *= pagesize;
244
245         swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
246 /* #endif HAVE_LIBKSTAT */
247
248 #elif defined(VM_SWAPUSAGE)
249         int              mib[3];
250         size_t           mib_len;
251         struct xsw_usage sw_usage;
252         size_t           sw_usage_len;
253
254         mib_len = 2;
255         mib[0]  = CTL_VM;
256         mib[1]  = VM_SWAPUSAGE;
257
258         sw_usage_len = sizeof (struct xsw_usage);
259
260         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
261                 return;
262
263         /* The returned values are bytes. */
264         swap_submit (sw_usage.xsu_used, sw_usage.xsu_avail, -1LL, -1LL);
265 /* #endif VM_SWAPUSAGE */
266
267 #elif HAVE_LIBKVM
268         struct kvm_swap data_s;
269         int             status;
270
271         unsigned long long used;
272         unsigned long long free;
273         unsigned long long total;
274
275         if (kvm_obj == NULL)
276                 return;
277
278         /* only one structure => only get the grand total, no details */
279         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
280         if (status == -1)
281                 return;
282
283         total = (unsigned long long) data_s.ksw_total;
284         used  = (unsigned long long) data_s.ksw_used;
285
286         total *= (unsigned long long) kvm_pagesize;
287         used  *= (unsigned long long) kvm_pagesize;
288
289         free = total - used;
290
291         swap_submit (used, free, -1LL, -1LL);
292 /* #endif HAVE_LIBKVM */
293
294 #elif HAVE_LIBSTATGRAB
295         sg_swap_stats *swap;
296
297         if ((swap = sg_get_swap_stats ()) != NULL)
298                 swap_submit (swap->used, swap->free, -1LL, -1LL);
299 #endif /* HAVE_LIBSTATGRAB */
300 }
301 #else
302 # define swap_read NULL
303 #endif /* SWAP_HAVE_READ */
304
305 void module_register (void)
306 {
307         plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);
308 }
309
310 #undef MODULE_NAME