1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* http://www.i-scream.org.uk |
4 |
* Copyright (C) 2000-2002 i-scream |
5 |
* |
6 |
* This program is free software; you can redistribute it and/or |
7 |
* modify it under the terms of the GNU General Public License |
8 |
* as published by the Free Software Foundation; either version 2 |
9 |
* of the License, or (at your option) any later version. |
10 |
* |
11 |
* This program is distributed in the hope that it will be useful, |
12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 |
* GNU General Public License for more details. |
15 |
* |
16 |
* You should have received a copy of the GNU General Public License |
17 |
* along with this program; if not, write to the Free Software |
18 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
19 |
*/ |
20 |
|
21 |
#ifdef HAVE_CONFIG_H |
22 |
#include "config.h" |
23 |
#endif |
24 |
|
25 |
#include <stdio.h> |
26 |
#ifdef LINUX |
27 |
#include <string.h> |
28 |
#endif |
29 |
#ifdef SOLARIS |
30 |
#include <unistd.h> |
31 |
#include <kstat.h> |
32 |
#endif |
33 |
#ifdef FREEBSD |
34 |
#include <sys/types.h> |
35 |
#include <sys/sysctl.h> |
36 |
#include <unistd.h> |
37 |
#endif |
38 |
#include "ukcprog.h" |
39 |
|
40 |
char *get_memory_stats(){ |
41 |
long long total=0; |
42 |
long long free=0; |
43 |
long long used=0; |
44 |
long long cache=0; |
45 |
char *xml_memory_stats; |
46 |
#ifdef LINUX |
47 |
FILE *f; |
48 |
char *line; |
49 |
long long buffers; |
50 |
#endif |
51 |
#ifdef SOLARIS |
52 |
kstat_ctl_t *kc; |
53 |
kstat_t *ksp; |
54 |
kstat_named_t *kn; |
55 |
u_long totalmem; |
56 |
int pagesize; |
57 |
#endif |
58 |
#ifdef FREEBSD |
59 |
#define TOTAL_MEM_NAME "hw.physmem" |
60 |
#define FREE_MEM_NAME "vm.stats.vm.v_free_count" |
61 |
#define INACTIVE_MEM_NAME "vm.stats.vm.v_inactive_count" |
62 |
#define CACHE_MEM_NAME "vm.stats.vm.v_cache_count" |
63 |
int pagesize; |
64 |
long inactive; |
65 |
size_t size; |
66 |
#endif |
67 |
|
68 |
#ifdef LINUX |
69 |
if ((f=fopen("/proc/meminfo", "r" ))==NULL) { |
70 |
errf("Failed to open memory stats (%m)"); |
71 |
return NULL; |
72 |
} |
73 |
while((line=fpgetline(f)) != NULL){ |
74 |
if (((strncmp(line,"Mem:",4)) == 0)) { |
75 |
if((sscanf(line, "%*s %lld %lld %lld %*s %lld %lld", &total, &used, &free, &buffers, &cache)) != 5){ |
76 |
errf("Failed to read memory details (%m)"); |
77 |
return NULL; |
78 |
} |
79 |
break; |
80 |
} |
81 |
} |
82 |
if ((fclose(f)) != 0) { |
83 |
errf("Failed to close file (%m)"); |
84 |
return NULL; |
85 |
} |
86 |
|
87 |
cache=cache+buffers; |
88 |
used=total-free; |
89 |
|
90 |
total=(total/1024)/1024; |
91 |
used=(used/1024)/1024; |
92 |
free=(free/1024)/1024; |
93 |
cache=(cache/1024)/1024; |
94 |
#endif |
95 |
#ifdef SOLARIS |
96 |
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
97 |
errf("Failed to get pagesize (%m)"); |
98 |
return NULL; |
99 |
} |
100 |
|
101 |
if((totalmem=sysconf(_SC_PHYS_PAGES)) == -1){ |
102 |
errf("Failed to get total memory"); |
103 |
return NULL; |
104 |
} |
105 |
|
106 |
if ((kc = kstat_open()) == NULL) { |
107 |
errf("kstat_open failure (%m)"); |
108 |
return NULL; |
109 |
} |
110 |
if((ksp=kstat_lookup(kc, "unix", 0, "system_pages")) == NULL){ |
111 |
errf("kstat lookup for \"system_pages\" (%m)"); |
112 |
return NULL; |
113 |
} |
114 |
if (kstat_read(kc, ksp, 0) == -1) { |
115 |
errf("Failed to read kstat details (%m)"); |
116 |
return NULL; |
117 |
} |
118 |
if((kn=kstat_data_lookup(ksp, "freemem")) == NULL){ |
119 |
errf("Failed to get free memory (%m)"); |
120 |
return NULL; |
121 |
} |
122 |
if((kstat_close(kc)) != 0){ |
123 |
errf("Failed to close kstat control structure (%m)"); |
124 |
return NULL; |
125 |
} |
126 |
total=((totalmem*pagesize)/1024)/1024; |
127 |
free=(((kn->value.ul)*pagesize)/1024)/1024; |
128 |
used=total-free; |
129 |
#endif |
130 |
#ifdef FREEBSD |
131 |
if ((pagesize=getpagesize()) == -1){ |
132 |
errf("Failed to get page size (%m)"); |
133 |
return NULL; |
134 |
} |
135 |
|
136 |
if (sysctlbyname(TOTAL_MEM_NAME, NULL, &size, NULL, NULL) < 0){ |
137 |
errf("sysctlbyname failed to get total memory (%m)"); |
138 |
return NULL; |
139 |
} |
140 |
if (sysctlbyname(TOTAL_MEM_NAME, &total, &size, NULL, NULL) < 0){ |
141 |
errf("Failed to get total memory stats (%m)"); |
142 |
return NULL; |
143 |
} |
144 |
|
145 |
if (sysctlbyname(FREE_MEM_NAME, NULL, &size, NULL, NULL) < 0){ |
146 |
errf("sysctlbyname failed to get free memory (%m)"); |
147 |
return NULL; |
148 |
} |
149 |
if (sysctlbyname(FREE_MEM_NAME, &free, &size, NULL, NULL) < 0){ |
150 |
errf("Failed to get free memory stats (%m)"); |
151 |
return NULL; |
152 |
} |
153 |
|
154 |
if (sysctlbyname(INACTIVE_MEM_NAME, NULL, &size, NULL, NULL) < 0){ |
155 |
errf("sysctlbyname failed to get inactive memory (%m)"); |
156 |
return NULL; |
157 |
} |
158 |
if (sysctlbyname(INACTIVE_MEM_NAME, &inactive, &size, NULL, NULL) < 0){ |
159 |
errf("Failed to get inactive memory stats (%m)"); |
160 |
return NULL; |
161 |
} |
162 |
|
163 |
if (sysctlbyname(CACHE_MEM_NAME, NULL, &size, NULL, NULL) < 0){ |
164 |
errf("sysctlbyname failed to get cache memory (%m)"); |
165 |
return NULL; |
166 |
} |
167 |
if (sysctlbyname(CACHE_MEM_NAME, &cache, &size, NULL, NULL) < 0){ |
168 |
errf("Failed to get cache memory stats (%m)"); |
169 |
return NULL; |
170 |
} |
171 |
|
172 |
total/=(1024*1024); |
173 |
free=((free*pagesize)/(1024*1024))+((inactive*pagesize)/(1024*1024)); |
174 |
cache=((cache*pagesize)/(1024*1024)); |
175 |
|
176 |
/* used should also be cache+active+wired but its less effort this way :) */ |
177 |
used=total-free; |
178 |
|
179 |
#endif |
180 |
|
181 |
/* casting these from a long long to a long is a KLUDGE! please fix it :) */ |
182 |
if((xml_memory_stats=strf("<memory><total>%ld</total><free>%ld</free><used>%ld</used><cache>%ld</cache></memory>", (long)total, (long)free, (long)used, (long)cache)) == NULL){ |
183 |
errf("strf failed (%m)"); |
184 |
return NULL; |
185 |
} |
186 |
return xml_memory_stats; |
187 |
} |