Tue Jun 7 17:24:32 2011 UTC ()
Zero is a valid value for some sensors so update statistics code to
handle that.


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

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

--- src/usr.sbin/envstat/envstat.c 2011/06/06 20:48:56 1.87
+++ src/usr.sbin/envstat/envstat.c 2011/06/07 17:24:32 1.88
@@ -1,14 +1,14 @@ @@ -1,14 +1,14 @@
1/* $NetBSD: envstat.c,v 1.87 2011/06/06 20:48:56 pgoyette Exp $ */ 1/* $NetBSD: envstat.c,v 1.88 2011/06/07 17:24:32 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,33 +17,34 @@ @@ -17,33 +17,34 @@
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.87 2011/06/06 20:48:56 pgoyette Exp $"); 30__RCSID("$NetBSD: envstat.c,v 1.88 2011/06/07 17:24:32 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 <stdint.h>
37#include <string.h> 38#include <string.h>
38#include <unistd.h> 39#include <unistd.h>
39#include <fcntl.h> 40#include <fcntl.h>
40#include <err.h> 41#include <err.h>
41#include <errno.h> 42#include <errno.h>
42#include <paths.h> 43#include <paths.h>
43#include <syslog.h> 44#include <syslog.h>
44#include <sys/envsys.h> 45#include <sys/envsys.h>
45#include <sys/ioctl.h> 46#include <sys/ioctl.h>
46#include <sys/types.h> 47#include <sys/types.h>
47#include <sys/queue.h> 48#include <sys/queue.h>
48#include <prop/proplib.h> 49#include <prop/proplib.h>
49 50
@@ -337,26 +338,28 @@ find_stats_sensor(const char *desc) @@ -337,26 +338,28 @@ find_stats_sensor(const char *desc)
337 /* 338 /*
338 * If we matched a sensor by its description return it, otherwise 339 * If we matched a sensor by its description return it, otherwise
339 * allocate a new one. 340 * allocate a new one.
340 */ 341 */
341 SIMPLEQ_FOREACH(stats, &sensor_stats_list, entries) 342 SIMPLEQ_FOREACH(stats, &sensor_stats_list, entries)
342 if (strcmp(stats->desc, desc) == 0) 343 if (strcmp(stats->desc, desc) == 0)
343 return stats; 344 return stats;
344 345
345 stats = calloc(1, sizeof(*stats)); 346 stats = calloc(1, sizeof(*stats));
346 if (stats == NULL) 347 if (stats == NULL)
347 return NULL; 348 return NULL;
348 349
349 (void)strlcpy(stats->desc, desc, sizeof(stats->desc)); 350 (void)strlcpy(stats->desc, desc, sizeof(stats->desc));
 351 stats->min = INT32_MAX;
 352 stats->max = INT32_MIN;
350 SIMPLEQ_INSERT_TAIL(&sensor_stats_list, stats, entries); 353 SIMPLEQ_INSERT_TAIL(&sensor_stats_list, stats, entries);
351 354
352 return stats; 355 return stats;
353} 356}
354 357
355static int 358static int
356parse_dictionary(int fd) 359parse_dictionary(int fd)
357{ 360{
358 sensor_t sensor = NULL; 361 sensor_t sensor = NULL;
359 dvprops_t edp = NULL; 362 dvprops_t edp = NULL;
360 prop_array_t array; 363 prop_array_t array;
361 prop_dictionary_t dict; 364 prop_dictionary_t dict;
362 prop_object_iterator_t iter; 365 prop_object_iterator_t iter;
@@ -615,54 +618,47 @@ find_sensors(prop_array_t array, const c @@ -615,54 +618,47 @@ find_sensors(prop_array_t array, const c
615 618
616 /* Add the sensor into the list */ 619 /* Add the sensor into the list */
617 SIMPLEQ_INSERT_TAIL(&sensors_list, sensor, entries); 620 SIMPLEQ_INSERT_TAIL(&sensors_list, sensor, entries);
618 621
619 /* Collect statistics if flag enabled */ 622 /* Collect statistics if flag enabled */
620 if (statistics) { 623 if (statistics) {
621 /* ignore sensors not relevant for statistics */ 624 /* ignore sensors not relevant for statistics */
622 if ((strcmp(sensor->type, "Indicator") == 0) || 625 if ((strcmp(sensor->type, "Indicator") == 0) ||
623 (strcmp(sensor->type, "Battery charge") == 0) || 626 (strcmp(sensor->type, "Battery charge") == 0) ||
624 (strcmp(sensor->type, "Drive") == 0)) 627 (strcmp(sensor->type, "Drive") == 0))
625 continue; 628 continue;
626 629
627 /* ignore invalid data */ 630 /* ignore invalid data */
628 if (sensor->invalid || !sensor->cur_value) 631 if (sensor->invalid)
629 continue; 632 continue;
630 633
631 /* find or allocate a new statistics sensor */ 634 /* find or allocate a new statistics sensor */
632 stats = find_stats_sensor(sensor->desc); 635 stats = find_stats_sensor(sensor->desc);
633 if (stats == NULL) { 636 if (stats == NULL) {
634 free(sensor); 637 free(sensor);
635 prop_object_iterator_release(iter); 638 prop_object_iterator_release(iter);
636 return ENOMEM; 639 return ENOMEM;
637 } 640 }
638 641
639 /* collect data */ 642 /* update data */
640 if (!stats->max) 
641 stats->max = sensor->cur_value; 
642 if (!stats->min) 
643 stats->min = sensor->cur_value; 
644 
645 if (sensor->cur_value > stats->max) 643 if (sensor->cur_value > stats->max)
646 stats->max = sensor->cur_value; 644 stats->max = sensor->cur_value;
647 645
648 if (sensor->cur_value < stats->min) 646 if (sensor->cur_value < stats->min)
649 stats->min = sensor->cur_value; 647 stats->min = sensor->cur_value;
650 648
651 /* compute avg value */ 649 /* compute avg value */
652 if (stats->max && stats->min) 650 stats->avg =
653 stats->avg = 651 (sensor->cur_value + stats->max + stats->min) / 3;
654 (sensor->cur_value + stats->max + 
655 stats->min) / 3; 
656 } 652 }
657 } 653 }
658 654
659 /* free memory */ 655 /* free memory */
660 prop_object_iterator_release(iter); 656 prop_object_iterator_release(iter);
661 return 0; 657 return 0;
662} 658}
663 659
664static int 660static int
665check_sensors(char *str) 661check_sensors(char *str)
666{ 662{
667 sensor_t sensor = NULL; 663 sensor_t sensor = NULL;
668 char *dvstring, *sstring, *p, *last; 664 char *dvstring, *sstring, *p, *last;