envstat.c revision 1.35 1 /* $NetBSD: envstat.c,v 1.35 2007/07/17 13:12:46 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 if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict))
245 return EINVAL;
246
247 if (mydevname) {
248 obj = prop_dictionary_get(dict, mydevname);
249 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
250 warnx("unknown device `%s'", mydevname);
251 prop_object_release(dict);
252 return EINVAL;
253 }
254
255 if (find_sensors(obj)) {
256 prop_object_release(dict);
257 return EINVAL;
258 }
259 }
260
261 /* find the type for selected sensor */
262 for (i = 0; i < gnelems; i++)
263 if (strcmp(sensors, gesen[i].desc) == 0)
264 break;
265
266 /* we know the type of the sensor now, release kernel dict */
267 prop_object_release(dict);
268
269 /*
270 * part 2: userland dictionary.
271 *
272 * This parts consists in setting the values provided
273 * by the user and convert when necesssary to send
274 * them to the kernel again.
275 */
276 udict = prop_dictionary_create();
277
278 #define MKPROP(var, str) \
279 do { \
280 obj = prop_string_create_cstring_nocopy(var); \
281 if (obj == NULL || !prop_dictionary_set(udict, (str), obj)) { \
282 error = EINVAL; \
283 goto out; \
284 } \
285 } while (/* CONSTCOND */ 0)
286
287 /* create the driver-name object */
288 MKPROP(mydevname, "driver-name");
289 prop_object_release(obj);
290
291 /* create the sensor-name object */
292 MKPROP(sensors, "sensor-name");
293 prop_object_release(obj);
294
295 #undef MKPROP
296
297 /*
298 * parse the -m argument; we understand the following ways:
299 *
300 * -m critical/crit{max,min}=value
301 * -m critical/crit{max,min}=remove
302 * -m desc="BLAH"
303 * -m rfact=value
304 */
305 if (userreq) {
306 buf = strtok(userreq, "=");
307 target = strdup(buf);
308 if (target == NULL) {
309 error = ENOMEM;
310 goto out;
311 }
312
313 while (buf != NULL) {
314 /*
315 * skip current string if it's the same
316 * than target requested.
317 */
318 if (strcmp(target, buf) == 0)
319 buf = strtok(NULL, "=");
320
321 /* check what target was requested */
322 if (strcmp(target, "desc") == 0) {
323 uflag |= USERF_SDESCR;
324 obj = prop_string_create_cstring_nocopy(buf);
325 break;
326 #define SETNCHECKVAL(a, b) \
327 do { \
328 if (strcmp(buf, "remove") == 0) \
329 uflag |= (a); \
330 else { \
331 uflag |= (b); \
332 val = strtod(buf, &endptr); \
333 if (*endptr != '\0') { \
334 (void)printf("%s: invalid value\n", \
335 getprogname()); \
336 error = EINVAL; \
337 goto out; \
338 } \
339 } \
340 } while (/* CONSTCOND */ 0)
341
342 } else if (strcmp(target, "critical") == 0) {
343 SETNCHECKVAL(USERF_RCRITICAL, USERF_SCRITICAL);
344 break;
345 } else if (strcmp(target, "critmax") == 0) {
346 SETNCHECKVAL(USERF_RCRITMAX, USERF_SCRITMAX);
347 break;
348 } else if (strcmp(target, "critmin") == 0) {
349 SETNCHECKVAL(USERF_RCRITMIN, USERF_SCRITMIN);
350 break;
351 } else if (strcmp(target, "rfact") == 0) {
352 uflag |= USERF_SRFACT;
353 val = strtod(buf, &endptr);
354 if (*endptr != '\0') {
355 (void)printf("%s: invalid value\n",
356 getprogname());
357 error = EINVAL;
358 goto out;
359 }
360 break;
361 } else {
362 (void)printf("%s: invalid target\n",
363 getprogname());
364 error = EINVAL;
365 goto out;
366 }
367 }
368 free(target);
369 }
370
371 #undef SETNCHECKVAL
372
373 /* critical capacity for percentage sensors */
374 if (uflag & USERF_SCRITICAL) {
375 /* sanity check */
376 if (val < 0 || val > 100) {
377 (void)printf("%s: invalid value (0><100)\n",
378 getprogname());
379 error = EINVAL;
380 goto out;
381 }
382
383 /* ok... convert the value */
384 val = (val / 100) * gesen[i].max_value;
385 obj = prop_number_create_unsigned_integer(val);
386 }
387
388 /*
389 * conversions required to send a proper value to the kernel.
390 */
391 if ((uflag & USERF_SCRITMAX) || (uflag & USERF_SCRITMIN)) {
392 /* temperatures */
393 if (strcmp(gesen[i].type, "Temperature") == 0) {
394 /* convert from farenheit to celsius */
395 if (flags & ENVSYS_FFLAG)
396 val = (val - 32.0) * (5.0 / 9.0);
397
398 /* convert to microKelvin */
399 val = val * 1000000 + 273150000;
400 /* printf("val=%d\n", (int)val); */
401 obj = prop_number_create_unsigned_integer(val);
402 /* fans */
403 } else if (strcmp(gesen[i].type, "Fan") == 0) {
404 if (val < 0 || val > 10000) {
405 error = EINVAL;
406 goto out;
407 }
408 /* printf("val=%d\n", (int)val); */
409 obj = prop_number_create_unsigned_integer(val);
410
411 /* volts, watts, ohms, etc */
412 } else {
413 /* convert to m[V,W,Ohms] again */
414 val *= 1000000.0;
415 /* printf("val=%5.0f\n", val); */
416 obj = prop_number_create_integer(val);
417 }
418 }
419
420 #define SETPROP(str) \
421 do { \
422 if (!prop_dictionary_set(udict, (str), obj)) { \
423 error = EINVAL; \
424 goto out; \
425 } \
426 } while ( /*CONSTCOND*/ 0)
427
428 /* user wanted to set a new description */
429 if (uflag & USERF_SDESCR) {
430 SETPROP("new-description");
431
432 /* user wanted to set a new critical capacity */
433 } else if (uflag & USERF_SCRITICAL) {
434 SETPROP("critical-capacity");
435
436 } else if (uflag & USERF_RCRITICAL) {
437 obj = prop_bool_create(1);
438 SETPROP("remove-critical-cap");
439
440 /* user wanted to remove a critical min limit */
441 } else if (uflag & USERF_RCRITMIN) {
442 obj = prop_bool_create(1);
443 SETPROP("remove-cmin-limit");
444
445 /* user wanted to remove a critical max limit */
446 } else if (uflag & USERF_RCRITMAX) {
447 obj = prop_bool_create(1);
448 SETPROP("remove-cmax-limit");
449
450 /* user wanted to set a new critical min value */
451 } else if (uflag & USERF_SCRITMIN) {
452 SETPROP("critical-min-limit");
453
454 /* user wanted to set a new critical max value */
455 } else if (uflag & USERF_SCRITMAX) {
456 SETPROP("critical-max-limit");
457
458 /* user wanted to set a new rfact */
459 } else if (uflag & USERF_SRFACT) {
460 obj = prop_number_create_integer(val);
461 SETPROP("new-rfact");
462
463 } else {
464 (void)printf("%s: unknown operation\n", getprogname());
465 error = EINVAL;
466 goto out;
467 }
468
469 #undef SETPROP
470
471 prop_object_release(obj);
472
473 #ifdef DEBUG
474 printf("%s", prop_dictionary_externalize(udict));
475 return error;
476 #endif
477
478 /* all done? send our dictionary now */
479 error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
480
481 if (error)
482 (void)printf("%s: %s\n", getprogname(), strerror(error));
483 out:
484 prop_object_release(udict);
485 return error;
486 }
487
488 static int
489 parse_dictionary(int fd)
490 {
491 prop_array_t array;
492 prop_dictionary_t dict;
493 prop_object_iterator_t iter;
494 prop_object_t obj;
495 const char *dnp = NULL;
496 int rval = 0;
497
498 /* receive dictionary from kernel */
499 if (prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict))
500 return EINVAL;
501
502 if (mydevname) {
503 obj = prop_dictionary_get(dict, mydevname);
504 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
505 warnx("unknown device `%s'", mydevname);
506 rval = EINVAL;
507 goto out;
508 }
509
510 rval = find_sensors(obj);
511 if (rval)
512 goto out;
513 } else {
514 iter = prop_dictionary_iterator(dict);
515 if (iter == NULL) {
516 rval = EINVAL;
517 goto out;
518 }
519
520 /* iterate over the dictionary returned by the kernel */
521 while ((obj = prop_object_iterator_next(iter)) != NULL) {
522
523 array = prop_dictionary_get_keysym(dict, obj);
524 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
525 warnx("no sensors found");
526 rval = EINVAL;
527 goto out;
528 }
529
530 dnp = prop_dictionary_keysym_cstring_nocopy(obj);
531
532 if (flags & ENVSYS_DFLAG) {
533 (void)printf("%s\n", dnp);
534 } else {
535 rval = find_sensors(array);
536 if (rval)
537 goto out;
538 }
539 }
540
541 prop_object_iterator_release(iter);
542 }
543
544 if (userreq == NULL)
545 if ((flags & ENVSYS_LFLAG) == 0)
546 print_sensors(gesen, gnelems);
547
548 if (interval)
549 (void)printf("\n");
550
551 out:
552 if (gesen) {
553 free(gesen);
554 gesen = NULL;
555 gnelems = 0;
556 newsize = 0;
557 }
558 prop_object_release(dict);
559 return rval;
560 }
561
562 static int
563 find_sensors(prop_array_t array)
564 {
565 prop_object_iterator_t iter;
566 prop_object_t obj, obj1;
567 prop_string_t state, desc = NULL;
568 struct envsys_sensor *esen = NULL;
569 int rval = 0;
570 size_t oldsize;
571 char *str = NULL;
572
573 oldsize = newsize;
574 newsize += prop_array_count(array) * sizeof(*gesen);
575 esen = realloc(gesen, newsize);
576 if (esen == NULL) {
577 if (gesen)
578 free(gesen);
579 gesen = NULL;
580 rval = ENOMEM;
581 goto out;
582 }
583 gesen = esen;
584
585 iter = prop_array_iterator(array);
586 if (iter == NULL) {
587 rval = EINVAL;
588 goto out;
589 }
590
591 /* iterate over the array of dictionaries */
592 while ((obj = prop_object_iterator_next(iter)) != NULL) {
593
594 gesen[gnelems].visible = false;
595
596 /* check sensor's state */
597 state = prop_dictionary_get(obj, "state");
598
599 /* mark invalid sensors */
600 if (prop_string_equals_cstring(state, "invalid"))
601 gesen[gnelems].invalid = true;
602 else
603 gesen[gnelems].invalid = false;
604
605 /* description string */
606 desc = prop_dictionary_get(obj, "description");
607 /* copy description */
608 (void)strlcpy(gesen[gnelems].desc,
609 prop_string_cstring_nocopy(desc),
610 sizeof(gesen[gnelems].desc));
611
612 /* type string */
613 obj1 = prop_dictionary_get(obj, "type");
614 /* copy type */
615 (void)strlcpy(gesen[gnelems].type,
616 prop_string_cstring_nocopy(obj1),
617 sizeof(gesen[gnelems].type));
618
619 /* get current drive state string */
620 obj1 = prop_dictionary_get(obj, "drive-state");
621 if (obj1 != NULL)
622 (void)strlcpy(gesen[gnelems].drvstate,
623 prop_string_cstring_nocopy(obj1),
624 sizeof(gesen[gnelems].drvstate));
625
626 /* get current value */
627 obj1 = prop_dictionary_get(obj, "cur-value");
628 gesen[gnelems].cur_value = prop_number_integer_value(obj1);
629
630 /* get max value */
631 obj1 = prop_dictionary_get(obj, "max-value");
632 if (obj1 != NULL)
633 gesen[gnelems].max_value =
634 prop_number_integer_value(obj1);
635 else
636 gesen[gnelems].max_value = 0;
637
638 /* get min value */
639 obj1 = prop_dictionary_get(obj, "min-value");
640 if (obj1 != NULL)
641 gesen[gnelems].min_value =
642 prop_number_integer_value(obj1);
643 else
644 gesen[gnelems].min_value = 0;
645
646 /* get avg value */
647 obj1 = prop_dictionary_get(obj, "avg-value");
648 if (obj1 != NULL)
649 gesen[gnelems].avg_value =
650 prop_number_integer_value(obj1);
651 else
652 gesen[gnelems].avg_value = 0;
653
654 /* get percentage flag */
655 obj1 = prop_dictionary_get(obj, "want-percentage");
656 if (obj1 != NULL)
657 gesen[gnelems].percentage = prop_bool_true(obj1);
658
659 /* get critical max value if available */
660 obj1 = prop_dictionary_get(obj, "critical-max-limit");
661 if (obj1 != NULL) {
662 gesen[gnelems].critmax_value =
663 prop_number_integer_value(obj1);
664 } else
665 gesen[gnelems].critmax_value = 0;
666
667 /* get critical min value if available */
668 obj1 = prop_dictionary_get(obj, "critical-min-limit");
669 if (obj1 != NULL) {
670 gesen[gnelems].critmin_value =
671 prop_number_integer_value(obj1);
672 } else
673 gesen[gnelems].critmin_value = 0;
674
675 /* get critical capacity value if available */
676 obj1 = prop_dictionary_get(obj, "critical-capacity");
677 if (obj1 != NULL) {
678 gesen[gnelems].critcap_value =
679 prop_number_integer_value(obj1);
680 } else
681 gesen[gnelems].critcap_value = 0;
682
683 /* pass to the next struct and increase the counter */
684 gnelems++;
685
686 /* print sensor names if -l was given */
687 if (flags & ENVSYS_LFLAG) {
688 if (width)
689 (void)printf("%*s\n", width,
690 prop_string_cstring_nocopy(desc));
691 else
692 (void)printf("%s\n",
693 prop_string_cstring_nocopy(desc));
694 }
695 }
696
697 /* free memory */
698 prop_object_iterator_release(iter);
699
700 /*
701 * if -s was specified, we need a way to mark if a sensor
702 * was found.
703 */
704 if (sensors) {
705 str = strdup(sensors);
706 if (str == NULL)
707 return ENOMEM;
708
709 rval = check_sensors(gesen, str, gnelems);
710 if (rval)
711 goto out;
712 }
713
714 out:
715 if (str)
716 free(str);
717 return rval;
718 }
719
720 static int
721 check_sensors(struct envsys_sensor *es, char *str, size_t nelems)
722 {
723 int i;
724 char *sname;
725
726 sname = strtok(str, ",");
727 while (sname != NULL) {
728 for (i = 0; i < nelems; i++) {
729 if (strcmp(sname, es[i].desc) == 0) {
730 es[i].visible = true;
731 break;
732 }
733 }
734 if (i >= nelems) {
735 if (mydevname) {
736 warnx("unknown sensor `%s' for device `%s'",
737 sname, mydevname);
738 return EINVAL;
739 } else {
740 warnx("unknown sensor `%s'", sname);
741 return EINVAL;
742 }
743 }
744 sname = strtok(NULL, ",");
745 }
746
747 /* check if all sensors were ok, and error out if not */
748 for (i = 0; i < nelems; i++) {
749 if (es[i].visible)
750 return 0;
751 }
752
753 warnx("no sensors selected to display");
754 return EINVAL;
755 }
756
757 static void
758 print_sensors(struct envsys_sensor *es, size_t nelems)
759 {
760 size_t maxlen = 0;
761 double temp = 0;
762 const char *invalid = "N/A";
763 const char *degrees = NULL;
764 int i;
765
766 /* find the longest description */
767 for (i = 0; i < nelems; i++) {
768 if (strlen(es[i].desc) > maxlen)
769 maxlen = strlen(es[i].desc);
770 }
771
772 if (width)
773 maxlen = width;
774
775 /* print the sensors */
776 for (i = 0; i < nelems; i++) {
777
778 /* skip sensors that were not marked as visible */
779 if (sensors && !es[i].visible)
780 continue;
781
782 (void)printf("%*.*s", (int)maxlen, (int)maxlen, es[i].desc);
783
784 if (es[i].invalid) {
785 (void)printf(": %10s\n", invalid);
786 continue;
787 }
788
789 if (strcmp(es[i].type, "Indicator") == 0) {
790
791 (void)printf(": %10s", es[i].cur_value ? "ON" : "OFF");
792
793 /* converts the value to degC or degF */
794 #define CONVERTTEMP(a, b, c) \
795 do { \
796 (a) = ((b) / 1000000.0) - 273.15; \
797 if (flags & ENVSYS_FFLAG) { \
798 (a) = (9.0 / 5.0) * (a) + 32.0; \
799 (c) = "degF"; \
800 } else \
801 (c) = "degC"; \
802 } while (/* CONSTCOND */ 0)
803
804
805 /* temperatures */
806 } else if (strcmp(es[i].type, "Temperature") == 0) {
807
808 CONVERTTEMP(temp, es[i].cur_value, degrees);
809 (void)printf(": %10.3f %s", temp, degrees);
810
811 if (es[i].critmax_value || es[i].critmin_value)
812 (void)printf(" ");
813
814 if (es[i].critmax_value) {
815 CONVERTTEMP(temp, es[i].critmax_value, degrees);
816 (void)printf("max: %8.3f %s ", temp, degrees);
817 }
818
819 if (es[i].critmin_value) {
820 CONVERTTEMP(temp, es[i].critmin_value, degrees);
821 (void)printf("min: %8.3f %s", temp, degrees);
822 }
823 #undef CONVERTTEMP
824
825 /* fans */
826 } else if (strcmp(es[i].type, "Fan") == 0) {
827
828 (void)printf(": %10u RPM", es[i].cur_value);
829
830 if (es[i].critmax_value || es[i].critmin_value)
831 (void)printf(" ");
832 if (es[i].critmax_value)
833 (void)printf("max: %8u RPM ",
834 es[i].critmax_value);
835 if (es[i].critmin_value)
836 (void)printf("min: %8u RPM",
837 es[i].critmin_value);
838
839 /* integers */
840 } else if (strcmp(es[i].type, "Integer") == 0) {
841
842 (void)printf(": %10d", es[i].cur_value);
843
844 /* drives */
845 } else if (strcmp(es[i].type, "Drive") == 0) {
846
847 (void)printf(": %10s", es[i].drvstate);
848
849 /* everything else */
850 } else {
851 const char *type;
852
853 if (strcmp(es[i].type, "Voltage DC") == 0)
854 type = "V";
855 else if (strcmp(es[i].type, "Voltage AC") == 0)
856 type = "VAC";
857 else if (strcmp(es[i].type, "Ampere") == 0)
858 type = "A";
859 else if (strcmp(es[i].type, "Watts") == 0)
860 type = "W";
861 else if (strcmp(es[i].type, "Ohms") == 0)
862 type = "Ohms";
863 else if (strcmp(es[i].type, "Watt hour") == 0)
864 type = "Wh";
865 else if (strcmp(es[i].type, "Ampere hour") == 0)
866 type = "Ah";
867 else
868 type = NULL;
869
870 (void)printf(": %10.3f %s",
871 es[i].cur_value / 1000000.0, type);
872
873 if (es[i].percentage && es[i].max_value) {
874 (void)printf(" (%5.2f%%)",
875 (es[i].cur_value * 100.0) /
876 es[i].max_value);
877 }
878
879 if (es[i].critcap_value) {
880 (void)printf(" critical (%5.2f%%)",
881 (es[i].critcap_value * 100.0) /
882 es[i].max_value);
883 }
884
885 if (es[i].critmax_value || es[i].critmin_value)
886 (void)printf(" ");
887 if (es[i].critmax_value)
888 (void)printf("max: %8.3f %s ",
889 es[i].critmax_value / 1000000.0,
890 type);
891 if (es[i].critmin_value)
892 (void)printf("min: %8.3f %s",
893 es[i].critmin_value / 1000000.0,
894 type);
895
896 }
897
898 (void)printf("\n");
899 }
900 }
901
902 static int
903 usage(void)
904 {
905 (void)fprintf(stderr, "Usage: %s [-Dflrx] ", getprogname());
906 (void)fprintf(stderr, "[-m ...] [-s s1,s2 ] [-w num] ");
907 (void)fprintf(stderr, "[-i num] [-d ...]\n");
908 exit(EXIT_FAILURE);
909 /* NOTREACHED */
910 }
911