1 /**
2 * collectd - src/swap.c
3 * Copyright (C) 2005 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 #ifdef KERNEL_SOLARIS
36 #include <sys/swap.h>
37 #endif /* KERNEL_SOLARIS */
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:25:0:1099511627776",
48 "DS:free:GAUGE:25:0:1099511627776",
49 "DS:cached:GAUGE:25:0:1099511627776",
50 "DS:resv:GAUGE:25:0:1099511627776",
51 NULL
52 };
53 static int ds_num = 4;
55 #ifdef KERNEL_SOLARIS
56 static int 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 = 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, "N:%llu:%llu:%llu:%llu", swap_used,
86 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;
148 /* unsigned long long swap_free; */
150 long long availrmem;
151 long long swapfs_minfree;
153 struct anoninfo ai;
155 if (swapctl (SC_AINFO, &ai) == -1)
156 return;
158 availrmem = get_kstat_value (ksp, "availrmem");
159 swapfs_minfree = get_kstat_value (ksp, "minfree");
161 if ((availrmem < 0LL) || (swapfs_minfree < 0LL))
162 return;
164 /*
165 * Calculations learned by reading
166 * http://www.itworld.com/Comp/2377/UIR980701perf/
167 *
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)
172 *
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)
178 */
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)
188 sg_swap_stats *swap;
190 if ((swap = sg_get_swap_stats ()) != NULL)
191 swap_submit (swap->used, swap->free, -1LL, -1LL);
192 #endif /* HAVE_LIBSTATGRAB */
193 }
194 #else
195 # define swap_read NULL
196 #endif /* SWAP_HAVE_READ */
198 void module_register (void)
199 {
200 plugin_register (MODULE_NAME, swap_init, swap_read, swap_write);
201 }
203 #undef MODULE_NAME