1 |
|
/* |
2 |
< |
* i-scream central monitoring system |
2 |
> |
* i-scream libstatgrab |
3 |
|
* http://www.i-scream.org |
4 |
|
* Copyright (C) 2000-2004 i-scream |
5 |
|
* |
42 |
|
#include <sys/sysctl.h> |
43 |
|
#include <sys/user.h> |
44 |
|
#else |
45 |
– |
#include <unistd.h> |
45 |
|
#include <sys/types.h> |
46 |
|
#include <kvm.h> |
47 |
|
#endif |
48 |
+ |
#include <unistd.h> |
49 |
|
#endif |
50 |
|
#if defined(NETBSD) || defined(OPENBSD) |
51 |
|
#include <sys/param.h> |
52 |
|
#include <sys/time.h> |
53 |
|
#include <uvm/uvm.h> |
54 |
+ |
#include <unistd.h> |
55 |
|
#endif |
56 |
+ |
#ifdef HPUX |
57 |
+ |
#include <sys/param.h> |
58 |
+ |
#include <sys/pstat.h> |
59 |
+ |
#include <unistd.h> |
60 |
+ |
#define SWAP_BATCH 5 |
61 |
+ |
#endif |
62 |
|
|
63 |
< |
swap_stat_t *get_swap_stats(){ |
63 |
> |
sg_swap_stats *sg_get_swap_stats(){ |
64 |
|
|
65 |
< |
static swap_stat_t swap_stat; |
65 |
> |
static sg_swap_stats swap_stat; |
66 |
|
|
67 |
+ |
#ifdef HPUX |
68 |
+ |
struct pst_swapinfo pstat_swapinfo[SWAP_BATCH]; |
69 |
+ |
int swapidx = 0; |
70 |
+ |
int num, i; |
71 |
+ |
#endif |
72 |
|
#ifdef SOLARIS |
73 |
|
struct anoninfo ai; |
74 |
|
int pagesize; |
93 |
|
struct uvmexp *uvm; |
94 |
|
#endif |
95 |
|
|
96 |
+ |
#ifdef HPUX |
97 |
+ |
swap_stat.total = 0; |
98 |
+ |
swap_stat.used = 0; |
99 |
+ |
swap_stat.free = 0; |
100 |
+ |
|
101 |
+ |
while (1) { |
102 |
+ |
num = pstat_getswap(pstat_swapinfo, sizeof pstat_swapinfo[0], |
103 |
+ |
SWAP_BATCH, swapidx); |
104 |
+ |
if (num == -1) { |
105 |
+ |
sg_set_error_with_errno(SG_ERROR_PSTAT, |
106 |
+ |
"pstat_getswap"); |
107 |
+ |
return NULL; |
108 |
+ |
} else if (num == 0) { |
109 |
+ |
break; |
110 |
+ |
} |
111 |
+ |
|
112 |
+ |
for (i = 0; i < num; i++) { |
113 |
+ |
struct pst_swapinfo *si = &pstat_swapinfo[i]; |
114 |
+ |
|
115 |
+ |
if ((si->pss_flags & SW_ENABLED) != SW_ENABLED) { |
116 |
+ |
continue; |
117 |
+ |
} |
118 |
+ |
|
119 |
+ |
if ((si->pss_flags & SW_BLOCK) == SW_BLOCK) { |
120 |
+ |
swap_stat.total += ((long long) si->pss_nblksavail) * 1024LL; |
121 |
+ |
swap_stat.used += ((long long) si->pss_nfpgs) * 1024LL; |
122 |
+ |
swap_stat.free = swap_stat.total - swap_stat.used; |
123 |
+ |
} |
124 |
+ |
if ((si->pss_flags & SW_FS) == SW_FS) { |
125 |
+ |
swap_stat.total += ((long long) si->pss_limit) * 1024LL; |
126 |
+ |
swap_stat.used += ((long long) si->pss_allocated) * 1024LL; |
127 |
+ |
swap_stat.free = swap_stat.total - swap_stat.used; |
128 |
+ |
} |
129 |
+ |
} |
130 |
+ |
swapidx = pstat_swapinfo[num - 1].pss_idx + 1; |
131 |
+ |
} |
132 |
+ |
#endif |
133 |
|
#ifdef SOLARIS |
134 |
|
if((pagesize=sysconf(_SC_PAGESIZE)) == -1){ |
135 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCONF, "_SC_PAGESIZE"); |
136 |
|
return NULL; |
137 |
|
} |
138 |
|
if (swapctl(SC_AINFO, &ai) == -1) { |
139 |
+ |
sg_set_error_with_errno(SG_ERROR_SWAPCTL, NULL); |
140 |
|
return NULL; |
141 |
|
} |
142 |
|
swap_stat.total = (long long)ai.ani_max * (long long)pagesize; |
145 |
|
#endif |
146 |
|
#if defined(LINUX) || defined(CYGWIN) |
147 |
|
if ((f = fopen("/proc/meminfo", "r")) == NULL) { |
148 |
+ |
sg_set_error_with_errno(SG_ERROR_OPEN, "/proc/meminfo"); |
149 |
|
return NULL; |
150 |
|
} |
151 |
|
|
152 |
< |
while ((line_ptr = f_read_line(f, "")) != NULL) { |
152 |
> |
while ((line_ptr = sg_f_read_line(f, "")) != NULL) { |
153 |
|
if (sscanf(line_ptr, "%*s %llu kB", &value) != 1) { |
154 |
|
continue; |
155 |
|
} |
174 |
|
|
175 |
|
mibsize = sizeof mib / sizeof mib[0]; |
176 |
|
if (sysctlnametomib("vm.swap_info", mib, &mibsize) < 0) { |
177 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTLNAMETOMIB, |
178 |
+ |
"vm.swap_info"); |
179 |
|
return NULL; |
180 |
|
} |
181 |
|
for (n = 0; ; ++n) { |
185 |
|
break; |
186 |
|
} |
187 |
|
if (xsw.xsw_version != XSWDEV_VERSION) { |
188 |
+ |
sg_set_error(SG_ERROR_XSW_VER_MISMATCH, NULL); |
189 |
|
return NULL; |
190 |
|
} |
191 |
|
swap_stat.total += (long long) xsw.xsw_nblks; |
192 |
|
swap_stat.used += (long long) xsw.xsw_used; |
193 |
|
} |
194 |
|
if (errno != ENOENT) { |
195 |
+ |
sg_set_error_with_errno(SG_ERROR_SYSCTL, "vm.swap_info"); |
196 |
|
return NULL; |
197 |
|
} |
198 |
|
#else |
199 |
< |
if((kvmd = get_kvm()) == NULL){ |
199 |
> |
if((kvmd = sg_get_kvm()) == NULL){ |
200 |
|
return NULL; |
201 |
|
} |
202 |
|
if ((kvm_getswapinfo(kvmd, &swapinfo, 1,0)) == -1){ |
203 |
+ |
sg_set_error(SG_ERROR_KVM_GETSWAPINFO, NULL); |
204 |
|
return NULL; |
205 |
|
} |
206 |
|
|
212 |
|
swap_stat.free = swap_stat.total - swap_stat.used; |
213 |
|
#endif |
214 |
|
#if defined(NETBSD) || defined(OPENBSD) |
215 |
< |
if ((uvm = get_uvmexp()) == NULL) { |
215 |
> |
if ((uvm = sg_get_uvmexp()) == NULL) { |
216 |
|
return NULL; |
217 |
|
} |
218 |
|
|