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