| 1 |
pajs |
1.1 |
#include <stdio.h> |
| 2 |
|
|
#include <string.h> |
| 3 |
|
|
#include <local/ukcprog.h> |
| 4 |
|
|
|
| 5 |
|
|
|
| 6 |
|
|
char *get_memory_stats(){ |
| 7 |
|
|
long total=0; |
| 8 |
|
|
long free=0; |
| 9 |
|
|
long used=0; |
| 10 |
|
|
long cache=0; |
| 11 |
|
|
char *xml_memory_stats; |
| 12 |
|
|
#ifdef LINUX |
| 13 |
|
|
FILE *f; |
| 14 |
|
|
char *line; |
| 15 |
|
|
long buffers; |
| 16 |
|
|
#endif |
| 17 |
|
|
|
| 18 |
|
|
#ifdef LINUX |
| 19 |
|
|
if ((f=fopen("/proc/meminfo", "r" ))==NULL) { |
| 20 |
|
|
errf("Failed to open memory stats (%m)"); |
| 21 |
|
|
return NULL; |
| 22 |
|
|
} |
| 23 |
|
|
while((line=fpgetline(f)) != NULL){ |
| 24 |
|
|
if (((strncmp(line,"Mem:",4)) == 0)) { |
| 25 |
|
|
if((sscanf(line, "%*s %ld %ld %ld %*s %ld %ld", &total, &used, &free, &buffers, &cache)) != 5){ |
| 26 |
|
|
errf("Failed to read memory details (%m)"); |
| 27 |
|
|
return NULL; |
| 28 |
|
|
} |
| 29 |
|
|
break; |
| 30 |
|
|
} |
| 31 |
|
|
} |
| 32 |
|
|
if ((fclose(f)) != 0) { |
| 33 |
|
|
errf("Failed to close file (%m)"); |
| 34 |
|
|
return NULL; |
| 35 |
|
|
} |
| 36 |
|
|
|
| 37 |
|
|
cache=cache+buffers; |
| 38 |
|
|
used=total-(free+cache); |
| 39 |
|
|
|
| 40 |
|
|
total=(total/1024)/1024; |
| 41 |
|
|
used=(used/1024)/1024; |
| 42 |
|
|
free=(free/1024)/1024; |
| 43 |
|
|
cache=(free/1024)/1024; |
| 44 |
|
|
#endif |
| 45 |
|
|
|
| 46 |
|
|
if((xml_memory_stats=strf("<memory><total>%ld</total><free>%ld</free><used>%ld</used><cache>%ld</cache></memory>", total, free, used, cache)) == NULL){ |
| 47 |
|
|
errf("strf failed (%m)"); |
| 48 |
|
|
return NULL; |
| 49 |
|
|
} |
| 50 |
|
|
} |
| 51 |
|
|
|
| 52 |
|
|
int main(){ |
| 53 |
|
|
printf("%s\n", get_memory_stats()); |
| 54 |
|
|
exit(0); |
| 55 |
|
|
} |