1 |
pajs |
1.1 |
/* |
2 |
|
|
* i-scream central monitoring system |
3 |
tdb |
1.10 |
* http://www.i-scream.org |
4 |
|
|
* Copyright (C) 2000-2003 i-scream |
5 |
pajs |
1.1 |
* |
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 <time.h> |
26 |
|
|
#include "statgrab.h" |
27 |
|
|
#ifdef SOLARIS |
28 |
|
|
#include <kstat.h> |
29 |
|
|
#include <sys/sysinfo.h> |
30 |
pajs |
1.6 |
#include <string.h> |
31 |
pajs |
1.1 |
#endif |
32 |
pajs |
1.7 |
#ifdef LINUX |
33 |
|
|
#include <stdio.h> |
34 |
|
|
#endif |
35 |
pajs |
1.9 |
#ifdef FREEBSD |
36 |
|
|
#include <sys/sysctl.h> |
37 |
|
|
#include <sys/dkstat.h> |
38 |
|
|
#endif |
39 |
pajs |
1.1 |
|
40 |
|
|
static cpu_states_t cpu_now; |
41 |
|
|
static int cpu_now_uninit=1; |
42 |
|
|
|
43 |
|
|
cpu_states_t *get_cpu_totals(){ |
44 |
|
|
|
45 |
|
|
#ifdef SOLARIS |
46 |
|
|
kstat_ctl_t *kc; |
47 |
|
|
kstat_t *ksp; |
48 |
pajs |
1.7 |
cpu_stat_t cs; |
49 |
|
|
#endif |
50 |
|
|
#ifdef LINUX |
51 |
|
|
FILE *f; |
52 |
|
|
#endif |
53 |
pajs |
1.9 |
#ifdef FREEBSD |
54 |
|
|
long cp_time[CPUSTATES]; |
55 |
|
|
size_t size; |
56 |
|
|
#endif |
57 |
pajs |
1.7 |
|
58 |
pajs |
1.1 |
cpu_now.user=0; |
59 |
pajs |
1.9 |
/* Not stored in linux or freebsd */ |
60 |
pajs |
1.1 |
cpu_now.iowait=0; |
61 |
|
|
cpu_now.kernel=0; |
62 |
|
|
cpu_now.idle=0; |
63 |
pajs |
1.9 |
/* Not stored in linux or freebsd */ |
64 |
pajs |
1.1 |
cpu_now.swap=0; |
65 |
|
|
cpu_now.total=0; |
66 |
|
|
/* Not stored in solaris */ |
67 |
|
|
cpu_now.nice=0; |
68 |
|
|
|
69 |
pajs |
1.7 |
#ifdef SOLARIS |
70 |
pajs |
1.1 |
if ((kc = kstat_open()) == NULL) { |
71 |
|
|
return NULL; |
72 |
|
|
} |
73 |
|
|
for (ksp = kc->kc_chain; ksp!=NULL; ksp = ksp->ks_next) { |
74 |
|
|
if ((strcmp(ksp->ks_module, "cpu_stat")) != 0) continue; |
75 |
|
|
if (kstat_read(kc, ksp, &cs) == -1) { |
76 |
|
|
continue; |
77 |
|
|
} |
78 |
pajs |
1.5 |
cpu_now.user+=(long long)cs.cpu_sysinfo.cpu[CPU_USER]; |
79 |
|
|
cpu_now.iowait+=(long long)cs.cpu_sysinfo.cpu[CPU_WAIT]; |
80 |
|
|
cpu_now.kernel+=(long long)cs.cpu_sysinfo.cpu[CPU_KERNEL]; |
81 |
|
|
cpu_now.idle+=(long long)cs.cpu_sysinfo.cpu[CPU_IDLE]; |
82 |
|
|
cpu_now.swap+=(long long)cs.cpu_sysinfo.cpu[CPU_STATES]; |
83 |
pajs |
1.1 |
} |
84 |
|
|
|
85 |
|
|
cpu_now.total=cpu_now.user+cpu_now.iowait+cpu_now.kernel+cpu_now.idle+cpu_now.swap; |
86 |
pajs |
1.7 |
|
87 |
|
|
kstat_close(kc); |
88 |
|
|
#endif |
89 |
|
|
#ifdef LINUX |
90 |
|
|
if ((f=fopen("/proc/stat", "r" ))==NULL) { |
91 |
|
|
return NULL; |
92 |
|
|
} |
93 |
|
|
/* The very first line should be cpu */ |
94 |
|
|
if((fscanf(f, "cpu %lld %lld %lld %lld", \ |
95 |
|
|
&cpu_now.user, \ |
96 |
|
|
&cpu_now.nice, \ |
97 |
|
|
&cpu_now.kernel, \ |
98 |
|
|
&cpu_now.idle)) != 4){ |
99 |
pajs |
1.8 |
fclose(f); |
100 |
pajs |
1.7 |
return NULL; |
101 |
|
|
} |
102 |
|
|
|
103 |
|
|
fclose(f); |
104 |
pajs |
1.1 |
|
105 |
pajs |
1.7 |
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
106 |
pajs |
1.9 |
#endif |
107 |
|
|
#ifdef FREEBSD |
108 |
ats |
1.12 |
size = sizeof cp_time; |
109 |
ats |
1.11 |
if (sysctlbyname("kern.cp_time", &cp_time, &size, NULL, 0) < 0){ |
110 |
pajs |
1.9 |
return NULL; |
111 |
|
|
} |
112 |
|
|
|
113 |
|
|
cpu_now.user=cp_time[CP_USER]; |
114 |
|
|
cpu_now.nice=cp_time[CP_NICE]; |
115 |
|
|
cpu_now.kernel=cp_time[CP_SYS]; |
116 |
|
|
cpu_now.idle=cp_time[CP_IDLE]; |
117 |
|
|
|
118 |
|
|
cpu_now.total=cpu_now.user+cpu_now.nice+cpu_now.kernel+cpu_now.idle; |
119 |
|
|
|
120 |
pajs |
1.1 |
#endif |
121 |
|
|
|
122 |
|
|
cpu_now.systime=time(NULL); |
123 |
pajs |
1.7 |
cpu_now_uninit=0; |
124 |
|
|
|
125 |
pajs |
1.1 |
|
126 |
|
|
return &cpu_now; |
127 |
|
|
} |
128 |
|
|
|
129 |
|
|
cpu_states_t *get_cpu_diff(){ |
130 |
|
|
static cpu_states_t cpu_diff; |
131 |
|
|
cpu_states_t cpu_then, *cpu_tmp; |
132 |
|
|
|
133 |
|
|
if (cpu_now_uninit){ |
134 |
|
|
if((cpu_tmp=get_cpu_totals())==NULL){ |
135 |
|
|
/* Should get_cpu_totals fail */ |
136 |
|
|
return NULL; |
137 |
|
|
} |
138 |
|
|
return cpu_tmp; |
139 |
|
|
} |
140 |
|
|
|
141 |
|
|
|
142 |
|
|
cpu_then.user=cpu_now.user; |
143 |
|
|
cpu_then.kernel=cpu_now.kernel; |
144 |
|
|
cpu_then.idle=cpu_now.idle; |
145 |
|
|
cpu_then.iowait=cpu_now.iowait; |
146 |
|
|
cpu_then.swap=cpu_now.swap; |
147 |
|
|
cpu_then.nice=cpu_now.nice; |
148 |
|
|
cpu_then.total=cpu_now.total; |
149 |
|
|
cpu_then.systime=cpu_now.systime; |
150 |
|
|
|
151 |
|
|
if((cpu_tmp=get_cpu_totals())==NULL){ |
152 |
|
|
return NULL; |
153 |
|
|
} |
154 |
|
|
|
155 |
|
|
cpu_diff.user = cpu_now.user - cpu_then.user; |
156 |
|
|
cpu_diff.kernel = cpu_now.kernel - cpu_then.kernel; |
157 |
|
|
cpu_diff.idle = cpu_now.idle - cpu_then.idle; |
158 |
|
|
cpu_diff.iowait = cpu_now.iowait - cpu_then.iowait; |
159 |
|
|
cpu_diff.swap = cpu_now.swap - cpu_then.swap; |
160 |
|
|
cpu_diff.nice = cpu_now.nice - cpu_then.nice; |
161 |
|
|
cpu_diff.total = cpu_now.total - cpu_then.total; |
162 |
|
|
cpu_diff.systime = cpu_now.systime - cpu_then.systime; |
163 |
|
|
|
164 |
|
|
return &cpu_diff; |
165 |
|
|
} |
166 |
|
|
|
167 |
|
|
cpu_percent_t *cpu_percent_usage(){ |
168 |
|
|
static cpu_percent_t cpu_usage; |
169 |
|
|
cpu_states_t *cs_ptr; |
170 |
|
|
|
171 |
|
|
cs_ptr=get_cpu_diff(); |
172 |
|
|
if(cs_ptr==NULL){ |
173 |
|
|
return NULL; |
174 |
|
|
} |
175 |
|
|
|
176 |
|
|
cpu_usage.user = ((float)cs_ptr->user / (float)cs_ptr->total)*100; |
177 |
|
|
cpu_usage.kernel = ((float)cs_ptr->kernel / (float)cs_ptr->total)*100; |
178 |
|
|
cpu_usage.idle = ((float)cs_ptr->idle / (float)cs_ptr->total)*100; |
179 |
|
|
cpu_usage.iowait = ((float)cs_ptr->iowait / (float)cs_ptr->total)*100; |
180 |
|
|
cpu_usage.swap = ((float)cs_ptr->swap / (float)cs_ptr->total)*100; |
181 |
|
|
cpu_usage.nice = ((float)cs_ptr->nice / (float)cs_ptr->total)*100; |
182 |
|
|
cpu_usage.time_taken = cs_ptr->systime; |
183 |
|
|
|
184 |
|
|
return &cpu_usage; |
185 |
|
|
|
186 |
|
|
} |
187 |
|
|
|