1 |
/* |
2 |
* i-scream central monitoring system |
3 |
* Copyright (C) 2000-2002 i-scream |
4 |
* |
5 |
* This program is free software; you can redistribute it and/or |
6 |
* modify it under the terms of the GNU General Public License |
7 |
* as published by the Free Software Foundation; either version 2 |
8 |
* of the License, or (at your option) any later version. |
9 |
* |
10 |
* This program is distributed in the hope that it will be useful, |
11 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 |
* GNU General Public License for more details. |
14 |
* |
15 |
* You should have received a copy of the GNU General Public License |
16 |
* along with this program; if not, write to the Free Software |
17 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
18 |
*/ |
19 |
|
20 |
#include <stdio.h> |
21 |
#include "ukcprog.h" |
22 |
#include <unistd.h> |
23 |
#ifdef SOLARIS |
24 |
#include <kstat.h> |
25 |
#include <sys/sysinfo.h> |
26 |
#endif |
27 |
#ifdef LINUX |
28 |
#include <string.h> |
29 |
#endif |
30 |
#ifdef FREEBSD |
31 |
#include <sys/types.h> |
32 |
#include <sys/sysctl.h> |
33 |
#endif |
34 |
|
35 |
#define WAIT_TIME_IN_SECS 1 |
36 |
|
37 |
char *get_page_stats(){ |
38 |
char *xml_page_stats; |
39 |
int swap_pageins; |
40 |
int swap_pageouts; |
41 |
#ifdef SOLARIS |
42 |
kstat_ctl_t *kc; |
43 |
kstat_t *ksp; |
44 |
cpu_stat_t cs; |
45 |
uint_t swapin, swapout; |
46 |
#endif |
47 |
#ifdef LINUX |
48 |
FILE *f; |
49 |
char *line; |
50 |
long swapin[2], swapout[2]; |
51 |
#endif |
52 |
#ifdef FREEBSD |
53 |
size_t size; |
54 |
long swapin[2], swapout[2]; |
55 |
#endif |
56 |
|
57 |
|
58 |
#ifdef SOLARIS |
59 |
if ((kc = kstat_open()) == NULL) { |
60 |
errf("kstat_open failure (%m)"); |
61 |
return NULL; |
62 |
} |
63 |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
64 |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
65 |
if (kstat_read(kc, ksp, &cs) == -1) { |
66 |
errf("kstat read failure"); |
67 |
continue; |
68 |
} |
69 |
} |
70 |
|
71 |
swapin=cs.cpu_vminfo.pgswapin; |
72 |
swapout=cs.cpu_vminfo.pgswapout; |
73 |
|
74 |
sleep(WAIT_TIME_IN_SECS); |
75 |
|
76 |
if (kstat_chain_update(kc) == -1) { |
77 |
errf("Kstat update failure (%m)"); |
78 |
return NULL; |
79 |
} |
80 |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
81 |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
82 |
if (kstat_read(kc, ksp, &cs) == -1) { |
83 |
errf("kstat read failure"); |
84 |
continue; |
85 |
} |
86 |
} |
87 |
if((kstat_close(kc)) != 0){ |
88 |
errf("Failed to close kstat control structure (%m)"); |
89 |
return NULL; |
90 |
} |
91 |
swap_pageins=(cs.cpu_vminfo.pgswapin-swapin)/WAIT_TIME_IN_SECS; |
92 |
swap_pageouts=(cs.cpu_vminfo.pgswapout-swapout)/WAIT_TIME_IN_SECS; |
93 |
#endif |
94 |
#ifdef LINUX |
95 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
96 |
errf("Failed to open /proc/stat (%m)"); |
97 |
return NULL; |
98 |
} |
99 |
while((line=fpgetline(f)) != NULL){ |
100 |
if (((strncmp(line,"swap",4)) == 0)) { |
101 |
if((sscanf(line, "%*s %ld %ld", &swapin[0], &swapout[0])) != 2){ |
102 |
errf("Failed to read swap paging details (%m)"); |
103 |
return NULL; |
104 |
} |
105 |
break; |
106 |
} |
107 |
} |
108 |
if ((fclose(f)) != 0) { |
109 |
errf("Failed to close file (%m)"); |
110 |
return NULL; |
111 |
} |
112 |
sleep(WAIT_TIME_IN_SECS); |
113 |
|
114 |
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
115 |
errf("Failed to open /proc/stat (%m)"); |
116 |
return NULL; |
117 |
} |
118 |
while((line=fpgetline(f)) != NULL){ |
119 |
if (((strncmp(line,"swap",4)) == 0)) { |
120 |
if((sscanf(line, "%*s %ld %ld", &swapin[1], &swapout[1])) != 2){ |
121 |
errf("Failed to read swap paging details (%m)"); |
122 |
return NULL; |
123 |
} |
124 |
break; |
125 |
} |
126 |
} |
127 |
if ((fclose(f)) != 0) { |
128 |
errf("Failed to close file (%m)"); |
129 |
return NULL; |
130 |
} |
131 |
|
132 |
swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS; |
133 |
swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS; |
134 |
#endif |
135 |
#ifdef FREEBSD |
136 |
#define SWAP_IN_NAME "vm.stats.vm.v_swappgsin" |
137 |
#define SWAP_OUT_NAME "vm.stats.vm.v_swappgsout" |
138 |
if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){ |
139 |
errf("sysctlbyname failed to get total memory (%m)"); |
140 |
return NULL; |
141 |
} |
142 |
if (sysctlbyname(SWAP_IN_NAME, &swapin[0], &size, NULL, NULL) < 0){ |
143 |
errf("Failed to get total memory stats (%m)"); |
144 |
return NULL; |
145 |
} |
146 |
if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){ |
147 |
errf("sysctlbyname failed to get total memory (%m)"); |
148 |
return NULL; |
149 |
} |
150 |
if (sysctlbyname(SWAP_OUT_NAME, &swapout[0], &size, NULL, NULL) < 0){ |
151 |
errf("Failed to get total memory stats (%m)"); |
152 |
return NULL; |
153 |
} |
154 |
|
155 |
sleep(WAIT_TIME_IN_SECS); |
156 |
|
157 |
if (sysctlbyname(SWAP_IN_NAME, NULL, &size, NULL, NULL) < 0){ |
158 |
errf("sysctlbyname failed to get total memory (%m)"); |
159 |
return NULL; |
160 |
} |
161 |
if (sysctlbyname(SWAP_IN_NAME, &swapin[1], &size, NULL, NULL) < 0){ |
162 |
errf("Failed to get total memory stats (%m)"); |
163 |
return NULL; |
164 |
} |
165 |
if (sysctlbyname(SWAP_OUT_NAME, NULL, &size, NULL, NULL) < 0){ |
166 |
errf("sysctlbyname failed to get total memory (%m)"); |
167 |
return NULL; |
168 |
} |
169 |
if (sysctlbyname(SWAP_OUT_NAME, &swapout[1], &size, NULL, NULL) < 0){ |
170 |
errf("Failed to get total memory stats (%m)"); |
171 |
return NULL; |
172 |
} |
173 |
|
174 |
swap_pageins=(swapin[1]-swapin[0])/WAIT_TIME_IN_SECS; |
175 |
swap_pageouts=(swapout[1]-swapout[0])/WAIT_TIME_IN_SECS; |
176 |
|
177 |
#endif |
178 |
|
179 |
|
180 |
if((xml_page_stats=strf("<pages><swapins>%d</swapins><swapouts>%d</swapouts></pages>", swap_pageins, swap_pageouts)) == NULL){ |
181 |
errf("strf failed (%m)"); |
182 |
return NULL; |
183 |
} |
184 |
return xml_page_stats; |
185 |
} |