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