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