Lines 1-294
Link Here
|
1 |
--- src/stats-bsd.c.orig Mon Sep 30 02:58:36 2002 |
1 |
--- src/stats-bsd.c.orig Sat Nov 30 03:27:16 2002 |
2 |
+++ src/stats-bsd.c Sat Oct 19 22:50:30 2002 |
2 |
+++ src/stats-bsd.c Wed Dec 18 03:28:16 2002 |
3 |
@@ -41,139 +41,171 @@ |
3 |
@@ -64,6 +64,8 @@ |
4 |
#include "upclient.h" |
|
|
5 |
#include "options.h" |
6 |
#include "stats.h" |
7 |
-#include "uplog.h" /* wrapper for <syslog.h> */ |
8 |
|
9 |
/* System includes */ |
10 |
-#include <kvm.h> |
11 |
-#include <stdio.h> |
12 |
+#include <err.h> |
13 |
#include <errno.h> |
14 |
#include <fcntl.h> |
15 |
+#include <kvm.h> |
16 |
#include <limits.h> |
17 |
-#include <sys/time.h> |
18 |
-#include <sys/param.h> |
19 |
+#include <stdio.h> |
20 |
+#include <syslog.h> |
21 |
#include <sys/dkstat.h> |
22 |
+#include <sys/param.h> |
23 |
#include <sys/sysctl.h> |
24 |
+#include <sys/time.h> |
25 |
#include <sys/utsname.h> |
26 |
|
4 |
|
|
|
5 |
#include "locale.h" /* gettext */ |
6 |
|
7 |
+void logcalc(char *, char *); |
8 |
+ |
27 |
/** |
9 |
/** |
28 |
* @desc Get statistics |
10 |
* @desc Verbose level 2 logging of calulations |
29 |
*/ |
11 |
*/ |
30 |
-void getstats(unsigned long *puptime, double *pload, int *pidle, char *os, char *oslevel, char *cpu) { |
12 |
@@ -81,12 +83,13 @@ |
31 |
- struct utsname uts; |
13 |
void |
32 |
- struct timeval boottime; |
14 |
initCPU(char *cpu) |
33 |
+void |
15 |
{ |
34 |
+getstats(unsigned long *puptime, double *pload, int *pidle, |
16 |
- char buf[CPU_SIZE] = ""; |
35 |
+ char *os, char *oslevel, char *cpu) |
17 |
static int initialized = 0; |
36 |
+{ |
|
|
37 |
+ size_t size; |
38 |
+ int mib[2]; |
39 |
+ |
40 |
+ { /* ========== uptime ========== */ |
41 |
+ static struct timeval boottime; |
42 |
+ static int initialized = 0; |
43 |
time_t now; |
44 |
- size_t size; |
18 |
- size_t size; |
45 |
- double loadavgs[2]; |
19 |
- int mib[2]; |
46 |
- int mib[2]; |
20 |
|
47 |
- |
21 |
if (!initialized) { |
48 |
- /* Get uptime */ |
22 |
+ char buf[CPU_SIZE] = ""; |
49 |
- time(&now); |
23 |
+ size_t size; |
50 |
- mib[0] = CTL_KERN; |
24 |
+ int mib[2]; |
51 |
- mib[1] = KERN_BOOTTIME; |
|
|
52 |
- size = sizeof(boottime); |
53 |
- if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && (boottime.tv_sec != 0)) { |
54 |
- *puptime = now - boottime.tv_sec; |
55 |
- *puptime /= 60; |
56 |
- } |
57 |
- |
58 |
- if (cfg_sendload) { |
59 |
- /* Get load average */ |
60 |
- getloadavg(loadavgs, 3); |
61 |
- /* Use the 3rd element (15 minute load average) */ |
62 |
- *pload = loadavgs[2]; |
63 |
- } |
64 |
- |
65 |
- if (cfg_sendos) { |
66 |
- /* Get os info */ |
67 |
- uname(&uts); |
68 |
- } |
69 |
- |
70 |
- if (cfg_sendcpu) { |
71 |
- /* Get CPU info */ |
72 |
- mib[0] = CTL_HW; |
73 |
- if (cfg_sendcpulevel == 1) { |
74 |
- mib[1] = HW_MACHINE; /* Send architecture */ |
75 |
- } |
76 |
- else { |
77 |
- mib[1] = HW_MODEL; /* Send architecture's specific model */ |
78 |
- } |
79 |
- size = CPU_SIZE; |
80 |
- sysctl(mib, 2, cpu, &size, NULL, 0); |
81 |
- } |
82 |
- |
83 |
- if (cfg_sendos) { |
84 |
- strncpy(os, uts.sysname, OS_SIZE - 1); |
85 |
- if (cfg_sendoslevel) { |
86 |
- strncpy(oslevel, uts.release, OSLEVEL_SIZE - 1); |
87 |
- } |
88 |
- } |
89 |
- |
90 |
- if (cfg_sendidle) { |
91 |
- static kvm_t *kp; |
92 |
- static int initialized = 0; |
93 |
- static struct nlist namelist [] = { |
94 |
+ |
95 |
+ if (!initialized) { |
96 |
+ initialized = 1; |
97 |
+ |
98 |
+ mib[0] = CTL_KERN; |
99 |
+ mib[1] = KERN_BOOTTIME; |
100 |
+ size = sizeof(boottime); |
101 |
+ |
102 |
+ if (sysctl(mib, 2, &boottime, &size, NULL, 0) == -1) { |
103 |
+ uplog(LOG_ERR, "sysctl: kern.boottime"); |
104 |
+ err(1, "sysctl: kern.boottime"); |
105 |
+ } |
106 |
+ } |
107 |
+ |
108 |
+ if (boottime.tv_sec) { |
109 |
+ time_t now; |
110 |
+ |
111 |
+ time (&now); |
112 |
+ |
113 |
+ *puptime = now - boottime.tv_sec; |
114 |
+ *puptime /= 60; |
115 |
+ } |
116 |
+ } |
117 |
+ |
118 |
+ if (cfg_sendload) { /* ========== loadavg ========== */ |
119 |
+ double loadavgs[3] = { 0. }; |
120 |
+ |
121 |
+ getloadavg(loadavgs, 3); |
122 |
+ /* Use the 3rd element (15 minute load average) */ |
123 |
+ *pload = loadavgs[2]; |
124 |
+ } |
125 |
+ |
126 |
+ if (cfg_sendcpu) { /* ========== cpu ========== */ |
127 |
+ static char buf[CPU_SIZE] = ""; |
128 |
+ static int initialized = 0; |
129 |
+ |
130 |
+ if (!initialized) { |
131 |
+ initialized = 1; |
132 |
+ |
133 |
+ mib[0] = CTL_HW; |
134 |
+ if (cfg_sendcpulevel == 1) |
135 |
+ mib[1] = HW_MACHINE; /* Send architecture */ |
136 |
+ else |
137 |
+ mib[1] = HW_MODEL; /* Send architecture's specific model */ |
138 |
+ size = CPU_SIZE; |
139 |
+ |
140 |
+ if (sysctl(mib, 2, buf, &size, NULL, 0) == -1) { |
141 |
+ uplog(LOG_WARNING, "sysctl: hw.model"); |
142 |
+ /* warn("sysctl: hw.model"); */ |
143 |
+ } |
144 |
+ } |
145 |
+ |
146 |
+ strlcpy(cpu, buf, CPU_SIZE); |
147 |
+ } |
148 |
+ |
149 |
+ if (cfg_sendos) { |
150 |
+ static struct utsname uts; |
151 |
+ static int initialized = 0; |
152 |
+ |
153 |
+ if (!initialized) { |
154 |
+ initialized = 1; |
155 |
+ |
156 |
+ uname(&uts); |
157 |
+ } |
158 |
+ |
159 |
+ strlcpy(os, uts.sysname, OS_SIZE); |
160 |
+ |
161 |
+ if (cfg_sendoslevel) |
162 |
+ strlcpy(oslevel, uts.release, OSLEVEL_SIZE); |
163 |
+ } |
164 |
+ |
25 |
+ |
165 |
+ if (cfg_sendidle) { /* ========== idle_time ========== */ |
26 |
initialized = 1; |
166 |
+ static kvm_t *kp; |
27 |
|
167 |
+ static int initialized = 0; |
28 |
mib[0] = CTL_HW; |
168 |
+ static struct nlist namelist[] = { |
29 |
@@ -98,7 +101,6 @@ |
169 |
#define X_CP_TIME 0 |
30 |
|
170 |
- { "_cp_time" }, |
31 |
if (sysctl(mib, 2, buf, &size, NULL, 0) == -1) { |
171 |
- { NULL }, |
32 |
uplog(LOG_WARNING, "sysctl: hw.model"); |
172 |
- }; |
33 |
- /* warn("sysctl: hw.model"); */ |
173 |
- |
34 |
} |
174 |
- if (!initialized) { |
35 |
strlcpy(cpu, buf, CPU_SIZE); |
175 |
- char errbuf[_POSIX2_LINE_MAX]; |
36 |
logcalc(_("CPU"), cpu); |
176 |
- initialized = 1; |
37 |
@@ -154,7 +156,6 @@ |
177 |
- |
38 |
err(1, "sysctl: kern.boottime"); |
178 |
- setgid(getgid()); |
39 |
} |
179 |
- |
40 |
boottimesecs = boottime.tv_sec; |
180 |
- kp = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf); |
41 |
- /* logcalc(_("boot time"), boottimesecs); */ |
181 |
- |
42 |
#if defined DEBUG |
182 |
- if (!kp) { |
43 |
uplog(LOG_DEBUG, "initBoottime() initialized %d", boottimesecs); |
183 |
- uplog(LOG_WARNING, "kvm_openfiles: %s. (Did you forget to start upclient as root?)", errbuf); |
44 |
#endif /* DEBUG */ |
184 |
-// warn("kvm_openfiles: %s", errbuf); |
45 |
@@ -173,19 +174,18 @@ |
185 |
- } |
46 |
_("%s %s not implemented for this operating system %s"), |
186 |
- else if (kvm_nlist(kp,namelist) == -1) { |
47 |
_("WARNING:"), _("Load-average"), strerror(errno)); |
187 |
- uplog(LOG_WARNING, "kvm_nlist: %s", kvm_geterr(kp)); |
48 |
cfg_SendUsage = 0; |
188 |
-// warn("kvm_nlist: %s", kvm_geterr(kp)); |
49 |
- return; |
189 |
- } |
50 |
} |
190 |
- } |
51 |
else if (loads < 0) { |
191 |
- |
52 |
uplog(LOG_WARNING, _("%s Could not get load-average %s"), _("WARNING:"), |
192 |
- if (kp) { |
53 |
strerror(errno)); |
193 |
- long ctime[CPUSTATES]; |
54 |
- return; |
194 |
- static long stime[CPUSTATES]; |
55 |
} |
195 |
- static int first_time = 1; |
|
|
196 |
- |
197 |
- if (first_time) { |
198 |
- first_time = 0; |
199 |
- |
200 |
- if (kvm_read(kp, namelist[X_CP_TIME].n_value, stime, sizeof(stime)) == -1) { |
201 |
- uplog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kp)); |
202 |
-// warn("kvm_read: %s", kvm_geterr(kp)); |
203 |
- } |
204 |
- else |
205 |
- sleep(1); /* to avoid the initial 0 idle value */ |
206 |
- } |
207 |
- |
208 |
- if (kvm_read(kp, namelist[X_CP_TIME].n_value, ctime, sizeof(ctime)) == -1) { |
209 |
- uplog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kp)); |
210 |
-// warn("kvm_read: %s", kvm_geterr(kp)); |
211 |
- } |
212 |
- else { |
213 |
- int state; |
214 |
- double time = 0.; |
215 |
- |
216 |
- for(state = 0; state < CPUSTATES; state++) |
217 |
- time += ctime[state] - stime[state]; |
218 |
- |
219 |
- if (time == 0.) |
220 |
- time = 1.; |
221 |
- |
222 |
- *pidle = (ctime[CP_IDLE] - stime[CP_IDLE]) * 100. / time; |
223 |
- |
56 |
- |
224 |
- for(state = 0; state < CPUSTATES; state++) |
57 |
- /* Use the 3rd element (15 minute Load-average) */ |
225 |
- stime[state] = ctime[state]; |
58 |
- *loadavg = loadavgs[2]; |
226 |
- } |
59 |
+ else { |
227 |
- } |
60 |
+ /* Use the 3rd element (15 minute Load-average) */ |
228 |
- else { |
61 |
+ *loadavg = loadavgs[2]; |
229 |
- cfg_sendidle = 0; |
62 |
#if defined DEBUG |
230 |
- } |
63 |
- uplog(LOG_DEBUG, "getLoadavg() %.2f", *loadavg); |
231 |
+ { "_cp_time" }, |
64 |
+ uplog(LOG_DEBUG, "getLoadavg() %.2f", *loadavg); |
232 |
+ { NULL }, |
65 |
#endif /* DEBUG */ |
233 |
+ }; |
|
|
234 |
+ |
235 |
+ if (!initialized) { |
236 |
+ char errbuf[_POSIX2_LINE_MAX]; |
237 |
+ initialized = 1; |
238 |
+ |
239 |
+ setgid(getgid()); |
240 |
+ |
241 |
+ kp = kvm_openfiles(NULL,NULL,NULL,O_RDONLY,errbuf); |
242 |
+ |
243 |
+ if (!kp) { |
244 |
+ uplog(LOG_WARNING, "kvm_openfiles: %s",errbuf); |
245 |
+ /* warn("kvm_openfiles: %s",errbuf); */ |
246 |
+ } else if (kvm_nlist(kp,namelist) == -1) { |
247 |
+ uplog(LOG_WARNING, "kvm_nlist: %s",kvm_geterr(kp)); |
248 |
+ /* warn("kvm_nlist: %s",kvm_geterr(kp)); */ |
249 |
+ } |
250 |
+ } |
66 |
+ } |
251 |
+ |
67 |
} |
252 |
+ if (kp) { |
68 |
|
253 |
+ long ctime[CPUSTATES]; |
69 |
#if !defined __MACH__ /* Mach kernel stuff is in stats-mach.c */ |
254 |
+ static long stime[CPUSTATES]; |
70 |
@@ -245,7 +245,7 @@ |
255 |
+ static int first_time = 1; |
71 |
|
256 |
+ |
72 |
for (state = 0; state < CPUSTATES; state++) { |
257 |
+ if (first_time) { |
73 |
time += ctime[state] - stime[state]; |
258 |
+ first_time = 0; |
74 |
- if (!(state == CPU_STATE_IDLE)) { |
259 |
+ |
75 |
+ if (!(state == CP_IDLE)) { |
260 |
+ if (kvm_read(kp, namelist[X_CP_TIME].n_value, |
76 |
nonidlecpu += (float)(ctime[state] - stime[state]); |
261 |
+ stime, sizeof(stime)) == -1) { |
77 |
} |
262 |
+ uplog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kp)); |
78 |
} |
263 |
+ /* warn("kvm_read: %s", kvm_geterr(kp)); */ |
79 |
@@ -258,6 +258,10 @@ |
264 |
+ } else |
80 |
|
265 |
+ sleep(1); /* to avoid the initial 0 idle value */ |
81 |
for (state = 0; state < CPUSTATES; state++) |
266 |
+ } |
82 |
stime[state] = ctime[state]; |
267 |
+ |
83 |
+#if defined DEBUG |
268 |
+ if (kvm_read(kp, namelist[X_CP_TIME].n_value, |
84 |
+ uplog(LOG_DEBUG, "getLoadIdle() load=%.1f idle=%.1f", *UsagePercent, |
269 |
+ ctime, sizeof(ctime)) == -1) { |
85 |
+ *IdlePercent); |
270 |
+ uplog(LOG_WARNING, "kvm_read: %s", kvm_geterr(kp)); |
86 |
+#endif /* DEBUG */ |
271 |
+ /* warn("kvm_read: %s", kvm_geterr(kp)); */ |
87 |
} |
272 |
+ } else { |
|
|
273 |
+ int state; |
274 |
+ double time = 0.; |
275 |
+ |
276 |
+ for (state = 0; state < CPUSTATES; state++) |
277 |
+ time += ctime[state] - stime[state]; |
278 |
+ |
279 |
+ if (time == 0.) |
280 |
+ time = 1.; |
281 |
+ |
282 |
+ *pidle = (ctime[CP_IDLE] - stime[CP_IDLE]) * 100. / time; |
283 |
+ |
284 |
+ for (state = 0; state < CPUSTATES; state++) |
285 |
+ stime[state] = ctime[state]; |
286 |
+ } |
287 |
+ } else { |
288 |
+ cfg_sendidle = 0; |
289 |
} |
88 |
} |
290 |
+ } |
89 |
else { |
|
|
90 |
@@ -268,17 +272,13 @@ |
91 |
cfg_SendUsage = 0; |
92 |
cfg_SendIdle = 0; |
93 |
} |
94 |
-#if defined DEBUG |
95 |
- uplog(LOG_DEBUG, "getLoadIdle() load=%.1f idle=%.1f", *UsagePercent, |
96 |
- *IdlePercent); |
97 |
-#endif /* DEBUG */ |
291 |
} |
98 |
} |
292 |
-#endif /* PLATFORM_BSD */ |
99 |
#endif /* !__MACH__ */ |
293 |
+ |
100 |
|
294 |
+#endif /* PLATFORM_BSD */ |
101 |
void |
|
|
102 |
getUptime(unsigned long *uptimeminutes) |
103 |
{ |
104 |
- static time_t boottimesecs; |
105 |
+ time_t boottimesecs; |
106 |
|
107 |
boottimesecs = initBoottime(); |
108 |
|
109 |
@@ -293,7 +293,7 @@ |
110 |
#endif /* DEBUG */ |
111 |
} |
112 |
else { |
113 |
- uplog(LOG_INFO, _("%s: %s failed"), _("ERROR:"), |
114 |
+ uplog(LOG_ERR, _("%s %s failed"), _("ERROR:"), |
115 |
"getUptime() boottime.tv_sec"); |
116 |
} |
117 |
} |
118 |
@@ -306,19 +306,18 @@ |
119 |
double *IdlePercent, char *osname, char *osversion, char *cpu, |
120 |
double *loadavg) |
121 |
{ |
122 |
- getUptime(&*uptimeminutes); |
123 |
+ getUptime(uptimeminutes); |
124 |
|
125 |
if (cfg_SendUsage || cfg_SendIdle) |
126 |
- getLoadIdle(&*UsagePercent, &*IdlePercent); |
127 |
+ getLoadIdle(UsagePercent, IdlePercent); |
128 |
|
129 |
if (cfg_sendosname || cfg_sendosversion) |
130 |
- initOS(&*osname, &*osversion); |
131 |
+ initOS(osname, osversion); |
132 |
|
133 |
if (cfg_sendcpu) |
134 |
- initCPU(&*cpu); |
135 |
- |
136 |
- /* if (cfg_sendloadavg) */ |
137 |
- getLoadavg(&*loadavg); |
138 |
+ initCPU(cpu); |
139 |
|
140 |
+ if (cfg_sendloadavg) |
141 |
+ getLoadavg(loadavg); |
142 |
} |
143 |
#endif /* PLATFORM_BSD */ |