memory plugin: Fix the Linux-code: It submitted the same value for all RRD-files.
[collectd.git] / src / memory.c
1 /**
2  * collectd - src/memory.c
3  * Copyright (C) 2005-2007  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; only version 2 of the License is applicable.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
17  *
18  * Authors:
19  *   Florian octo Forster <octo at verplant.org>
20  **/
21
22 #include "collectd.h"
23 #include "common.h"
24 #include "plugin.h"
25
26 #ifdef HAVE_SYS_SYSCTL_H
27 # include <sys/sysctl.h>
28 #endif
29
30 #ifdef HAVE_MACH_KERN_RETURN_H
31 # include <mach/kern_return.h>
32 #endif
33 #ifdef HAVE_MACH_MACH_INIT_H
34 # include <mach/mach_init.h>
35 #endif
36 #ifdef HAVE_MACH_MACH_HOST_H
37 # include <mach/mach_host.h>
38 #endif
39 #ifdef HAVE_MACH_HOST_PRIV_H
40 # include <mach/host_priv.h>
41 #endif
42 #ifdef HAVE_MACH_VM_STATISTICS_H
43 # include <mach/vm_statistics.h>
44 #endif
45
46 #if defined (HOST_VM_INFO) || HAVE_SYSCTLBYNAME || KERNEL_LINUX || HAVE_LIBKSTAT
47 # define MEMORY_HAVE_READ 1
48 #else
49 # define MEMORY_HAVE_READ 0
50 #endif
51
52 /* vm_statistics_data_t */
53 #if defined(HOST_VM_INFO)
54 static mach_port_t port_host;
55 static vm_size_t pagesize;
56 /* #endif HOST_VM_INFO */
57
58 #elif HAVE_SYSCTLBYNAME
59 /* no global variables */
60 /* #endif HAVE_SYSCTLBYNAME */
61
62 #elif KERNEL_LINUX
63 /* no global variables */
64 /* #endif KERNEL_LINUX */
65
66 #elif HAVE_LIBKSTAT
67 static int pagesize;
68 static kstat_t *ksp;
69 #endif /* HAVE_LIBKSTAT */
70
71 #if MEMORY_HAVE_READ
72 static int memory_init (void)
73 {
74 #if defined(HOST_VM_INFO)
75         port_host = mach_host_self ();
76         host_page_size (port_host, &pagesize);
77 /* #endif HOST_VM_INFO */
78
79 #elif HAVE_SYSCTLBYNAME
80 /* no init stuff */
81 /* #endif HAVE_SYSCTLBYNAME */
82
83 #elif defined(KERNEL_LINUX)
84 /* no init stuff */
85 /* #endif KERNEL_LINUX */
86
87 #elif defined(HAVE_LIBKSTAT)
88         /* getpagesize(3C) tells me this does not fail.. */
89         pagesize = getpagesize ();
90         if (get_kstat (&ksp, "unix", 0, "system_pages"))
91                 ksp = NULL;
92 #endif /* HAVE_LIBKSTAT */
93
94         return (0);
95 } /* int memory_init */
96
97 static void memory_submit (const char *type_instance, gauge_t value)
98 {
99         value_t values[1];
100         value_list_t vl = VALUE_LIST_INIT;
101
102         values[0].gauge = value;
103
104         vl.values = values;
105         vl.values_len = 4;
106         vl.time = time (NULL);
107         strcpy (vl.host, hostname_g);
108         strcpy (vl.plugin, "memory");
109         strncpy (vl.type_instance, type_instance, sizeof (vl.type_instance));
110         vl.type_instance[sizeof (vl.type_instance) - 1] = '\0';
111
112         plugin_dispatch_values ("memory", &vl);
113 }
114
115 static int memory_read (void)
116 {
117 #if defined(HOST_VM_INFO)
118         kern_return_t status;
119         vm_statistics_data_t   vm_data;
120         mach_msg_type_number_t vm_data_len;
121
122         long long wired;
123         long long active;
124         long long inactive;
125         long long free;
126
127         if (!port_host || !pagesize)
128                 return (-1);
129
130         vm_data_len = sizeof (vm_data) / sizeof (natural_t);
131         if ((status = host_statistics (port_host, HOST_VM_INFO,
132                                         (host_info_t) &vm_data,
133                                         &vm_data_len)) != KERN_SUCCESS)
134         {
135                 ERROR ("memory-plugin: host_statistics failed and returned the value %i", (int) status);
136                 return (-1);
137         }
138
139         /*
140          * From <http://docs.info.apple.com/article.html?artnum=107918>:
141          *
142          * Wired memory
143          *   This information can't be cached to disk, so it must stay in RAM.
144          *   The amount depends on what applications you are using.
145          *
146          * Active memory
147          *   This information is currently in RAM and actively being used.
148          *
149          * Inactive memory
150          *   This information is no longer being used and has been cached to
151          *   disk, but it will remain in RAM until another application needs
152          *   the space. Leaving this information in RAM is to your advantage if
153          *   you (or a client of your computer) come back to it later.
154          *
155          * Free memory
156          *   This memory is not being used.
157          */
158
159         wired    = vm_data.wire_count     * pagesize;
160         active   = vm_data.active_count   * pagesize;
161         inactive = vm_data.inactive_count * pagesize;
162         free     = vm_data.free_count     * pagesize;
163
164         memory_submit ("wired",    wired);
165         memory_submit ("active",   active);
166         memory_submit ("inactive", inactive);
167         memory_submit ("free",     free);
168 /* #endif HOST_VM_INFO */
169
170 #elif HAVE_SYSCTLBYNAME
171         /*
172          * vm.stats.vm.v_page_size: 4096
173          * vm.stats.vm.v_page_count: 246178
174          * vm.stats.vm.v_free_count: 28760
175          * vm.stats.vm.v_wire_count: 37526
176          * vm.stats.vm.v_active_count: 55239
177          * vm.stats.vm.v_inactive_count: 113730
178          * vm.stats.vm.v_cache_count: 10809
179          */
180         char *sysctl_keys[8] =
181         {
182                 "vm.stats.vm.v_page_size",
183                 "vm.stats.vm.v_page_count",
184                 "vm.stats.vm.v_free_count",
185                 "vm.stats.vm.v_wire_count",
186                 "vm.stats.vm.v_active_count",
187                 "vm.stats.vm.v_inactive_count",
188                 "vm.stats.vm.v_cache_count",
189                 NULL
190         };
191         double sysctl_vals[8];
192
193         size_t len;
194         int    i;
195
196         for (i = 0; sysctl_keys[i] != NULL; i++)
197         {
198                 int value;
199                 size_t value_len = sizeof (value);
200
201                 if (sysctlbyname (sysctl_keys[i], (void *) &value, &value_len,
202                                         NULL, 0) == 0)
203                 {
204                         sysctl_vals[i] = value;
205                         DEBUG ("memory plugin: %26s: %6i", sysctl_keys[i], sysctl_vals[i]);
206                 }
207                 else
208                 {
209                         sysctl_vals[i] = NAN;
210                 }
211         } /* for (sysctl_keys) */
212
213         /* multiply all all page counts with the pagesize */
214         for (i = 1; sysctl_keys[i] != NULL; i++)
215                 if (!isnan (sysctl_vals[i]))
216                         sysctl_vals[i] *= sysctl_vals[0];
217
218         memory_submit ("free",     sysctl_vals[2]);
219         memory_submit ("wired",    sysctl_vals[3]);
220         memory_submit ("active",   sysctl_vals[4]);
221         memory_submit ("inactive", sysctl_vals[5]);
222         memory_submit ("cache",    sysctl_vals[6]);
223 /* #endif HAVE_SYSCTLBYNAME */
224
225 #elif defined(KERNEL_LINUX)
226         FILE *fh;
227         char buffer[1024];
228         
229         char *fields[8];
230         int numfields;
231
232         long long mem_used = 0;
233         long long mem_buffered = 0;
234         long long mem_cached = 0;
235         long long mem_free = 0;
236
237         if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
238         {
239                 char errbuf[1024];
240                 WARNING ("memory: fopen: %s",
241                                 sstrerror (errno, errbuf, sizeof (errbuf)));
242                 return (-1);
243         }
244
245         while (fgets (buffer, 1024, fh) != NULL)
246         {
247                 long long *val = NULL;
248
249                 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
250                         val = &mem_used;
251                 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
252                         val = &mem_free;
253                 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
254                         val = &mem_buffered;
255                 else if (strncasecmp (buffer, "Cached:", 7) == 0)
256                         val = &mem_cached;
257                 else
258                         continue;
259
260                 numfields = strsplit (buffer, fields, 8);
261
262                 if (numfields < 2)
263                         continue;
264
265                 *val = atoll (fields[1]) * 1024LL;
266         }
267
268         if (fclose (fh))
269         {
270                 char errbuf[1024];
271                 WARNING ("memory: fclose: %s",
272                                 sstrerror (errno, errbuf, sizeof (errbuf)));
273         }
274
275         if (mem_used >= (mem_free + mem_buffered + mem_cached))
276         {
277                 mem_used -= mem_free + mem_buffered + mem_cached;
278                 memory_submit ("used",     mem_used);
279                 memory_submit ("buffered", mem_buffered);
280                 memory_submit ("cached",   mem_cached);
281                 memory_submit ("free",     mem_free);
282         }
283 /* #endif defined(KERNEL_LINUX) */
284
285 #elif defined(HAVE_LIBKSTAT)
286         long long mem_used;
287         long long mem_free;
288         long long mem_lock;
289
290         if (ksp == NULL)
291                 return (-1);
292
293         mem_used = get_kstat_value (ksp, "pagestotal");
294         mem_free = get_kstat_value (ksp, "pagesfree");
295         mem_lock = get_kstat_value (ksp, "pageslocked");
296
297         if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
298                 return (-1);
299         if (mem_used < (mem_free + mem_lock))
300                 return (-1);
301
302         mem_used -= mem_free + mem_lock;
303         mem_used *= pagesize; /* If this overflows you have some serious */
304         mem_free *= pagesize; /* memory.. Why not call me up and give me */
305         mem_lock *= pagesize; /* some? ;) */
306
307         memory_submit ("used",   mem_used);
308         memory_submit ("free",   mem_free);
309         memory_submit ("locked", mem_locked);
310 /* #endif defined(HAVE_LIBKSTAT) */
311
312 #elif defined(HAVE_LIBSTATGRAB)
313         sg_mem_stats *ios;
314
315         if ((ios = sg_get_mem_stats ()) != NULL)
316         {
317                 memory_submit ("used",   ios->used);
318                 memory_submit ("cached", ios->cached);
319                 memory_submit ("free",   ios->free);
320         }
321 #endif /* HAVE_LIBSTATGRAB */
322
323         return (0);
324 }
325 #endif /* MEMORY_HAVE_READ */
326
327 void module_register (void)
328 {
329 #if MEMORY_HAVE_READ
330         plugin_register_init ("memory", memory_init);
331         plugin_register_read ("memory", memory_read);
332 #endif /* MEMORY_HAVE_READ */
333 } /* void module_register */