1 1.105 brad /* $NetBSD: envstat.c,v 1.105 2025/04/01 11:39:19 brad Exp $ */ 2 1.1 groo 3 1.1 groo /*- 4 1.59 xtraeme * Copyright (c) 2007, 2008 Juan Romero Pardines. 5 1.1 groo * All rights reserved. 6 1.1 groo * 7 1.1 groo * Redistribution and use in source and binary forms, with or without 8 1.1 groo * modification, are permitted provided that the following conditions 9 1.1 groo * are met: 10 1.1 groo * 1. Redistributions of source code must retain the above copyright 11 1.1 groo * notice, this list of conditions and the following disclaimer. 12 1.1 groo * 2. Redistributions in binary form must reproduce the above copyright 13 1.1 groo * notice, this list of conditions and the following disclaimer in the 14 1.1 groo * documentation and/or other materials provided with the distribution. 15 1.1 groo * 16 1.56 xtraeme * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 17 1.56 xtraeme * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 18 1.56 xtraeme * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 19 1.56 xtraeme * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 20 1.56 xtraeme * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 21 1.56 xtraeme * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 22 1.56 xtraeme * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 23 1.56 xtraeme * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 24 1.56 xtraeme * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 25 1.56 xtraeme * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 1.1 groo */ 27 1.1 groo 28 1.56 xtraeme #include <sys/cdefs.h> 29 1.56 xtraeme #ifndef lint 30 1.105 brad __RCSID("$NetBSD: envstat.c,v 1.105 2025/04/01 11:39:19 brad Exp $"); 31 1.56 xtraeme #endif /* not lint */ 32 1.56 xtraeme 33 1.1 groo #include <stdio.h> 34 1.1 groo #include <stdlib.h> 35 1.25 xtraeme #include <stdbool.h> 36 1.81 pooka #include <stdarg.h> 37 1.88 pgoyette #include <stdint.h> 38 1.1 groo #include <string.h> 39 1.1 groo #include <unistd.h> 40 1.25 xtraeme #include <fcntl.h> 41 1.3 thorpej #include <err.h> 42 1.25 xtraeme #include <errno.h> 43 1.105 brad #include <mj.h> 44 1.79 jruoho #include <paths.h> 45 1.56 xtraeme #include <syslog.h> 46 1.105 brad #include <time.h> 47 1.1 groo #include <sys/envsys.h> 48 1.81 pooka #include <sys/ioctl.h> 49 1.58 xtraeme #include <sys/types.h> 50 1.59 xtraeme #include <sys/queue.h> 51 1.59 xtraeme #include <prop/proplib.h> 52 1.1 groo 53 1.56 xtraeme #include "envstat.h" 54 1.83 pooka #include "prog_ops.h" 55 1.81 pooka 56 1.25 xtraeme #define ENVSYS_DFLAG 0x00000001 /* list registered devices */ 57 1.102 andvar #define ENVSYS_FFLAG 0x00000002 /* show temp in fahrenheit */ 58 1.25 xtraeme #define ENVSYS_LFLAG 0x00000004 /* list sensors */ 59 1.25 xtraeme #define ENVSYS_XFLAG 0x00000008 /* externalize dictionary */ 60 1.61 xtraeme #define ENVSYS_IFLAG 0x00000010 /* skip invalid sensors */ 61 1.61 xtraeme #define ENVSYS_SFLAG 0x00000020 /* remove all properties set */ 62 1.61 xtraeme #define ENVSYS_TFLAG 0x00000040 /* make statistics */ 63 1.100 mlelstv #define ENVSYS_NFLAG 0x00000080 /* print value only */ 64 1.71 ahoka #define ENVSYS_KFLAG 0x00000100 /* show temp in kelvin */ 65 1.105 brad #define ENVSYS_JFLAG 0x00000200 /* print the output in JSON */ 66 1.105 brad #define ENVSYS_tFLAG 0x00000400 /* print a timestamp */ 67 1.25 xtraeme 68 1.61 xtraeme /* Sensors */ 69 1.59 xtraeme typedef struct envsys_sensor { 70 1.59 xtraeme SIMPLEQ_ENTRY(envsys_sensor) entries; 71 1.25 xtraeme int32_t cur_value; 72 1.25 xtraeme int32_t max_value; 73 1.25 xtraeme int32_t min_value; 74 1.25 xtraeme int32_t critmin_value; 75 1.25 xtraeme int32_t critmax_value; 76 1.70 pgoyette int32_t warnmin_value; 77 1.70 pgoyette int32_t warnmax_value; 78 1.25 xtraeme char desc[ENVSYS_DESCLEN]; 79 1.25 xtraeme char type[ENVSYS_DESCLEN]; 80 1.105 brad char index[ENVSYS_DESCLEN]; 81 1.25 xtraeme char drvstate[ENVSYS_DESCLEN]; 82 1.57 xtraeme char battcap[ENVSYS_DESCLEN]; 83 1.54 xtraeme char dvname[ENVSYS_DESCLEN]; 84 1.59 xtraeme bool invalid; 85 1.59 xtraeme bool visible; 86 1.59 xtraeme bool percentage; 87 1.59 xtraeme } *sensor_t; 88 1.25 xtraeme 89 1.61 xtraeme /* Sensor statistics */ 90 1.61 xtraeme typedef struct envsys_sensor_stats { 91 1.61 xtraeme SIMPLEQ_ENTRY(envsys_sensor_stats) entries; 92 1.61 xtraeme int32_t max; 93 1.61 xtraeme int32_t min; 94 1.61 xtraeme int32_t avg; 95 1.61 xtraeme char desc[ENVSYS_DESCLEN]; 96 1.61 xtraeme } *sensor_stats_t; 97 1.61 xtraeme 98 1.61 xtraeme /* Device properties */ 99 1.59 xtraeme typedef struct envsys_dvprops { 100 1.58 xtraeme uint64_t refresh_timo; 101 1.61 xtraeme /* more members could be added in the future */ 102 1.59 xtraeme } *dvprops_t; 103 1.58 xtraeme 104 1.59 xtraeme /* A simple queue to manage all sensors */ 105 1.79 jruoho static SIMPLEQ_HEAD(, envsys_sensor) sensors_list = 106 1.59 xtraeme SIMPLEQ_HEAD_INITIALIZER(sensors_list); 107 1.59 xtraeme 108 1.61 xtraeme /* A simple queue to manage statistics for all sensors */ 109 1.61 xtraeme static SIMPLEQ_HEAD(, envsys_sensor_stats) sensor_stats_list = 110 1.61 xtraeme SIMPLEQ_HEAD_INITIALIZER(sensor_stats_list); 111 1.61 xtraeme 112 1.59 xtraeme static unsigned int interval, flags, width; 113 1.59 xtraeme static char *mydevname, *sensors; 114 1.61 xtraeme static bool statistics; 115 1.61 xtraeme static u_int header_passes; 116 1.105 brad static time_t timestamp; 117 1.59 xtraeme 118 1.59 xtraeme static int parse_dictionary(int); 119 1.99 mlelstv static int add_sensors(prop_dictionary_t, prop_dictionary_t, const char *, const char *); 120 1.81 pooka static int send_dictionary(FILE *); 121 1.59 xtraeme static int find_sensors(prop_array_t, const char *, dvprops_t); 122 1.59 xtraeme static void print_sensors(void); 123 1.105 brad static void print_sensors_json(void); 124 1.93 christos static int check_sensors(const char *); 125 1.59 xtraeme static int usage(void); 126 1.25 xtraeme 127 1.81 pooka static int sysmonfd; /* fd of /dev/sysmon */ 128 1.81 pooka 129 1.25 xtraeme int main(int argc, char **argv) 130 1.25 xtraeme { 131 1.25 xtraeme prop_dictionary_t dict; 132 1.81 pooka int c, rval = 0; 133 1.56 xtraeme char *endptr, *configfile = NULL; 134 1.56 xtraeme FILE *cf; 135 1.25 xtraeme 136 1.83 pooka if (prog_init && prog_init() == -1) 137 1.83 pooka err(1, "init failed"); 138 1.81 pooka 139 1.25 xtraeme setprogname(argv[0]); 140 1.25 xtraeme 141 1.105 brad while ((c = getopt(argc, argv, "c:Dd:fIi:jklnrSs:Ttw:Wx")) != -1) { 142 1.25 xtraeme switch (c) { 143 1.56 xtraeme case 'c': /* configuration file */ 144 1.93 christos configfile = optarg; 145 1.56 xtraeme break; 146 1.45 xtraeme case 'D': /* list registered devices */ 147 1.45 xtraeme flags |= ENVSYS_DFLAG; 148 1.45 xtraeme break; 149 1.25 xtraeme case 'd': /* show sensors of a specific device */ 150 1.93 christos mydevname = optarg; 151 1.25 xtraeme break; 152 1.102 andvar case 'f': /* display temperature in Fahrenheit */ 153 1.45 xtraeme flags |= ENVSYS_FFLAG; 154 1.45 xtraeme break; 155 1.53 plunky case 'I': /* Skips invalid sensors */ 156 1.53 plunky flags |= ENVSYS_IFLAG; 157 1.53 plunky break; 158 1.25 xtraeme case 'i': /* wait time between intervals */ 159 1.56 xtraeme interval = (unsigned int)strtoul(optarg, &endptr, 10); 160 1.25 xtraeme if (*endptr != '\0') 161 1.54 xtraeme errx(EXIT_FAILURE, "bad interval '%s'", optarg); 162 1.25 xtraeme break; 163 1.105 brad case 'j': 164 1.105 brad flags |= ENVSYS_JFLAG; 165 1.105 brad break; 166 1.71 ahoka case 'k': /* display temperature in Kelvin */ 167 1.71 ahoka flags |= ENVSYS_KFLAG; 168 1.71 ahoka break; 169 1.25 xtraeme case 'l': /* list sensors */ 170 1.25 xtraeme flags |= ENVSYS_LFLAG; 171 1.25 xtraeme break; 172 1.100 mlelstv case 'n': /* print value only */ 173 1.100 mlelstv flags |= ENVSYS_NFLAG; 174 1.100 mlelstv break; 175 1.35 xtraeme case 'r': 176 1.79 jruoho /* 177 1.59 xtraeme * This flag is noop.. it's only here for 178 1.35 xtraeme * compatibility with the old implementation. 179 1.35 xtraeme */ 180 1.35 xtraeme break; 181 1.56 xtraeme case 'S': 182 1.56 xtraeme flags |= ENVSYS_SFLAG; 183 1.56 xtraeme break; 184 1.25 xtraeme case 's': /* only show specified sensors */ 185 1.93 christos sensors = optarg; 186 1.1 groo break; 187 1.61 xtraeme case 'T': /* make statistics */ 188 1.61 xtraeme flags |= ENVSYS_TFLAG; 189 1.61 xtraeme break; 190 1.105 brad case 't': /* produce a timestamp */ 191 1.105 brad flags |= ENVSYS_tFLAG; 192 1.105 brad break; 193 1.45 xtraeme case 'w': /* width value for the lines */ 194 1.61 xtraeme width = (unsigned int)strtoul(optarg, &endptr, 10); 195 1.45 xtraeme if (*endptr != '\0') 196 1.54 xtraeme errx(EXIT_FAILURE, "bad width '%s'", optarg); 197 1.45 xtraeme break; 198 1.45 xtraeme case 'x': /* print the dictionary in raw format */ 199 1.45 xtraeme flags |= ENVSYS_XFLAG; 200 1.1 groo break; 201 1.90 riz case 'W': /* No longer used, retained for compatibility */ 202 1.73 pgoyette break; 203 1.1 groo case '?': 204 1.1 groo default: 205 1.1 groo usage(); 206 1.1 groo /* NOTREACHED */ 207 1.1 groo } 208 1.1 groo } 209 1.1 groo 210 1.45 xtraeme argc -= optind; 211 1.45 xtraeme argv += optind; 212 1.45 xtraeme 213 1.98 mlelstv if (argc > 0 && (flags & ENVSYS_XFLAG) == 0) 214 1.50 xtraeme usage(); 215 1.50 xtraeme 216 1.61 xtraeme /* Check if we want to make statistics */ 217 1.61 xtraeme if (flags & ENVSYS_TFLAG) { 218 1.61 xtraeme if (!interval) 219 1.61 xtraeme errx(EXIT_FAILURE, 220 1.61 xtraeme "-T cannot be used without an interval (-i)"); 221 1.61 xtraeme else 222 1.61 xtraeme statistics = true; 223 1.61 xtraeme } 224 1.61 xtraeme 225 1.59 xtraeme if (mydevname && sensors) 226 1.59 xtraeme errx(EXIT_FAILURE, "-d flag cannot be used with -s"); 227 1.59 xtraeme 228 1.59 xtraeme /* Open the device in ro mode */ 229 1.83 pooka if ((sysmonfd = prog_open(_PATH_SYSMON, O_RDONLY)) == -1) 230 1.79 jruoho err(EXIT_FAILURE, "%s", _PATH_SYSMON); 231 1.1 groo 232 1.59 xtraeme /* Print dictionary in raw mode */ 233 1.47 xtraeme if (flags & ENVSYS_XFLAG) { 234 1.81 pooka rval = prop_dictionary_recv_ioctl(sysmonfd, 235 1.47 xtraeme ENVSYS_GETDICTIONARY, 236 1.47 xtraeme &dict); 237 1.54 xtraeme if (rval) 238 1.54 xtraeme errx(EXIT_FAILURE, "%s", strerror(rval)); 239 1.54 xtraeme 240 1.99 mlelstv if (mydevname || sensors) { 241 1.99 mlelstv prop_dictionary_t ndict; 242 1.99 mlelstv 243 1.99 mlelstv ndict = prop_dictionary_create(); 244 1.99 mlelstv if (ndict == NULL) 245 1.99 mlelstv err(EXIT_FAILURE, "prop_dictionary_create"); 246 1.99 mlelstv 247 1.99 mlelstv if (mydevname) { 248 1.99 mlelstv if (add_sensors(ndict, dict, mydevname, NULL)) 249 1.99 mlelstv err(EXIT_FAILURE, "add_sensors"); 250 1.99 mlelstv } 251 1.99 mlelstv if (sensors) { 252 1.104 rin char *sstring, *p, *last, *s; 253 1.104 rin char *dvstring = NULL; /* XXXGCC */ 254 1.99 mlelstv unsigned count = 0; 255 1.99 mlelstv 256 1.99 mlelstv s = strdup(sensors); 257 1.99 mlelstv if (s == NULL) 258 1.99 mlelstv err(EXIT_FAILURE, "strdup"); 259 1.99 mlelstv 260 1.99 mlelstv for ((p = strtok_r(s, ",", &last)); p; 261 1.99 mlelstv (p = strtok_r(NULL, ",", &last))) { 262 1.99 mlelstv /* get device name */ 263 1.99 mlelstv dvstring = strtok(p, ":"); 264 1.99 mlelstv if (dvstring == NULL) 265 1.99 mlelstv errx(EXIT_FAILURE, "missing device name"); 266 1.99 mlelstv 267 1.99 mlelstv /* get sensor description */ 268 1.99 mlelstv sstring = strtok(NULL, ":"); 269 1.99 mlelstv if (sstring == NULL) 270 1.99 mlelstv errx(EXIT_FAILURE, "missing sensor description"); 271 1.99 mlelstv 272 1.99 mlelstv if (add_sensors(ndict, dict, dvstring, sstring)) 273 1.99 mlelstv err(EXIT_FAILURE, "add_sensors"); 274 1.99 mlelstv 275 1.99 mlelstv ++count; 276 1.99 mlelstv } 277 1.99 mlelstv free(s); 278 1.99 mlelstv 279 1.99 mlelstv /* in case we asked for a single sensor 280 1.99 mlelstv * show only the sensor dictionary 281 1.99 mlelstv */ 282 1.99 mlelstv if (count == 1) { 283 1.99 mlelstv prop_object_t obj, obj2; 284 1.99 mlelstv 285 1.99 mlelstv obj = prop_dictionary_get(ndict, dvstring); 286 1.99 mlelstv obj2 = prop_array_get(obj, 0); 287 1.99 mlelstv prop_object_release(ndict); 288 1.99 mlelstv ndict = obj2; 289 1.99 mlelstv } 290 1.99 mlelstv } 291 1.99 mlelstv 292 1.99 mlelstv prop_object_release(dict); 293 1.99 mlelstv dict = ndict; 294 1.99 mlelstv } 295 1.99 mlelstv 296 1.98 mlelstv if (argc > 0) { 297 1.98 mlelstv for (; argc > 0; ++argv, --argc) 298 1.98 mlelstv config_dict_extract(dict, *argv, true); 299 1.98 mlelstv } else 300 1.98 mlelstv config_dict_dump(dict); 301 1.56 xtraeme 302 1.59 xtraeme /* Remove all properties set in dictionary */ 303 1.56 xtraeme } else if (flags & ENVSYS_SFLAG) { 304 1.59 xtraeme /* Close the ro descriptor */ 305 1.83 pooka (void)prog_close(sysmonfd); 306 1.56 xtraeme 307 1.59 xtraeme /* open the fd in rw mode */ 308 1.83 pooka if ((sysmonfd = prog_open(_PATH_SYSMON, O_RDWR)) == -1) 309 1.79 jruoho err(EXIT_FAILURE, "%s", _PATH_SYSMON); 310 1.56 xtraeme 311 1.56 xtraeme dict = prop_dictionary_create(); 312 1.56 xtraeme if (!dict) 313 1.56 xtraeme err(EXIT_FAILURE, "prop_dictionary_create"); 314 1.79 jruoho 315 1.56 xtraeme rval = prop_dictionary_set_bool(dict, 316 1.56 xtraeme "envsys-remove-props", 317 1.56 xtraeme true); 318 1.56 xtraeme if (!rval) 319 1.56 xtraeme err(EXIT_FAILURE, "prop_dict_set_bool"); 320 1.56 xtraeme 321 1.59 xtraeme /* send the dictionary to the kernel now */ 322 1.81 pooka rval = prop_dictionary_send_ioctl(dict, sysmonfd, 323 1.81 pooka ENVSYS_REMOVEPROPS); 324 1.56 xtraeme if (rval) 325 1.56 xtraeme warnx("%s", strerror(rval)); 326 1.56 xtraeme 327 1.59 xtraeme /* Set properties in dictionary */ 328 1.56 xtraeme } else if (configfile) { 329 1.56 xtraeme /* 330 1.56 xtraeme * Parse the configuration file. 331 1.56 xtraeme */ 332 1.56 xtraeme if ((cf = fopen(configfile, "r")) == NULL) { 333 1.56 xtraeme syslog(LOG_ERR, "fopen failed: %s", strerror(errno)); 334 1.56 xtraeme errx(EXIT_FAILURE, "%s", strerror(errno)); 335 1.56 xtraeme } 336 1.56 xtraeme 337 1.81 pooka rval = send_dictionary(cf); 338 1.56 xtraeme (void)fclose(cf); 339 1.56 xtraeme 340 1.59 xtraeme /* Show sensors with interval */ 341 1.25 xtraeme } else if (interval) { 342 1.25 xtraeme for (;;) { 343 1.105 brad timestamp = time(NULL); 344 1.81 pooka rval = parse_dictionary(sysmonfd); 345 1.25 xtraeme if (rval) 346 1.54 xtraeme break; 347 1.54 xtraeme 348 1.25 xtraeme (void)fflush(stdout); 349 1.25 xtraeme (void)sleep(interval); 350 1.25 xtraeme } 351 1.59 xtraeme /* Show sensors without interval */ 352 1.48 xtraeme } else { 353 1.105 brad timestamp = time(NULL); 354 1.81 pooka rval = parse_dictionary(sysmonfd); 355 1.48 xtraeme } 356 1.25 xtraeme 357 1.83 pooka (void)prog_close(sysmonfd); 358 1.54 xtraeme 359 1.56 xtraeme return rval ? EXIT_FAILURE : EXIT_SUCCESS; 360 1.25 xtraeme } 361 1.25 xtraeme 362 1.25 xtraeme static int 363 1.81 pooka send_dictionary(FILE *cf) 364 1.25 xtraeme { 365 1.56 xtraeme prop_dictionary_t kdict, udict; 366 1.56 xtraeme int error = 0; 367 1.25 xtraeme 368 1.59 xtraeme /* Retrieve dictionary from kernel */ 369 1.81 pooka error = prop_dictionary_recv_ioctl(sysmonfd, 370 1.81 pooka ENVSYS_GETDICTIONARY, &kdict); 371 1.56 xtraeme if (error) 372 1.37 xtraeme return error; 373 1.1 groo 374 1.56 xtraeme config_parse(cf, kdict); 375 1.1 groo 376 1.56 xtraeme /* 377 1.56 xtraeme * Dictionary built by the parser from the configuration file. 378 1.25 xtraeme */ 379 1.56 xtraeme udict = config_dict_parsed(); 380 1.1 groo 381 1.25 xtraeme /* 382 1.56 xtraeme * Close the read only descriptor and open a new one read write. 383 1.25 xtraeme */ 384 1.83 pooka (void)prog_close(sysmonfd); 385 1.83 pooka if ((sysmonfd = prog_open(_PATH_SYSMON, O_RDWR)) == -1) { 386 1.36 xtraeme error = errno; 387 1.79 jruoho warn("%s", _PATH_SYSMON); 388 1.56 xtraeme return error; 389 1.36 xtraeme } 390 1.36 xtraeme 391 1.79 jruoho /* 392 1.58 xtraeme * Send our sensor properties dictionary to the kernel then. 393 1.56 xtraeme */ 394 1.81 pooka error = prop_dictionary_send_ioctl(udict, 395 1.81 pooka sysmonfd, ENVSYS_SETDICTIONARY); 396 1.25 xtraeme if (error) 397 1.54 xtraeme warnx("%s", strerror(error)); 398 1.56 xtraeme 399 1.25 xtraeme prop_object_release(udict); 400 1.25 xtraeme return error; 401 1.25 xtraeme } 402 1.25 xtraeme 403 1.61 xtraeme static sensor_stats_t 404 1.69 christos find_stats_sensor(const char *desc) 405 1.61 xtraeme { 406 1.66 christos sensor_stats_t stats; 407 1.61 xtraeme 408 1.79 jruoho /* 409 1.61 xtraeme * If we matched a sensor by its description return it, otherwise 410 1.61 xtraeme * allocate a new one. 411 1.61 xtraeme */ 412 1.66 christos SIMPLEQ_FOREACH(stats, &sensor_stats_list, entries) 413 1.66 christos if (strcmp(stats->desc, desc) == 0) 414 1.66 christos return stats; 415 1.61 xtraeme 416 1.69 christos stats = calloc(1, sizeof(*stats)); 417 1.69 christos if (stats == NULL) 418 1.66 christos return NULL; 419 1.61 xtraeme 420 1.69 christos (void)strlcpy(stats->desc, desc, sizeof(stats->desc)); 421 1.88 pgoyette stats->min = INT32_MAX; 422 1.88 pgoyette stats->max = INT32_MIN; 423 1.69 christos SIMPLEQ_INSERT_TAIL(&sensor_stats_list, stats, entries); 424 1.69 christos 425 1.69 christos return stats; 426 1.61 xtraeme } 427 1.61 xtraeme 428 1.25 xtraeme static int 429 1.99 mlelstv add_sensors(prop_dictionary_t ndict, prop_dictionary_t dict, const char *dev, const char *sensor) 430 1.99 mlelstv { 431 1.99 mlelstv prop_object_iterator_t iter, iter2; 432 1.99 mlelstv prop_object_t obj, obj2, desc; 433 1.99 mlelstv prop_array_t array, narray; 434 1.99 mlelstv prop_dictionary_t sdict; 435 1.99 mlelstv const char *dnp; 436 1.99 mlelstv unsigned int capacity = 1; 437 1.99 mlelstv uint64_t dummy; 438 1.99 mlelstv bool found = false; 439 1.99 mlelstv 440 1.99 mlelstv if (prop_dictionary_count(dict) == 0) 441 1.99 mlelstv return 0; 442 1.99 mlelstv 443 1.99 mlelstv narray = prop_dictionary_get(ndict, dev); 444 1.99 mlelstv if (narray) 445 1.99 mlelstv found = true; 446 1.99 mlelstv else { 447 1.99 mlelstv narray = prop_array_create_with_capacity(capacity); 448 1.99 mlelstv if (!narray) 449 1.99 mlelstv return -1; 450 1.99 mlelstv if (!prop_dictionary_set(ndict, dev, narray)) { 451 1.99 mlelstv prop_object_release(narray); 452 1.99 mlelstv return -1; 453 1.99 mlelstv } 454 1.99 mlelstv } 455 1.99 mlelstv 456 1.99 mlelstv iter = prop_dictionary_iterator(dict); 457 1.99 mlelstv if (iter == NULL) 458 1.99 mlelstv goto fail; 459 1.99 mlelstv while ((obj = prop_object_iterator_next(iter)) != NULL) { 460 1.99 mlelstv array = prop_dictionary_get_keysym(dict, obj); 461 1.99 mlelstv if (prop_object_type(array) != PROP_TYPE_ARRAY) 462 1.99 mlelstv break; 463 1.99 mlelstv 464 1.99 mlelstv dnp = prop_dictionary_keysym_value(obj); 465 1.99 mlelstv if (strcmp(dev, dnp)) 466 1.99 mlelstv continue; 467 1.99 mlelstv found = true; 468 1.99 mlelstv 469 1.99 mlelstv iter2 = prop_array_iterator(array); 470 1.99 mlelstv while ((obj = prop_object_iterator_next(iter2)) != NULL) { 471 1.99 mlelstv obj2 = prop_dictionary_get(obj, "device-properties"); 472 1.99 mlelstv if (obj2) { 473 1.99 mlelstv if (!prop_dictionary_get_uint64(obj2, 474 1.99 mlelstv "refresh-timeout", &dummy)) 475 1.99 mlelstv continue; 476 1.99 mlelstv } 477 1.99 mlelstv 478 1.99 mlelstv if (sensor) { 479 1.99 mlelstv desc = prop_dictionary_get(obj, "description"); 480 1.99 mlelstv if (desc == NULL) 481 1.99 mlelstv continue; 482 1.99 mlelstv 483 1.99 mlelstv if (!prop_string_equals_string(desc, sensor)) 484 1.99 mlelstv continue; 485 1.99 mlelstv } 486 1.99 mlelstv 487 1.99 mlelstv if (!prop_array_ensure_capacity(narray, capacity)) 488 1.99 mlelstv goto fail; 489 1.99 mlelstv 490 1.99 mlelstv sdict = prop_dictionary_copy(obj); 491 1.99 mlelstv if (sdict == NULL) 492 1.99 mlelstv goto fail; 493 1.99 mlelstv prop_array_add(narray, sdict); 494 1.99 mlelstv ++capacity; 495 1.99 mlelstv } 496 1.99 mlelstv prop_object_iterator_release(iter2); 497 1.99 mlelstv } 498 1.99 mlelstv prop_object_iterator_release(iter); 499 1.99 mlelstv 500 1.99 mlelstv /* drop key and array when device wasn't found */ 501 1.99 mlelstv if (!found) { 502 1.99 mlelstv prop_dictionary_remove(ndict, dev); 503 1.99 mlelstv prop_object_release(narray); 504 1.99 mlelstv } 505 1.99 mlelstv 506 1.99 mlelstv return 0; 507 1.99 mlelstv 508 1.99 mlelstv fail: 509 1.99 mlelstv prop_dictionary_remove(ndict, dev); 510 1.99 mlelstv prop_object_release(narray); 511 1.99 mlelstv return -1; 512 1.99 mlelstv } 513 1.99 mlelstv 514 1.99 mlelstv static int 515 1.25 xtraeme parse_dictionary(int fd) 516 1.25 xtraeme { 517 1.59 xtraeme sensor_t sensor = NULL; 518 1.59 xtraeme dvprops_t edp = NULL; 519 1.25 xtraeme prop_array_t array; 520 1.25 xtraeme prop_dictionary_t dict; 521 1.25 xtraeme prop_object_iterator_t iter; 522 1.25 xtraeme prop_object_t obj; 523 1.25 xtraeme const char *dnp = NULL; 524 1.25 xtraeme int rval = 0; 525 1.25 xtraeme 526 1.25 xtraeme /* receive dictionary from kernel */ 527 1.37 xtraeme rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict); 528 1.37 xtraeme if (rval) 529 1.37 xtraeme return rval; 530 1.25 xtraeme 531 1.59 xtraeme /* No drivers registered? */ 532 1.42 xtraeme if (prop_dictionary_count(dict) == 0) { 533 1.42 xtraeme warnx("no drivers registered"); 534 1.42 xtraeme goto out; 535 1.42 xtraeme } 536 1.42 xtraeme 537 1.25 xtraeme if (mydevname) { 538 1.59 xtraeme /* -d flag specified, print sensors only for this device */ 539 1.25 xtraeme obj = prop_dictionary_get(dict, mydevname); 540 1.25 xtraeme if (prop_object_type(obj) != PROP_TYPE_ARRAY) { 541 1.25 xtraeme warnx("unknown device `%s'", mydevname); 542 1.25 xtraeme rval = EINVAL; 543 1.25 xtraeme goto out; 544 1.1 groo } 545 1.1 groo 546 1.58 xtraeme rval = find_sensors(obj, mydevname, NULL); 547 1.25 xtraeme if (rval) 548 1.25 xtraeme goto out; 549 1.54 xtraeme 550 1.25 xtraeme } else { 551 1.59 xtraeme /* print sensors for all devices registered */ 552 1.25 xtraeme iter = prop_dictionary_iterator(dict); 553 1.25 xtraeme if (iter == NULL) { 554 1.25 xtraeme rval = EINVAL; 555 1.25 xtraeme goto out; 556 1.25 xtraeme } 557 1.6 explorer 558 1.25 xtraeme /* iterate over the dictionary returned by the kernel */ 559 1.25 xtraeme while ((obj = prop_object_iterator_next(iter)) != NULL) { 560 1.25 xtraeme array = prop_dictionary_get_keysym(dict, obj); 561 1.25 xtraeme if (prop_object_type(array) != PROP_TYPE_ARRAY) { 562 1.25 xtraeme warnx("no sensors found"); 563 1.25 xtraeme rval = EINVAL; 564 1.25 xtraeme goto out; 565 1.1 groo } 566 1.1 groo 567 1.59 xtraeme edp = calloc(1, sizeof(*edp)); 568 1.58 xtraeme if (!edp) { 569 1.58 xtraeme rval = ENOMEM; 570 1.58 xtraeme goto out; 571 1.58 xtraeme } 572 1.58 xtraeme 573 1.97 thorpej dnp = prop_dictionary_keysym_value(obj); 574 1.58 xtraeme rval = find_sensors(array, dnp, edp); 575 1.58 xtraeme if (rval) 576 1.58 xtraeme goto out; 577 1.25 xtraeme 578 1.58 xtraeme if (((flags & ENVSYS_LFLAG) == 0) && 579 1.58 xtraeme (flags & ENVSYS_DFLAG)) { 580 1.58 xtraeme (void)printf("%s (checking events every ", 581 1.58 xtraeme dnp); 582 1.58 xtraeme if (edp->refresh_timo == 1) 583 1.58 xtraeme (void)printf("second)\n"); 584 1.58 xtraeme else 585 1.58 xtraeme (void)printf("%d seconds)\n", 586 1.58 xtraeme (int)edp->refresh_timo); 587 1.58 xtraeme } 588 1.79 jruoho 589 1.58 xtraeme free(edp); 590 1.58 xtraeme edp = NULL; 591 1.1 groo } 592 1.59 xtraeme prop_object_iterator_release(iter); 593 1.59 xtraeme } 594 1.31 xtraeme 595 1.59 xtraeme /* print sensors now */ 596 1.93 christos if (sensors) 597 1.93 christos rval = check_sensors(sensors); 598 1.105 brad if ((flags & ENVSYS_LFLAG) == 0 && 599 1.105 brad (flags & ENVSYS_DFLAG) == 0 && 600 1.105 brad (flags & ENVSYS_JFLAG) == 0) 601 1.59 xtraeme print_sensors(); 602 1.105 brad if ((flags & ENVSYS_LFLAG) == 0 && 603 1.105 brad (flags & ENVSYS_DFLAG) == 0 && 604 1.105 brad (flags & ENVSYS_JFLAG)) 605 1.105 brad print_sensors_json(); 606 1.105 brad if (interval && ((flags & ENVSYS_NFLAG) == 0 || sensors == NULL)) { 607 1.59 xtraeme (void)printf("\n"); 608 1.105 brad if (flags & ENVSYS_JFLAG) 609 1.105 brad (void)printf("#-------\n"); 610 1.105 brad } 611 1.31 xtraeme 612 1.25 xtraeme out: 613 1.59 xtraeme while ((sensor = SIMPLEQ_FIRST(&sensors_list))) { 614 1.59 xtraeme SIMPLEQ_REMOVE_HEAD(&sensors_list, entries); 615 1.59 xtraeme free(sensor); 616 1.31 xtraeme } 617 1.58 xtraeme if (edp) 618 1.58 xtraeme free(edp); 619 1.25 xtraeme prop_object_release(dict); 620 1.25 xtraeme return rval; 621 1.1 groo } 622 1.1 groo 623 1.25 xtraeme static int 624 1.59 xtraeme find_sensors(prop_array_t array, const char *dvname, dvprops_t edp) 625 1.25 xtraeme { 626 1.25 xtraeme prop_object_iterator_t iter; 627 1.58 xtraeme prop_object_t obj, obj1, obj2; 628 1.25 xtraeme prop_string_t state, desc = NULL; 629 1.59 xtraeme sensor_t sensor = NULL; 630 1.61 xtraeme sensor_stats_t stats = NULL; 631 1.25 xtraeme 632 1.25 xtraeme iter = prop_array_iterator(array); 633 1.56 xtraeme if (!iter) 634 1.59 xtraeme return ENOMEM; 635 1.25 xtraeme 636 1.25 xtraeme /* iterate over the array of dictionaries */ 637 1.25 xtraeme while ((obj = prop_object_iterator_next(iter)) != NULL) { 638 1.58 xtraeme /* get the refresh-timeout property */ 639 1.58 xtraeme obj2 = prop_dictionary_get(obj, "device-properties"); 640 1.58 xtraeme if (obj2) { 641 1.58 xtraeme if (!edp) 642 1.58 xtraeme continue; 643 1.58 xtraeme if (!prop_dictionary_get_uint64(obj2, 644 1.58 xtraeme "refresh-timeout", 645 1.58 xtraeme &edp->refresh_timo)) 646 1.58 xtraeme continue; 647 1.58 xtraeme } 648 1.58 xtraeme 649 1.59 xtraeme /* new sensor coming */ 650 1.59 xtraeme sensor = calloc(1, sizeof(*sensor)); 651 1.61 xtraeme if (sensor == NULL) { 652 1.61 xtraeme prop_object_iterator_release(iter); 653 1.59 xtraeme return ENOMEM; 654 1.61 xtraeme } 655 1.59 xtraeme 656 1.54 xtraeme /* copy device name */ 657 1.59 xtraeme (void)strlcpy(sensor->dvname, dvname, sizeof(sensor->dvname)); 658 1.25 xtraeme 659 1.25 xtraeme /* description string */ 660 1.25 xtraeme desc = prop_dictionary_get(obj, "description"); 661 1.56 xtraeme if (desc) { 662 1.37 xtraeme /* copy description */ 663 1.59 xtraeme (void)strlcpy(sensor->desc, 664 1.97 thorpej prop_string_value(desc), 665 1.59 xtraeme sizeof(sensor->desc)); 666 1.59 xtraeme } else { 667 1.59 xtraeme free(sensor); 668 1.37 xtraeme continue; 669 1.59 xtraeme } 670 1.25 xtraeme 671 1.25 xtraeme /* type string */ 672 1.25 xtraeme obj1 = prop_dictionary_get(obj, "type"); 673 1.59 xtraeme if (obj1) { 674 1.59 xtraeme /* copy type */ 675 1.59 xtraeme (void)strlcpy(sensor->type, 676 1.97 thorpej prop_string_value(obj1), 677 1.59 xtraeme sizeof(sensor->type)); 678 1.59 xtraeme } else { 679 1.59 xtraeme free(sensor); 680 1.59 xtraeme continue; 681 1.59 xtraeme } 682 1.59 xtraeme 683 1.105 brad /* index string */ 684 1.105 brad obj1 = prop_dictionary_get(obj, "index"); 685 1.105 brad if (obj1) { 686 1.105 brad /* copy type */ 687 1.105 brad (void)strlcpy(sensor->index, 688 1.105 brad prop_string_value(obj1), 689 1.105 brad sizeof(sensor->index)); 690 1.105 brad } else { 691 1.105 brad free(sensor); 692 1.105 brad continue; 693 1.105 brad } 694 1.105 brad 695 1.59 xtraeme /* check sensor's state */ 696 1.59 xtraeme state = prop_dictionary_get(obj, "state"); 697 1.59 xtraeme 698 1.59 xtraeme /* mark sensors with invalid/unknown state */ 699 1.97 thorpej if ((prop_string_equals_string(state, "invalid") || 700 1.97 thorpej prop_string_equals_string(state, "unknown"))) 701 1.59 xtraeme sensor->invalid = true; 702 1.25 xtraeme 703 1.25 xtraeme /* get current drive state string */ 704 1.25 xtraeme obj1 = prop_dictionary_get(obj, "drive-state"); 705 1.59 xtraeme if (obj1) { 706 1.59 xtraeme (void)strlcpy(sensor->drvstate, 707 1.97 thorpej prop_string_value(obj1), 708 1.59 xtraeme sizeof(sensor->drvstate)); 709 1.59 xtraeme } 710 1.25 xtraeme 711 1.57 xtraeme /* get current battery capacity string */ 712 1.57 xtraeme obj1 = prop_dictionary_get(obj, "battery-capacity"); 713 1.59 xtraeme if (obj1) { 714 1.59 xtraeme (void)strlcpy(sensor->battcap, 715 1.97 thorpej prop_string_value(obj1), 716 1.59 xtraeme sizeof(sensor->battcap)); 717 1.59 xtraeme } 718 1.44 xtraeme 719 1.25 xtraeme /* get current value */ 720 1.25 xtraeme obj1 = prop_dictionary_get(obj, "cur-value"); 721 1.59 xtraeme if (obj1) 722 1.97 thorpej sensor->cur_value = prop_number_signed_value(obj1); 723 1.25 xtraeme 724 1.25 xtraeme /* get max value */ 725 1.25 xtraeme obj1 = prop_dictionary_get(obj, "max-value"); 726 1.56 xtraeme if (obj1) 727 1.97 thorpej sensor->max_value = prop_number_signed_value(obj1); 728 1.25 xtraeme 729 1.25 xtraeme /* get min value */ 730 1.25 xtraeme obj1 = prop_dictionary_get(obj, "min-value"); 731 1.56 xtraeme if (obj1) 732 1.97 thorpej sensor->min_value = prop_number_signed_value(obj1); 733 1.25 xtraeme 734 1.25 xtraeme /* get percentage flag */ 735 1.25 xtraeme obj1 = prop_dictionary_get(obj, "want-percentage"); 736 1.56 xtraeme if (obj1) 737 1.59 xtraeme sensor->percentage = prop_bool_true(obj1); 738 1.25 xtraeme 739 1.25 xtraeme /* get critical max value if available */ 740 1.56 xtraeme obj1 = prop_dictionary_get(obj, "critical-max"); 741 1.59 xtraeme if (obj1) 742 1.97 thorpej sensor->critmax_value = prop_number_signed_value(obj1); 743 1.5 cgd 744 1.77 pgoyette /* get maximum capacity value if available */ 745 1.77 pgoyette obj1 = prop_dictionary_get(obj, "maximum-capacity"); 746 1.77 pgoyette if (obj1) 747 1.97 thorpej sensor->critmax_value = prop_number_signed_value(obj1); 748 1.77 pgoyette 749 1.25 xtraeme /* get critical min value if available */ 750 1.56 xtraeme obj1 = prop_dictionary_get(obj, "critical-min"); 751 1.59 xtraeme if (obj1) 752 1.97 thorpej sensor->critmin_value = prop_number_signed_value(obj1); 753 1.1 groo 754 1.25 xtraeme /* get critical capacity value if available */ 755 1.25 xtraeme obj1 = prop_dictionary_get(obj, "critical-capacity"); 756 1.59 xtraeme if (obj1) 757 1.97 thorpej sensor->critmin_value = prop_number_signed_value(obj1); 758 1.25 xtraeme 759 1.70 pgoyette /* get warning max value if available */ 760 1.70 pgoyette obj1 = prop_dictionary_get(obj, "warning-max"); 761 1.70 pgoyette if (obj1) 762 1.97 thorpej sensor->warnmax_value = prop_number_signed_value(obj1); 763 1.70 pgoyette 764 1.77 pgoyette /* get high capacity value if available */ 765 1.77 pgoyette obj1 = prop_dictionary_get(obj, "high-capacity"); 766 1.77 pgoyette if (obj1) 767 1.97 thorpej sensor->warnmax_value = prop_number_signed_value(obj1); 768 1.77 pgoyette 769 1.70 pgoyette /* get warning min value if available */ 770 1.70 pgoyette obj1 = prop_dictionary_get(obj, "warning-min"); 771 1.70 pgoyette if (obj1) 772 1.97 thorpej sensor->warnmin_value = prop_number_signed_value(obj1); 773 1.70 pgoyette 774 1.70 pgoyette /* get warning capacity value if available */ 775 1.70 pgoyette obj1 = prop_dictionary_get(obj, "warning-capacity"); 776 1.70 pgoyette if (obj1) 777 1.97 thorpej sensor->warnmin_value = prop_number_signed_value(obj1); 778 1.70 pgoyette 779 1.25 xtraeme /* print sensor names if -l was given */ 780 1.25 xtraeme if (flags & ENVSYS_LFLAG) { 781 1.25 xtraeme if (width) 782 1.25 xtraeme (void)printf("%*s\n", width, 783 1.97 thorpej prop_string_value(desc)); 784 1.25 xtraeme else 785 1.25 xtraeme (void)printf("%s\n", 786 1.97 thorpej prop_string_value(desc)); 787 1.25 xtraeme } 788 1.59 xtraeme 789 1.59 xtraeme /* Add the sensor into the list */ 790 1.59 xtraeme SIMPLEQ_INSERT_TAIL(&sensors_list, sensor, entries); 791 1.61 xtraeme 792 1.61 xtraeme /* Collect statistics if flag enabled */ 793 1.61 xtraeme if (statistics) { 794 1.69 christos /* ignore sensors not relevant for statistics */ 795 1.69 christos if ((strcmp(sensor->type, "Indicator") == 0) || 796 1.69 christos (strcmp(sensor->type, "Battery charge") == 0) || 797 1.69 christos (strcmp(sensor->type, "Drive") == 0)) 798 1.69 christos continue; 799 1.69 christos 800 1.61 xtraeme /* ignore invalid data */ 801 1.88 pgoyette if (sensor->invalid) 802 1.61 xtraeme continue; 803 1.61 xtraeme 804 1.61 xtraeme /* find or allocate a new statistics sensor */ 805 1.69 christos stats = find_stats_sensor(sensor->desc); 806 1.61 xtraeme if (stats == NULL) { 807 1.61 xtraeme free(sensor); 808 1.61 xtraeme prop_object_iterator_release(iter); 809 1.61 xtraeme return ENOMEM; 810 1.61 xtraeme } 811 1.61 xtraeme 812 1.88 pgoyette /* update data */ 813 1.61 xtraeme if (sensor->cur_value > stats->max) 814 1.61 xtraeme stats->max = sensor->cur_value; 815 1.61 xtraeme 816 1.61 xtraeme if (sensor->cur_value < stats->min) 817 1.61 xtraeme stats->min = sensor->cur_value; 818 1.61 xtraeme 819 1.61 xtraeme /* compute avg value */ 820 1.88 pgoyette stats->avg = 821 1.88 pgoyette (sensor->cur_value + stats->max + stats->min) / 3; 822 1.61 xtraeme } 823 1.25 xtraeme } 824 1.1 groo 825 1.25 xtraeme /* free memory */ 826 1.25 xtraeme prop_object_iterator_release(iter); 827 1.59 xtraeme return 0; 828 1.1 groo } 829 1.1 groo 830 1.25 xtraeme static int 831 1.93 christos check_sensors(const char *str) 832 1.1 groo { 833 1.59 xtraeme sensor_t sensor = NULL; 834 1.93 christos char *dvstring, *sstring, *p, *last, *s; 835 1.59 xtraeme bool sensor_found = false; 836 1.25 xtraeme 837 1.93 christos if ((s = strdup(str)) == NULL) 838 1.93 christos return errno; 839 1.93 christos 840 1.59 xtraeme /* 841 1.59 xtraeme * Parse device name and sensor description and find out 842 1.59 xtraeme * if the sensor is valid. 843 1.59 xtraeme */ 844 1.93 christos for ((p = strtok_r(s, ",", &last)); p; 845 1.59 xtraeme (p = strtok_r(NULL, ",", &last))) { 846 1.59 xtraeme /* get device name */ 847 1.59 xtraeme dvstring = strtok(p, ":"); 848 1.59 xtraeme if (dvstring == NULL) { 849 1.59 xtraeme warnx("missing device name"); 850 1.93 christos goto out; 851 1.59 xtraeme } 852 1.59 xtraeme 853 1.59 xtraeme /* get sensor description */ 854 1.59 xtraeme sstring = strtok(NULL, ":"); 855 1.59 xtraeme if (sstring == NULL) { 856 1.59 xtraeme warnx("missing sensor description"); 857 1.93 christos goto out; 858 1.59 xtraeme } 859 1.59 xtraeme 860 1.59 xtraeme SIMPLEQ_FOREACH(sensor, &sensors_list, entries) { 861 1.59 xtraeme /* skip until we match device */ 862 1.59 xtraeme if (strcmp(dvstring, sensor->dvname)) 863 1.59 xtraeme continue; 864 1.59 xtraeme if (strcmp(sstring, sensor->desc) == 0) { 865 1.59 xtraeme sensor->visible = true; 866 1.59 xtraeme sensor_found = true; 867 1.25 xtraeme break; 868 1.25 xtraeme } 869 1.25 xtraeme } 870 1.59 xtraeme if (sensor_found == false) { 871 1.59 xtraeme warnx("unknown sensor `%s' for device `%s'", 872 1.59 xtraeme sstring, dvstring); 873 1.93 christos goto out; 874 1.25 xtraeme } 875 1.59 xtraeme sensor_found = false; 876 1.25 xtraeme } 877 1.1 groo 878 1.25 xtraeme /* check if all sensors were ok, and error out if not */ 879 1.59 xtraeme SIMPLEQ_FOREACH(sensor, &sensors_list, entries) 880 1.93 christos if (sensor->visible) { 881 1.93 christos free(s); 882 1.25 xtraeme return 0; 883 1.93 christos } 884 1.25 xtraeme 885 1.25 xtraeme warnx("no sensors selected to display"); 886 1.93 christos out: 887 1.93 christos free(s); 888 1.25 xtraeme return EINVAL; 889 1.1 groo } 890 1.1 groo 891 1.105 brad /* When adding a new sensor type, be sure to address both 892 1.105 brad * print_sensors() and print_sensors_json() 893 1.105 brad */ 894 1.105 brad 895 1.25 xtraeme static void 896 1.59 xtraeme print_sensors(void) 897 1.1 groo { 898 1.59 xtraeme sensor_t sensor; 899 1.61 xtraeme sensor_stats_t stats = NULL; 900 1.73 pgoyette size_t maxlen = 0, ilen; 901 1.25 xtraeme double temp = 0; 902 1.61 xtraeme const char *invalid = "N/A", *degrees, *tmpstr, *stype; 903 1.73 pgoyette const char *a, *b, *c, *d, *e, *units; 904 1.100 mlelstv const char *sep; 905 1.100 mlelstv int flen; 906 1.100 mlelstv bool nflag = (flags & ENVSYS_NFLAG) != 0; 907 1.105 brad char tbuf[26]; 908 1.105 brad bool tflag = (flags & ENVSYS_tFLAG) != 0; 909 1.61 xtraeme 910 1.73 pgoyette tmpstr = stype = d = e = NULL; 911 1.1 groo 912 1.25 xtraeme /* find the longest description */ 913 1.59 xtraeme SIMPLEQ_FOREACH(sensor, &sensors_list, entries) 914 1.59 xtraeme if (strlen(sensor->desc) > maxlen) 915 1.59 xtraeme maxlen = strlen(sensor->desc); 916 1.1 groo 917 1.25 xtraeme if (width) 918 1.25 xtraeme maxlen = width; 919 1.1 groo 920 1.79 jruoho /* 921 1.61 xtraeme * Print a header at the bottom only once showing different 922 1.61 xtraeme * members if the statistics flag is set or not. 923 1.61 xtraeme * 924 1.61 xtraeme * As bonus if -s is set, only print this header every 10 iterations 925 1.61 xtraeme * to avoid redundancy... like vmstat(1). 926 1.61 xtraeme */ 927 1.79 jruoho 928 1.63 xtraeme a = "Current"; 929 1.61 xtraeme units = "Unit"; 930 1.61 xtraeme if (statistics) { 931 1.61 xtraeme b = "Max"; 932 1.61 xtraeme c = "Min"; 933 1.61 xtraeme d = "Avg"; 934 1.61 xtraeme } else { 935 1.61 xtraeme b = "CritMax"; 936 1.73 pgoyette c = "WarnMax"; 937 1.73 pgoyette d = "WarnMin"; 938 1.73 pgoyette e = "CritMin"; 939 1.61 xtraeme } 940 1.61 xtraeme 941 1.105 brad if (tflag) { 942 1.105 brad if (ctime_r(×tamp, tbuf) != NULL) 943 1.105 brad (void)printf("%s\n",tbuf); 944 1.105 brad } 945 1.105 brad 946 1.100 mlelstv if (!nflag) { 947 1.100 mlelstv if (!sensors || (!header_passes && sensors) || 948 1.100 mlelstv (header_passes == 10 && sensors)) { 949 1.100 mlelstv if (statistics) 950 1.100 mlelstv (void)printf("%s%*s %9s %8s %8s %8s %6s\n", 951 1.100 mlelstv mydevname ? "" : " ", (int)maxlen, 952 1.100 mlelstv "", a, b, c, d, units); 953 1.100 mlelstv else 954 1.100 mlelstv (void)printf("%s%*s %9s %8s %8s %8s %8s %5s\n", 955 1.100 mlelstv mydevname ? "" : " ", (int)maxlen, 956 1.100 mlelstv "", a, b, c, d, e, units); 957 1.100 mlelstv if (sensors && header_passes == 10) 958 1.100 mlelstv header_passes = 0; 959 1.100 mlelstv } 960 1.100 mlelstv if (sensors) 961 1.100 mlelstv header_passes++; 962 1.100 mlelstv 963 1.100 mlelstv sep = ":"; 964 1.100 mlelstv flen = 10; 965 1.100 mlelstv } else { 966 1.100 mlelstv sep = ""; 967 1.100 mlelstv flen = 1; 968 1.61 xtraeme } 969 1.61 xtraeme 970 1.25 xtraeme /* print the sensors */ 971 1.59 xtraeme SIMPLEQ_FOREACH(sensor, &sensors_list, entries) { 972 1.59 xtraeme /* skip sensors that were not marked as visible */ 973 1.59 xtraeme if (sensors && !sensor->visible) 974 1.54 xtraeme continue; 975 1.1 groo 976 1.59 xtraeme /* skip invalid sensors if -I is set */ 977 1.59 xtraeme if ((flags & ENVSYS_IFLAG) && sensor->invalid) 978 1.25 xtraeme continue; 979 1.1 groo 980 1.59 xtraeme /* print device name */ 981 1.100 mlelstv if (!nflag && !mydevname) { 982 1.59 xtraeme if (tmpstr == NULL || strcmp(tmpstr, sensor->dvname)) 983 1.59 xtraeme printf("[%s]\n", sensor->dvname); 984 1.59 xtraeme 985 1.59 xtraeme tmpstr = sensor->dvname; 986 1.59 xtraeme } 987 1.45 xtraeme 988 1.69 christos /* find out the statistics sensor */ 989 1.69 christos if (statistics) { 990 1.69 christos stats = find_stats_sensor(sensor->desc); 991 1.69 christos if (stats == NULL) { 992 1.69 christos /* No statistics for this sensor */ 993 1.69 christos continue; 994 1.69 christos } 995 1.69 christos } 996 1.69 christos 997 1.100 mlelstv if (!nflag) { 998 1.100 mlelstv /* print sensor description */ 999 1.100 mlelstv (void)printf("%s%*.*s", mydevname ? "" : " ", 1000 1.100 mlelstv (int)maxlen, 1001 1.100 mlelstv (int)maxlen, sensor->desc); 1002 1.100 mlelstv } 1003 1.1 groo 1004 1.59 xtraeme /* print invalid string */ 1005 1.59 xtraeme if (sensor->invalid) { 1006 1.100 mlelstv (void)printf("%s%*s\n", sep, flen, invalid); 1007 1.29 xtraeme continue; 1008 1.29 xtraeme } 1009 1.45 xtraeme 1010 1.57 xtraeme /* 1011 1.57 xtraeme * Indicator and Battery charge sensors. 1012 1.57 xtraeme */ 1013 1.59 xtraeme if ((strcmp(sensor->type, "Indicator") == 0) || 1014 1.59 xtraeme (strcmp(sensor->type, "Battery charge") == 0)) { 1015 1.29 xtraeme 1016 1.100 mlelstv (void)printf("%s%*s", sep, flen, 1017 1.100 mlelstv sensor->cur_value ? "TRUE" : "FALSE"); 1018 1.1 groo 1019 1.73 pgoyette /* convert and print a temp value in degC, degF, or Kelvin */ 1020 1.73 pgoyette #define PRINTTEMP(a) \ 1021 1.25 xtraeme do { \ 1022 1.73 pgoyette if (a) { \ 1023 1.73 pgoyette temp = ((a) / 1000000.0); \ 1024 1.73 pgoyette if (flags & ENVSYS_FFLAG) { \ 1025 1.73 pgoyette temp = temp * (9.0 / 5.0) - 459.67; \ 1026 1.73 pgoyette degrees = "degF"; \ 1027 1.73 pgoyette } else if (flags & ENVSYS_KFLAG) { \ 1028 1.73 pgoyette degrees = "K"; \ 1029 1.73 pgoyette } else { \ 1030 1.73 pgoyette temp = temp - 273.15; \ 1031 1.73 pgoyette degrees = "degC"; \ 1032 1.73 pgoyette } \ 1033 1.100 mlelstv (void)printf("%*.3f", (int)ilen, temp); \ 1034 1.100 mlelstv ilen = 9; \ 1035 1.73 pgoyette } else \ 1036 1.73 pgoyette ilen += 9; \ 1037 1.101 rillig } while (0) 1038 1.25 xtraeme 1039 1.25 xtraeme /* temperatures */ 1040 1.59 xtraeme } else if (strcmp(sensor->type, "Temperature") == 0) { 1041 1.25 xtraeme 1042 1.100 mlelstv ilen = nflag ? 1 : 10; 1043 1.73 pgoyette degrees = ""; 1044 1.100 mlelstv (void)printf("%s",sep); 1045 1.73 pgoyette PRINTTEMP(sensor->cur_value); 1046 1.61 xtraeme stype = degrees; 1047 1.25 xtraeme 1048 1.61 xtraeme if (statistics) { 1049 1.61 xtraeme /* show statistics if flag set */ 1050 1.73 pgoyette PRINTTEMP(stats->max); 1051 1.73 pgoyette PRINTTEMP(stats->min); 1052 1.73 pgoyette PRINTTEMP(stats->avg); 1053 1.75 cnst ilen += 2; 1054 1.100 mlelstv } else if (!nflag) { 1055 1.73 pgoyette PRINTTEMP(sensor->critmax_value); 1056 1.73 pgoyette PRINTTEMP(sensor->warnmax_value); 1057 1.73 pgoyette PRINTTEMP(sensor->warnmin_value); 1058 1.74 cnst PRINTTEMP(sensor->critmin_value); 1059 1.25 xtraeme } 1060 1.100 mlelstv if (!nflag) 1061 1.100 mlelstv (void)printf("%*s", (int)ilen - 3, stype); 1062 1.73 pgoyette #undef PRINTTEMP 1063 1.25 xtraeme 1064 1.25 xtraeme /* fans */ 1065 1.59 xtraeme } else if (strcmp(sensor->type, "Fan") == 0) { 1066 1.61 xtraeme stype = "RPM"; 1067 1.25 xtraeme 1068 1.100 mlelstv (void)printf("%s%*u", sep, flen, sensor->cur_value); 1069 1.73 pgoyette 1070 1.73 pgoyette ilen = 8; 1071 1.61 xtraeme if (statistics) { 1072 1.61 xtraeme /* show statistics if flag set */ 1073 1.100 mlelstv (void)printf(" %8u %8u %8u", 1074 1.61 xtraeme stats->max, stats->min, stats->avg); 1075 1.75 cnst ilen += 2; 1076 1.100 mlelstv } else if (!nflag) { 1077 1.73 pgoyette if (sensor->critmax_value) { 1078 1.100 mlelstv (void)printf(" %*u", (int)ilen, 1079 1.73 pgoyette sensor->critmax_value); 1080 1.73 pgoyette ilen = 8; 1081 1.73 pgoyette } else 1082 1.73 pgoyette ilen += 9; 1083 1.73 pgoyette 1084 1.70 pgoyette if (sensor->warnmax_value) { 1085 1.100 mlelstv (void)printf(" %*u", (int)ilen, 1086 1.70 pgoyette sensor->warnmax_value); 1087 1.73 pgoyette ilen = 8; 1088 1.73 pgoyette } else 1089 1.73 pgoyette ilen += 9; 1090 1.70 pgoyette 1091 1.70 pgoyette if (sensor->warnmin_value) { 1092 1.100 mlelstv (void)printf(" %*u", (int)ilen, 1093 1.70 pgoyette sensor->warnmin_value); 1094 1.73 pgoyette ilen = 8; 1095 1.73 pgoyette } else 1096 1.73 pgoyette ilen += 9; 1097 1.61 xtraeme 1098 1.61 xtraeme if (sensor->critmin_value) { 1099 1.100 mlelstv (void)printf( " %*u", (int)ilen, 1100 1.78 mlelstv sensor->critmin_value); 1101 1.73 pgoyette ilen = 8; 1102 1.73 pgoyette } else 1103 1.73 pgoyette ilen += 9; 1104 1.73 pgoyette 1105 1.61 xtraeme } 1106 1.25 xtraeme 1107 1.100 mlelstv if (!nflag) 1108 1.100 mlelstv (void)printf(" %*s", (int)ilen - 3, stype); 1109 1.25 xtraeme 1110 1.25 xtraeme /* integers */ 1111 1.59 xtraeme } else if (strcmp(sensor->type, "Integer") == 0) { 1112 1.25 xtraeme 1113 1.80 pgoyette stype = "none"; 1114 1.80 pgoyette 1115 1.100 mlelstv (void)printf("%s%*d", sep, flen, sensor->cur_value); 1116 1.80 pgoyette 1117 1.80 pgoyette ilen = 8; 1118 1.80 pgoyette 1119 1.87 pgoyette /* Print percentage of max_value */ 1120 1.87 pgoyette #define PRINTPCT(a) \ 1121 1.87 pgoyette do { \ 1122 1.87 pgoyette if (sensor->max_value) { \ 1123 1.100 mlelstv (void)printf(" %*.3f%%", (int)ilen, \ 1124 1.87 pgoyette ((a) * 100.0) / sensor->max_value); \ 1125 1.87 pgoyette ilen = 8; \ 1126 1.87 pgoyette } else \ 1127 1.87 pgoyette ilen += 9; \ 1128 1.87 pgoyette } while ( /* CONSTCOND*/ 0 ) 1129 1.80 pgoyette 1130 1.87 pgoyette /* Print an integer sensor value */ 1131 1.87 pgoyette #define PRINTINT(a) \ 1132 1.87 pgoyette do { \ 1133 1.100 mlelstv (void)printf(" %*u", (int)ilen, (a)); \ 1134 1.87 pgoyette ilen = 8; \ 1135 1.87 pgoyette } while ( /* CONSTCOND*/ 0 ) 1136 1.80 pgoyette 1137 1.100 mlelstv if (statistics) { 1138 1.100 mlelstv if (sensor->percentage) { 1139 1.100 mlelstv PRINTPCT(stats->max); 1140 1.100 mlelstv PRINTPCT(stats->min); 1141 1.100 mlelstv PRINTPCT(stats->avg); 1142 1.100 mlelstv } else { 1143 1.100 mlelstv PRINTINT(stats->max); 1144 1.100 mlelstv PRINTINT(stats->min); 1145 1.100 mlelstv PRINTINT(stats->avg); 1146 1.100 mlelstv } 1147 1.100 mlelstv ilen += 2; 1148 1.100 mlelstv } else if (!nflag) { 1149 1.87 pgoyette if (sensor->percentage) { 1150 1.87 pgoyette PRINTPCT(sensor->critmax_value); 1151 1.87 pgoyette PRINTPCT(sensor->warnmax_value); 1152 1.87 pgoyette PRINTPCT(sensor->warnmin_value); 1153 1.87 pgoyette PRINTPCT(sensor->critmin_value); 1154 1.87 pgoyette } else { 1155 1.87 pgoyette PRINTINT(sensor->critmax_value); 1156 1.87 pgoyette PRINTINT(sensor->warnmax_value); 1157 1.87 pgoyette PRINTINT(sensor->warnmin_value); 1158 1.87 pgoyette PRINTINT(sensor->critmin_value); 1159 1.87 pgoyette } 1160 1.80 pgoyette } 1161 1.80 pgoyette 1162 1.100 mlelstv if (!nflag) 1163 1.100 mlelstv (void)printf("%*s", (int)ilen - 3, stype); 1164 1.25 xtraeme 1165 1.87 pgoyette #undef PRINTINT 1166 1.87 pgoyette #undef PRINTPCT 1167 1.87 pgoyette 1168 1.46 xtraeme /* drives */ 1169 1.59 xtraeme } else if (strcmp(sensor->type, "Drive") == 0) { 1170 1.27 xtraeme 1171 1.100 mlelstv (void)printf("%s%*s", sep, flen, sensor->drvstate); 1172 1.25 xtraeme 1173 1.57 xtraeme /* Battery capacity */ 1174 1.59 xtraeme } else if (strcmp(sensor->type, "Battery capacity") == 0) { 1175 1.46 xtraeme 1176 1.100 mlelstv (void)printf("%s%*s", sep, flen, sensor->battcap); 1177 1.46 xtraeme 1178 1.96 thorpej /* Illuminance */ 1179 1.96 thorpej } else if (strcmp(sensor->type, "Illuminance") == 0) { 1180 1.96 thorpej 1181 1.96 thorpej stype = "lux"; 1182 1.96 thorpej 1183 1.100 mlelstv (void)printf("%s%*u", sep, flen, sensor->cur_value); 1184 1.96 thorpej 1185 1.96 thorpej ilen = 8; 1186 1.96 thorpej if (statistics) { 1187 1.96 thorpej /* show statistics if flag set */ 1188 1.100 mlelstv (void)printf(" %8u %8u %8u", 1189 1.96 thorpej stats->max, stats->min, stats->avg); 1190 1.96 thorpej ilen += 2; 1191 1.100 mlelstv } else if (!nflag) { 1192 1.96 thorpej if (sensor->critmax_value) { 1193 1.100 mlelstv (void)printf(" %*u", (int)ilen, 1194 1.96 thorpej sensor->critmax_value); 1195 1.96 thorpej ilen = 8; 1196 1.96 thorpej } else 1197 1.96 thorpej ilen += 9; 1198 1.96 thorpej 1199 1.96 thorpej if (sensor->warnmax_value) { 1200 1.100 mlelstv (void)printf(" %*u", (int)ilen, 1201 1.96 thorpej sensor->warnmax_value); 1202 1.96 thorpej ilen = 8; 1203 1.96 thorpej } else 1204 1.96 thorpej ilen += 9; 1205 1.96 thorpej 1206 1.96 thorpej if (sensor->warnmin_value) { 1207 1.100 mlelstv (void)printf(" %*u", (int)ilen, 1208 1.96 thorpej sensor->warnmin_value); 1209 1.96 thorpej ilen = 8; 1210 1.96 thorpej } else 1211 1.96 thorpej ilen += 9; 1212 1.96 thorpej 1213 1.96 thorpej if (sensor->critmin_value) { 1214 1.100 mlelstv (void)printf( " %*u", (int)ilen, 1215 1.96 thorpej sensor->critmin_value); 1216 1.96 thorpej ilen = 8; 1217 1.96 thorpej } else 1218 1.96 thorpej ilen += 9; 1219 1.96 thorpej 1220 1.96 thorpej } 1221 1.96 thorpej 1222 1.100 mlelstv if (!nflag) 1223 1.100 mlelstv (void)printf(" %*s", (int)ilen - 3, stype); 1224 1.96 thorpej 1225 1.103 brad /* Pressure */ 1226 1.103 brad } else if (strcmp(sensor->type, "pressure") == 0) { 1227 1.103 brad stype = "hPa"; 1228 1.103 brad 1229 1.103 brad (void)printf("%s%*.3f", sep, flen, 1230 1.103 brad sensor->cur_value / 10000.0); 1231 1.103 brad 1232 1.103 brad ilen = 8; 1233 1.103 brad if (statistics) { 1234 1.103 brad /* show statistics if flag set */ 1235 1.103 brad (void)printf(" %.3f %.3f %.3f", 1236 1.103 brad stats->max / 10000.0, stats->min / 10000.0, stats->avg / 10000.0); 1237 1.103 brad ilen += 2; 1238 1.103 brad } else if (!nflag) { 1239 1.103 brad if (sensor->critmax_value) { 1240 1.103 brad (void)printf(" %*u", (int)ilen, 1241 1.103 brad sensor->critmax_value); 1242 1.103 brad ilen = 8; 1243 1.103 brad } else 1244 1.103 brad ilen += 9; 1245 1.103 brad 1246 1.103 brad if (sensor->warnmax_value) { 1247 1.103 brad (void)printf(" %*u", (int)ilen, 1248 1.103 brad sensor->warnmax_value); 1249 1.103 brad ilen = 8; 1250 1.103 brad } else 1251 1.103 brad ilen += 9; 1252 1.103 brad 1253 1.103 brad if (sensor->warnmin_value) { 1254 1.103 brad (void)printf(" %*u", (int)ilen, 1255 1.103 brad sensor->warnmin_value); 1256 1.103 brad ilen = 8; 1257 1.103 brad } else 1258 1.103 brad ilen += 9; 1259 1.103 brad 1260 1.103 brad if (sensor->critmin_value) { 1261 1.103 brad (void)printf( " %*u", (int)ilen, 1262 1.103 brad sensor->critmin_value); 1263 1.103 brad ilen = 8; 1264 1.103 brad } else 1265 1.103 brad ilen += 9; 1266 1.103 brad 1267 1.103 brad } 1268 1.103 brad 1269 1.103 brad if (!nflag) 1270 1.103 brad (void)printf(" %*s", (int)ilen - 3, stype); 1271 1.103 brad 1272 1.25 xtraeme /* everything else */ 1273 1.25 xtraeme } else { 1274 1.59 xtraeme if (strcmp(sensor->type, "Voltage DC") == 0) 1275 1.61 xtraeme stype = "V"; 1276 1.59 xtraeme else if (strcmp(sensor->type, "Voltage AC") == 0) 1277 1.61 xtraeme stype = "VAC"; 1278 1.59 xtraeme else if (strcmp(sensor->type, "Ampere") == 0) 1279 1.61 xtraeme stype = "A"; 1280 1.59 xtraeme else if (strcmp(sensor->type, "Watts") == 0) 1281 1.61 xtraeme stype = "W"; 1282 1.59 xtraeme else if (strcmp(sensor->type, "Ohms") == 0) 1283 1.61 xtraeme stype = "Ohms"; 1284 1.59 xtraeme else if (strcmp(sensor->type, "Watt hour") == 0) 1285 1.61 xtraeme stype = "Wh"; 1286 1.59 xtraeme else if (strcmp(sensor->type, "Ampere hour") == 0) 1287 1.61 xtraeme stype = "Ah"; 1288 1.95 kardel else if (strcmp(sensor->type, "relative Humidity") == 0) 1289 1.95 kardel stype = "%rH"; 1290 1.87 pgoyette else 1291 1.87 pgoyette stype = "?"; 1292 1.61 xtraeme 1293 1.100 mlelstv (void)printf("%s%*.3f", sep, flen, 1294 1.61 xtraeme sensor->cur_value / 1000000.0); 1295 1.61 xtraeme 1296 1.100 mlelstv ilen = 9; 1297 1.73 pgoyette 1298 1.73 pgoyette /* Print percentage of max_value */ 1299 1.73 pgoyette #define PRINTPCT(a) \ 1300 1.73 pgoyette do { \ 1301 1.87 pgoyette if ((a) && sensor->max_value) { \ 1302 1.73 pgoyette (void)printf("%*.3f%%", (int)ilen, \ 1303 1.87 pgoyette ((a) * 100.0) / sensor->max_value); \ 1304 1.73 pgoyette ilen = 8; \ 1305 1.73 pgoyette } else \ 1306 1.73 pgoyette ilen += 9; \ 1307 1.73 pgoyette } while ( /* CONSTCOND*/ 0 ) 1308 1.73 pgoyette 1309 1.73 pgoyette /* Print a generic sensor value */ 1310 1.73 pgoyette #define PRINTVAL(a) \ 1311 1.73 pgoyette do { \ 1312 1.87 pgoyette if ((a)) { \ 1313 1.100 mlelstv (void)printf("%*.3f", (int)ilen, (a) / 1000000.0); \ 1314 1.100 mlelstv ilen = 9; \ 1315 1.73 pgoyette } else \ 1316 1.73 pgoyette ilen += 9; \ 1317 1.73 pgoyette } while ( /* CONSTCOND*/ 0 ) 1318 1.73 pgoyette 1319 1.100 mlelstv if (statistics) { 1320 1.100 mlelstv if (sensor->percentage) { 1321 1.100 mlelstv PRINTPCT(stats->max); 1322 1.100 mlelstv PRINTPCT(stats->min); 1323 1.100 mlelstv PRINTPCT(stats->avg); 1324 1.100 mlelstv } else { 1325 1.100 mlelstv PRINTVAL(stats->max); 1326 1.100 mlelstv PRINTVAL(stats->min); 1327 1.100 mlelstv PRINTVAL(stats->avg); 1328 1.100 mlelstv } 1329 1.100 mlelstv ilen += 2; 1330 1.100 mlelstv } else if (!nflag) { 1331 1.87 pgoyette if (sensor->percentage) { 1332 1.87 pgoyette PRINTPCT(sensor->critmax_value); 1333 1.87 pgoyette PRINTPCT(sensor->warnmax_value); 1334 1.87 pgoyette PRINTPCT(sensor->warnmin_value); 1335 1.87 pgoyette PRINTPCT(sensor->critmin_value); 1336 1.87 pgoyette } else { 1337 1.73 pgoyette 1338 1.87 pgoyette PRINTVAL(sensor->critmax_value); 1339 1.87 pgoyette PRINTVAL(sensor->warnmax_value); 1340 1.87 pgoyette PRINTVAL(sensor->warnmin_value); 1341 1.87 pgoyette PRINTVAL(sensor->critmin_value); 1342 1.87 pgoyette } 1343 1.61 xtraeme } 1344 1.87 pgoyette #undef PRINTPCT 1345 1.87 pgoyette #undef PRINTVAL 1346 1.29 xtraeme 1347 1.100 mlelstv if (!nflag) { 1348 1.100 mlelstv (void)printf(" %*s", (int)ilen - 4, stype); 1349 1.100 mlelstv if (sensor->percentage && sensor->max_value) { 1350 1.100 mlelstv (void)printf(" (%5.2f%%)", 1351 1.100 mlelstv (sensor->cur_value * 100.0) / 1352 1.100 mlelstv sensor->max_value); 1353 1.100 mlelstv } 1354 1.25 xtraeme } 1355 1.25 xtraeme } 1356 1.25 xtraeme (void)printf("\n"); 1357 1.1 groo } 1358 1.25 xtraeme } 1359 1.1 groo 1360 1.105 brad static void 1361 1.105 brad print_sensors_json(void) 1362 1.105 brad { 1363 1.105 brad sensor_t sensor; 1364 1.105 brad sensor_stats_t stats = NULL; 1365 1.105 brad double temp = 0; 1366 1.105 brad 1367 1.105 brad const char *degrees, *tmpstr, *stype; 1368 1.105 brad const char *d, *e; 1369 1.105 brad mj_t envstatj; 1370 1.105 brad mj_t devices; 1371 1.105 brad mj_t sensors_per_dev; 1372 1.105 brad mj_t this_sensor; 1373 1.105 brad mj_t a_float; 1374 1.105 brad bool tflag = (flags & ENVSYS_tFLAG) != 0; 1375 1.105 brad char tbuf[26]; 1376 1.105 brad char *output_s = NULL; 1377 1.105 brad size_t output_size = 0; 1378 1.105 brad 1379 1.105 brad tmpstr = stype = d = e = NULL; 1380 1.105 brad 1381 1.105 brad memset(&envstatj, 0x0, sizeof(envstatj)); 1382 1.105 brad mj_create(&envstatj, "object"); 1383 1.105 brad memset(&devices, 0x0, sizeof(devices)); 1384 1.105 brad mj_create(&devices, "object"); 1385 1.105 brad 1386 1.105 brad /* print the sensors */ 1387 1.105 brad SIMPLEQ_FOREACH(sensor, &sensors_list, entries) { 1388 1.105 brad /* completely skip sensors that were not marked as visible */ 1389 1.105 brad if (sensors && !sensor->visible) 1390 1.105 brad continue; 1391 1.105 brad 1392 1.105 brad /* completely skip invalid sensors if -I is set */ 1393 1.105 brad if ((flags & ENVSYS_IFLAG) && sensor->invalid) 1394 1.105 brad continue; 1395 1.105 brad 1396 1.105 brad /* find out the statistics sensor */ 1397 1.105 brad if (statistics) { 1398 1.105 brad stats = find_stats_sensor(sensor->desc); 1399 1.105 brad if (stats == NULL) { 1400 1.105 brad /* No statistics for this sensor, completely skip */ 1401 1.105 brad continue; 1402 1.105 brad } 1403 1.105 brad } 1404 1.105 brad 1405 1.105 brad if (tmpstr == NULL || strcmp(tmpstr, sensor->dvname)) { 1406 1.105 brad if (tmpstr != NULL) { 1407 1.105 brad mj_append_field(&devices, tmpstr, "array", &sensors_per_dev); 1408 1.105 brad mj_delete(&sensors_per_dev); 1409 1.105 brad } 1410 1.105 brad 1411 1.105 brad memset(&sensors_per_dev, 0x0, sizeof(sensors_per_dev)); 1412 1.105 brad mj_create(&sensors_per_dev, "array"); 1413 1.105 brad 1414 1.105 brad tmpstr = sensor->dvname; 1415 1.105 brad } 1416 1.105 brad 1417 1.105 brad /* Any condition that that causes the sensor to be 1418 1.105 brad * completely skipped should be checked before we are 1419 1.105 brad * at this point */ 1420 1.105 brad 1421 1.105 brad memset(&this_sensor, 0x0, sizeof(this_sensor)); 1422 1.105 brad mj_create(&this_sensor, "object"); 1423 1.105 brad 1424 1.105 brad mj_append_field(&this_sensor, "index", "string", 1425 1.105 brad sensor->index, strlen(sensor->index)); 1426 1.105 brad mj_append_field(&this_sensor, "description", "string", 1427 1.105 brad sensor->desc, strlen(sensor->desc)); 1428 1.105 brad mj_append_field(&this_sensor, "type", "string", 1429 1.105 brad sensor->type, strlen(sensor->type)); 1430 1.105 brad mj_append_field(&this_sensor, "invalid", "integer", 1431 1.105 brad (int64_t)sensor->invalid); 1432 1.105 brad 1433 1.105 brad /* The sensor is invalid, semi-skip it */ 1434 1.105 brad if (sensor->invalid) { 1435 1.105 brad goto around; 1436 1.105 brad } 1437 1.105 brad 1438 1.105 brad /* 1439 1.105 brad * Indicator and Battery charge sensors. 1440 1.105 brad */ 1441 1.105 brad if ((strcmp(sensor->type, "Indicator") == 0) || 1442 1.105 brad (strcmp(sensor->type, "Battery charge") == 0)) { 1443 1.105 brad 1444 1.105 brad mj_append_field(&this_sensor, "cur-value", "integer", 1445 1.105 brad (int64_t)sensor->cur_value); 1446 1.105 brad 1447 1.105 brad /* convert and print a temp value in degC, degF, or Kelvin */ 1448 1.105 brad #define PRINTTEMP(a,b) \ 1449 1.105 brad do { \ 1450 1.105 brad if (a) { \ 1451 1.105 brad temp = ((a) / 1000000.0); \ 1452 1.105 brad if (flags & ENVSYS_FFLAG) { \ 1453 1.105 brad temp = temp * (9.0 / 5.0) - 459.67; \ 1454 1.105 brad degrees = "degF"; \ 1455 1.105 brad } else if (flags & ENVSYS_KFLAG) { \ 1456 1.105 brad degrees = "K"; \ 1457 1.105 brad } else { \ 1458 1.105 brad temp = temp - 273.15; \ 1459 1.105 brad degrees = "degC"; \ 1460 1.105 brad } \ 1461 1.105 brad memset(&a_float, 0x0, sizeof(a_float)); \ 1462 1.105 brad mj_create(&a_float, "number", temp); \ 1463 1.105 brad mj_append_field(&this_sensor, b, "object", &a_float); \ 1464 1.105 brad mj_delete(&a_float); \ 1465 1.105 brad } \ 1466 1.105 brad } while (0) 1467 1.105 brad 1468 1.105 brad /* temperatures */ 1469 1.105 brad } else if (strcmp(sensor->type, "Temperature") == 0) { 1470 1.105 brad 1471 1.105 brad degrees = ""; 1472 1.105 brad PRINTTEMP(sensor->cur_value, "cur-value"); 1473 1.105 brad stype = degrees; 1474 1.105 brad 1475 1.105 brad if (statistics) { 1476 1.105 brad /* show statistics if flag set */ 1477 1.105 brad PRINTTEMP(stats->max, "max"); 1478 1.105 brad PRINTTEMP(stats->min, "min"); 1479 1.105 brad PRINTTEMP(stats->avg, "avg"); 1480 1.105 brad } 1481 1.105 brad 1482 1.105 brad PRINTTEMP(sensor->critmax_value, "critical-max"); 1483 1.105 brad PRINTTEMP(sensor->warnmax_value, "warning-max"); 1484 1.105 brad PRINTTEMP(sensor->warnmin_value, "warning-min"); 1485 1.105 brad PRINTTEMP(sensor->critmin_value, "critical-min"); 1486 1.105 brad 1487 1.105 brad mj_append_field(&this_sensor, "units", "string", 1488 1.105 brad stype, strlen(stype)); 1489 1.105 brad #undef PRINTTEMP 1490 1.105 brad 1491 1.105 brad /* fans */ 1492 1.105 brad } else if (strcmp(sensor->type, "Fan") == 0) { 1493 1.105 brad stype = "RPM"; 1494 1.105 brad 1495 1.105 brad mj_append_field(&this_sensor, "cur-value", "integer", 1496 1.105 brad (int64_t)sensor->cur_value); 1497 1.105 brad 1498 1.105 brad if (statistics) { 1499 1.105 brad /* show statistics if flag set */ 1500 1.105 brad mj_append_field(&this_sensor, "max", "integer", 1501 1.105 brad (int64_t)stats->max); 1502 1.105 brad mj_append_field(&this_sensor, "min", "integer", 1503 1.105 brad (int64_t)stats->min); 1504 1.105 brad mj_append_field(&this_sensor, "avg", "integer", 1505 1.105 brad (int64_t)stats->avg); 1506 1.105 brad } 1507 1.105 brad mj_append_field(&this_sensor, "critical-max", "integer", 1508 1.105 brad (int64_t)sensor->critmax_value); 1509 1.105 brad mj_append_field(&this_sensor, "warning-max", "integer", 1510 1.105 brad (int64_t)sensor->warnmax_value); 1511 1.105 brad mj_append_field(&this_sensor, "warning-min", "integer", 1512 1.105 brad (int64_t)sensor->warnmin_value); 1513 1.105 brad mj_append_field(&this_sensor, "critical-min", "integer", 1514 1.105 brad (int64_t)sensor->critmin_value); 1515 1.105 brad 1516 1.105 brad mj_append_field(&this_sensor, "units", "string", 1517 1.105 brad stype, strlen(stype)); 1518 1.105 brad 1519 1.105 brad /* integers */ 1520 1.105 brad } else if (strcmp(sensor->type, "Integer") == 0) { 1521 1.105 brad 1522 1.105 brad stype = "none"; 1523 1.105 brad 1524 1.105 brad mj_append_field(&this_sensor, "cur-value", "integer", 1525 1.105 brad (int64_t)sensor->cur_value); 1526 1.105 brad 1527 1.105 brad /* Print percentage of max_value */ 1528 1.105 brad #define PRINTPCT(a,b) \ 1529 1.105 brad do { \ 1530 1.105 brad if ((a) && sensor->max_value) { \ 1531 1.105 brad mj_append_field(&this_sensor, b, "integer", \ 1532 1.105 brad (int64_t)((a) * 100.0) / sensor->max_value); \ 1533 1.105 brad } \ 1534 1.105 brad } while ( /* CONSTCOND*/ 0 ) 1535 1.105 brad if (statistics) { 1536 1.105 brad if (sensor->percentage) { 1537 1.105 brad PRINTPCT(stats->max, "max"); 1538 1.105 brad PRINTPCT(stats->min, "min"); 1539 1.105 brad PRINTPCT(stats->avg, "avg"); 1540 1.105 brad } else { 1541 1.105 brad mj_append_field(&this_sensor, "max", "integer", 1542 1.105 brad (int64_t)stats->max); 1543 1.105 brad mj_append_field(&this_sensor, "min", "integer", 1544 1.105 brad (int64_t)stats->min); 1545 1.105 brad mj_append_field(&this_sensor, "avg", "integer", 1546 1.105 brad (int64_t)stats->avg); 1547 1.105 brad } 1548 1.105 brad } 1549 1.105 brad if (sensor->percentage) { 1550 1.105 brad PRINTPCT(sensor->critmax_value, "critical-max"); 1551 1.105 brad PRINTPCT(sensor->warnmax_value, "warning-max"); 1552 1.105 brad PRINTPCT(sensor->warnmin_value, "warning-min"); 1553 1.105 brad PRINTPCT(sensor->critmin_value, "critical-min"); 1554 1.105 brad } else { 1555 1.105 brad mj_append_field(&this_sensor, "critmax_value", "integer", 1556 1.105 brad (int64_t)sensor->critmax_value); 1557 1.105 brad mj_append_field(&this_sensor, "warning-max", "integer", 1558 1.105 brad (int64_t)sensor->warnmax_value); 1559 1.105 brad mj_append_field(&this_sensor, "warning-min", "integer", 1560 1.105 brad (int64_t)sensor->warnmin_value); 1561 1.105 brad mj_append_field(&this_sensor, "critical-min", "integer", 1562 1.105 brad (int64_t)sensor->critmin_value); 1563 1.105 brad } 1564 1.105 brad 1565 1.105 brad mj_append_field(&this_sensor, "units", "string", 1566 1.105 brad stype, strlen(stype)); 1567 1.105 brad /* drives */ 1568 1.105 brad } else if (strcmp(sensor->type, "Drive") == 0) { 1569 1.105 brad mj_append_field(&this_sensor, "drvstate", "string", 1570 1.105 brad sensor->drvstate, strlen(sensor->drvstate)); 1571 1.105 brad /* Battery capacity */ 1572 1.105 brad } else if (strcmp(sensor->type, "Battery capacity") == 0) { 1573 1.105 brad mj_append_field(&this_sensor, "battcap", "string", 1574 1.105 brad sensor->battcap, strlen(sensor->battcap)); 1575 1.105 brad /* Illuminance */ 1576 1.105 brad } else if (strcmp(sensor->type, "Illuminance") == 0) { 1577 1.105 brad 1578 1.105 brad stype = "lux"; 1579 1.105 brad 1580 1.105 brad mj_append_field(&this_sensor, "cur-value", "integer", 1581 1.105 brad (int64_t)sensor->cur_value); 1582 1.105 brad 1583 1.105 brad if (statistics) { 1584 1.105 brad /* show statistics if flag set */ 1585 1.105 brad mj_append_field(&this_sensor, "max", "integer", 1586 1.105 brad (int64_t)stats->max); 1587 1.105 brad mj_append_field(&this_sensor, "min", "integer", 1588 1.105 brad (int64_t)stats->min); 1589 1.105 brad mj_append_field(&this_sensor, "avg", "integer", 1590 1.105 brad (int64_t)stats->avg); 1591 1.105 brad } 1592 1.105 brad 1593 1.105 brad mj_append_field(&this_sensor, "critmax_value", "integer", 1594 1.105 brad (int64_t)sensor->critmax_value); 1595 1.105 brad mj_append_field(&this_sensor, "warning-max", "integer", 1596 1.105 brad (int64_t)sensor->warnmax_value); 1597 1.105 brad mj_append_field(&this_sensor, "warning-min", "integer", 1598 1.105 brad (int64_t)sensor->warnmin_value); 1599 1.105 brad mj_append_field(&this_sensor, "critical-min", "integer", 1600 1.105 brad (int64_t)sensor->critmin_value); 1601 1.105 brad 1602 1.105 brad mj_append_field(&this_sensor, "units", "string", 1603 1.105 brad stype, strlen(stype)); 1604 1.105 brad /* Pressure */ 1605 1.105 brad } else if (strcmp(sensor->type, "pressure") == 0) { 1606 1.105 brad #define PRINTFLOAT(a,b) \ 1607 1.105 brad do { \ 1608 1.105 brad if (a) { \ 1609 1.105 brad memset(&a_float, 0x0, sizeof(a_float)); \ 1610 1.105 brad mj_create(&a_float, "number", a); \ 1611 1.105 brad mj_append_field(&this_sensor, b, "object", &a_float); \ 1612 1.105 brad mj_delete(&a_float); \ 1613 1.105 brad } \ 1614 1.105 brad } while (0) 1615 1.105 brad stype = "hPa"; 1616 1.105 brad 1617 1.105 brad PRINTFLOAT(sensor->cur_value / 10000.0, "cur-value"); 1618 1.105 brad 1619 1.105 brad if (statistics) { 1620 1.105 brad /* show statistics if flag set */ 1621 1.105 brad PRINTFLOAT(stats->max / 10000.0, "max"); 1622 1.105 brad PRINTFLOAT(stats->min / 10000.0, "min"); 1623 1.105 brad PRINTFLOAT(stats->avg / 10000.0, "avg"); 1624 1.105 brad } 1625 1.105 brad 1626 1.105 brad PRINTFLOAT(sensor->critmax_value, "critmax_value"); 1627 1.105 brad PRINTFLOAT(sensor->warnmax_value, "warning-max"); 1628 1.105 brad PRINTFLOAT(sensor->warnmin_value, "warning-min"); 1629 1.105 brad PRINTFLOAT(sensor->critmin_value, "critical-min"); 1630 1.105 brad 1631 1.105 brad mj_append_field(&this_sensor, "units", "string", 1632 1.105 brad stype, strlen(stype)); 1633 1.105 brad /* everything else */ 1634 1.105 brad } else { 1635 1.105 brad if (strcmp(sensor->type, "Voltage DC") == 0) 1636 1.105 brad stype = "V"; 1637 1.105 brad else if (strcmp(sensor->type, "Voltage AC") == 0) 1638 1.105 brad stype = "VAC"; 1639 1.105 brad else if (strcmp(sensor->type, "Ampere") == 0) 1640 1.105 brad stype = "A"; 1641 1.105 brad else if (strcmp(sensor->type, "Watts") == 0) 1642 1.105 brad stype = "W"; 1643 1.105 brad else if (strcmp(sensor->type, "Ohms") == 0) 1644 1.105 brad stype = "Ohms"; 1645 1.105 brad else if (strcmp(sensor->type, "Watt hour") == 0) 1646 1.105 brad stype = "Wh"; 1647 1.105 brad else if (strcmp(sensor->type, "Ampere hour") == 0) 1648 1.105 brad stype = "Ah"; 1649 1.105 brad else if (strcmp(sensor->type, "relative Humidity") == 0) 1650 1.105 brad stype = "%rH"; 1651 1.105 brad else 1652 1.105 brad stype = "?"; 1653 1.105 brad 1654 1.105 brad PRINTFLOAT(sensor->cur_value / 1000000.0, "cur-value"); 1655 1.105 brad 1656 1.105 brad mj_append_field(&this_sensor, "units", "string", 1657 1.105 brad stype, strlen(stype)); 1658 1.105 brad #undef PRINTFLOAT 1659 1.105 brad 1660 1.105 brad /* Print a generic sensor value */ 1661 1.105 brad #define PRINTVAL(a,b) \ 1662 1.105 brad do { \ 1663 1.105 brad if ((a)) { \ 1664 1.105 brad memset(&a_float, 0x0, sizeof(a_float)); \ 1665 1.105 brad mj_create(&a_float, "number", a / 1000000.0); \ 1666 1.105 brad mj_append_field(&this_sensor, b, "object", &a_float); \ 1667 1.105 brad mj_delete(&a_float); \ 1668 1.105 brad } \ 1669 1.105 brad } while ( /* CONSTCOND*/ 0 ) 1670 1.105 brad 1671 1.105 brad if (statistics) { 1672 1.105 brad if (sensor->percentage) { 1673 1.105 brad PRINTPCT(stats->max, "max"); 1674 1.105 brad PRINTPCT(stats->min, "min"); 1675 1.105 brad PRINTPCT(stats->avg, "avg"); 1676 1.105 brad } else { 1677 1.105 brad PRINTVAL(stats->max, "max"); 1678 1.105 brad PRINTVAL(stats->min, "min"); 1679 1.105 brad PRINTVAL(stats->avg, "avg"); 1680 1.105 brad } 1681 1.105 brad } 1682 1.105 brad if (sensor->percentage) { 1683 1.105 brad PRINTPCT(sensor->critmax_value, "critmax_value"); 1684 1.105 brad PRINTPCT(sensor->warnmax_value, "warning-max"); 1685 1.105 brad PRINTPCT(sensor->warnmin_value, "warning-min"); 1686 1.105 brad PRINTPCT(sensor->critmin_value, "critical-min"); 1687 1.105 brad } else { 1688 1.105 brad 1689 1.105 brad PRINTVAL(sensor->critmax_value, "critmax_value"); 1690 1.105 brad PRINTVAL(sensor->warnmax_value, "warning-max"); 1691 1.105 brad PRINTVAL(sensor->warnmin_value, "warning-min"); 1692 1.105 brad PRINTVAL(sensor->critmin_value, "critical-min"); 1693 1.105 brad } 1694 1.105 brad #undef PRINTPCT 1695 1.105 brad #undef PRINTVAL 1696 1.105 brad 1697 1.105 brad mj_append_field(&this_sensor, "units", "string", 1698 1.105 brad stype, strlen(stype)); 1699 1.105 brad if (sensor->percentage && sensor->max_value) { 1700 1.105 brad memset(&a_float, 0x0, sizeof(a_float)); 1701 1.105 brad mj_create(&a_float, "number", 1702 1.105 brad (sensor->cur_value * 100.0) / sensor->max_value); 1703 1.105 brad mj_append_field(&this_sensor, "pct", "object", 1704 1.105 brad &a_float); 1705 1.105 brad mj_delete(&a_float); 1706 1.105 brad } 1707 1.105 brad } 1708 1.105 brad around: 1709 1.105 brad mj_append(&sensors_per_dev, "object", &this_sensor); 1710 1.105 brad mj_delete(&this_sensor); 1711 1.105 brad } 1712 1.105 brad 1713 1.105 brad if (tmpstr != NULL) { 1714 1.105 brad mj_append_field(&devices, tmpstr, "array", &sensors_per_dev); 1715 1.105 brad mj_delete(&sensors_per_dev); 1716 1.105 brad 1717 1.105 brad mj_append_field(&envstatj, "devices", "object", &devices); 1718 1.105 brad if (tflag) { 1719 1.105 brad mj_append_field(&envstatj, "timestamp", "integer", (int64_t)timestamp); 1720 1.105 brad if (ctime_r(×tamp, tbuf) != NULL) 1721 1.105 brad /* Pull off the newline */ 1722 1.105 brad mj_append_field(&envstatj, "human_timestamp", "string", tbuf, strlen(tbuf) - 1); 1723 1.105 brad } 1724 1.105 brad 1725 1.105 brad #ifdef __NOT 1726 1.105 brad mj_asprint(&output_s, &envstatj, MJ_JSON_ENCODE); 1727 1.105 brad printf("%s", output_s); 1728 1.105 brad if (output_s != NULL) 1729 1.105 brad free(output_s); 1730 1.105 brad #endif 1731 1.105 brad /* Work around lib/59230 for now. Way over allocate 1732 1.105 brad * the output buffer. Summary of the problem: 1733 1.105 brad * mj_string_size does not appear to allocate enough 1734 1.105 brad * space if there are arrays present in the serialized 1735 1.105 brad * superatom. It appears that for every array that is 1736 1.105 brad * placed inside of another object (or probably array) 1737 1.105 brad * the resulting buffer is a single character too 1738 1.105 brad * small. So just do what mj_asprint() does, except 1739 1.105 brad * make the size much larger than is needed. 1740 1.105 brad */ 1741 1.105 brad 1742 1.105 brad output_size = mj_string_size(&envstatj) * 3; 1743 1.105 brad output_s = calloc(1, output_size); 1744 1.105 brad if (output_s != NULL) { 1745 1.105 brad mj_snprint(output_s, output_size, &envstatj, MJ_JSON_ENCODE); 1746 1.105 brad printf("%s", output_s); 1747 1.105 brad free(output_s); 1748 1.105 brad } 1749 1.105 brad } 1750 1.105 brad 1751 1.105 brad mj_delete(&devices); 1752 1.105 brad mj_delete(&envstatj); 1753 1.105 brad } 1754 1.105 brad 1755 1.25 xtraeme static int 1756 1.25 xtraeme usage(void) 1757 1.25 xtraeme { 1758 1.105 brad (void)fprintf(stderr, "Usage: %s [-DfIjklnrSTt] ", getprogname()); 1759 1.56 xtraeme (void)fprintf(stderr, "[-c file] [-d device] [-i interval] "); 1760 1.59 xtraeme (void)fprintf(stderr, "[-s device:sensor,...] [-w width]\n"); 1761 1.99 mlelstv (void)fprintf(stderr, " %s ", getprogname()); 1762 1.99 mlelstv (void)fprintf(stderr, "[-d device] "); 1763 1.99 mlelstv (void)fprintf(stderr, "[-s device:sensor,...] "); 1764 1.99 mlelstv (void)fprintf(stderr, "-x [property]\n"); 1765 1.25 xtraeme exit(EXIT_FAILURE); 1766 1.25 xtraeme /* NOTREACHED */ 1767 1.1 groo } 1768