envstat.c revision 1.69 1 /* $NetBSD: envstat.c,v 1.69 2008/08/09 04:49:23 christos Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008 Juan Romero Pardines.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 #ifndef lint
30 __RCSID("$NetBSD: envstat.c,v 1.69 2008/08/09 04:49:23 christos Exp $");
31 #endif /* not lint */
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <stdbool.h>
36 #include <string.h>
37 #include <unistd.h>
38 #include <fcntl.h>
39 #include <err.h>
40 #include <errno.h>
41 #include <syslog.h>
42 #include <sys/envsys.h>
43 #include <sys/types.h>
44 #include <sys/queue.h>
45 #include <prop/proplib.h>
46
47 #include "envstat.h"
48
49 #define _PATH_DEV_SYSMON "/dev/sysmon"
50
51 #define ENVSYS_DFLAG 0x00000001 /* list registered devices */
52 #define ENVSYS_FFLAG 0x00000002 /* show temp in farenheit */
53 #define ENVSYS_LFLAG 0x00000004 /* list sensors */
54 #define ENVSYS_XFLAG 0x00000008 /* externalize dictionary */
55 #define ENVSYS_IFLAG 0x00000010 /* skip invalid sensors */
56 #define ENVSYS_SFLAG 0x00000020 /* remove all properties set */
57 #define ENVSYS_TFLAG 0x00000040 /* make statistics */
58
59 /* Sensors */
60 typedef struct envsys_sensor {
61 SIMPLEQ_ENTRY(envsys_sensor) entries;
62 int32_t cur_value;
63 int32_t max_value;
64 int32_t min_value;
65 int32_t avg_value;
66 int32_t critcap_value;
67 int32_t critmin_value;
68 int32_t critmax_value;
69 char desc[ENVSYS_DESCLEN];
70 char type[ENVSYS_DESCLEN];
71 char drvstate[ENVSYS_DESCLEN];
72 char battcap[ENVSYS_DESCLEN];
73 char dvname[ENVSYS_DESCLEN];
74 bool invalid;
75 bool visible;
76 bool percentage;
77 } *sensor_t;
78
79 /* Sensor statistics */
80 typedef struct envsys_sensor_stats {
81 SIMPLEQ_ENTRY(envsys_sensor_stats) entries;
82 int32_t max;
83 int32_t min;
84 int32_t avg;
85 char desc[ENVSYS_DESCLEN];
86 } *sensor_stats_t;
87
88 /* Device properties */
89 typedef struct envsys_dvprops {
90 uint64_t refresh_timo;
91 /* more members could be added in the future */
92 } *dvprops_t;
93
94 /* A simple queue to manage all sensors */
95 static SIMPLEQ_HEAD(, envsys_sensor) sensors_list =
96 SIMPLEQ_HEAD_INITIALIZER(sensors_list);
97
98 /* A simple queue to manage statistics for all sensors */
99 static SIMPLEQ_HEAD(, envsys_sensor_stats) sensor_stats_list =
100 SIMPLEQ_HEAD_INITIALIZER(sensor_stats_list);
101
102 static unsigned int interval, flags, width;
103 static char *mydevname, *sensors;
104 static bool statistics;
105 static u_int header_passes;
106
107 static int parse_dictionary(int);
108 static int send_dictionary(FILE *, int);
109 static int find_sensors(prop_array_t, const char *, dvprops_t);
110 static void print_sensors(void);
111 static int check_sensors(char *);
112 static int usage(void);
113
114
115 int main(int argc, char **argv)
116 {
117 prop_dictionary_t dict;
118 int c, fd, rval = 0;
119 char *endptr, *configfile = NULL;
120 FILE *cf;
121
122 setprogname(argv[0]);
123
124 while ((c = getopt(argc, argv, "c:Dd:fIi:lrSs:Tw:x")) != -1) {
125 switch (c) {
126 case 'c': /* configuration file */
127 configfile = strdup(optarg);
128 if (configfile == NULL)
129 err(EXIT_FAILURE, "strdup");
130 break;
131 case 'D': /* list registered devices */
132 flags |= ENVSYS_DFLAG;
133 break;
134 case 'd': /* show sensors of a specific device */
135 mydevname = strdup(optarg);
136 if (mydevname == NULL)
137 err(EXIT_FAILURE, "strdup");
138 break;
139 case 'f': /* display temperature in Farenheit */
140 flags |= ENVSYS_FFLAG;
141 break;
142 case 'I': /* Skips invalid sensors */
143 flags |= ENVSYS_IFLAG;
144 break;
145 case 'i': /* wait time between intervals */
146 interval = (unsigned int)strtoul(optarg, &endptr, 10);
147 if (*endptr != '\0')
148 errx(EXIT_FAILURE, "bad interval '%s'", optarg);
149 break;
150 case 'l': /* list sensors */
151 flags |= ENVSYS_LFLAG;
152 break;
153 case 'r':
154 /*
155 * This flag is noop.. it's only here for
156 * compatibility with the old implementation.
157 */
158 break;
159 case 'S':
160 flags |= ENVSYS_SFLAG;
161 break;
162 case 's': /* only show specified sensors */
163 sensors = strdup(optarg);
164 if (sensors == NULL)
165 err(EXIT_FAILURE, "strdup");
166 break;
167 case 'T': /* make statistics */
168 flags |= ENVSYS_TFLAG;
169 break;
170 case 'w': /* width value for the lines */
171 width = (unsigned int)strtoul(optarg, &endptr, 10);
172 if (*endptr != '\0')
173 errx(EXIT_FAILURE, "bad width '%s'", optarg);
174 break;
175 case 'x': /* print the dictionary in raw format */
176 flags |= ENVSYS_XFLAG;
177 break;
178 case '?':
179 default:
180 usage();
181 /* NOTREACHED */
182 }
183 }
184
185 argc -= optind;
186 argv += optind;
187
188 if (argc > 0)
189 usage();
190
191 /* Check if we want to make statistics */
192 if (flags & ENVSYS_TFLAG) {
193 if (!interval)
194 errx(EXIT_FAILURE,
195 "-T cannot be used without an interval (-i)");
196 else
197 statistics = true;
198 }
199
200 if (mydevname && sensors)
201 errx(EXIT_FAILURE, "-d flag cannot be used with -s");
202
203 /* Open the device in ro mode */
204 if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
205 err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
206
207 /* Print dictionary in raw mode */
208 if (flags & ENVSYS_XFLAG) {
209 rval = prop_dictionary_recv_ioctl(fd,
210 ENVSYS_GETDICTIONARY,
211 &dict);
212 if (rval)
213 errx(EXIT_FAILURE, "%s", strerror(rval));
214
215 config_dict_dump(dict);
216
217 /* Remove all properties set in dictionary */
218 } else if (flags & ENVSYS_SFLAG) {
219 /* Close the ro descriptor */
220 (void)close(fd);
221
222 /* open the fd in rw mode */
223 if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1)
224 err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
225
226 dict = prop_dictionary_create();
227 if (!dict)
228 err(EXIT_FAILURE, "prop_dictionary_create");
229
230 rval = prop_dictionary_set_bool(dict,
231 "envsys-remove-props",
232 true);
233 if (!rval)
234 err(EXIT_FAILURE, "prop_dict_set_bool");
235
236 /* send the dictionary to the kernel now */
237 rval = prop_dictionary_send_ioctl(dict, fd, ENVSYS_REMOVEPROPS);
238 if (rval)
239 warnx("%s", strerror(rval));
240
241 /* Set properties in dictionary */
242 } else if (configfile) {
243 /*
244 * Parse the configuration file.
245 */
246 if ((cf = fopen(configfile, "r")) == NULL) {
247 syslog(LOG_ERR, "fopen failed: %s", strerror(errno));
248 errx(EXIT_FAILURE, "%s", strerror(errno));
249 }
250
251 rval = send_dictionary(cf, fd);
252 (void)fclose(cf);
253
254 /* Show sensors with interval */
255 } else if (interval) {
256 for (;;) {
257 rval = parse_dictionary(fd);
258 if (rval)
259 break;
260
261 (void)fflush(stdout);
262 (void)sleep(interval);
263 }
264 /* Show sensors without interval */
265 } else {
266 rval = parse_dictionary(fd);
267 }
268
269 if (sensors)
270 free(sensors);
271 if (mydevname)
272 free(mydevname);
273 (void)close(fd);
274
275 return rval ? EXIT_FAILURE : EXIT_SUCCESS;
276 }
277
278 static int
279 send_dictionary(FILE *cf, int fd)
280 {
281 prop_dictionary_t kdict, udict;
282 int error = 0;
283
284 /* Retrieve dictionary from kernel */
285 error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &kdict);
286 if (error)
287 return error;
288
289 config_parse(cf, kdict);
290
291 /*
292 * Dictionary built by the parser from the configuration file.
293 */
294 udict = config_dict_parsed();
295
296 /*
297 * Close the read only descriptor and open a new one read write.
298 */
299 (void)close(fd);
300 if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
301 error = errno;
302 warn("%s", _PATH_DEV_SYSMON);
303 return error;
304 }
305
306 /*
307 * Send our sensor properties dictionary to the kernel then.
308 */
309 error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
310 if (error)
311 warnx("%s", strerror(error));
312
313 prop_object_release(udict);
314 return error;
315 }
316
317 static sensor_stats_t
318 find_stats_sensor(const char *desc)
319 {
320 sensor_stats_t stats;
321
322 /*
323 * If we matched a sensor by its description return it, otherwise
324 * allocate a new one.
325 */
326 SIMPLEQ_FOREACH(stats, &sensor_stats_list, entries)
327 if (strcmp(stats->desc, desc) == 0)
328 return stats;
329
330 stats = calloc(1, sizeof(*stats));
331 if (stats == NULL)
332 return NULL;
333
334 (void)strlcpy(stats->desc, desc, sizeof(stats->desc));
335 SIMPLEQ_INSERT_TAIL(&sensor_stats_list, stats, entries);
336
337 return stats;
338 }
339
340 static int
341 parse_dictionary(int fd)
342 {
343 sensor_t sensor = NULL;
344 dvprops_t edp = NULL;
345 prop_array_t array;
346 prop_dictionary_t dict;
347 prop_object_iterator_t iter;
348 prop_object_t obj;
349 const char *dnp = NULL;
350 int rval = 0;
351
352 /* receive dictionary from kernel */
353 rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
354 if (rval)
355 return rval;
356
357 /* No drivers registered? */
358 if (prop_dictionary_count(dict) == 0) {
359 warnx("no drivers registered");
360 goto out;
361 }
362
363 if (mydevname) {
364 /* -d flag specified, print sensors only for this device */
365 obj = prop_dictionary_get(dict, mydevname);
366 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
367 warnx("unknown device `%s'", mydevname);
368 rval = EINVAL;
369 goto out;
370 }
371
372 rval = find_sensors(obj, mydevname, NULL);
373 if (rval)
374 goto out;
375
376 } else {
377 /* print sensors for all devices registered */
378 iter = prop_dictionary_iterator(dict);
379 if (iter == NULL) {
380 rval = EINVAL;
381 goto out;
382 }
383
384 /* iterate over the dictionary returned by the kernel */
385 while ((obj = prop_object_iterator_next(iter)) != NULL) {
386 array = prop_dictionary_get_keysym(dict, obj);
387 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
388 warnx("no sensors found");
389 rval = EINVAL;
390 goto out;
391 }
392
393 edp = calloc(1, sizeof(*edp));
394 if (!edp) {
395 rval = ENOMEM;
396 goto out;
397 }
398
399 dnp = prop_dictionary_keysym_cstring_nocopy(obj);
400 rval = find_sensors(array, dnp, edp);
401 if (rval)
402 goto out;
403
404 if (((flags & ENVSYS_LFLAG) == 0) &&
405 (flags & ENVSYS_DFLAG)) {
406 (void)printf("%s (checking events every ",
407 dnp);
408 if (edp->refresh_timo == 1)
409 (void)printf("second)\n");
410 else
411 (void)printf("%d seconds)\n",
412 (int)edp->refresh_timo);
413 }
414
415 free(edp);
416 edp = NULL;
417 }
418 prop_object_iterator_release(iter);
419 }
420
421 /* print sensors now */
422 if (sensors) {
423 char *str = strdup(sensors);
424 if (!str) {
425 rval = ENOMEM;
426 goto out;
427 }
428 rval = check_sensors(str);
429 if (rval) {
430 free(str);
431 goto out;
432 }
433 free(str);
434 }
435 if ((flags & ENVSYS_LFLAG) == 0 && (flags & ENVSYS_DFLAG) == 0)
436 print_sensors();
437 if (interval)
438 (void)printf("\n");
439
440 out:
441 while ((sensor = SIMPLEQ_FIRST(&sensors_list))) {
442 SIMPLEQ_REMOVE_HEAD(&sensors_list, entries);
443 free(sensor);
444 }
445 if (edp)
446 free(edp);
447 prop_object_release(dict);
448 return rval;
449 }
450
451 static int
452 find_sensors(prop_array_t array, const char *dvname, dvprops_t edp)
453 {
454 prop_object_iterator_t iter;
455 prop_object_t obj, obj1, obj2;
456 prop_string_t state, desc = NULL;
457 sensor_t sensor = NULL;
458 sensor_stats_t stats = NULL;
459
460 iter = prop_array_iterator(array);
461 if (!iter)
462 return ENOMEM;
463
464 /* iterate over the array of dictionaries */
465 while ((obj = prop_object_iterator_next(iter)) != NULL) {
466 /* get the refresh-timeout property */
467 obj2 = prop_dictionary_get(obj, "device-properties");
468 if (obj2) {
469 if (!edp)
470 continue;
471 if (!prop_dictionary_get_uint64(obj2,
472 "refresh-timeout",
473 &edp->refresh_timo))
474 continue;
475 }
476
477 /* new sensor coming */
478 sensor = calloc(1, sizeof(*sensor));
479 if (sensor == NULL) {
480 prop_object_iterator_release(iter);
481 return ENOMEM;
482 }
483
484 /* copy device name */
485 (void)strlcpy(sensor->dvname, dvname, sizeof(sensor->dvname));
486
487 /* description string */
488 desc = prop_dictionary_get(obj, "description");
489 if (desc) {
490 /* copy description */
491 (void)strlcpy(sensor->desc,
492 prop_string_cstring_nocopy(desc),
493 sizeof(sensor->desc));
494 } else {
495 free(sensor);
496 continue;
497 }
498
499 /* type string */
500 obj1 = prop_dictionary_get(obj, "type");
501 if (obj1) {
502 /* copy type */
503 (void)strlcpy(sensor->type,
504 prop_string_cstring_nocopy(obj1),
505 sizeof(sensor->type));
506 } else {
507 free(sensor);
508 continue;
509 }
510
511 /* check sensor's state */
512 state = prop_dictionary_get(obj, "state");
513
514 /* mark sensors with invalid/unknown state */
515 if ((prop_string_equals_cstring(state, "invalid") ||
516 prop_string_equals_cstring(state, "unknown")))
517 sensor->invalid = true;
518
519 /* get current drive state string */
520 obj1 = prop_dictionary_get(obj, "drive-state");
521 if (obj1) {
522 (void)strlcpy(sensor->drvstate,
523 prop_string_cstring_nocopy(obj1),
524 sizeof(sensor->drvstate));
525 }
526
527 /* get current battery capacity string */
528 obj1 = prop_dictionary_get(obj, "battery-capacity");
529 if (obj1) {
530 (void)strlcpy(sensor->battcap,
531 prop_string_cstring_nocopy(obj1),
532 sizeof(sensor->battcap));
533 }
534
535 /* get current value */
536 obj1 = prop_dictionary_get(obj, "cur-value");
537 if (obj1)
538 sensor->cur_value = prop_number_integer_value(obj1);
539
540 /* get max value */
541 obj1 = prop_dictionary_get(obj, "max-value");
542 if (obj1)
543 sensor->max_value = prop_number_integer_value(obj1);
544
545 /* get min value */
546 obj1 = prop_dictionary_get(obj, "min-value");
547 if (obj1)
548 sensor->min_value = prop_number_integer_value(obj1);
549
550 /* get avg value */
551 obj1 = prop_dictionary_get(obj, "avg-value");
552 if (obj1)
553 sensor->avg_value = prop_number_integer_value(obj1);
554
555 /* get percentage flag */
556 obj1 = prop_dictionary_get(obj, "want-percentage");
557 if (obj1)
558 sensor->percentage = prop_bool_true(obj1);
559
560 /* get critical max value if available */
561 obj1 = prop_dictionary_get(obj, "critical-max");
562 if (obj1)
563 sensor->critmax_value = prop_number_integer_value(obj1);
564
565 /* get critical min value if available */
566 obj1 = prop_dictionary_get(obj, "critical-min");
567 if (obj1)
568 sensor->critmin_value = prop_number_integer_value(obj1);
569
570 /* get critical capacity value if available */
571 obj1 = prop_dictionary_get(obj, "critical-capacity");
572 if (obj1)
573 sensor->critcap_value = prop_number_integer_value(obj1);
574
575 /* print sensor names if -l was given */
576 if (flags & ENVSYS_LFLAG) {
577 if (width)
578 (void)printf("%*s\n", width,
579 prop_string_cstring_nocopy(desc));
580 else
581 (void)printf("%s\n",
582 prop_string_cstring_nocopy(desc));
583 }
584
585 /* Add the sensor into the list */
586 SIMPLEQ_INSERT_TAIL(&sensors_list, sensor, entries);
587
588 /* Collect statistics if flag enabled */
589 if (statistics) {
590 /* ignore sensors not relevant for statistics */
591 if ((strcmp(sensor->type, "Indicator") == 0) ||
592 (strcmp(sensor->type, "Battery charge") == 0) ||
593 (strcmp(sensor->type, "Drive") == 0))
594 continue;
595
596 /* ignore invalid data */
597 if (sensor->invalid || !sensor->cur_value)
598 continue;
599
600 /* find or allocate a new statistics sensor */
601 stats = find_stats_sensor(sensor->desc);
602 if (stats == NULL) {
603 free(sensor);
604 prop_object_iterator_release(iter);
605 return ENOMEM;
606 }
607
608 /* collect data */
609 if (!stats->max)
610 stats->max = sensor->cur_value;
611 if (!stats->min)
612 stats->min = sensor->cur_value;
613
614 if (sensor->cur_value > stats->max)
615 stats->max = sensor->cur_value;
616
617 if (sensor->cur_value < stats->min)
618 stats->min = sensor->cur_value;
619
620 /* compute avg value */
621 if (stats->max && stats->min)
622 stats->avg =
623 (sensor->cur_value + stats->max +
624 stats->min) / 3;
625 }
626 }
627
628 /* free memory */
629 prop_object_iterator_release(iter);
630 return 0;
631 }
632
633 static int
634 check_sensors(char *str)
635 {
636 sensor_t sensor = NULL;
637 char *dvstring, *sstring, *p, *last;
638 bool sensor_found = false;
639
640 /*
641 * Parse device name and sensor description and find out
642 * if the sensor is valid.
643 */
644 for ((p = strtok_r(str, ",", &last)); p;
645 (p = strtok_r(NULL, ",", &last))) {
646 /* get device name */
647 dvstring = strtok(p, ":");
648 if (dvstring == NULL) {
649 warnx("missing device name");
650 return EINVAL;
651 }
652
653 /* get sensor description */
654 sstring = strtok(NULL, ":");
655 if (sstring == NULL) {
656 warnx("missing sensor description");
657 return EINVAL;
658 }
659
660 SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
661 /* skip until we match device */
662 if (strcmp(dvstring, sensor->dvname))
663 continue;
664 if (strcmp(sstring, sensor->desc) == 0) {
665 sensor->visible = true;
666 sensor_found = true;
667 break;
668 }
669 }
670 if (sensor_found == false) {
671 warnx("unknown sensor `%s' for device `%s'",
672 sstring, dvstring);
673 return EINVAL;
674 }
675 sensor_found = false;
676 }
677
678 /* check if all sensors were ok, and error out if not */
679 SIMPLEQ_FOREACH(sensor, &sensors_list, entries)
680 if (sensor->visible)
681 return 0;
682
683 warnx("no sensors selected to display");
684 return EINVAL;
685 }
686
687 static void
688 print_sensors(void)
689 {
690 sensor_t sensor;
691 sensor_stats_t stats = NULL;
692 size_t maxlen = 0, ilen = 32 + 3;
693 double temp = 0;
694 const char *invalid = "N/A", *degrees, *tmpstr, *stype;
695 const char *a, *b, *c, *d, *units;
696
697 tmpstr = stype = d = NULL;
698
699 /* find the longest description */
700 SIMPLEQ_FOREACH(sensor, &sensors_list, entries)
701 if (strlen(sensor->desc) > maxlen)
702 maxlen = strlen(sensor->desc);
703
704 if (width)
705 maxlen = width;
706
707 /*
708 * Print a header at the bottom only once showing different
709 * members if the statistics flag is set or not.
710 *
711 * As bonus if -s is set, only print this header every 10 iterations
712 * to avoid redundancy... like vmstat(1).
713 */
714
715 a = "Current";
716 units = "Unit";
717 if (statistics) {
718 b = "Max";
719 c = "Min";
720 d = "Avg";
721 } else {
722 b = "CritMax";
723 c = "CritMin";
724 d = "CritCap";
725 }
726
727 if (!sensors || (!header_passes && sensors) ||
728 (header_passes == 10 && sensors)) {
729 (void)printf("%s%*s %10s %8s %8s %8s %8s\n",
730 mydevname ? "" : " ", (int)maxlen,
731 "", a, b, c, d, units);
732 if (sensors && header_passes == 10)
733 header_passes = 0;
734 }
735 if (sensors)
736 header_passes++;
737
738 /* print the sensors */
739 SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
740 /* skip sensors that were not marked as visible */
741 if (sensors && !sensor->visible)
742 continue;
743
744 /* skip invalid sensors if -I is set */
745 if ((flags & ENVSYS_IFLAG) && sensor->invalid)
746 continue;
747
748 /* print device name */
749 if (!mydevname) {
750 if (tmpstr == NULL || strcmp(tmpstr, sensor->dvname))
751 printf("[%s]\n", sensor->dvname);
752
753 tmpstr = sensor->dvname;
754 }
755
756 /* find out the statistics sensor */
757 if (statistics) {
758 stats = find_stats_sensor(sensor->desc);
759 if (stats == NULL) {
760 /* No statistics for this sensor */
761 continue;
762 }
763 }
764
765 /* print sensor description */
766 (void)printf("%s%*.*s", mydevname ? "" : " ", (int)maxlen,
767 (int)maxlen, sensor->desc);
768
769 /* print invalid string */
770 if (sensor->invalid) {
771 (void)printf(": %10s\n", invalid);
772 continue;
773 }
774
775 /*
776 * Indicator and Battery charge sensors.
777 */
778 if ((strcmp(sensor->type, "Indicator") == 0) ||
779 (strcmp(sensor->type, "Battery charge") == 0)) {
780
781 (void)printf(": %10s", sensor->cur_value ? "ON" : "OFF");
782
783 /* converts the value to degC or degF */
784 #define CONVERTTEMP(a, b, c) \
785 do { \
786 if (b) \
787 (a) = ((b) / 1000000.0) - 273.15; \
788 if (flags & ENVSYS_FFLAG) { \
789 if (b) \
790 (a) = (9.0 / 5.0) * (a) + 32.0; \
791 (c) = "degF"; \
792 } else \
793 (c) = "degC"; \
794 } while (/* CONSTCOND */ 0)
795
796
797 /* temperatures */
798 } else if (strcmp(sensor->type, "Temperature") == 0) {
799
800 CONVERTTEMP(temp, sensor->cur_value, degrees);
801 stype = degrees;
802 (void)printf(": %10.3f ", temp);
803
804 if (statistics) {
805 /* show statistics if flag set */
806 CONVERTTEMP(temp, stats->max, degrees);
807 (void)printf("%8.3f ", temp);
808 CONVERTTEMP(temp, stats->min, degrees);
809 (void)printf("%8.3f ", temp);
810 CONVERTTEMP(temp, stats->avg, degrees);
811 (void)printf("%8.3f ", temp);
812 ilen = 8;
813 } else {
814 if (sensor->critmax_value) {
815 CONVERTTEMP(temp,
816 sensor->critmax_value, degrees);
817 (void)printf( "%8.3f ", temp);
818 ilen = 24 + 2;
819 }
820
821 if (sensor->critmin_value) {
822 CONVERTTEMP(temp,
823 sensor->critmin_value, degrees);
824 if (sensor->critmax_value)
825 ilen = 8;
826 else
827 ilen = 16 + 1;
828
829 (void)printf("%*.3f ", (int)ilen, temp);
830 ilen = 16 + 1;
831 }
832 }
833
834 (void)printf("%*s", (int)ilen, stype);
835 #undef CONVERTTEMP
836
837 /* fans */
838 } else if (strcmp(sensor->type, "Fan") == 0) {
839 stype = "RPM";
840
841 (void)printf(": %10u ", sensor->cur_value);
842 if (statistics) {
843 /* show statistics if flag set */
844 (void)printf("%8u %8u %8u ",
845 stats->max, stats->min, stats->avg);
846 ilen = 8;
847 } else {
848 if (sensor->critmax_value) {
849 (void)printf("%8u ",
850 sensor->critmax_value);
851 ilen = 24 + 2;
852 }
853
854 if (sensor->critmin_value) {
855 if (sensor->critmax_value)
856 ilen = 8;
857 else
858 ilen = 16 + 1;
859 (void)printf("%*u ", (int)ilen,
860 sensor->critmin_value);
861 ilen = 16 + 1;
862 }
863 }
864
865 (void)printf("%*s", (int)ilen, stype);
866
867 /* integers */
868 } else if (strcmp(sensor->type, "Integer") == 0) {
869
870 (void)printf(": %10d", sensor->cur_value);
871
872 /* drives */
873 } else if (strcmp(sensor->type, "Drive") == 0) {
874
875 (void)printf(": %10s", sensor->drvstate);
876
877 /* Battery capacity */
878 } else if (strcmp(sensor->type, "Battery capacity") == 0) {
879
880 (void)printf(": %10s", sensor->battcap);
881
882 /* everything else */
883 } else {
884 if (strcmp(sensor->type, "Voltage DC") == 0)
885 stype = "V";
886 else if (strcmp(sensor->type, "Voltage AC") == 0)
887 stype = "VAC";
888 else if (strcmp(sensor->type, "Ampere") == 0)
889 stype = "A";
890 else if (strcmp(sensor->type, "Watts") == 0)
891 stype = "W";
892 else if (strcmp(sensor->type, "Ohms") == 0)
893 stype = "Ohms";
894 else if (strcmp(sensor->type, "Watt hour") == 0)
895 stype = "Wh";
896 else if (strcmp(sensor->type, "Ampere hour") == 0)
897 stype = "Ah";
898
899 (void)printf(": %10.3f ",
900 sensor->cur_value / 1000000.0);
901
902 if (!statistics) {
903 if (sensor->critmax_value) {
904 (void)printf("%8.3f ",
905 sensor->critmax_value / 1000000.0);
906 ilen = 24 + 2;
907 }
908
909 if (sensor->critmin_value) {
910 if (sensor->critmax_value)
911 ilen = 8;
912 else
913 ilen = 16 + 1;
914 (void)printf("%*.3f ", (int)ilen,
915 sensor->critmin_value / 1000000.0);
916 ilen = 16 + 1;
917 }
918
919 if (sensor->critcap_value) {
920 ilen = 24 + 2;
921 (void)printf("%*.2f%% ", (int)ilen,
922 (sensor->critcap_value * 100.0) /
923 sensor->max_value);
924 ilen = 8;
925 }
926 }
927
928 if (statistics && !sensor->percentage) {
929 /* show statistics if flag set */
930 (void)printf("%8.3f %8.3f %8.3f ",
931 stats->max / 1000000.0,
932 stats->min / 1000000.0,
933 stats->avg / 1000000.0);
934 ilen = 8;
935 }
936
937 (void)printf("%*s", (int)ilen, stype);
938 if (sensor->percentage && sensor->max_value) {
939 (void)printf(" (%5.2f%%)",
940 (sensor->cur_value * 100.0) /
941 sensor->max_value);
942 }
943 }
944 (void)printf("\n");
945 ilen = 32 + 3;
946 }
947 }
948
949 static int
950 usage(void)
951 {
952 (void)fprintf(stderr, "Usage: %s [-DfIlrSTx] ", getprogname());
953 (void)fprintf(stderr, "[-c file] [-d device] [-i interval] ");
954 (void)fprintf(stderr, "[-s device:sensor,...] [-w width]\n");
955 exit(EXIT_FAILURE);
956 /* NOTREACHED */
957 }
958