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