| 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 |
|
#include <stdio.h> |
| 22 |
|
#ifdef LINUX |
| 23 |
|
#include <string.h> |
| 31 |
|
#include <sys/sysctl.h> |
| 32 |
|
#include <unistd.h> |
| 33 |
|
#endif |
| 34 |
< |
#include <ukcprog.h> |
| 34 |
> |
#include "ukcprog.h" |
| 35 |
|
|
| 36 |
|
char *get_memory_stats(){ |
| 37 |
< |
long total=0; |
| 38 |
< |
long free=0; |
| 39 |
< |
long used=0; |
| 40 |
< |
long cache=0; |
| 37 |
> |
long long total=0; |
| 38 |
> |
long long free=0; |
| 39 |
> |
long long used=0; |
| 40 |
> |
long long cache=0; |
| 41 |
|
char *xml_memory_stats; |
| 42 |
|
#ifdef LINUX |
| 43 |
|
FILE *f; |
| 44 |
|
char *line; |
| 45 |
< |
long buffers; |
| 45 |
> |
long long buffers; |
| 46 |
|
#endif |
| 47 |
|
#ifdef SOLARIS |
| 48 |
|
kstat_ctl_t *kc; |
| 68 |
|
} |
| 69 |
|
while((line=fpgetline(f)) != NULL){ |
| 70 |
|
if (((strncmp(line,"Mem:",4)) == 0)) { |
| 71 |
< |
if((sscanf(line, "%*s %ld %ld %ld %*s %ld %ld", &total, &used, &free, &buffers, &cache)) != 5){ |
| 71 |
> |
if((sscanf(line, "%*s %lld %lld %lld %*s %lld %lld", &total, &used, &free, &buffers, &cache)) != 5){ |
| 72 |
|
errf("Failed to read memory details (%m)"); |
| 73 |
|
return NULL; |
| 74 |
|
} |
| 81 |
|
} |
| 82 |
|
|
| 83 |
|
cache=cache+buffers; |
| 84 |
< |
used=total-(free+cache); |
| 84 |
> |
used=total-free; |
| 85 |
|
|
| 86 |
|
total=(total/1024)/1024; |
| 87 |
|
used=(used/1024)/1024; |
| 88 |
|
free=(free/1024)/1024; |
| 89 |
< |
cache=(free/1024)/1024; |
| 89 |
> |
cache=(cache/1024)/1024; |
| 90 |
|
#endif |
| 91 |
|
#ifdef SOLARIS |
| 92 |
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
| 115 |
|
errf("Failed to get free memory (%m)"); |
| 116 |
|
return NULL; |
| 117 |
|
} |
| 118 |
+ |
if((kstat_close(kc)) != 0){ |
| 119 |
+ |
errf("Failed to close kstat control structure (%m)"); |
| 120 |
+ |
return NULL; |
| 121 |
+ |
} |
| 122 |
|
total=((totalmem*pagesize)/1024)/1024; |
| 123 |
|
free=(((kn->value.ul)*pagesize)/1024)/1024; |
| 124 |
|
used=total-free; |
| 174 |
|
|
| 175 |
|
#endif |
| 176 |
|
|
| 177 |
< |
|
| 178 |
< |
if((xml_memory_stats=strf("<memory><total>%ld</total><free>%ld</free><used>%ld</used><cache>%ld</cache></memory>", total, free, used, cache)) == NULL){ |
| 177 |
> |
/* casting these from a long long to a long is a KLUDGE! please fix it :) */ |
| 178 |
> |
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){ |
| 179 |
|
errf("strf failed (%m)"); |
| 180 |
|
return NULL; |
| 181 |
|
} |
| 182 |
|
return xml_memory_stats; |
| 159 |
– |
} |
| 160 |
– |
|
| 161 |
– |
int main(){ |
| 162 |
– |
printf("%s\n", get_memory_stats()); |
| 163 |
– |
exit(0); |
| 183 |
|
} |