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