Mon Jun 6 20:48:56 2011 UTC ()
1. Don't print blank for Integer sensors with value of zero
2. Properly print percentages in -T statistics mode
3. Always set Sensor Type (units) to a non-NULL value
4. Enable printing of percentages for Integer sensors


(pgoyette)
diff -r1.86 -r1.87 src/usr.sbin/envstat/envstat.c

cvs diff -r1.86 -r1.87 src/usr.sbin/envstat/envstat.c (expand / switch to unified diff)

--- src/usr.sbin/envstat/envstat.c 2011/06/04 13:29:02 1.86
+++ src/usr.sbin/envstat/envstat.c 2011/06/06 20:48:56 1.87
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: envstat.c,v 1.86 2011/06/04 13:29:02 pgoyette Exp $ */ 1/* $NetBSD: envstat.c,v 1.87 2011/06/06 20:48:56 pgoyette Exp $ */
2 2
3/*- 3/*-
4 * Copyright (c) 2007, 2008 Juan Romero Pardines. 4 * Copyright (c) 2007, 2008 Juan Romero Pardines.
5 * All rights reserved. 5 * All rights reserved.
6 * 6 *
7 * Redistribution and use in source and binary forms, with or without 7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions 8 * modification, are permitted provided that the following conditions
9 * are met: 9 * are met:
10 * 1. Redistributions of source code must retain the above copyright 10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer. 11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright 12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the 13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution. 14 * documentation and/or other materials provided with the distribution.
@@ -17,27 +17,27 @@ @@ -17,27 +17,27 @@
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 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 22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 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 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. 25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */ 26 */
27 27
28#include <sys/cdefs.h> 28#include <sys/cdefs.h>
29#ifndef lint 29#ifndef lint
30__RCSID("$NetBSD: envstat.c,v 1.86 2011/06/04 13:29:02 pgoyette Exp $"); 30__RCSID("$NetBSD: envstat.c,v 1.87 2011/06/06 20:48:56 pgoyette Exp $");
31#endif /* not lint */ 31#endif /* not lint */
32 32
33#include <stdio.h> 33#include <stdio.h>
34#include <stdlib.h> 34#include <stdlib.h>
35#include <stdbool.h> 35#include <stdbool.h>
36#include <stdarg.h> 36#include <stdarg.h>
37#include <string.h> 37#include <string.h>
38#include <unistd.h> 38#include <unistd.h>
39#include <fcntl.h> 39#include <fcntl.h>
40#include <err.h> 40#include <err.h>
41#include <errno.h> 41#include <errno.h>
42#include <paths.h> 42#include <paths.h>
43#include <syslog.h> 43#include <syslog.h>
@@ -753,27 +753,27 @@ print_sensors(void) @@ -753,27 +753,27 @@ print_sensors(void)
753 b = "CritMax"; 753 b = "CritMax";
754 c = "WarnMax"; 754 c = "WarnMax";
755 d = "WarnMin"; 755 d = "WarnMin";
756 e = "CritMin"; 756 e = "CritMin";
757 } 757 }
758 758
759 if (!sensors || (!header_passes && sensors) || 759 if (!sensors || (!header_passes && sensors) ||
760 (header_passes == 10 && sensors)) { 760 (header_passes == 10 && sensors)) {
761 if (statistics) 761 if (statistics)
762 (void)printf("%s%*s %9s %8s %8s %8s %6s\n", 762 (void)printf("%s%*s %9s %8s %8s %8s %6s\n",
763 mydevname ? "" : " ", (int)maxlen, 763 mydevname ? "" : " ", (int)maxlen,
764 "", a, b, c, d, units); 764 "", a, b, c, d, units);
765 else 765 else
766 (void)printf("%s%*s %9s %8s %8s %8s %8s %4s\n", 766 (void)printf("%s%*s %9s %8s %8s %8s %8s %5s\n",
767 mydevname ? "" : " ", (int)maxlen, 767 mydevname ? "" : " ", (int)maxlen,
768 "", a, b, c, d, e, units); 768 "", a, b, c, d, e, units);
769 if (sensors && header_passes == 10) 769 if (sensors && header_passes == 10)
770 header_passes = 0; 770 header_passes = 0;
771 } 771 }
772 if (sensors) 772 if (sensors)
773 header_passes++; 773 header_passes++;
774 774
775 /* print the sensors */ 775 /* print the sensors */
776 SIMPLEQ_FOREACH(sensor, &sensors_list, entries) { 776 SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
777 /* skip sensors that were not marked as visible */ 777 /* skip sensors that were not marked as visible */
778 if (sensors && !sensor->visible) 778 if (sensors && !sensor->visible)
779 continue; 779 continue;
@@ -904,145 +904,159 @@ do { \ @@ -904,145 +904,159 @@ do { \
904 904
905 } 905 }
906 906
907 (void)printf("%*s", (int)ilen - 4, stype); 907 (void)printf("%*s", (int)ilen - 4, stype);
908 908
909 /* integers */ 909 /* integers */
910 } else if (strcmp(sensor->type, "Integer") == 0) { 910 } else if (strcmp(sensor->type, "Integer") == 0) {
911 911
912 stype = "none"; 912 stype = "none";
913 913
914 (void)printf(":%10d ", sensor->cur_value); 914 (void)printf(":%10d ", sensor->cur_value);
915 915
916 ilen = 8; 916 ilen = 8;
917 if (statistics) { 
918 /* show statistics if flag set */ 
919 (void)printf("%8u %8u %8u ", 
920 stats->max, stats->min, stats->avg); 
921 ilen += 2; 
922 } else { 
923 if (sensor->critmax_value) { 
924 (void)printf("%*u ", (int)ilen, 
925 sensor->critmax_value); 
926 ilen = 8; 
927 } else 
928 ilen += 9; 
929 
930 if (sensor->warnmax_value) { 
931 (void)printf("%*u ", (int)ilen, 
932 sensor->warnmax_value); 
933 ilen = 8; 
934 } else 
935 ilen += 9; 
936 917
937 if (sensor->warnmin_value) { 918/* Print percentage of max_value */
938 (void)printf("%*u ", (int)ilen, 919#define PRINTPCT(a) \
939 sensor->warnmin_value); 920do { \
940 ilen = 8; 921 if (sensor->max_value) { \
941 } else 922 (void)printf("%*.3f%%", (int)ilen, \
942 ilen += 9; 923 ((a) * 100.0) / sensor->max_value); \
 924 ilen = 8; \
 925 } else \
 926 ilen += 9; \
 927} while ( /* CONSTCOND*/ 0 )
943 928
944 if (sensor->critmin_value) { 929/* Print an integer sensor value */
945 (void)printf( "%*u ", (int)ilen, 930#define PRINTINT(a) \
946 sensor->critmin_value); 931do { \
947 ilen = 8; 932 (void)printf("%*u ", (int)ilen, (a)); \
948 } else 933 ilen = 8; \
949 ilen += 9; 934} while ( /* CONSTCOND*/ 0 )
950 935
 936 if (!statistics) {
 937 if (sensor->percentage) {
 938 PRINTPCT(sensor->critmax_value);
 939 PRINTPCT(sensor->warnmax_value);
 940 PRINTPCT(sensor->warnmin_value);
 941 PRINTPCT(sensor->critmin_value);
 942 } else {
 943 PRINTINT(sensor->critmax_value);
 944 PRINTINT(sensor->warnmax_value);
 945 PRINTINT(sensor->warnmin_value);
 946 PRINTINT(sensor->critmin_value);
 947 }
 948 } else {
 949 if (sensor->percentage) {
 950 PRINTPCT(stats->max);
 951 PRINTPCT(stats->min);
 952 PRINTPCT(stats->avg);
 953 } else {
 954 PRINTINT(stats->max);
 955 PRINTINT(stats->min);
 956 PRINTINT(stats->avg);
 957 }
 958 ilen += 2;
951 } 959 }
952 960
953 (void)printf("%*s", (int)ilen - 4, stype); 961 (void)printf("%*s", (int)ilen - 4, stype);
954 962
 963#undef PRINTINT
 964#undef PRINTPCT
 965
955 /* drives */ 966 /* drives */
956 } else if (strcmp(sensor->type, "Drive") == 0) { 967 } else if (strcmp(sensor->type, "Drive") == 0) {
957 968
958 (void)printf(":%10s", sensor->drvstate); 969 (void)printf(":%10s", sensor->drvstate);
959 970
960 /* Battery capacity */ 971 /* Battery capacity */
961 } else if (strcmp(sensor->type, "Battery capacity") == 0) { 972 } else if (strcmp(sensor->type, "Battery capacity") == 0) {
962 973
963 (void)printf(":%10s", sensor->battcap); 974 (void)printf(":%10s", sensor->battcap);
964 975
965 /* everything else */ 976 /* everything else */
966 } else { 977 } else {
967 if (strcmp(sensor->type, "Voltage DC") == 0) 978 if (strcmp(sensor->type, "Voltage DC") == 0)
968 stype = "V"; 979 stype = "V";
969 else if (strcmp(sensor->type, "Voltage AC") == 0) 980 else if (strcmp(sensor->type, "Voltage AC") == 0)
970 stype = "VAC"; 981 stype = "VAC";
971 else if (strcmp(sensor->type, "Ampere") == 0) 982 else if (strcmp(sensor->type, "Ampere") == 0)
972 stype = "A"; 983 stype = "A";
973 else if (strcmp(sensor->type, "Watts") == 0) 984 else if (strcmp(sensor->type, "Watts") == 0)
974 stype = "W"; 985 stype = "W";
975 else if (strcmp(sensor->type, "Ohms") == 0) 986 else if (strcmp(sensor->type, "Ohms") == 0)
976 stype = "Ohms"; 987 stype = "Ohms";
977 else if (strcmp(sensor->type, "Watt hour") == 0) 988 else if (strcmp(sensor->type, "Watt hour") == 0)
978 stype = "Wh"; 989 stype = "Wh";
979 else if (strcmp(sensor->type, "Ampere hour") == 0) 990 else if (strcmp(sensor->type, "Ampere hour") == 0)
980 stype = "Ah"; 991 stype = "Ah";
 992 else
 993 stype = "?";
981 994
982 (void)printf(":%10.3f ", 995 (void)printf(":%10.3f ",
983 sensor->cur_value / 1000000.0); 996 sensor->cur_value / 1000000.0);
984 997
985 ilen = 8; 998 ilen = 8;
986 if (!statistics) { 
987 999
988/* Print percentage of max_value */ 1000/* Print percentage of max_value */
989#define PRINTPCT(a) \ 1001#define PRINTPCT(a) \
990do { \ 1002do { \
991 if (sensor->a && sensor->max_value) { \ 1003 if ((a) && sensor->max_value) { \
992 (void)printf("%*.3f%%", (int)ilen, \ 1004 (void)printf("%*.3f%%", (int)ilen, \
993 (sensor->a * 100.0) / sensor->max_value); \ 1005 ((a) * 100.0) / sensor->max_value); \
994 ilen = 8; \ 1006 ilen = 8; \
995 } else \ 1007 } else \
996 ilen += 9; \ 1008 ilen += 9; \
997} while ( /* CONSTCOND*/ 0 ) 1009} while ( /* CONSTCOND*/ 0 )
998 1010
999/* Print a generic sensor value */ 1011/* Print a generic sensor value */
1000#define PRINTVAL(a) \ 1012#define PRINTVAL(a) \
1001do { \ 1013do { \
1002 if (sensor->a) { \ 1014 if ((a)) { \
1003 (void)printf("%*.3f ", (int)ilen, sensor->a / 1000000.0); \ 1015 (void)printf("%*.3f ", (int)ilen, (a) / 1000000.0); \
1004 ilen = 8; \ 1016 ilen = 8; \
1005 } else \ 1017 } else \
1006 ilen += 9; \ 1018 ilen += 9; \
1007} while ( /* CONSTCOND*/ 0 ) 1019} while ( /* CONSTCOND*/ 0 )
1008 1020
1009 1021 if (!statistics) {
1010 if (sensor->percentage) { 1022 if (sensor->percentage) {
1011 PRINTPCT(critmax_value); 1023 PRINTPCT(sensor->critmax_value);
1012 PRINTPCT(warnmax_value); 1024 PRINTPCT(sensor->warnmax_value);
1013 PRINTPCT(warnmin_value); 1025 PRINTPCT(sensor->warnmin_value);
1014 PRINTPCT(critmin_value); 1026 PRINTPCT(sensor->critmin_value);
1015 } else { 1027 } else {
1016 1028
1017 PRINTVAL(critmax_value); 1029 PRINTVAL(sensor->critmax_value);
1018 PRINTVAL(warnmax_value); 1030 PRINTVAL(sensor->warnmax_value);
1019 PRINTVAL(warnmin_value); 1031 PRINTVAL(sensor->warnmin_value);
1020 PRINTVAL(critmin_value); 1032 PRINTVAL(sensor->critmin_value);
1021#undef PRINTPCT 1033 }
1022#undef PRINTVAL 1034 } else {
 1035 if (sensor->percentage) {
 1036 PRINTPCT(stats->max);
 1037 PRINTPCT(stats->min);
 1038 PRINTPCT(stats->avg);
 1039 } else {
 1040 PRINTVAL(stats->max);
 1041 PRINTVAL(stats->min);
 1042 PRINTVAL(stats->avg);
1023 } 1043 }
1024 } 
1025 
1026 if (statistics && !sensor->percentage) { 
1027 /* show statistics if flag set */ 
1028 (void)printf("%8.3f %8.3f %8.3f ", 
1029 stats->max / 1000000.0, 
1030 stats->min / 1000000.0, 
1031 stats->avg / 1000000.0); 
1032 ilen += 2; 1044 ilen += 2;
1033 } 1045 }
 1046#undef PRINTPCT
 1047#undef PRINTVAL
1034 1048
1035 (void)printf("%*s", (int)ilen - 4, stype); 1049 (void)printf("%*s", (int)ilen - 3, stype);
1036 1050
1037 if (sensor->percentage && sensor->max_value) { 1051 if (sensor->percentage && sensor->max_value) {
1038 (void)printf(" (%5.2f%%)", 1052 (void)printf(" (%5.2f%%)",
1039 (sensor->cur_value * 100.0) / 1053 (sensor->cur_value * 100.0) /
1040 sensor->max_value); 1054 sensor->max_value);
1041 } 1055 }
1042 } 1056 }
1043 (void)printf("\n"); 1057 (void)printf("\n");
1044 } 1058 }
1045} 1059}
1046 1060
1047static int 1061static int
1048usage(void) 1062usage(void)