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