Module Name: src Committed By: mlelstv Date: Sat Nov 14 12:36:50 UTC 2020
Modified Files: src/usr.sbin/envstat: envstat.8 envstat.c Log Message: Allow to select raw output for a single device or a list of sensors. To generate a diff of this commit: cvs rdiff -u -r1.63 -r1.64 src/usr.sbin/envstat/envstat.8 cvs rdiff -u -r1.98 -r1.99 src/usr.sbin/envstat/envstat.c Please note that diffs are not public domain; they are subject to the copyright notices on the relevant files.
Modified files: Index: src/usr.sbin/envstat/envstat.8 diff -u src/usr.sbin/envstat/envstat.8:1.63 src/usr.sbin/envstat/envstat.8:1.64 --- src/usr.sbin/envstat/envstat.8:1.63 Sat Nov 14 09:11:55 2020 +++ src/usr.sbin/envstat/envstat.8 Sat Nov 14 12:36:49 2020 @@ -1,4 +1,4 @@ -.\" $NetBSD: envstat.8,v 1.63 2020/11/14 09:11:55 mlelstv Exp $ +.\" $NetBSD: envstat.8,v 1.64 2020/11/14 12:36:49 mlelstv Exp $ .\" .\" Copyright (c) 2000, 2007, 2008, 2009, 2014 The NetBSD Foundation, Inc. .\" All rights reserved. @@ -35,12 +35,16 @@ .Nd utility to handle environmental sensors .Sh SYNOPSIS .Nm -.Op Fl DfIklSTWx +.Op Fl DfIklSTW .Op Fl c Ar file .Op Fl d Ar device .Op Fl i Ar interval .Op Fl s Ar "device:sensor,..." .Op Fl w Ar width +.Nm +.Fl x +.Op Fl d Ar device +.Op Fl s Ar "device:sensor,..." .Op Ar property... .Sh DESCRIPTION .Nm @@ -143,6 +147,12 @@ and sensors. If .Ar property is specified, the value of that property is printed. +The property list can be filtered with the +.Fl d or +.Fl s +options. If +.Fl s is used with only a single sensor, only the properties +of this sensor are returned, not a list of sensors. .El .Sh UNITS The display mode may show some values with abbreviated units; @@ -205,10 +215,22 @@ To display statistics for all sensors an invalid states every second: .Pp .Dl $ envstat -ITi1 +.Pp +To return a single temperature value: +.Pp +.Dl $ envstat -s vcmbox0:temperature -x /cur-value +.Pp +To return values of multiple temperature sensors from a single device +.Pp +.Dl $ envstat -s 'thinkpad0:temperature 0,thinkpad0:temperature 1' -x /thinkpad0/0/cur-value /thinkpad0/1/cur-value +.Pp +To return values of temperature sensors from multiple devices: +.Pp +.Dl $ envstat -s 'coretemp0:cpu0 temperature,coretemp1:cpu 2 temperature' -x /coretemp0/0/cur-value /coretemp1/0/cur-value .Sh SEE ALSO .Xr units 1 , .Xr proplib 3 , -.Xr acpiacad 4 , +.r acpiacad 4 , .Xr acpibat 4 , .Xr acpitz 4 , .Xr admtemp 4 , Index: src/usr.sbin/envstat/envstat.c diff -u src/usr.sbin/envstat/envstat.c:1.98 src/usr.sbin/envstat/envstat.c:1.99 --- src/usr.sbin/envstat/envstat.c:1.98 Sat Nov 14 09:11:55 2020 +++ src/usr.sbin/envstat/envstat.c Sat Nov 14 12:36:49 2020 @@ -1,4 +1,4 @@ -/* $NetBSD: envstat.c,v 1.98 2020/11/14 09:11:55 mlelstv Exp $ */ +/* $NetBSD: envstat.c,v 1.99 2020/11/14 12:36:49 mlelstv Exp $ */ /*- * Copyright (c) 2007, 2008 Juan Romero Pardines. @@ -27,7 +27,7 @@ #include <sys/cdefs.h> #ifndef lint -__RCSID("$NetBSD: envstat.c,v 1.98 2020/11/14 09:11:55 mlelstv Exp $"); +__RCSID("$NetBSD: envstat.c,v 1.99 2020/11/14 12:36:49 mlelstv Exp $"); #endif /* not lint */ #include <stdio.h> @@ -109,6 +109,7 @@ static bool statistics; static u_int header_passes; static int parse_dictionary(int); +static int add_sensors(prop_dictionary_t, prop_dictionary_t, const char *, const char *); static int send_dictionary(FILE *); static int find_sensors(prop_array_t, const char *, dvprops_t); static void print_sensors(void); @@ -219,6 +220,61 @@ int main(int argc, char **argv) if (rval) errx(EXIT_FAILURE, "%s", strerror(rval)); + if (mydevname || sensors) { + prop_dictionary_t ndict; + + ndict = prop_dictionary_create(); + if (ndict == NULL) + err(EXIT_FAILURE, "prop_dictionary_create"); + + if (mydevname) { + if (add_sensors(ndict, dict, mydevname, NULL)) + err(EXIT_FAILURE, "add_sensors"); + } + if (sensors) { + char *dvstring, *sstring, *p, *last, *s; + unsigned count = 0; + + s = strdup(sensors); + if (s == NULL) + err(EXIT_FAILURE, "strdup"); + + for ((p = strtok_r(s, ",", &last)); p; + (p = strtok_r(NULL, ",", &last))) { + /* get device name */ + dvstring = strtok(p, ":"); + if (dvstring == NULL) + errx(EXIT_FAILURE, "missing device name"); + + /* get sensor description */ + sstring = strtok(NULL, ":"); + if (sstring == NULL) + errx(EXIT_FAILURE, "missing sensor description"); + + if (add_sensors(ndict, dict, dvstring, sstring)) + err(EXIT_FAILURE, "add_sensors"); + + ++count; + } + free(s); + + /* in case we asked for a single sensor + * show only the sensor dictionary + */ + if (count == 1) { + prop_object_t obj, obj2; + + obj = prop_dictionary_get(ndict, dvstring); + obj2 = prop_array_get(obj, 0); + prop_object_release(ndict); + ndict = obj2; + } + } + + prop_object_release(dict); + dict = ndict; + } + if (argc > 0) { for (; argc > 0; ++argv, --argc) config_dict_extract(dict, *argv, true); @@ -350,6 +406,92 @@ find_stats_sensor(const char *desc) } static int +add_sensors(prop_dictionary_t ndict, prop_dictionary_t dict, const char *dev, const char *sensor) +{ + prop_object_iterator_t iter, iter2; + prop_object_t obj, obj2, desc; + prop_array_t array, narray; + prop_dictionary_t sdict; + const char *dnp; + unsigned int capacity = 1; + uint64_t dummy; + bool found = false; + + if (prop_dictionary_count(dict) == 0) + return 0; + + narray = prop_dictionary_get(ndict, dev); + if (narray) + found = true; + else { + narray = prop_array_create_with_capacity(capacity); + if (!narray) + return -1; + if (!prop_dictionary_set(ndict, dev, narray)) { + prop_object_release(narray); + return -1; + } + } + + iter = prop_dictionary_iterator(dict); + if (iter == NULL) + goto fail; + while ((obj = prop_object_iterator_next(iter)) != NULL) { + array = prop_dictionary_get_keysym(dict, obj); + if (prop_object_type(array) != PROP_TYPE_ARRAY) + break; + + dnp = prop_dictionary_keysym_value(obj); + if (strcmp(dev, dnp)) + continue; + found = true; + + iter2 = prop_array_iterator(array); + while ((obj = prop_object_iterator_next(iter2)) != NULL) { + obj2 = prop_dictionary_get(obj, "device-properties"); + if (obj2) { + if (!prop_dictionary_get_uint64(obj2, + "refresh-timeout", &dummy)) + continue; + } + + if (sensor) { + desc = prop_dictionary_get(obj, "description"); + if (desc == NULL) + continue; + + if (!prop_string_equals_string(desc, sensor)) + continue; + } + + if (!prop_array_ensure_capacity(narray, capacity)) + goto fail; + + sdict = prop_dictionary_copy(obj); + if (sdict == NULL) + goto fail; + prop_array_add(narray, sdict); + ++capacity; + } + prop_object_iterator_release(iter2); + } + prop_object_iterator_release(iter); + + /* drop key and array when device wasn't found */ + if (!found) { + prop_dictionary_remove(ndict, dev); + prop_object_release(narray); + } + + return 0; + +fail: + prop_dictionary_remove(ndict, dev); + prop_object_release(narray); + return -1; +} + +static int parse_dictionary(int fd) { sensor_t sensor = NULL; @@ -1099,10 +1241,13 @@ do { \ static int usage(void) { - (void)fprintf(stderr, "Usage: %s [-DfIklrSTx] ", getprogname()); + (void)fprintf(stderr, "Usage: %s [-DfIklrST] ", getprogname()); (void)fprintf(stderr, "[-c file] [-d device] [-i interval] "); (void)fprintf(stderr, "[-s device:sensor,...] [-w width]\n"); - (void)fprintf(stderr, " %s -x [property]", getprogname()); + (void)fprintf(stderr, " %s ", getprogname()); + (void)fprintf(stderr, "[-d device] "); + (void)fprintf(stderr, "[-s device:sensor,...] "); + (void)fprintf(stderr, "-x [property]\n"); exit(EXIT_FAILURE); /* NOTREACHED */ }