2 * collectd - src/swap.c
3 * Copyright (C) 2005,2006 Florian octo Forster
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.
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.
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
20 * Florian octo Forster <octo at verplant.org>
27 #define MODULE_NAME "swap"
29 #if defined(KERNEL_LINUX) || defined(KERNEL_SOLARIS) || defined(HAVE_LIBSTATGRAB)
30 # define SWAP_HAVE_READ 1
32 # define SWAP_HAVE_READ 0
36 # include <sys/swap.h>
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[] =
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",
53 static int ds_num = 4;
58 #endif /* KERNEL_SOLARIS */
60 static void swap_init (void)
63 /* getpagesize(3C) tells me this does not fail.. */
64 pagesize = getpagesize ();
65 if (get_kstat (&ksp, "unix", 0, "system_pages"))
67 #endif /* KERNEL_SOLARIS */
72 static void swap_write (char *host, char *inst, char *val)
74 rrd_update_file (host, swap_file, val, ds_def, ds_num);
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)
85 if (snprintf (buffer, 512, "%u:%llu:%llu:%llu:%llu", (unsigned int) curtime,
86 swap_used, swap_free, swap_cached, swap_resv) >= 512)
89 plugin_submit (MODULE_NAME, "-", buffer);
92 static void swap_read (void)
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)
108 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
112 while (fgets (buffer, 1024, fh) != NULL)
114 unsigned long long *val = NULL;
116 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
118 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
120 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
125 numfields = strsplit (buffer, fields, 8);
130 *val = atoll (fields[1]) * 1024LL;
134 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
136 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
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;
148 /* unsigned long long swap_free; */
151 long long swapfs_minfree;
155 if (swapctl (SC_AINFO, &ai) == -1)
158 availrmem = get_kstat_value (ksp, "availrmem");
159 swapfs_minfree = get_kstat_value (ksp, "minfree");
161 if ((availrmem < 0LL) || (swapfs_minfree < 0LL))
165 * Calculations learned by reading
166 * http://www.itworld.com/Comp/2377/UIR980701perf/
168 * swap_resv += ani_resv
169 * swap_alloc += MAX(ani_resv, ani_max) - ani_free
170 * swap_avail += MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
171 * swap_free += ani_free + (availrmem - swapfs_minfree)
173 * To clear up the terminology a bit:
174 * resv = reserved (but not neccessarily used)
175 * alloc = used (neccessarily reserved)
176 * avail = not reserved (neccessarily free)
177 * free = not allocates (possibly reserved)
179 swap_resv = pagesize * ai.ani_resv;
180 swap_alloc = pagesize * (MAX(ai.ani_resv, ai.ani_max) - ai.ani_free);
181 swap_avail = pagesize * (MAX(ai.ani_max - ai.ani_resv, 0) + (availrmem - swapfs_minfree));
182 /* swap_free = pagesize * (ai.ani_free + (availrmem - swapfs_minfree)); */
184 swap_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
185 /* #endif defined(KERNEL_SOLARIS) */
187 #elif defined(HAVE_LIBSTATGRAB)
190 if ((swap = sg_get_swap_stats ()) != NULL)
191 swap_submit (swap->used, swap->free, -1LL, -1LL);
192 #endif /* HAVE_LIBSTATGRAB */
195 # define swap_read NULL
196 #endif /* SWAP_HAVE_READ */
198 void module_register (void)
200 plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);