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 |
#include "ukcprog.h" |
27 |
#include <unistd.h> |
28 |
#ifdef SOLARIS |
29 |
#include <kstat.h> |
30 |
#include <sys/sysinfo.h> |
31 |
#endif |
32 |
#ifdef LINUX |
33 |
#include <string.h> |
34 |
#endif |
35 |
#ifdef FREEBSD |
36 |
#include <sys/types.h> |
37 |
#include <sys/sysctl.h> |
38 |
#endif |
39 |
|
40 |
#define WAIT_TIME_IN_SECS 1 |
41 |
|
42 |
char *get_page_stats(){ |
43 |
char *xml_page_stats; |
44 |
int swap_pageins; |
45 |
int swap_pageouts; |
46 |
#ifdef SOLARIS |
47 |
kstat_ctl_t *kc; |
48 |
kstat_t *ksp; |
49 |
cpu_stat_t cs; |
50 |
uint_t swapin, swapout; |
51 |
#endif |
52 |
#ifdef LINUX |
53 |
FILE *f; |
54 |
char *line; |
55 |
long swapin[2], swapout[2]; |
56 |
#endif |
57 |
#ifdef FREEBSD |
58 |
size_t size; |
59 |
long swapin[2], swapout[2]; |
60 |
#endif |
61 |
|
62 |
|
63 |
#ifdef SOLARIS |
64 |
if ((kc = kstat_open()) == NULL) { |
65 |
errf("kstat_open failure (%m)"); |
66 |
return NULL; |
67 |
} |
68 |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
69 |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
70 |
if (kstat_read(kc, ksp, &cs) == -1) { |
71 |
errf("kstat read failure"); |
72 |
continue; |
73 |
} |
74 |
} |
75 |
|
76 |
swapin=cs.cpu_vminfo.pgswapin; |
77 |
swapout=cs.cpu_vminfo.pgswapout; |
78 |
|
79 |
sleep(WAIT_TIME_IN_SECS); |
80 |
|
81 |
if (kstat_chain_update(kc) == -1) { |
82 |
errf("Kstat update failure (%m)"); |
83 |
return NULL; |
84 |
} |
85 |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
86 |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
87 |
if (kstat_read(kc, ksp, &cs) == -1) { |
88 |
errf("kstat read failure"); |
89 |
continue; |
90 |
} |
91 |
} |
92 |
if((kstat_close(kc)) != 0){ |
93 |
errf("Failed to close kstat control structure (%m)"); |
94 |
return NULL; |
95 |
} |
96 |
swap_pageins=(cs.cpu_vminfo.pgswapin-swapin)/WAIT_TIME_IN_SECS; |
97 |
swap_pageouts=(cs.cpu_vminfo.pgswapout-swapout)/WAIT_TIME_IN_SECS; |
98 |
#endif |
99 |
#ifdef LINUX |
100 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
101 |
errf("Failed to open /proc/stat (%m)"); |
102 |
return NULL; |
103 |
} |
104 |
while((line=fpgetline(f)) != NULL){ |
105 |
if (((strncmp(line,"swap",4)) == 0)) { |
106 |
if((sscanf(line, "%*s %ld %ld", &swapin[0], &swapout[0])) != 2){ |
107 |
errf("Failed to read swap paging details (%m)"); |
108 |
return NULL; |
109 |
} |
110 |
break; |
111 |
} |
112 |
} |
113 |
if ((fclose(f)) != 0) { |
114 |
errf("Failed to close file (%m)"); |
115 |
return NULL; |
116 |
} |
117 |
sleep(WAIT_TIME_IN_SECS); |
118 |
|
119 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
120 |
errf("Failed to open /proc/stat (%m)"); |
121 |
return NULL; |
122 |
} |
123 |
while((line=fpgetline(f)) != NULL){ |
124 |
if (((strncmp(line,"swap",4)) == 0)) { |
125 |
if((sscanf(line, "%*s %ld %ld", &swapin[1], &swapout[1])) != 2){ |
126 |
errf("Failed to read swap paging details (%m)"); |
127 |
return NULL; |
128 |
} |
129 |
break; |
130 |
} |
131 |
} |
132 |
if ((fclose(f)) != 0) { |
133 |
errf("Failed to close file (%m)"); |
134 |
return NULL; |
135 |
} |
136 |
|
137 |
swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS; |
138 |
swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS; |
139 |
#endif |
140 |
#ifdef FREEBSD |
141 |
#define SWAP_IN_NAME "vm.stats.vm.v_swappgsin" |
142 |
#define SWAP_OUT_NAME "vm.stats.vm.v_swappgsout" |
143 |
if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){ |
144 |
errf("sysctlbyname failed to get total memory (%m)"); |
145 |
return NULL; |
146 |
} |
147 |
if (sysctlbyname(SWAP_IN_NAME, &swapin[0], &size, NULL, NULL) < 0){ |
148 |
errf("Failed to get total memory stats (%m)"); |
149 |
return NULL; |
150 |
} |
151 |
if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){ |
152 |
errf("sysctlbyname failed to get total memory (%m)"); |
153 |
return NULL; |
154 |
} |
155 |
if (sysctlbyname(SWAP_OUT_NAME, &swapout[0], &size, NULL, NULL) < 0){ |
156 |
errf("Failed to get total memory stats (%m)"); |
157 |
return NULL; |
158 |
} |
159 |
|
160 |
sleep(WAIT_TIME_IN_SECS); |
161 |
|
162 |
if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){ |
163 |
errf("sysctlbyname failed to get total memory (%m)"); |
164 |
return NULL; |
165 |
} |
166 |
if (sysctlbyname(SWAP_IN_NAME, &swapin[1], &size, NULL, NULL) < 0){ |
167 |
errf("Failed to get total memory stats (%m)"); |
168 |
return NULL; |
169 |
} |
170 |
if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){ |
171 |
errf("sysctlbyname failed to get total memory (%m)"); |
172 |
return NULL; |
173 |
} |
174 |
if (sysctlbyname(SWAP_OUT_NAME, &swapout[1], &size, NULL, NULL) < 0){ |
175 |
errf("Failed to get total memory stats (%m)"); |
176 |
return NULL; |
177 |
} |
178 |
|
179 |
swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS; |
180 |
swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS; |
181 |
|
182 |
#endif |
183 |
|
184 |
|
185 |
if((xml_page_stats=strf("<pages><swapins>%d</swapins><swapouts>%d</swapouts></pages>", swap_pageins, swap_pageouts)) == NULL){ |
186 |
errf("strf failed (%m)"); |
187 |
return NULL; |
188 |
} |
189 |
return xml_page_stats; |
190 |
} |