1 |
tdb |
1.3 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.4 |
* http://www.i-scream.org.uk |
4 |
tdb |
1.3 |
* 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 |
tdb |
1.5 |
#ifdef HAVE_CONFIG_H |
22 |
|
|
#include "config.h" |
23 |
|
|
#endif |
24 |
|
|
|
25 |
pajs |
1.1 |
#include <stdio.h> |
26 |
pajs |
1.2 |
#include <ukcprog.h> |
27 |
pajs |
1.1 |
#include <unistd.h> |
28 |
pajs |
1.2 |
#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 |
pajs |
1.1 |
#define WAIT_TIME_IN_SECS 1 |
37 |
|
|
char *get_cpu_stats(){ |
38 |
pajs |
1.2 |
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 |
pajs |
1.1 |
#ifdef LINUX |
45 |
|
|
long total, user, idle, kernel, nice; |
46 |
|
|
long cpu_states[4][2]; |
47 |
|
|
FILE *f; |
48 |
|
|
#endif |
49 |
pajs |
1.2 |
#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 |
pajs |
1.1 |
|
63 |
|
|
#ifdef LINUX |
64 |
|
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
65 |
|
|
errf("Failed to open /proc/stat (%m)"); |
66 |
|
|
return NULL; |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
/* cpu stats should be the first line.. */ |
70 |
|
|
if((fscanf(f,"%*s %ld %ld %ld %ld", &cpu_states[0][0], &cpu_states[1][0], &cpu_states[2][0] ,&cpu_states[3][0])) != 4){ |
71 |
|
|
errf("Failed to read in correct number of CPU stats (%m)"); |
72 |
|
|
return NULL; |
73 |
|
|
} |
74 |
|
|
|
75 |
|
|
if ((fclose(f)) != 0) { |
76 |
|
|
errf("Failed to close file (%m)"); |
77 |
|
|
return NULL; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
sleep(WAIT_TIME_IN_SECS); |
81 |
|
|
|
82 |
|
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
83 |
|
|
errf("Failed to open /proc/stat (%m)"); |
84 |
|
|
return NULL; |
85 |
|
|
} |
86 |
|
|
if((fscanf(f,"%*s %ld %ld %ld %ld", &cpu_states[0][1], &cpu_states[1][1], &cpu_states[2][1] ,&cpu_states[3][1])) != 4){ |
87 |
|
|
errf("Failed to read in correct number of CPU stats (%m)"); |
88 |
|
|
return NULL; |
89 |
|
|
} |
90 |
|
|
|
91 |
|
|
if ((fclose(f)) != 0) { |
92 |
|
|
errf("Failed to close file (%m)"); |
93 |
|
|
return NULL; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
user=cpu_states[0][1]-cpu_states[0][0]+cpu_states[1][1]-cpu_states[1][0]; |
97 |
|
|
kernel=cpu_states[2][1]-cpu_states[2][0]; |
98 |
|
|
idle=cpu_states[3][1]-cpu_states[3][0]; |
99 |
|
|
total=user+kernel+idle; |
100 |
|
|
|
101 |
|
|
u_p=((double)user/(double)total)*100.00; |
102 |
|
|
k_p=((double)kernel/(double)total)*100.00; |
103 |
|
|
i_p=((double)idle/(double)total)*100.00; |
104 |
|
|
|
105 |
|
|
#endif |
106 |
pajs |
1.2 |
#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 |
|
|
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 |
|
|
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 |
pajs |
1.1 |
|
197 |
pajs |
1.2 |
#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 |
pajs |
1.1 |
} |
207 |
|
|
|