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 **/
23 #include "collectd.h"
24 #include "common.h"
25 #include "plugin.h"
27 #define MODULE_NAME "swap"
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
35 #if HAVE_SYS_SWAP_H
36 # include <sys/swap.h>
37 #endif
39 #undef MAX
40 #define MAX(x,y) ((x) > (y) ? (x) : (y))
42 static char *swap_file = "swap.rrd";
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;
55 #ifdef KERNEL_SOLARIS
56 static unsigned long long pagesize;
57 static kstat_t *ksp;
58 #endif /* KERNEL_SOLARIS */
60 static void swap_init (void)
61 {
62 #ifdef KERNEL_SOLARIS
63 /* getpagesize(3C) tells me this does not fail.. */
64 pagesize = (unsigned long long) getpagesize ();
65 if (get_kstat (&ksp, "unix", 0, "system_pages"))
66 ksp = NULL;
67 #endif /* KERNEL_SOLARIS */
69 return;
70 }
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 }
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];
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;
89 plugin_submit (MODULE_NAME, "-", buffer);
90 }
92 static void swap_read (void)
93 {
94 #ifdef KERNEL_LINUX
95 FILE *fh;
96 char buffer[1024];
98 char *fields[8];
99 int numfields;
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;
106 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
107 {
108 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
109 return;
110 }
112 while (fgets (buffer, 1024, fh) != NULL)
113 {
114 unsigned long long *val = NULL;
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;
125 numfields = strsplit (buffer, fields, 8);
127 if (numfields < 2)
128 continue;
130 *val = atoll (fields[1]) * 1024LL;
131 }
133 if (fclose (fh))
134 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
136 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
137 return;
139 swap_used = swap_total - (swap_free + swap_cached);
141 swap_submit (swap_used, swap_free, swap_cached, -1LL);
142 /* #endif defined(KERNEL_LINUX) */
144 #elif defined(KERNEL_SOLARIS)
145 unsigned long long swap_alloc;
146 unsigned long long swap_resv;
147 unsigned long long swap_avail;
149 struct anoninfo ai;
151 if (swapctl (SC_AINFO, &ai) == -1)
152 {
153 syslog (LOG_ERR, "swap plugin: swapctl failed: %s",
154 strerror (errno));
155 return;
156 }
158 /*
159 * Calculations from:
160 * http://cvs.opensolaris.org/source/xref/on/usr/src/cmd/swap/swap.c
161 * Also see:
162 * http://www.itworld.com/Comp/2377/UIR980701perf/ (outdated?)
163 * /usr/include/vm/anon.h
164 *
165 * In short, swap -s shows: allocated + reserved = used, available
166 *
167 * However, Solaris does not allow to allocated/reserved more than the
168 * available swap (physical memory + disk swap), so the pedant may
169 * prefer: allocated + unallocated = reserved, available
170 *
171 * We map the above to: used + resv = n/a, free
172 *
173 * Does your brain hurt yet? - Christophe Kalt
174 *
175 * Oh, and in case you wonder,
176 * swap_alloc = pagesize * ( ai.ani_max - ai.ani_free );
177 * can suffer from a 32bit overflow.
178 */
179 swap_alloc = ai.ani_max - ai.ani_free;
180 swap_alloc *= pagesize;
181 swap_resv = ai.ani_resv + ai.ani_free - ai.ani_max;
182 swap_resv *= pagesize;
183 swap_avail = ai.ani_max - ai.ani_resv;
184 swap_avail *= pagesize;
186 swap_submit (swap_alloc, swap_avail, -1LL, swap_resv);
187 /* #endif defined(KERNEL_SOLARIS) */
189 #elif defined(HAVE_LIBSTATGRAB)
190 sg_swap_stats *swap;
192 if ((swap = sg_get_swap_stats ()) != NULL)
193 swap_submit (swap->used, swap->free, -1LL, -1LL);
194 #endif /* HAVE_LIBSTATGRAB */
195 }
196 #else
197 # define swap_read NULL
198 #endif /* SWAP_HAVE_READ */
200 void module_register (void)
201 {
202 plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);
203 }
205 #undef MODULE_NAME