Added copyright/GPL header to all .c and .h files in trunk
[collectd.git] / src / swap.c
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  **/
22
23 #include "swap.h"
24
25 #if COLLECT_SWAP
26 #define MODULE_NAME "swap"
27
28 #ifdef KERNEL_SOLARIS
29 #include <sys/swap.h>
30 #endif /* KERNEL_SOLARIS */
31
32 #include "plugin.h"
33 #include "common.h"
34
35 #undef  MAX
36 #define MAX(x,y) ((x) > (y) ? (x) : (y))
37
38 static char *swap_file = "swap.rrd";
39
40 /* 1099511627776 == 1TB ought to be enough for anyone ;) */
41 static char *ds_def[] =
42 {
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",
47         NULL
48 };
49 static int ds_num = 4;
50
51 #ifdef KERNEL_SOLARIS
52 static int pagesize;
53 static kstat_t *ksp;
54 #endif /* KERNEL_SOLARIS */
55
56 void module_init (void)
57 {
58 #ifdef KERNEL_SOLARIS
59         /* getpagesize(3C) tells me this does not fail.. */
60         pagesize = getpagesize ();
61         if (get_kstat (&ksp, "unix", 0, "system_pages"))
62                 ksp = NULL;
63 #endif /* KERNEL_SOLARIS */
64
65         return;
66 }
67
68 void module_write (char *host, char *inst, char *val)
69 {
70         rrd_update_file (host, swap_file, val, ds_def, ds_num);
71 }
72
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)
77 {
78         char buffer[512];
79
80         if (snprintf (buffer, 512, "N:%llu:%llu:%llu:%llu", swap_used,
81                                 swap_free, swap_cached, swap_resv) >= 512)
82                 return;
83
84         plugin_submit (MODULE_NAME, "-", buffer);
85 }
86
87 void module_read (void)
88 {
89 #ifdef KERNEL_LINUX
90         FILE *fh;
91         char buffer[1024];
92         
93         char *fields[8];
94         int numfields;
95
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;
100
101         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
102         {
103                 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
104                 return;
105         }
106
107         while (fgets (buffer, 1024, fh) != NULL)
108         {
109                 unsigned long long *val = NULL;
110
111                 if (strncasecmp (buffer, "SwapTotal:", 10) == 0)
112                         val = &swap_total;
113                 else if (strncasecmp (buffer, "SwapFree:", 9) == 0)
114                         val = &swap_free;
115                 else if (strncasecmp (buffer, "SwapCached:", 11) == 0)
116                         val = &swap_cached;
117                 else
118                         continue;
119
120                 numfields = strsplit (buffer, fields, 8);
121
122                 if (numfields < 2)
123                         continue;
124
125                 *val = atoll (fields[1]) * 1024LL;
126         }
127
128         if (fclose (fh))
129                 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
130
131         if ((swap_total == 0LL) || ((swap_free + swap_cached) > swap_total))
132                 return;
133
134         swap_used = swap_total - (swap_free + swap_cached);
135
136         module_submit (swap_used, swap_free, swap_cached, -1LL);
137 /* #endif defined(KERNEL_LINUX) */
138
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; */
144
145         long long availrmem;
146         long long swapfs_minfree;
147
148         struct anoninfo ai;
149
150         if (swapctl (SC_AINFO, &ai) == -1)
151                 return;
152
153         availrmem      = get_kstat_value (ksp, "availrmem");
154         swapfs_minfree = get_kstat_value (ksp, "minfree");
155
156         if ((availrmem < 0LL) || (swapfs_minfree < 0LL))
157                 return;
158
159         /* 
160          * Calculations learned by reading
161          * http://www.itworld.com/Comp/2377/UIR980701perf/
162          *
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)
167          *
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)
173          */
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)); */
178
179         module_submit (swap_alloc, swap_avail, -1LL, swap_resv - swap_alloc);
180 /* #endif defined(KERNEL_SOLARIS) */
181
182 #elif defined(HAVE_LIBSTATGRAB)
183         sg_swap_stats *swap;
184
185         if ((swap = sg_get_swap_stats ()) != NULL)
186                 module_submit (swap->used, swap->free, -1LL, -1LL);
187 #endif /* HAVE_LIBSTATGRAB */
188 }
189
190 void module_register (void)
191 {
192         plugin_register (MODULE_NAME, module_init, module_read, module_write);
193 }
194
195 #undef MODULE_NAME
196 #endif /* COLLECT_SWAP */