1 |
|
/* |
2 |
|
* i-scream central monitoring system |
3 |
< |
* http://www.i-scream.org.uk |
4 |
< |
* Copyright (C) 2000-2002 i-scream |
3 |
> |
* http://www.i-scream.org |
4 |
> |
* Copyright (C) 2000-2003 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 |
23 |
|
#endif |
24 |
|
|
25 |
|
#include "statgrab.h" |
26 |
+ |
#include "tools.h" |
27 |
|
#ifdef SOLARIS |
28 |
|
#include <sys/stat.h> |
29 |
|
#include <sys/swap.h> |
30 |
|
#include <unistd.h> |
31 |
|
#endif |
32 |
< |
#ifdef LINUX |
32 |
> |
#if defined(LINUX) || defined(CYGWIN) |
33 |
|
#include <stdio.h> |
34 |
< |
#include "tools.h" |
34 |
> |
#include <string.h> |
35 |
|
#endif |
36 |
+ |
#ifdef FREEBSD |
37 |
+ |
#include <unistd.h> |
38 |
+ |
#include <sys/types.h> |
39 |
+ |
#include <kvm.h> |
40 |
+ |
#endif |
41 |
|
|
42 |
|
swap_stat_t *get_swap_stats(){ |
43 |
|
|
47 |
|
struct anoninfo ai; |
48 |
|
int pagesize; |
49 |
|
#endif |
50 |
< |
#ifdef LINUX |
50 |
> |
#if defined(LINUX) || defined(CYGWIN) |
51 |
|
FILE *f; |
52 |
|
char *line_ptr; |
53 |
+ |
unsigned long long value; |
54 |
|
#endif |
55 |
+ |
#ifdef FREEBSD |
56 |
+ |
struct kvm_swap swapinfo; |
57 |
+ |
int pagesize; |
58 |
+ |
kvm_t *kvmd; |
59 |
+ |
#endif |
60 |
+ |
#ifdef NETBSD |
61 |
+ |
struct uvmexp *uvm; |
62 |
+ |
#endif |
63 |
|
|
64 |
|
#ifdef SOLARIS |
65 |
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
72 |
|
swap_stat.used = (long long)ai.ani_resv * (long long)pagesize; |
73 |
|
swap_stat.free = swap_stat.total - swap_stat.used; |
74 |
|
#endif |
75 |
< |
#ifdef LINUX |
76 |
< |
if ((f=fopen("/proc/meminfo", "r" ))==NULL) { |
75 |
> |
#if defined(LINUX) || defined(CYGWIN) |
76 |
> |
if ((f = fopen("/proc/meminfo", "r")) == NULL) { |
77 |
|
return NULL; |
78 |
|
} |
79 |
< |
if((line_ptr=f_read_line(f, "Swap:"))==NULL){ |
80 |
< |
fclose(f); |
79 |
> |
|
80 |
> |
while ((line_ptr = f_read_line(f, "")) != NULL) { |
81 |
> |
if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) { |
82 |
> |
continue; |
83 |
> |
} |
84 |
> |
value *= 1024; |
85 |
> |
|
86 |
> |
if (strncmp(line_ptr, "SwapTotal:", 10) == 0) { |
87 |
> |
swap_stat.total = value; |
88 |
> |
} else if (strncmp(line_ptr, "SwapFree:", 9) == 0) { |
89 |
> |
swap_stat.free = value; |
90 |
> |
} |
91 |
> |
} |
92 |
> |
|
93 |
> |
fclose(f); |
94 |
> |
swap_stat.used = swap_stat.total - swap_stat.free; |
95 |
> |
#endif |
96 |
> |
#ifdef FREEBSD |
97 |
> |
if((kvmd = get_kvm()) == NULL){ |
98 |
|
return NULL; |
99 |
|
} |
100 |
< |
if((sscanf(line_ptr, "Swap: %lld %lld %lld", &swap_stat.total, &swap_stat.used, &swap_stat.free))!=3){ |
69 |
< |
fclose(f); |
100 |
> |
if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){ |
101 |
|
return NULL; |
102 |
|
} |
103 |
< |
fclose(f); |
103 |
> |
pagesize=getpagesize(); |
104 |
> |
|
105 |
> |
swap_stat.total= (long long)swapinfo.ksw_total * (long long)pagesize; |
106 |
> |
swap_stat.used = (long long)swapinfo.ksw_used * (long long)pagesize; |
107 |
> |
swap_stat.free = swap_stat.total-swap_stat.used; |
108 |
|
#endif |
109 |
+ |
#ifdef NETBSD |
110 |
+ |
if ((uvm = get_uvmexp()) == NULL) { |
111 |
+ |
return NULL; |
112 |
+ |
} |
113 |
|
|
114 |
+ |
swap_stat.total = (long long)uvm->pagesize * (long long)uvm->swpages; |
115 |
+ |
swap_stat.used = (long long)uvm->pagesize * (long long)uvm->swpginuse; |
116 |
+ |
swap_stat.free = swap_stat.total - swap_stat.used; |
117 |
+ |
#endif |
118 |
|
return &swap_stat; |
119 |
|
|
120 |
|
} |