2 * collectd - src/swap.c
3 * Copyright (C) 2005 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>
26 #define MODULE_NAME "swap"
30 #endif /* KERNEL_SOLARIS */
36 #define MAX(x,y) ((x) > (y) ? (x) : (y))
38 static char *swap_file = "swap.rrd";
40 /* 1099511627776 == 1TB ought to be enough for anyone ;) */
41 static char *ds_def[] =
43 "DS:used:GAUGE:25:0:1099511627776",
44 "DS:free:GAUGE:25:0:1099511627776",
45 "DS:cached:GAUGE:25:0:1099511627776",
46 "DS:resv:GAUGE:25:0:1099511627776",
49 static int ds_num = 4;
54 #endif /* KERNEL_SOLARIS */
56 void module_init (void)
59 /* getpagesize(3C) tells me this does not fail.. */
60 pagesize = getpagesize ();
61 if (get_kstat (&ksp, "unix", 0, "system_pages"))
63 #endif /* KERNEL_SOLARIS */
68 void module_write (char *host, char *inst, char *val)
70 rrd_update_file (host, swap_file, val, ds_def, ds_num);
73 void module_submit (unsigned long long swap_used,
74 unsigned long long swap_free,
75 unsigned long long swap_cached,
76 unsigned long long swap_resv)
80 if (snprintf (buffer, 512, "N:%llu:%llu:%llu:%llu", swap_used,
81 swap_free, swap_cached, swap_resv) >= 512)
84 plugin_submit (MODULE_NAME, "-", buffer);
87 void module_read (void)
96 unsigned long long swap_used = 0LL;
97 unsigned long long swap_cached = 0LL;
98 unsigned long long swap_free = 0LL;
99 unsigned long long swap_total = 0LL;
101 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
103 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
107 while (fgets (buffer, 1024, fh) != NULL)
109 unsigned long long *val = NULL;
111 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
113 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
115 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
120 numfields = strsplit (buffer, fields, 8);
125 *val = atoll (fields[1]) * 1024LL;
129 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
131 if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
134 swap_used = swap_total - (swap_free + swap_cached);
136 module_submit (swap_used, swap_free, swap_cached, -1LL);
137 /* #endif defined(KERNEL_LINUX) */
139 #elif defined(KERNEL_SOLARIS)
140 unsigned long long swap_alloc;
141 unsigned long long swap_resv;
142 unsigned long long swap_avail;
143 /* unsigned long long swap_free; */
146 long long swapfs_minfree;
150 if (swapctl (SC_AINFO, &ai) == -1)
153 availrmem = get_kstat_value (ksp, "availrmem");
154 swapfs_minfree = get_kstat_value (ksp, "minfree");
156 if ((availrmem < 0LL) || (swapfs_minfree < 0LL))
160 * Calculations learned by reading
161 * http://www.itworld.com/Comp/2377/UIR980701perf/
163 * swap_resv += ani_resv
164 * swap_alloc += MAX(ani_resv, ani_max) - ani_free
165 * swap_avail += MAX(ani_max - ani_resv, 0) + (availrmem - swapfs_minfree)
166 * swap_free += ani_free + (availrmem - swapfs_minfree)
168 * To clear up the terminology a bit:
169 * resv = reserved (but not neccessarily used)
170 * alloc = used (neccessarily reserved)
171 * avail = not reserved (neccessarily free)
172 * free = not allocates (possibly reserved)
174 swap_resv = pagesize * ai.ani_resv;
175 swap_alloc = pagesize * (MAX(ai.ani_resv, ai.ani_max) - ai.ani_free);
176 swap_avail = pagesize * (MAX(ai.ani_max - ai.ani_resv, 0) + (availrmem - swapfs_minfree));
177 /* swap_free = pagesize * (ai.ani_free + (availrmem - swapfs_minfree)); */
179 module_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
180 /* #endif defined(KERNEL_SOLARIS) */
182 #elif defined(HAVE_LIBSTATGRAB)
185 if ((swap = sg_get_swap_stats ()) != NULL)
186 module_submit (swap->used, swap->free, -1LL, -1LL);
187 #endif /* HAVE_LIBSTATGRAB */
190 void module_register (void)
192 plugin_register (MODULE_NAME, module_init, module_read, module_write);
196 #endif /* COLLECT_SWAP */