1 |
pajs |
1.1 |
#include <stdio.h> |
2 |
pajs |
1.2 |
#include <ukcprog.h> |
3 |
pajs |
1.1 |
#include <unistd.h> |
4 |
pajs |
1.2 |
#ifdef SOLARIS |
5 |
|
|
#include <kstat.h> |
6 |
|
|
#include <sys/sysinfo.h> |
7 |
|
|
#endif |
8 |
|
|
#ifdef FREEBSD |
9 |
|
|
#include <sys/sysctl.h> |
10 |
|
|
#include <sys/dkstat.h> |
11 |
|
|
#endif |
12 |
pajs |
1.1 |
#define WAIT_TIME_IN_SECS 1 |
13 |
|
|
char *get_cpu_stats(){ |
14 |
pajs |
1.2 |
char *xml_cpu_stats; |
15 |
|
|
double u_p=0.00; |
16 |
|
|
double i_p=0.00; |
17 |
|
|
double k_p=0.00; |
18 |
|
|
double io_p=0.00; |
19 |
|
|
double s_p=0.00; |
20 |
pajs |
1.1 |
#ifdef LINUX |
21 |
|
|
long total, user, idle, kernel, nice; |
22 |
|
|
long cpu_states[4][2]; |
23 |
|
|
FILE *f; |
24 |
|
|
#endif |
25 |
pajs |
1.2 |
#ifdef SOLARIS |
26 |
|
|
kstat_ctl_t *kc; |
27 |
|
|
kstat_t *ksp; |
28 |
|
|
uint_t cpu_states[5][2]; |
29 |
|
|
uint_t user, kernel, idle, iowait, swap, total; |
30 |
|
|
cpu_stat_t cs; |
31 |
|
|
|
32 |
|
|
#endif |
33 |
|
|
#ifdef FREEBSD |
34 |
|
|
long cp_time[CPUSTATES]; |
35 |
|
|
long total, user, idle, kernel, nice; |
36 |
|
|
size_t size; |
37 |
|
|
#endif |
38 |
pajs |
1.1 |
|
39 |
|
|
#ifdef LINUX |
40 |
|
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
41 |
|
|
errf("Failed to open /proc/stat (%m)"); |
42 |
|
|
return NULL; |
43 |
|
|
} |
44 |
|
|
|
45 |
|
|
/* cpu stats should be the first line.. */ |
46 |
|
|
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){ |
47 |
|
|
errf("Failed to read in correct number of CPU stats (%m)"); |
48 |
|
|
return NULL; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
if ((fclose(f)) != 0) { |
52 |
|
|
errf("Failed to close file (%m)"); |
53 |
|
|
return NULL; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
sleep(WAIT_TIME_IN_SECS); |
57 |
|
|
|
58 |
|
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
59 |
|
|
errf("Failed to open /proc/stat (%m)"); |
60 |
|
|
return NULL; |
61 |
|
|
} |
62 |
|
|
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){ |
63 |
|
|
errf("Failed to read in correct number of CPU stats (%m)"); |
64 |
|
|
return NULL; |
65 |
|
|
} |
66 |
|
|
|
67 |
|
|
if ((fclose(f)) != 0) { |
68 |
|
|
errf("Failed to close file (%m)"); |
69 |
|
|
return NULL; |
70 |
|
|
} |
71 |
|
|
|
72 |
|
|
user=cpu_states[0][1]-cpu_states[0][0]+cpu_states[1][1]-cpu_states[1][0]; |
73 |
|
|
kernel=cpu_states[2][1]-cpu_states[2][0]; |
74 |
|
|
idle=cpu_states[3][1]-cpu_states[3][0]; |
75 |
|
|
total=user+kernel+idle; |
76 |
|
|
|
77 |
|
|
u_p=((double)user/(double)total)*100.00; |
78 |
|
|
k_p=((double)kernel/(double)total)*100.00; |
79 |
|
|
i_p=((double)idle/(double)total)*100.00; |
80 |
|
|
|
81 |
|
|
#endif |
82 |
pajs |
1.2 |
#ifdef SOLARIS |
83 |
|
|
if ((kc = kstat_open()) == NULL) { |
84 |
|
|
errf("kstat_open failure (%m)"); |
85 |
|
|
return NULL; |
86 |
|
|
} |
87 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
88 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
89 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
90 |
|
|
errf("kstat read failure"); |
91 |
|
|
continue; |
92 |
|
|
} |
93 |
|
|
} |
94 |
|
|
cpu_states[0][0]=cs.cpu_sysinfo.cpu[CPU_USER]; |
95 |
|
|
cpu_states[1][0]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
96 |
|
|
cpu_states[2][0]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
97 |
|
|
cpu_states[3][0]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
98 |
|
|
cpu_states[4][0]=cs.cpu_sysinfo.cpu[CPU_STATES]; |
99 |
|
|
|
100 |
|
|
sleep(WAIT_TIME_IN_SECS); |
101 |
|
|
|
102 |
|
|
if (kstat_chain_update(kc) == -1) { |
103 |
|
|
errf("Kstat update failure (%m)"); |
104 |
|
|
return NULL; |
105 |
|
|
} |
106 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
107 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
108 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
109 |
|
|
errf("kstat read failure"); |
110 |
|
|
continue; |
111 |
|
|
} |
112 |
|
|
} |
113 |
|
|
cpu_states[0][1]=cs.cpu_sysinfo.cpu[CPU_USER]; |
114 |
|
|
cpu_states[1][1]=cs.cpu_sysinfo.cpu[CPU_WAIT]; |
115 |
|
|
cpu_states[2][1]=cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
116 |
|
|
cpu_states[3][1]=cs.cpu_sysinfo.cpu[CPU_IDLE]; |
117 |
|
|
cpu_states[4][1]=cs.cpu_sysinfo.cpu[CPU_STATES]; |
118 |
|
|
|
119 |
|
|
if((kstat_close(kc)) != 0){ |
120 |
|
|
errf("Failed to close kstat control structure (%m)"); |
121 |
|
|
return NULL; |
122 |
|
|
} |
123 |
|
|
|
124 |
|
|
user=cpu_states[0][1]-cpu_states[0][0]; |
125 |
|
|
iowait=cpu_states[1][1]-cpu_states[1][0]; |
126 |
|
|
kernel=cpu_states[2][1]-cpu_states[2][0]; |
127 |
|
|
idle=cpu_states[3][1]-cpu_states[3][0]; |
128 |
|
|
swap=cpu_states[4][1]-cpu_states[4][0]; |
129 |
|
|
total=user+kernel+idle+iowait+swap; |
130 |
|
|
|
131 |
|
|
u_p=((double)user/(double)total)*100.00; |
132 |
|
|
k_p=((double)kernel/(double)total)*100.00; |
133 |
|
|
i_p=((double)idle/(double)total)*100.00; |
134 |
|
|
io_p=((double)iowait/(double)total)*100.00; |
135 |
|
|
s_p=((double)swap/(double)total)*100.00; |
136 |
|
|
|
137 |
|
|
#endif |
138 |
|
|
#ifdef FREEBSD |
139 |
|
|
|
140 |
|
|
#define CPU_STAT_NAME "kern.cp_time" |
141 |
|
|
|
142 |
|
|
if (sysctlbyname(CPU_STAT_NAME, NULL, &size, NULL, NULL) < 0){ |
143 |
|
|
errf("sysctrlbyname failed (%m)"); |
144 |
|
|
return NULL; |
145 |
|
|
} |
146 |
|
|
if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){ |
147 |
|
|
errf("Failed to get cpu stats (%m)"); |
148 |
|
|
return NULL; |
149 |
|
|
} |
150 |
|
|
|
151 |
|
|
user=cp_time[CP_USER]; |
152 |
|
|
nice=cp_time[CP_NICE]; |
153 |
|
|
kernel=cp_time[CP_SYS]; |
154 |
|
|
idle=cp_time[CP_IDLE]; |
155 |
|
|
|
156 |
|
|
sleep(WAIT_TIME_IN_SECS); |
157 |
|
|
|
158 |
|
|
if (sysctlbyname(CPU_STAT_NAME, &cp_time, &size, NULL, NULL) < 0){ |
159 |
|
|
errf("Failed to get cpu stats (%m)"); |
160 |
|
|
return NULL; |
161 |
|
|
} |
162 |
|
|
|
163 |
|
|
user=cp_time[CP_USER]-user; |
164 |
|
|
nice=cp_time[CP_NICE]-nice; |
165 |
|
|
kernel=cp_time[CP_SYS]-kernel; |
166 |
|
|
idle=cp_time[CP_IDLE]-idle; |
167 |
|
|
total=user+nice+kernel+idle; |
168 |
|
|
|
169 |
|
|
u_p=((float)(user+nice)/(float)total)*100.0; |
170 |
|
|
k_p=((float)(kernel)/(float)total)*100.0; |
171 |
|
|
i_p=((float)(idle)/(float)total)*100.0; |
172 |
pajs |
1.1 |
|
173 |
pajs |
1.2 |
#endif |
174 |
|
|
|
175 |
|
|
|
176 |
|
|
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){ |
177 |
|
|
errf("strf failed (%m)"); |
178 |
|
|
return NULL; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
return xml_cpu_stats; |
182 |
pajs |
1.1 |
} |
183 |
|
|
|