envstat.c revision 1.37 1 /* $NetBSD: envstat.c,v 1.37 2007/07/17 17:40:59 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juan Romero Pardines.
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 Juan Romero Pardines
21 * for the NetBSD 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 /*
40 * TODO:
41 *
42 * o Some checks should be added to ensure that the user does not
43 * set unwanted values for the critical limits.
44 */
45
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <stdbool.h>
49 #include <string.h>
50 #include <unistd.h>
51 #include <fcntl.h>
52 #include <err.h>
53 #include <errno.h>
54 #include <prop/proplib.h>
55 #include <sys/envsys.h>
56
57 #define _PATH_DEV_SYSMON "/dev/sysmon"
58
59 #define ENVSYS_DFLAG 0x00000001 /* list registered devices */
60 #define ENVSYS_FFLAG 0x00000002 /* show temp in farenheit */
61 #define ENVSYS_LFLAG 0x00000004 /* list sensors */
62 #define ENVSYS_XFLAG 0x00000008 /* externalize dictionary */
63
64 /*
65 * Operation flags for -m.
66 */
67 #define USERF_SCRITICAL 0x00000001 /* set a critical limit */
68 #define USERF_RCRITICAL 0x00000002 /* remove a critical limit */
69 #define USERF_SCRITMAX 0x00000004 /* set a critical max limit */
70 #define USERF_RCRITMAX 0x00000008 /* remove a critical max limit */
71 #define USERF_SCRITMIN 0x00000010 /* set a critical min limit */
72 #define USERF_RCRITMIN 0x00000020 /* remove a critical min limit */
73 #define USERF_SRFACT 0x00000040 /* set a new rfact */
74 #define USERF_SDESCR 0x00000080 /* set a new description */
75
76 struct envsys_sensor {
77 bool invalid;
78 bool visible;
79 bool percentage;
80 int32_t cur_value;
81 int32_t max_value;
82 int32_t min_value;
83 int32_t avg_value;
84 int32_t critcap_value;
85 int32_t critmin_value;
86 int32_t critmax_value;
87 char desc[ENVSYS_DESCLEN];
88 char type[ENVSYS_DESCLEN];
89 char drvstate[ENVSYS_DESCLEN];
90 };
91
92 static int interval, flags, width;
93 static char *mydevname, *sensors, *userreq;
94 static struct envsys_sensor *gesen;
95 static size_t gnelems, newsize;
96
97 static int parse_dictionary(int);
98 static int send_dictionary(int);
99 static int find_sensors(prop_array_t);
100 static void print_sensors(struct envsys_sensor *, size_t);
101 static int check_sensors(struct envsys_sensor *, char *, size_t);
102 static int usage(void);
103
104
105 int main(int argc, char **argv)
106 {
107 prop_dictionary_t dict;
108 int c, fd, rval;
109 char *endptr;
110
111 rval = flags = interval = width = 0;
112 newsize = gnelems = 0;
113 gesen = NULL;
114
115 setprogname(argv[0]);
116
117 while ((c = getopt(argc, argv, "Dd:fi:lm:rs:w:x")) != -1) {
118 switch (c) {
119 case 'd': /* show sensors of a specific device */
120 mydevname = strdup(optarg);
121 if (mydevname == NULL)
122 err(ENOMEM, "out of memory");
123 break;
124 case 'i': /* wait time between intervals */
125 interval = strtoul(optarg, &endptr, 10);
126 if (*endptr != '\0')
127 errx(1, "interval must be an integer");
128 break;
129 case 'D': /* list registered devices */
130 flags |= ENVSYS_DFLAG;
131 break;
132 case 'f': /* display temperature in Farenheit */
133 flags |= ENVSYS_FFLAG;
134 break;
135 case 'l': /* list sensors */
136 flags |= ENVSYS_LFLAG;
137 break;
138 case 'w': /* width value for the lines */
139 width = strtoul(optarg, &endptr, 10);
140 if (*endptr != '\0')
141 errx(1, "width must be an integer");
142 break;
143 case 'x': /* print the dictionary in raw format */
144 flags |= ENVSYS_XFLAG;
145 break;
146 case 'r':
147 /*
148 * This flag doesn't do anything... it's only here for
149 * compatibility with the old implementation.
150 */
151 break;
152 case 's': /* only show specified sensors */
153 sensors = strdup(optarg);
154 if (sensors == NULL)
155 err(ENOMEM, "out of memory");
156 break;
157 case 'm':
158 userreq = strdup(optarg);
159 if (userreq == NULL)
160 err(ENOMEM, "out of memory");
161 break;
162 case '?':
163 default:
164 usage();
165 /* NOTREACHED */
166 }
167 }
168
169 if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
170 err(EXIT_FAILURE, "open");
171
172 if (!interval && (flags & ENVSYS_XFLAG)) {
173 if (prop_dictionary_recv_ioctl(fd,
174 ENVSYS_GETDICTIONARY,
175 &dict)) {
176 (void)close(fd);
177 err(EINVAL, "recv_ioctl");
178 }
179 }
180
181 if (argc == 1) {
182 rval = parse_dictionary(fd);
183
184 } else if (userreq) {
185 if (!sensors || !mydevname) {
186 (void)fprintf(stderr, "%s: -m cannot be used without "
187 "-s and -d\n", getprogname());
188 return EINVAL;
189 }
190
191 rval = send_dictionary(fd);
192 goto out;
193
194 } else if (interval) {
195 for (;;) {
196 rval = parse_dictionary(fd);
197 if (rval)
198 goto out;
199 (void)fflush(stdout);
200 (void)sleep(interval);
201 }
202
203 } else if (!interval) {
204 if (flags & ENVSYS_XFLAG)
205 (void)printf("%s", prop_dictionary_externalize(dict));
206 else
207 rval = parse_dictionary(fd);
208
209 } else
210 usage();
211
212 out:
213 if (sensors)
214 free(sensors);
215 if (userreq)
216 free(userreq);
217 if (mydevname)
218 free(mydevname);
219 if (gesen)
220 free(gesen);
221 (void)close(fd);
222 return rval;
223 }
224
225 static int
226 send_dictionary(int fd)
227 {
228 prop_dictionary_t dict, udict;
229 prop_object_t obj;
230 char *buf, *target, *endptr;
231 int error, i, uflag;
232 double val;
233
234 error = uflag = val = 0;
235
236 /*
237 * part 1: kernel dictionary.
238 *
239 * This parts consists in parsing the kernel dictionary
240 * to check for unknown device or sensor and we must
241 * know what type of sensor are we trying to set
242 * a critical condition.
243 */
244 error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
245 if (error)
246 return error;
247
248 if (mydevname) {
249 obj = prop_dictionary_get(dict, mydevname);
250 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
251 warnx("unknown device `%s'", mydevname);
252 prop_object_release(dict);
253 return EINVAL;
254 }
255
256 if (find_sensors(obj)) {
257 prop_object_release(dict);
258 return EINVAL;
259 }
260 }
261
262 /* find the type for selected sensor */
263 for (i = 0; i < gnelems; i++)
264 if (strcmp(sensors, gesen[i].desc) == 0)
265 break;
266
267 /* we know the type of the sensor now, release kernel dict */
268 prop_object_release(dict);
269 /* we don't need the rdonly fd */
270 (void)close(fd);
271
272
273 /*
274 * part 2: userland dictionary.
275 *
276 * This parts consists in setting the values provided
277 * by the user and convert when necesssary to send
278 * them to the kernel again.
279 */
280 udict = prop_dictionary_create();
281
282 #define MKPROP(var, str) \
283 do { \
284 obj = prop_string_create_cstring_nocopy(var); \
285 if (obj == NULL || !prop_dictionary_set(udict, (str), obj)) { \
286 error = EINVAL; \
287 goto out; \
288 } \
289 } while (/* CONSTCOND */ 0)
290
291 /* create the driver-name object */
292 MKPROP(mydevname, "driver-name");
293 prop_object_release(obj);
294
295 /* create the sensor-name object */
296 MKPROP(sensors, "sensor-name");
297 prop_object_release(obj);
298
299 #undef MKPROP
300
301 /*
302 * parse the -m argument; we understand the following ways:
303 *
304 * -m critical/crit{max,min}=value
305 * -m critical/crit{max,min}=remove
306 * -m desc="BLAH"
307 * -m rfact=value
308 */
309 if (userreq) {
310 buf = strtok(userreq, "=");
311 target = strdup(buf);
312 if (target == NULL) {
313 error = ENOMEM;
314 goto out;
315 }
316
317 while (buf != NULL) {
318 /*
319 * skip current string if it's the same
320 * than target requested.
321 */
322 if (strcmp(target, buf) == 0)
323 buf = strtok(NULL, "=");
324
325 /* check what target was requested */
326 if (strcmp(target, "desc") == 0) {
327 uflag |= USERF_SDESCR;
328 obj = prop_string_create_cstring_nocopy(buf);
329 break;
330 #define SETNCHECKVAL(a, b) \
331 do { \
332 if (strcmp(buf, "remove") == 0) \
333 uflag |= (a); \
334 else { \
335 uflag |= (b); \
336 val = strtod(buf, &endptr); \
337 if (*endptr != '\0') { \
338 (void)printf("%s: invalid value\n", \
339 getprogname()); \
340 error = EINVAL; \
341 goto out; \
342 } \
343 } \
344 } while (/* CONSTCOND */ 0)
345
346 } else if (strcmp(target, "critical") == 0) {
347 SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
348 break;
349 } else if (strcmp(target, "critmax") == 0) {
350 SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
351 break;
352 } else if (strcmp(target, "critmin") == 0) {
353 SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
354 break;
355 } else if (strcmp(target, "rfact") == 0) {
356 uflag |= USERF_SRFACT;
357 val = strtod(buf, &endptr);
358 if (*endptr != '\0') {
359 (void)printf("%s: invalid value\n",
360 getprogname());
361 error = EINVAL;
362 goto out;
363 }
364 break;
365 } else {
366 (void)printf("%s: invalid target\n",
367 getprogname());
368 error = EINVAL;
369 goto out;
370 }
371 }
372 free(target);
373 }
374
375 #undef SETNCHECKVAL
376
377 /* critical capacity for percentage sensors */
378 if (uflag & USERF_SCRITICAL) {
379 /* sanity check */
380 if (val < 0 || val > 100) {
381 (void)printf("%s: invalid value (0><100)\n",
382 getprogname());
383 error = EINVAL;
384 goto out;
385 }
386
387 /* ok... convert the value */
388 val = (val / 100) * gesen[i].max_value;
389 obj = prop_number_create_unsigned_integer(val);
390 }
391
392 /*
393 * conversions required to send a proper value to the kernel.
394 */
395 if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
396 /* temperatures */
397 if (strcmp(gesen[i].type, "Temperature") == 0) {
398 /* convert from farenheit to celsius */
399 if (flags & ENVSYS_FFLAG)
400 val = (val - 32.0) * (5.0 / 9.0);
401
402 /* convert to microKelvin */
403 val = val * 1000000 + 273150000;
404 /* printf("val=%d\n", (int)val); */
405 obj = prop_number_create_unsigned_integer(val);
406 /* fans */
407 } else if (strcmp(gesen[i].type, "Fan") == 0) {
408 if (val < 0 || val > 10000) {
409 error = EINVAL;
410 goto out;
411 }
412 /* printf("val=%d\n", (int)val); */
413 obj = prop_number_create_unsigned_integer(val);
414
415 /* volts, watts, ohms, etc */
416 } else {
417 /* convert to m[V,W,Ohms] again */
418 val *= 1000000.0;
419 /* printf("val=%5.0f\n", val); */
420 obj = prop_number_create_integer(val);
421 }
422 }
423
424 #define SETPROP(str) \
425 do { \
426 if (!prop_dictionary_set(udict, (str), obj)) { \
427 error = EINVAL; \
428 goto out; \
429 } \
430 } while ( /*CONSTCOND*/ 0)
431
432 /* user wanted to set a new description */
433 if (uflag & USERF_SDESCR) {
434 SETPROP("new-description");
435
436 /* user wanted to set a new critical capacity */
437 } else if (uflag & USERF_SCRITICAL) {
438 SETPROP("critical-capacity");
439
440 } else if (uflag & USERF_RCRITICAL) {
441 obj = prop_bool_create(1);
442 SETPROP("remove-critical-cap");
443
444 /* user wanted to remove a critical min limit */
445 } else if (uflag & USERF_RCRITMIN) {
446 obj = prop_bool_create(1);
447 SETPROP("remove-cmin-limit");
448
449 /* user wanted to remove a critical max limit */
450 } else if (uflag & USERF_RCRITMAX) {
451 obj = prop_bool_create(1);
452 SETPROP("remove-cmax-limit");
453
454 /* user wanted to set a new critical min value */
455 } else if (uflag & USERF_SCRITMIN) {
456 SETPROP("critical-min-limit");
457
458 /* user wanted to set a new critical max value */
459 } else if (uflag & USERF_SCRITMAX) {
460 SETPROP("critical-max-limit");
461
462 /* user wanted to set a new rfact */
463 } else if (uflag & USERF_SRFACT) {
464 obj = prop_number_create_integer(val);
465 SETPROP("new-rfact");
466
467 } else {
468 (void)printf("%s: unknown operation\n", getprogname());
469 error = EINVAL;
470 goto out;
471 }
472
473 #undef SETPROP
474
475 prop_object_release(obj);
476
477 #ifdef DEBUG
478 printf("%s", prop_dictionary_externalize(udict));
479 return error;
480 #endif
481
482 if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
483 error = errno;
484 warnx("%s", strerror(errno));
485 goto out;
486 }
487
488 /* all done? send our dictionary now */
489 error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
490
491 if (error)
492 (void)printf("%s: %s\n", getprogname(), strerror(error));
493 out:
494 prop_object_release(udict);
495 return error;
496 }
497
498 static int
499 parse_dictionary(int fd)
500 {
501 prop_array_t array;
502 prop_dictionary_t dict;
503 prop_object_iterator_t iter;
504 prop_object_t obj;
505 const char *dnp = NULL;
506 int rval = 0;
507
508 /* receive dictionary from kernel */
509 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
510 if (rval)
511 return rval;
512
513 if (mydevname) {
514 obj = prop_dictionary_get(dict, mydevname);
515 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
516 warnx("unknown device `%s'", mydevname);
517 rval = EINVAL;
518 goto out;
519 }
520
521 rval = find_sensors(obj);
522 if (rval)
523 goto out;
524 } else {
525 iter = prop_dictionary_iterator(dict);
526 if (iter == NULL) {
527 rval = EINVAL;
528 goto out;
529 }
530
531 /* iterate over the dictionary returned by the kernel */
532 while ((obj = prop_object_iterator_next(iter)) != NULL) {
533
534 array = prop_dictionary_get_keysym(dict, obj);
535 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
536 warnx("no sensors found");
537 rval = EINVAL;
538 goto out;
539 }
540
541 dnp = prop_dictionary_keysym_cstring_nocopy(obj);
542
543 if (flags & ENVSYS_DFLAG) {
544 (void)printf("%s\n", dnp);
545 } else {
546 rval = find_sensors(array);
547 if (rval)
548 goto out;
549 }
550 }
551
552 prop_object_iterator_release(iter);
553 }
554
555 if (userreq == NULL)
556 if ((flags & ENVSYS_LFLAG) == 0)
557 print_sensors(gesen, gnelems);
558
559 if (interval)
560 (void)printf("\n");
561
562 out:
563 if (gesen) {
564 free(gesen);
565 gesen = NULL;
566 gnelems = 0;
567 newsize = 0;
568 }
569 prop_object_release(dict);
570 return rval;
571 }
572
573 static int
574 find_sensors(prop_array_t array)
575 {
576 prop_object_iterator_t iter;
577 prop_object_t obj, obj1;
578 prop_string_t state, desc = NULL;
579 struct envsys_sensor *esen = NULL;
580 int rval = 0;
581 size_t oldsize;
582 char *str = NULL;
583
584 oldsize = newsize;
585 newsize += prop_array_count(array) * sizeof(*gesen);
586 esen = realloc(gesen, newsize);
587 if (esen == NULL) {
588 if (gesen)
589 free(gesen);
590 gesen = NULL;
591 rval = ENOMEM;
592 goto out;
593 }
594 gesen = esen;
595
596 iter = prop_array_iterator(array);
597 if (iter == NULL) {
598 rval = EINVAL;
599 goto out;
600 }
601
602 /* iterate over the array of dictionaries */
603 while ((obj = prop_object_iterator_next(iter)) != NULL) {
604
605 gesen[gnelems].visible = false;
606
607 /* check sensor's state */
608 state = prop_dictionary_get(obj, "state");
609
610 /* mark invalid sensors */
611 if (prop_string_equals_cstring(state, "invalid"))
612 gesen[gnelems].invalid = true;
613 else
614 gesen[gnelems].invalid = false;
615
616 /* description string */
617 desc = prop_dictionary_get(obj, "description");
618 if (desc != NULL) {
619 /* copy description */
620 (void)strlcpy(gesen[gnelems].desc,
621 prop_string_cstring_nocopy(desc),
622 sizeof(gesen[gnelems].desc));
623 } else
624 continue;
625
626 /* type string */
627 obj1 = prop_dictionary_get(obj, "type");
628 /* copy type */
629 (void)strlcpy(gesen[gnelems].type,
630 prop_string_cstring_nocopy(obj1),
631 sizeof(gesen[gnelems].type));
632
633 /* get current drive state string */
634 obj1 = prop_dictionary_get(obj, "drive-state");
635 if (obj1 != NULL)
636 (void)strlcpy(gesen[gnelems].drvstate,
637 prop_string_cstring_nocopy(obj1),
638 sizeof(gesen[gnelems].drvstate));
639
640 /* get current value */
641 obj1 = prop_dictionary_get(obj, "cur-value");
642 gesen[gnelems].cur_value = prop_number_integer_value(obj1);
643
644 /* get max value */
645 obj1 = prop_dictionary_get(obj, "max-value");
646 if (obj1 != NULL)
647 gesen[gnelems].max_value =
648 prop_number_integer_value(obj1);
649 else
650 gesen[gnelems].max_value = 0;
651
652 /* get min value */
653 obj1 = prop_dictionary_get(obj, "min-value");
654 if (obj1 != NULL)
655 gesen[gnelems].min_value =
656 prop_number_integer_value(obj1);
657 else
658 gesen[gnelems].min_value = 0;
659
660 /* get avg value */
661 obj1 = prop_dictionary_get(obj, "avg-value");
662 if (obj1 != NULL)
663 gesen[gnelems].avg_value =
664 prop_number_integer_value(obj1);
665 else
666 gesen[gnelems].avg_value = 0;
667
668 /* get percentage flag */
669 obj1 = prop_dictionary_get(obj, "want-percentage");
670 if (obj1 != NULL)
671 gesen[gnelems].percentage = prop_bool_true(obj1);
672
673 /* get critical max value if available */
674 obj1 = prop_dictionary_get(obj, "critical-max-limit");
675 if (obj1 != NULL) {
676 gesen[gnelems].critmax_value =
677 prop_number_integer_value(obj1);
678 } else
679 gesen[gnelems].critmax_value = 0;
680
681 /* get critical min value if available */
682 obj1 = prop_dictionary_get(obj, "critical-min-limit");
683 if (obj1 != NULL) {
684 gesen[gnelems].critmin_value =
685 prop_number_integer_value(obj1);
686 } else
687 gesen[gnelems].critmin_value = 0;
688
689 /* get critical capacity value if available */
690 obj1 = prop_dictionary_get(obj, "critical-capacity");
691 if (obj1 != NULL) {
692 gesen[gnelems].critcap_value =
693 prop_number_integer_value(obj1);
694 } else
695 gesen[gnelems].critcap_value = 0;
696
697 /* pass to the next struct and increase the counter */
698 gnelems++;
699
700 /* print sensor names if -l was given */
701 if (flags & ENVSYS_LFLAG) {
702 if (width)
703 (void)printf("%*s\n", width,
704 prop_string_cstring_nocopy(desc));
705 else
706 (void)printf("%s\n",
707 prop_string_cstring_nocopy(desc));
708 }
709 }
710
711 /* free memory */
712 prop_object_iterator_release(iter);
713
714 /*
715 * if -s was specified, we need a way to mark if a sensor
716 * was found.
717 */
718 if (sensors) {
719 str = strdup(sensors);
720 if (str == NULL)
721 return ENOMEM;
722
723 rval = check_sensors(gesen, str, gnelems);
724 if (rval)
725 goto out;
726 }
727
728 out:
729 if (str)
730 free(str);
731 return rval;
732 }
733
734 static int
735 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
736 {
737 int i;
738 char *sname;
739
740 sname = strtok(str, ",");
741 while (sname != NULL) {
742 for (i = 0; i < nelems; i++) {
743 if (strcmp(sname, es[i].desc) == 0) {
744 es[i].visible = true;
745 break;
746 }
747 }
748 if (i >= nelems) {
749 if (mydevname) {
750 warnx("unknown sensor `%s' for device `%s'",
751 sname, mydevname);
752 return EINVAL;
753 } else {
754 warnx("unknown sensor `%s'", sname);
755 return EINVAL;
756 }
757 }
758 sname = strtok(NULL, ",");
759 }
760
761 /* check if all sensors were ok, and error out if not */
762 for (i = 0; i < nelems; i++) {
763 if (es[i].visible)
764 return 0;
765 }
766
767 warnx("no sensors selected to display");
768 return EINVAL;
769 }
770
771 static void
772 print_sensors(struct envsys_sensor *es, size_t nelems)
773 {
774 size_t maxlen = 0;
775 double temp = 0;
776 const char *invalid = "N/A";
777 const char *degrees = NULL;
778 int i;
779
780 /* find the longest description */
781 for (i = 0; i < nelems; i++) {
782 if (strlen(es[i].desc) > maxlen)
783 maxlen = strlen(es[i].desc);
784 }
785
786 if (width)
787 maxlen = width;
788
789 /* print the sensors */
790 for (i = 0; i < nelems; i++) {
791
792 /* skip sensors that were not marked as visible */
793 if (sensors && !es[i].visible)
794 continue;
795
796 (void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
797
798 if (es[i].invalid) {
799 (void)printf(": %10s\n", invalid);
800 continue;
801 }
802
803 if (strcmp(es[i].type, "Indicator") == 0) {
804
805 (void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
806
807 /* converts the value to degC or degF */
808 #define CONVERTTEMP(a, b, c) \
809 do { \
810 (a) = ((b) / 1000000.0) - 273.15; \
811 if (flags & ENVSYS_FFLAG) { \
812 (a) = (9.0 / 5.0) * (a) + 32.0; \
813 (c) = "degF"; \
814 } else \
815 (c) = "degC"; \
816 } while (/* CONSTCOND */ 0)
817
818
819 /* temperatures */
820 } else if (strcmp(es[i].type, "Temperature") == 0) {
821
822 CONVERTTEMP(temp, es[i].cur_value, degrees);
823 (void)printf(": %10.3f %s", temp, degrees);
824
825 if (es[i].critmax_value || es[i].critmin_value)
826 (void)printf(" ");
827
828 if (es[i].critmax_value) {
829 CONVERTTEMP(temp, es[i].critmax_value, degrees);
830 (void)printf("max: %8.3f %s ", temp, degrees);
831 }
832
833 if (es[i].critmin_value) {
834 CONVERTTEMP(temp, es[i].critmin_value, degrees);
835 (void)printf("min: %8.3f %s", temp, degrees);
836 }
837 #undef CONVERTTEMP
838
839 /* fans */
840 } else if (strcmp(es[i].type, "Fan") == 0) {
841
842 (void)printf(": %10u RPM", es[i].cur_value);
843
844 if (es[i].critmax_value || es[i].critmin_value)
845 (void)printf(" ");
846 if (es[i].critmax_value)
847 (void)printf("max: %8u RPM ",
848 es[i].critmax_value);
849 if (es[i].critmin_value)
850 (void)printf("min: %8u RPM",
851 es[i].critmin_value);
852
853 /* integers */
854 } else if (strcmp(es[i].type, "Integer") == 0) {
855
856 (void)printf(": %10d", es[i].cur_value);
857
858 /* drives */
859 } else if (strcmp(es[i].type, "Drive") == 0) {
860
861 (void)printf(": %10s", es[i].drvstate);
862
863 /* everything else */
864 } else {
865 const char *type;
866
867 if (strcmp(es[i].type, "Voltage DC") == 0)
868 type = "V";
869 else if (strcmp(es[i].type, "Voltage AC") == 0)
870 type = "VAC";
871 else if (strcmp(es[i].type, "Ampere") == 0)
872 type = "A";
873 else if (strcmp(es[i].type, "Watts") == 0)
874 type = "W";
875 else if (strcmp(es[i].type, "Ohms") == 0)
876 type = "Ohms";
877 else if (strcmp(es[i].type, "Watt hour") == 0)
878 type = "Wh";
879 else if (strcmp(es[i].type, "Ampere hour") == 0)
880 type = "Ah";
881 else
882 type = NULL;
883
884 (void)printf(": %10.3f %s",
885 es[i].cur_value / 1000000.0, type);
886
887 if (es[i].percentage && es[i].max_value) {
888 (void)printf(" (%5.2f%%)",
889 (es[i].cur_value * 100.0) /
890 es[i].max_value);
891 }
892
893 if (es[i].critcap_value) {
894 (void)printf(" critical (%5.2f%%)",
895 (es[i].critcap_value * 100.0) /
896 es[i].max_value);
897 }
898
899 if (es[i].critmax_value || es[i].critmin_value)
900 (void)printf(" ");
901 if (es[i].critmax_value)
902 (void)printf("max: %8.3f %s ",
903 es[i].critmax_value / 1000000.0,
904 type);
905 if (es[i].critmin_value)
906 (void)printf("min: %8.3f %s",
907 es[i].critmin_value / 1000000.0,
908 type);
909
910 }
911
912 (void)printf("\n");
913 }
914 }
915
916 static int
917 usage(void)
918 {
919 (void)fprintf(stderr, "Usage: %s [-Dflrx] ", getprogname());
920 (void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
921 (void)fprintf(stderr, "[-i num] [-d ...]\n");
922 exit(EXIT_FAILURE);
923 /* NOTREACHED */
924 }
925