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