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