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