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 <local/ukcprog.h> |
26 |
> |
#include <ukcprog.h> |
27 |
|
#include <unistd.h> |
28 |
< |
|
28 |
> |
#ifdef SOLARIS |
29 |
> |
#include <kstat.h> |
30 |
> |
#include <sys/sysinfo.h> |
31 |
> |
#endif |
32 |
> |
#ifdef FREEBSD |
33 |
> |
#include <sys/sysctl.h> |
34 |
> |
#include <sys/dkstat.h> |
35 |
> |
#endif |
36 |
|
#define WAIT_TIME_IN_SECS 1 |
37 |
|
char *get_cpu_stats(){ |
38 |
< |
double u_p, i_p, k_p; |
38 |
> |
char *xml_cpu_stats; |
39 |
> |
double u_p=0.00; |
40 |
> |
double i_p=0.00; |
41 |
> |
double k_p=0.00; |
42 |
> |
double io_p=0.00; |
43 |
> |
double s_p=0.00; |
44 |
|
#ifdef LINUX |
45 |
|
long total, user, idle, kernel, nice; |
46 |
|
long cpu_states[4][2]; |
47 |
|
FILE *f; |
48 |
|
#endif |
49 |
+ |
#ifdef SOLARIS |
50 |
+ |
kstat_ctl_t *kc; |
51 |
+ |
kstat_t *ksp; |
52 |
+ |
uint_t cpu_states[5][2]; |
53 |
+ |
uint_t user, kernel, idle, iowait, swap, total; |
54 |
+ |
cpu_stat_t cs; |
55 |
+ |
|
56 |
+ |
#endif |
57 |
+ |
#ifdef FREEBSD |
58 |
+ |
long cp_time[CPUSTATES]; |
59 |
+ |
long total, user, idle, kernel, nice; |
60 |
+ |
size_t size; |
61 |
+ |
#endif |
62 |
|
|
63 |
|
#ifdef LINUX |
64 |
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
103 |
|
i_p=((double)idle/(double)total)*100.00; |
104 |
|
|
105 |
|
#endif |
106 |
+ |
#ifdef SOLARIS |
107 |
+ |
if ((kc = kstat_open()) == NULL) { |
108 |
+ |
errf("kstat_open failure (%m)"); |
109 |
+ |
return NULL; |
110 |
+ |
} |
111 |
+ |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
112 |
+ |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
113 |
+ |
if (kstat_read(kc, ksp, &cs) == -1) { |
114 |
+ |
errf("kstat read failure"); |
115 |
+ |
continue; |
116 |
+ |
} |
117 |
+ |
} |
118 |
+ |
cpu_states[0][0]=cs.cpu_sysinfo.cpu[CPU_USER]; |
119 |
+ |
cpu_states[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
120 |
+ |
cpu_states[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
121 |
+ |
cpu_states[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
122 |
+ |
cpu_states[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES]; |
123 |
+ |
|
124 |
+ |
sleep(WAIT_TIME_IN_SECS); |
125 |
|
|
126 |
< |
return "jibble"; |
127 |
< |
} |
126 |
> |
if (kstat_chain_update(kc) == -1) { |
127 |
> |
errf("Kstat update failure (%m)"); |
128 |
> |
return NULL; |
129 |
> |
} |
130 |
> |
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
131 |
> |
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
132 |
> |
if (kstat_read(kc, ksp, &cs) == -1) { |
133 |
> |
errf("kstat read failure"); |
134 |
> |
continue; |
135 |
> |
} |
136 |
> |
} |
137 |
> |
cpu_states[0][1]=cs.cpu_sysinfo.cpu[CPU_USER]; |
138 |
> |
cpu_states[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
139 |
> |
cpu_states[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
140 |
> |
cpu_states[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
141 |
> |
cpu_states[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES]; |
142 |
|
|
143 |
< |
int main(){ |
144 |
< |
get_cpu_stats(); |
145 |
< |
exit(0); |
143 |
> |
if((kstat_close(kc)) != 0){ |
144 |
> |
errf("Failed to close kstat control structure (%m)"); |
145 |
> |
return NULL; |
146 |
> |
} |
147 |
> |
|
148 |
> |
user=cpu_states[0][1]-cpu_states[0][0]; |
149 |
> |
iowait=cpu_states[1][1]-cpu_states[1][0]; |
150 |
> |
kernel=cpu_states[2][1]-cpu_states[2][0]; |
151 |
> |
idle=cpu_states[3][1]-cpu_states[3][0]; |
152 |
> |
swap=cpu_states[4][1]-cpu_states[4][0]; |
153 |
> |
total=user+kernel+idle+iowait+swap; |
154 |
> |
|
155 |
> |
u_p=((double)user/(double)total)*100.00; |
156 |
> |
k_p=((double)kernel/(double)total)*100.00; |
157 |
> |
i_p=((double)idle/(double)total)*100.00; |
158 |
> |
io_p=((double)iowait/(double)total)*100.00; |
159 |
> |
s_p=((double)swap/(double)total)*100.00; |
160 |
> |
|
161 |
> |
#endif |
162 |
> |
#ifdef FREEBSD |
163 |
> |
|
164 |
> |
#define CPU_STAT_NAME "kern.cp_time" |
165 |
> |
|
166 |
> |
if (sysctlbyname(CPU_STAT_NAME, NULL, &size, NULL, NULL) < 0){ |
167 |
> |
errf("sysctrlbyname failed (%m)"); |
168 |
> |
return NULL; |
169 |
> |
} |
170 |
> |
if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){ |
171 |
> |
errf("Failed to get cpu stats (%m)"); |
172 |
> |
return NULL; |
173 |
> |
} |
174 |
> |
|
175 |
> |
user=cp_time[CP_USER]; |
176 |
> |
nice=cp_time[CP_NICE]; |
177 |
> |
kernel=cp_time[CP_SYS]; |
178 |
> |
idle=cp_time[CP_IDLE]; |
179 |
> |
|
180 |
> |
sleep(WAIT_TIME_IN_SECS); |
181 |
> |
|
182 |
> |
if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){ |
183 |
> |
errf("Failed to get cpu stats (%m)"); |
184 |
> |
return NULL; |
185 |
> |
} |
186 |
> |
|
187 |
> |
user=cp_time[CP_USER]-user; |
188 |
> |
nice=cp_time[CP_NICE]-nice; |
189 |
> |
kernel=cp_time[CP_SYS]-kernel; |
190 |
> |
idle=cp_time[CP_IDLE]-idle; |
191 |
> |
total=user+nice+kernel+idle; |
192 |
> |
|
193 |
> |
u_p=((float)(user+nice)/(float)total)*100.0; |
194 |
> |
k_p=((float)(kernel)/(float)total)*100.0; |
195 |
> |
i_p=((float)(idle)/(float)total)*100.0; |
196 |
> |
|
197 |
> |
#endif |
198 |
> |
|
199 |
> |
|
200 |
> |
if((xml_cpu_stats=strf("<cpu><user>%3.2f</user><kernel>%3.2f</kernel><idle>%3.2f</idle><iowait>%3.2f</iowait><swap>%3.2f</swap></cpu>", u_p, k_p, i_p, io_p, s_p)) == NULL){ |
201 |
> |
errf("strf failed (%m)"); |
202 |
> |
return NULL; |
203 |
> |
} |
204 |
> |
|
205 |
> |
return xml_cpu_stats; |
206 |
|
} |
207 |
+ |
|