1 /**
2 * collectd - src/memory.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 #if defined(KERNEL_LINUX) || defined(HAVE_LIBKSTAT)
28 # define MEMORY_HAVE_READ 1
29 #else
30 # define MEMORY_HAVE_READ 0
31 #endif
33 #define MODULE_NAME "memory"
35 static char *memory_file = "memory.rrd";
37 /* 9223372036854775807 == LLONG_MAX */
38 static char *ds_def[] =
39 {
40 "DS:used:GAUGE:25:0:9223372036854775807",
41 "DS:free:GAUGE:25:0:9223372036854775807",
42 "DS:buffers:GAUGE:25:0:9223372036854775807",
43 "DS:cached:GAUGE:25:0:9223372036854775807",
44 NULL
45 };
46 static int ds_num = 4;
48 #ifdef HAVE_LIBKSTAT
49 static int pagesize;
50 static kstat_t *ksp;
51 #endif /* HAVE_LIBKSTAT */
53 static void memory_init (void)
54 {
55 #ifdef HAVE_LIBKSTAT
56 /* getpagesize(3C) tells me this does not fail.. */
57 pagesize = getpagesize ();
58 if (get_kstat (&ksp, "unix", 0, "system_pages"))
59 ksp = NULL;
60 #endif /* HAVE_LIBKSTAT */
62 return;
63 }
65 static void memory_write (char *host, char *inst, char *val)
66 {
67 rrd_update_file (host, memory_file, val, ds_def, ds_num);
68 }
70 #if MEMORY_HAVE_READ
71 #define BUFSIZE 512
72 static void memory_submit (long long mem_used, long long mem_buffered,
73 long long mem_cached, long long mem_free)
74 {
75 char buf[BUFSIZE];
77 if (snprintf (buf, BUFSIZE, "%u:%lli:%lli:%lli:%lli",
78 (unsigned int) curtime, mem_used, mem_free,
79 mem_buffered, mem_cached) >= BUFSIZE)
80 return;
82 plugin_submit (MODULE_NAME, "-", buf);
83 }
84 #undef BUFSIZE
86 static void memory_read (void)
87 {
88 #ifdef KERNEL_LINUX
89 FILE *fh;
90 char buffer[1024];
92 char *fields[8];
93 int numfields;
95 long long mem_used = 0;
96 long long mem_buffered = 0;
97 long long mem_cached = 0;
98 long long mem_free = 0;
100 if ((fh = fopen ("/proc/meminfo", "r")) == NULL)
101 {
102 syslog (LOG_WARNING, "memory: fopen: %s", strerror (errno));
103 return;
104 }
106 while (fgets (buffer, 1024, fh) != NULL)
107 {
108 long long *val = NULL;
110 if (strncasecmp (buffer, "MemTotal:", 9) == 0)
111 val = &mem_used;
112 else if (strncasecmp (buffer, "MemFree:", 8) == 0)
113 val = &mem_free;
114 else if (strncasecmp (buffer, "Buffers:", 8) == 0)
115 val = &mem_buffered;
116 else if (strncasecmp (buffer, "Cached:", 7) == 0)
117 val = &mem_cached;
118 else
119 continue;
121 numfields = strsplit (buffer, fields, 8);
123 if (numfields < 2)
124 continue;
126 *val = atoll (fields[1]) * 1024LL;
127 }
129 if (fclose (fh))
130 syslog (LOG_WARNING, "memory: fclose: %s", strerror (errno));
132 if (mem_used >= (mem_free + mem_buffered + mem_cached))
133 {
134 mem_used -= mem_free + mem_buffered + mem_cached;
135 memory_submit (mem_used, mem_buffered, mem_cached, mem_free);
136 }
137 /* #endif defined(KERNEL_LINUX) */
139 #elif defined(HAVE_LIBKSTAT)
140 long long mem_used;
141 long long mem_free;
142 long long mem_lock;
144 if (ksp == NULL)
145 return;
147 mem_used = get_kstat_value (ksp, "pagestotal");
148 mem_free = get_kstat_value (ksp, "pagesfree");
149 mem_lock = get_kstat_value (ksp, "pageslocked");
151 if ((mem_used < 0LL) || (mem_free < 0LL) || (mem_lock < 0LL))
152 return;
153 if (mem_used < (mem_free + mem_lock))
154 return;
156 mem_used -= mem_free + mem_lock;
157 mem_used *= pagesize; /* If this overflows you have some serious */
158 mem_free *= pagesize; /* memory.. Why not call me up and give me */
159 mem_lock *= pagesize; /* some? ;) */
161 memory_submit (mem_used, mem_lock, 0LL, mem_free);
162 /* #endif defined(HAVE_LIBKSTAT) */
164 #elif defined(HAVE_LIBSTATGRAB)
165 sg_mem_stats *ios;
167 if ((ios = sg_get_mem_stats ()) != NULL)
168 memory_submit (ios->used, 0LL, ios->cache, ios->free);
169 #endif /* HAVE_LIBSTATGRAB */
170 }
171 #else
172 # define memory_read NULL
173 #endif /* MEMORY_HAVE_READ */
175 void module_register (void)
176 {
177 plugin_register (MODULE_NAME, memory_init, memory_read, memory_write);
178 }
180 #undef MODULE_NAME