envstat.c revision 1.18 1 1.18 mrg /* $NetBSD: envstat.c,v 1.18 2004/03/21 10:52:26 mrg Exp $ */
2 1.1 groo
3 1.1 groo /*-
4 1.1 groo * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 groo * All rights reserved.
6 1.1 groo *
7 1.1 groo * This code is derived from software contributed to The NetBSD Foundation
8 1.1 groo * by Bill Squier.
9 1.1 groo *
10 1.1 groo * Redistribution and use in source and binary forms, with or without
11 1.1 groo * modification, are permitted provided that the following conditions
12 1.1 groo * are met:
13 1.1 groo * 1. Redistributions of source code must retain the above copyright
14 1.1 groo * notice, this list of conditions and the following disclaimer.
15 1.1 groo * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 groo * notice, this list of conditions and the following disclaimer in the
17 1.1 groo * documentation and/or other materials provided with the distribution.
18 1.1 groo * 3. All advertising materials mentioning features or use of this software
19 1.1 groo * must display the following acknowledgement:
20 1.1 groo * This product includes software developed by the NetBSD
21 1.1 groo * Foundation, Inc. and its contributors.
22 1.1 groo * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.1 groo * contributors may be used to endorse or promote products derived
24 1.1 groo * from this software without specific prior written permission.
25 1.1 groo *
26 1.1 groo * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.1 groo * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.1 groo * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.1 groo * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.1 groo * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.1 groo * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.1 groo * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.1 groo * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.1 groo * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.1 groo * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.1 groo * POSSIBILITY OF SUCH DAMAGE.
37 1.1 groo */
38 1.1 groo
39 1.1 groo #include <sys/cdefs.h>
40 1.1 groo #ifndef lint
41 1.18 mrg __RCSID("$NetBSD: envstat.c,v 1.18 2004/03/21 10:52:26 mrg Exp $");
42 1.1 groo #endif
43 1.1 groo
44 1.1 groo #include <fcntl.h>
45 1.1 groo #include <stdio.h>
46 1.1 groo #include <stdlib.h>
47 1.1 groo #include <string.h>
48 1.1 groo #include <unistd.h>
49 1.3 thorpej #include <err.h>
50 1.8 augustss #include <paths.h>
51 1.1 groo
52 1.12 christos #define ENVSYSUNITNAMES
53 1.1 groo #include <sys/envsys.h>
54 1.1 groo #include <sys/ioctl.h>
55 1.1 groo
56 1.7 augustss int main(int, char **);
57 1.12 christos void listsensors(envsys_basic_info_t *, size_t);
58 1.12 christos size_t numsensors(int);
59 1.12 christos void fillsensors(int, envsys_tre_data_t *, envsys_basic_info_t *, size_t);
60 1.12 christos size_t longestname(envsys_basic_info_t *, size_t);
61 1.12 christos int marksensors(envsys_basic_info_t *, int *, char *, size_t);
62 1.12 christos int strtosnum(envsys_basic_info_t *, const char *, size_t);
63 1.12 christos void header(size_t, int, envsys_basic_info_t *, const int * const, size_t);
64 1.12 christos void values(size_t, int, envsys_tre_data_t *, const int * const, size_t);
65 1.7 augustss void usage(void);
66 1.1 groo
67 1.6 explorer int rflag = 0;
68 1.6 explorer
69 1.1 groo int
70 1.7 augustss main(int argc, char **argv)
71 1.1 groo {
72 1.12 christos int c, fd, ls, celsius;
73 1.12 christos size_t ns;
74 1.12 christos size_t interval, width, headrep, headcnt;
75 1.1 groo envsys_tre_data_t *etds;
76 1.1 groo envsys_basic_info_t *ebis;
77 1.1 groo int *cetds;
78 1.1 groo char *sensors;
79 1.3 thorpej const char *dev;
80 1.1 groo
81 1.1 groo fd = -1;
82 1.1 groo ls = 0;
83 1.1 groo celsius = 1;
84 1.1 groo interval = 0;
85 1.1 groo width = 0;
86 1.1 groo sensors = NULL;
87 1.1 groo headrep = 22;
88 1.1 groo
89 1.6 explorer while ((c = getopt(argc, argv, "cfi:ln:rs:w:r")) != -1) {
90 1.1 groo switch(c) {
91 1.6 explorer case 'r':
92 1.18 mrg rflag = 1;
93 1.6 explorer break;
94 1.1 groo case 'i': /* wait time between displays */
95 1.1 groo interval = atoi(optarg);
96 1.1 groo break;
97 1.1 groo case 'w': /* minimum column width */
98 1.1 groo width = atoi(optarg);
99 1.1 groo break;
100 1.1 groo case 'l': /* list sensor names */
101 1.1 groo ls = 1;
102 1.1 groo break;
103 1.1 groo case 'n': /* repeat header every headrep lines */
104 1.1 groo headrep = atoi(optarg);
105 1.1 groo break;
106 1.1 groo case 's': /* restrict display to named sensors */
107 1.1 groo sensors = (char *)malloc(strlen(optarg) + 1);
108 1.1 groo if (sensors == NULL)
109 1.1 groo exit(1);
110 1.1 groo strcpy(sensors, optarg);
111 1.1 groo break;
112 1.1 groo case 'f': /* display temp in degF */
113 1.1 groo celsius = 0;
114 1.1 groo break;
115 1.1 groo case '?':
116 1.1 groo default:
117 1.1 groo usage();
118 1.1 groo /* NOTREACHED */
119 1.1 groo }
120 1.1 groo }
121 1.1 groo
122 1.3 thorpej if (optind < argc)
123 1.3 thorpej dev = argv[optind];
124 1.3 thorpej else
125 1.3 thorpej dev = _PATH_SYSMON;
126 1.1 groo
127 1.3 thorpej if ((fd = open(dev, O_RDONLY)) == -1)
128 1.3 thorpej err(1, "unable to open %s", dev);
129 1.1 groo
130 1.1 groo /*
131 1.1 groo * Determine number of sensors, allocate and fill arrays with
132 1.1 groo * initial information. Determine column width
133 1.1 groo */
134 1.12 christos if ((ns = numsensors(fd)) == 0)
135 1.12 christos errx(1, "No sensors found");
136 1.3 thorpej
137 1.1 groo cetds = (int *)malloc(ns * sizeof(int));
138 1.1 groo etds = (envsys_tre_data_t *)malloc(ns * sizeof(envsys_tre_data_t));
139 1.1 groo ebis = (envsys_basic_info_t *)malloc(ns * sizeof(envsys_basic_info_t));
140 1.1 groo
141 1.3 thorpej if ((cetds == NULL) || (etds == NULL) || (ebis == NULL))
142 1.12 christos err(1, "Out of memory");
143 1.1 groo
144 1.12 christos fillsensors(fd, etds, ebis, ns);
145 1.1 groo
146 1.1 groo if (ls) {
147 1.1 groo listsensors(ebis, ns);
148 1.1 groo exit(0);
149 1.1 groo }
150 1.1 groo
151 1.1 groo
152 1.1 groo #define MAX(x, y) (((x) > (y)) ? (x) : (y))
153 1.1 groo if (!width) {
154 1.1 groo width = longestname(ebis, ns);
155 1.1 groo width = MAX(((79 - ns) / ns), width);
156 1.1 groo }
157 1.1 groo
158 1.1 groo /* Mark which sensor numbers are to be displayed */
159 1.1 groo if (marksensors(ebis, cetds, sensors, ns) == -1)
160 1.1 groo exit(1);
161 1.1 groo
162 1.6 explorer if (rflag) {
163 1.6 explorer int i;
164 1.6 explorer
165 1.6 explorer for (i = 0 ; i < ns ; i++) {
166 1.18 mrg if (cetds[i] == 0)
167 1.18 mrg continue;
168 1.18 mrg
169 1.14 mycroft if ((etds[i].validflags & ENVSYS_FCURVALID) == 0)
170 1.6 explorer continue;
171 1.6 explorer
172 1.14 mycroft if (ebis[i].units == ENVSYS_INDICATOR &&
173 1.14 mycroft etds[i].cur.data_s == 0)
174 1.6 explorer continue;
175 1.6 explorer
176 1.14 mycroft printf("%*.*s", (int)width, (int)width, ebis[i].desc);
177 1.9 chris /* different units need some magic */
178 1.9 chris switch (ebis[i].units)
179 1.9 chris {
180 1.14 mycroft case ENVSYS_INDICATOR:
181 1.14 mycroft break;
182 1.14 mycroft case ENVSYS_INTEGER:
183 1.14 mycroft printf(": %10d", etds[i].cur.data_s);
184 1.14 mycroft break;
185 1.14 mycroft case ENVSYS_STEMP: {
186 1.14 mycroft double temp = (etds[i].cur.data_s / 1000000.0)
187 1.17 soren - 273.15;
188 1.14 mycroft if (celsius)
189 1.14 mycroft printf(": %10.3f degC", temp);
190 1.14 mycroft else {
191 1.14 mycroft temp = (9.0 / 5.0) * temp + 32.0;
192 1.14 mycroft printf(": %10.3f degF", temp);
193 1.14 mycroft }
194 1.14 mycroft break;
195 1.14 mycroft }
196 1.14 mycroft case ENVSYS_SFANRPM:
197 1.14 mycroft printf(": %10u RPM", etds[i].cur.data_us);
198 1.14 mycroft break;
199 1.14 mycroft default:
200 1.14 mycroft printf(": %10.3f %s",
201 1.14 mycroft etds[i].cur.data_s / 1000000.0,
202 1.14 mycroft envsysunitnames[ebis[i].units]);
203 1.14 mycroft break;
204 1.9 chris }
205 1.14 mycroft
206 1.6 explorer if (etds[i].validflags & ENVSYS_FFRACVALID) {
207 1.6 explorer printf(" (%5.2f%%)",
208 1.6 explorer (etds[i].cur.data_s * 100.0) /
209 1.6 explorer etds[i].max.data_s);
210 1.6 explorer }
211 1.6 explorer
212 1.6 explorer printf("\n");
213 1.6 explorer }
214 1.10 christos exit(0);
215 1.6 explorer }
216 1.6 explorer
217 1.6 explorer
218 1.6 explorer
219 1.1 groo /* If we didn't specify an interval value, print the sensors once */
220 1.1 groo if (!interval) {
221 1.1 groo if (headrep)
222 1.1 groo header(width, celsius, ebis, cetds, ns);
223 1.1 groo values(width, celsius, etds, cetds, ns);
224 1.1 groo
225 1.1 groo exit (0);
226 1.1 groo }
227 1.1 groo
228 1.1 groo headcnt = 0;
229 1.1 groo if (headrep)
230 1.1 groo header(width, celsius, ebis, cetds, ns);
231 1.1 groo
232 1.1 groo for (;;) {
233 1.1 groo values(width, celsius, etds, cetds, ns);
234 1.1 groo if (headrep && (++headcnt == headrep)) {
235 1.1 groo headcnt = 0;
236 1.1 groo header(width, celsius, ebis, cetds, ns);
237 1.1 groo }
238 1.1 groo
239 1.1 groo sleep(interval);
240 1.1 groo
241 1.12 christos fillsensors(fd, etds, ebis, ns);
242 1.1 groo }
243 1.1 groo
244 1.1 groo /* NOTREACHED */
245 1.1 groo return (0);
246 1.1 groo }
247 1.1 groo
248 1.1 groo
249 1.1 groo /*
250 1.1 groo * pre: cetds[i] != 0 iff sensor i should appear in the output
251 1.1 groo * post: a column header line is displayed on stdout
252 1.1 groo */
253 1.1 groo void
254 1.12 christos header(size_t width, int celsius, envsys_basic_info_t *ebis,
255 1.12 christos const int * const cetds, size_t ns)
256 1.1 groo {
257 1.1 groo int i;
258 1.1 groo const char *s;
259 1.1 groo
260 1.1 groo /* sensor names */
261 1.1 groo for (i = 0; i < ns; ++i)
262 1.1 groo if (cetds[i])
263 1.1 groo printf(" %*.*s", (int)width, (int)width, ebis[i].desc);
264 1.1 groo
265 1.1 groo printf("\n");
266 1.1 groo
267 1.1 groo /* units */
268 1.1 groo for (i = 0; i < ns; ++i)
269 1.1 groo if (cetds[i]) {
270 1.1 groo if ((ebis[i].units == ENVSYS_STEMP) &&
271 1.1 groo !celsius)
272 1.1 groo s = "degF";
273 1.6 explorer else if (ebis[i].units >= ENVSYS_NSENSORS)
274 1.12 christos s = envsysunitnames[ENVSYS_NSENSORS];
275 1.1 groo else
276 1.12 christos s = envsysunitnames[ebis[i].units];
277 1.6 explorer
278 1.1 groo printf(" %*.*s", (int)width, (int)width, s);
279 1.1 groo }
280 1.1 groo printf("\n");
281 1.1 groo }
282 1.1 groo
283 1.1 groo void
284 1.12 christos values(size_t width, int celsius, envsys_tre_data_t *etds,
285 1.12 christos const int * const cetds, size_t ns)
286 1.1 groo {
287 1.1 groo int i;
288 1.1 groo double temp;
289 1.6 explorer
290 1.1 groo for (i = 0; i < ns; ++i)
291 1.1 groo if (cetds[i]) {
292 1.1 groo
293 1.1 groo /* * sensors without valid data */
294 1.1 groo if ((etds[i].validflags & ENVSYS_FCURVALID) == 0) {
295 1.1 groo printf(" %*.*s", (int)width, (int)width, "*");
296 1.1 groo continue;
297 1.1 groo }
298 1.1 groo
299 1.1 groo switch(etds[i].units) {
300 1.6 explorer case ENVSYS_INDICATOR:
301 1.6 explorer printf(" %*.*s", (int)width, (int)width,
302 1.6 explorer etds[i].cur.data_us ? "ON" : "OFF");
303 1.6 explorer break;
304 1.1 groo case ENVSYS_STEMP:
305 1.1 groo temp = (etds[i].cur.data_us / 1000000.0) -
306 1.17 soren 273.15;
307 1.1 groo if (!celsius)
308 1.1 groo temp = (9.0 / 5.0) * temp + 32.0;
309 1.13 martin printf(" %*.2f", (int)width, temp);
310 1.1 groo break;
311 1.1 groo case ENVSYS_SFANRPM:
312 1.13 martin printf(" %*u", (int)width, etds[i].cur.data_us);
313 1.1 groo break;
314 1.1 groo default:
315 1.13 martin printf(" %*.2f", (int)width, etds[i].cur.data_s /
316 1.1 groo 1000000.0);
317 1.1 groo break;
318 1.1 groo }
319 1.1 groo }
320 1.1 groo printf("\n");
321 1.1 groo }
322 1.1 groo
323 1.1 groo
324 1.1 groo /*
325 1.1 groo * post: displays usage on stderr
326 1.1 groo */
327 1.1 groo void
328 1.7 augustss usage(void)
329 1.1 groo {
330 1.5 cgd
331 1.15 jmmv (void)fprintf(stderr, "usage: %s [-cr] [-s s1,s2,...]", getprogname());
332 1.12 christos (void)fprintf(stderr, " [-i interval] [-n headrep] [-w width]");
333 1.12 christos (void)fprintf(stderr, " [device]\n");
334 1.12 christos (void)fprintf(stderr, " %s -l [device]\n", getprogname());
335 1.1 groo exit(1);
336 1.1 groo }
337 1.1 groo
338 1.1 groo
339 1.1 groo /*
340 1.1 groo * post: a list of sensor names supported by the device is displayed on stdout
341 1.1 groo */
342 1.1 groo void
343 1.12 christos listsensors(envsys_basic_info_t *ebis, size_t ns)
344 1.1 groo {
345 1.1 groo int i;
346 1.1 groo
347 1.1 groo for (i = 0; i < ns; ++i)
348 1.1 groo if (ebis[i].validflags & ENVSYS_FVALID)
349 1.1 groo printf("%s\n", ebis[i].desc);
350 1.1 groo }
351 1.1 groo
352 1.6 explorer
353 1.1 groo /*
354 1.1 groo * pre: fd contains a valid file descriptor of an envsys(4) supporting device
355 1.1 groo * post: returns the number of valid sensors provided by the device
356 1.1 groo * or -1 on error
357 1.1 groo */
358 1.12 christos size_t
359 1.7 augustss numsensors(int fd)
360 1.1 groo {
361 1.1 groo int count = 0, valid = 1;
362 1.1 groo envsys_tre_data_t etd;
363 1.1 groo etd.sensor = 0;
364 1.1 groo
365 1.1 groo while (valid) {
366 1.12 christos if (ioctl(fd, ENVSYS_GTREDATA, &etd) == -1)
367 1.12 christos err(1, "Can't get sensor data");
368 1.1 groo
369 1.1 groo valid = etd.validflags & ENVSYS_FVALID;
370 1.1 groo if (valid)
371 1.1 groo ++count;
372 1.1 groo
373 1.1 groo ++etd.sensor;
374 1.1 groo }
375 1.1 groo
376 1.1 groo return count;
377 1.1 groo }
378 1.1 groo
379 1.1 groo /*
380 1.1 groo * pre: fd contains a valid file descriptor of an envsys(4) supporting device
381 1.1 groo * && ns is the number of sensors
382 1.1 groo * && etds and ebis are arrays of sufficient size
383 1.1 groo * post: returns 0 and etds and ebis arrays are filled with sensor info
384 1.1 groo * or returns -1 on failure
385 1.1 groo */
386 1.12 christos void
387 1.12 christos fillsensors(int fd, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
388 1.12 christos size_t ns)
389 1.1 groo {
390 1.1 groo int i;
391 1.1 groo
392 1.1 groo for (i = 0; i < ns; ++i) {
393 1.1 groo ebis[i].sensor = i;
394 1.1 groo if (ioctl(fd, ENVSYS_GTREINFO, &ebis[i]) == -1)
395 1.12 christos err(1, "Can't get sensor info for sensor %d", i);
396 1.1 groo
397 1.1 groo etds[i].sensor = i;
398 1.1 groo if (ioctl(fd, ENVSYS_GTREDATA, &etds[i]) == -1)
399 1.12 christos err(1, "Can't get sensor data for sensor %d", i);
400 1.1 groo }
401 1.1 groo }
402 1.1 groo
403 1.1 groo
404 1.1 groo /*
405 1.1 groo * post: returns the strlen() of the longest sensor name
406 1.1 groo */
407 1.12 christos size_t
408 1.12 christos longestname(envsys_basic_info_t *ebis, size_t ns)
409 1.1 groo {
410 1.12 christos size_t i, maxlen, cur;
411 1.1 groo
412 1.1 groo maxlen = 0;
413 1.1 groo
414 1.1 groo for (i = 0; i < ns; ++i) {
415 1.1 groo cur = strlen(ebis[i].desc);
416 1.1 groo if (cur > maxlen)
417 1.1 groo maxlen = cur;
418 1.1 groo }
419 1.1 groo
420 1.1 groo return maxlen;
421 1.1 groo }
422 1.1 groo
423 1.1 groo /*
424 1.1 groo * post: returns 0 and cetds[i] != 0 iff sensor i should appear in the output
425 1.1 groo * or returns -1
426 1.1 groo */
427 1.1 groo int
428 1.12 christos marksensors(envsys_basic_info_t *ebis, int *cetds, char *sensors, size_t ns)
429 1.1 groo {
430 1.12 christos size_t i;
431 1.1 groo char *s;
432 1.1 groo
433 1.1 groo if (sensors == NULL) {
434 1.1 groo /* No sensors specified, include them all */
435 1.1 groo for (i = 0; i < ns; ++i)
436 1.1 groo cetds[i] = 1;
437 1.1 groo
438 1.1 groo return 0;
439 1.1 groo }
440 1.1 groo
441 1.1 groo /* Assume no sensors in display */
442 1.1 groo memset(cetds, 0, ns * sizeof(int));
443 1.1 groo
444 1.1 groo s = strtok(sensors, ",");
445 1.1 groo while (s != NULL) {
446 1.12 christos int snum;
447 1.12 christos if ((snum = strtosnum(ebis, s, ns)) != -1)
448 1.12 christos cetds[snum] = 1;
449 1.1 groo else {
450 1.12 christos warnx("Unknown sensor %s", s);
451 1.1 groo return (-1);
452 1.1 groo }
453 1.1 groo
454 1.1 groo s = strtok(NULL, ",");
455 1.1 groo }
456 1.1 groo
457 1.1 groo /* Check if we have at least one sensor selected for output */
458 1.1 groo for (i = 0; i < ns; ++i)
459 1.1 groo if (cetds[i] == 1)
460 1.1 groo return (0);
461 1.1 groo
462 1.12 christos warnx("No sensors selected for display");
463 1.1 groo return (-1);
464 1.1 groo }
465 1.6 explorer
466 1.1 groo
467 1.1 groo /*
468 1.1 groo * returns -1 if s is not a valid sensor name for the device
469 1.1 groo * or the sensor number of a sensor which has that name
470 1.1 groo */
471 1.1 groo int
472 1.12 christos strtosnum(envsys_basic_info_t *ebis, const char *s, size_t ns)
473 1.1 groo {
474 1.12 christos size_t i;
475 1.1 groo
476 1.1 groo for (i = 0; i < ns; ++i) {
477 1.1 groo if((ebis[i].validflags & ENVSYS_FVALID) &&
478 1.1 groo !strcmp(s, ebis[i].desc))
479 1.1 groo return ebis[i].sensor;
480 1.1 groo }
481 1.1 groo
482 1.1 groo return -1;
483 1.1 groo }
484