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