octo@leeloo:~/collectd $ svn merge -r786:HEAD branches/freebsd trunk
[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 int 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 = 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         /* unsigned long long swap_free; */
208
209         long long availrmem;
210         long long swapfs_minfree;
211
212         struct anoninfo ai;
213
214         if (swapctl (SC_AINFO, &ai) == -1)
215                 return;
216
217         availrmem      = get_kstat_value (ksp, "availrmem");
218         swapfs_minfree = get_kstat_value (ksp, "minfree");
219
220         if ((availrmem < 0LL) || (swapfs_minfree < 0LL))
221                 return;
222
223         /* 
224          * Calculations learned by reading
225          * http://www.itworld.com/Comp/2377/UIR980701perf/
226          *
227          * swap_resv += ani_resv
228          * swap_alloc += MAX(ani_resv, ani_max) - ani_free
229          * swap_avail += MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
230          * swap_free += ani_free + (availrmem - swapfs_minfree)
231          *
232          * To clear up the terminology a bit:
233          * resv  = reserved (but not neccessarily used)
234          * alloc = used     (neccessarily reserved)
235          * avail = not reserved  (neccessarily free)
236          * free  = not allocates (possibly reserved)
237          */
238         swap_resv  = pagesize * ai.ani_resv;
239         swap_alloc = pagesize * (MAX(ai.ani_resv, ai.ani_max) - ai.ani_free);
240         swap_avail = pagesize * (MAX(ai.ani_max - ai.ani_resv, 0) + (availrmem - swapfs_minfree));
241         /* swap_free  = pagesize * (ai.ani_free + (availrmem - swapfs_minfree)); */
242
243         swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
244 /* #endif HAVE_LIBKSTAT */
245
246 #elif defined(VM_SWAPUSAGE)
247         int              mib[3];
248         size_t           mib_len;
249         struct xsw_usage sw_usage;
250         size_t           sw_usage_len;
251         int              status;
252
253         mib_len = 2;
254         mib[0]  = CTL_VM;
255         mib[1]  = VM_SWAPUSAGE;
256
257         sw_usage_len = sizeof (struct xsw_usage);
258
259         if (sysctl (mib, mib_len, &sw_usage, &sw_usage_len, NULL, 0) != 0)
260                 return;
261
262         /* The returned values are bytes. */
263         swap_submit (sw_usage.xsu_used, sw_usage.xsu_avail, -1LL, -1LL);
264 /* #endif VM_SWAPUSAGE */
265
266 #elif HAVE_LIBKVM
267         struct kvm_swap data_s;
268         int             status;
269
270         unsigned long long used;
271         unsigned long long free;
272         unsigned long long total;
273
274         if (kvm_obj == NULL)
275                 return;
276
277         /* only one structure => only get the grand total, no details */
278         status = kvm_getswapinfo (kvm_obj, &data_s, 1, 0);
279         if (status == -1)
280                 return;
281
282         total = (unsigned long long) data_s.ksw_total;
283         used  = (unsigned long long) data_s.ksw_used;
284
285         total *= (unsigned long long) kvm_pagesize;
286         used  *= (unsigned long long) kvm_pagesize;
287
288         free = total - used;
289
290         swap_submit (used, free, -1LL, -1LL);
291 /* #endif HAVE_LIBKVM */
292
293 #elif HAVE_LIBSTATGRAB
294         sg_swap_stats *swap;
295
296         if ((swap = sg_get_swap_stats ()) != NULL)
297                 swap_submit (swap->used, swap->free, -1LL, -1LL);
298 #endif /* HAVE_LIBSTATGRAB */
299 }
300 #else
301 # define swap_read NULL
302 #endif /* SWAP_HAVE_READ */
303
304 void module_register (void)
305 {
306         plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);
307 }
308
309 #undef MODULE_NAME