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