solaris-fixes branch: Applied the swap-patch by Christophe Kalt.
[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 #define MODULE_NAME "swap"
28
29 #if defined(KERNEL_LINUX) || defined(KERNEL_SOLARIS) || defined(HAVE_LIBSTATGRAB)
30 # define SWAP_HAVE_READ 1
31 #else
32 # define SWAP_HAVE_READ 0
33 #endif
34
35 #if HAVE_SYS_SWAP_H
36 # include <sys/swap.h>
37 #endif
38
39 #undef  MAX
40 #define MAX(x,y) ((x) > (y) ? (x) : (y))
41
42 static char *swap_file = "swap.rrd";
43
44 /* 1099511627776 == 1TB ought to be enough for anyone ;) */
45 static char *ds_def[] =
46 {
47         "DS:used:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
48         "DS:free:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
49         "DS:cached:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
50         "DS:resv:GAUGE:"COLLECTD_HEARTBEAT":0:1099511627776",
51         NULL
52 };
53 static int ds_num = 4;
54
55 #ifdef KERNEL_SOLARIS
56 static int pagesize;
57 static kstat_t *ksp;
58 #endif /* KERNEL_SOLARIS */
59
60 static void swap_init (void)
61 {
62 #ifdef KERNEL_SOLARIS
63         /* getpagesize(3C) tells me this does not fail.. */
64         pagesize = getpagesize ();
65         if (get_kstat (&ksp, "unix", 0, "system_pages"))
66                 ksp = NULL;
67 #endif /* KERNEL_SOLARIS */
68
69         return;
70 }
71
72 static void swap_write (char *host, char *inst, char *val)
73 {
74         rrd_update_file (host, swap_file, val, ds_def, ds_num);
75 }
76
77 #if SWAP_HAVE_READ
78 static void swap_submit (unsigned long long swap_used,
79                 unsigned long long swap_free,
80                 unsigned long long swap_cached,
81                 unsigned long long swap_resv)
82 {
83         char buffer[512];
84
85         if (snprintf (buffer, 512, "%u:%llu:%llu:%llu:%llu", (unsigned int) curtime,
86                                 swap_used, swap_free, swap_cached, swap_resv) >= 512)
87                 return;
88
89         plugin_submit (MODULE_NAME, "-", buffer);
90 }
91
92 static void swap_read (void)
93 {
94 #ifdef KERNEL_LINUX
95         FILE *fh;
96         char buffer[1024];
97         
98         char *fields[8];
99         int numfields;
100
101         unsigned long long swap_used   = 0LL;
102         unsigned long long swap_cached = 0LL;
103         unsigned long long swap_free   = 0LL;
104         unsigned long long swap_total  = 0LL;
105
106         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
107         {
108                 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
109                 return;
110         }
111
112         while (fgets (buffer, 1024, fh) != NULL)
113         {
114                 unsigned long long *val = NULL;
115
116                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
117                         val = &swap_total;
118                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
119                         val = &swap_free;
120                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
121                         val = &swap_cached;
122                 else
123                         continue;
124
125                 numfields = strsplit (buffer, fields, 8);
126
127                 if (numfields < 2)
128                         continue;
129
130                 *val = atoll (fields[1]) * 1024LL;
131         }
132
133         if (fclose (fh))
134                 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
135
136         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
137                 return;
138
139         swap_used = swap_total - (swap_free + swap_cached);
140
141         swap_submit (swap_used, swap_free, swap_cached, -1LL);
142 /* #endif defined(KERNEL_LINUX) */
143
144 #elif defined(KERNEL_SOLARIS)
145         unsigned long long swap_alloc;
146         unsigned long long swap_resv;
147         unsigned long long swap_avail;
148
149         struct anoninfo ai;
150
151         if (swapctl (SC_AINFO, &ai) == -1)
152                 return;
153
154         /*
155          * Calculations from:
156          * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
157          * Also see:
158          * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
159          * /usr/include/vm/anon.h
160          *
161          * In short, swap -s shows: allocated + reserved = used, available
162          *
163          * However, Solaris does not allow to allocated/reserved more than the
164          * available swap (physical memory + disk swap), so the pedant may
165          * prefer: allocated + unallocated = reserved, available
166          * 
167          * We map the above to: used + resv = n/a, free
168          *
169          * Does your brain hurt yet?  - Christophe Kalt
170          *
171          * Oh, and in case you wonder,
172          * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
173          * can suffer from a 32bit overflow.
174          */
175         swap_alloc  = ai.ani_max - ai.ani_free;
176         swap_alloc *= pagesize;
177         swap_resv   = ai.ani_resv + ai.ani_free - ai.ani_max;
178         swap_resv  *= pagesize;
179         swap_avail  = ai.ani_max - ai.ani_resv;
180         swap_avail *= pagesize;
181
182         swap_submit (swap_alloc, swap_avail, -1LL, swap_resv);
183 /* #endif defined(KERNEL_SOLARIS) */
184
185 #elif defined(HAVE_LIBSTATGRAB)
186         sg_swap_stats *swap;
187
188         if ((swap = sg_get_swap_stats ()) != NULL)
189                 swap_submit (swap->used, swap->free, -1LL, -1LL);
190 #endif /* HAVE_LIBSTATGRAB */
191 }
192 #else
193 # define swap_read NULL
194 #endif /* SWAP_HAVE_READ */
195
196 void module_register (void)
197 {
198         plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);
199 }
200
201 #undef MODULE_NAME