1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
|
/* $NetBSD: envstat.c,v 1.77 2010/02/15 22:37:14 pgoyette Exp $ */
/*-
* Copyright (c) 2007, 2008 Juan Romero Pardines.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: envstat.c,v 1.77 2010/02/15 22:37:14 pgoyette Exp $");
#endif /* not lint */
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <err.h>
#include <errno.h>
#include <syslog.h>
#include <sys/envsys.h>
#include <sys/types.h>
#include <sys/queue.h>
#include <prop/proplib.h>
#include "envstat.h"
#define _PATH_DEV_SYSMON "/dev/sysmon"
#define ENVSYS_DFLAG 0x00000001 /* list registered devices */
#define ENVSYS_FFLAG 0x00000002 /* show temp in farenheit */
#define ENVSYS_LFLAG 0x00000004 /* list sensors */
#define ENVSYS_XFLAG 0x00000008 /* externalize dictionary */
#define ENVSYS_IFLAG 0x00000010 /* skip invalid sensors */
#define ENVSYS_SFLAG 0x00000020 /* remove all properties set */
#define ENVSYS_TFLAG 0x00000040 /* make statistics */
#define ENVSYS_KFLAG 0x00000100 /* show temp in kelvin */
/* Sensors */
typedef struct envsys_sensor {
SIMPLEQ_ENTRY(envsys_sensor) entries;
int32_t cur_value;
int32_t max_value;
int32_t min_value;
int32_t avg_value;
int32_t critmin_value;
int32_t critmax_value;
int32_t warnmin_value;
int32_t warnmax_value;
char desc[ENVSYS_DESCLEN];
char type[ENVSYS_DESCLEN];
char drvstate[ENVSYS_DESCLEN];
char battcap[ENVSYS_DESCLEN];
char dvname[ENVSYS_DESCLEN];
bool invalid;
bool visible;
bool percentage;
} *sensor_t;
/* Sensor statistics */
typedef struct envsys_sensor_stats {
SIMPLEQ_ENTRY(envsys_sensor_stats) entries;
int32_t max;
int32_t min;
int32_t avg;
char desc[ENVSYS_DESCLEN];
} *sensor_stats_t;
/* Device properties */
typedef struct envsys_dvprops {
uint64_t refresh_timo;
/* more members could be added in the future */
} *dvprops_t;
/* A simple queue to manage all sensors */
static SIMPLEQ_HEAD(, envsys_sensor) sensors_list =
SIMPLEQ_HEAD_INITIALIZER(sensors_list);
/* A simple queue to manage statistics for all sensors */
static SIMPLEQ_HEAD(, envsys_sensor_stats) sensor_stats_list =
SIMPLEQ_HEAD_INITIALIZER(sensor_stats_list);
static unsigned int interval, flags, width;
static char *mydevname, *sensors;
static bool statistics;
static u_int header_passes;
static int parse_dictionary(int);
static int send_dictionary(FILE *, int);
static int find_sensors(prop_array_t, const char *, dvprops_t);
static void print_sensors(void);
static int check_sensors(char *);
static int usage(void);
int main(int argc, char **argv)
{
prop_dictionary_t dict;
int c, fd, rval = 0;
char *endptr, *configfile = NULL;
FILE *cf;
setprogname(argv[0]);
while ((c = getopt(argc, argv, "c:Dd:fIi:klrSs:Tw:Wx")) != -1) {
switch (c) {
case 'c': /* configuration file */
configfile = strdup(optarg);
if (configfile == NULL)
err(EXIT_FAILURE, "strdup");
break;
case 'D': /* list registered devices */
flags |= ENVSYS_DFLAG;
break;
case 'd': /* show sensors of a specific device */
mydevname = strdup(optarg);
if (mydevname == NULL)
err(EXIT_FAILURE, "strdup");
break;
case 'f': /* display temperature in Farenheit */
flags |= ENVSYS_FFLAG;
break;
case 'I': /* Skips invalid sensors */
flags |= ENVSYS_IFLAG;
break;
case 'i': /* wait time between intervals */
interval = (unsigned int)strtoul(optarg, &endptr, 10);
if (*endptr != '\0')
errx(EXIT_FAILURE, "bad interval '%s'", optarg);
break;
case 'k': /* display temperature in Kelvin */
flags |= ENVSYS_KFLAG;
break;
case 'l': /* list sensors */
flags |= ENVSYS_LFLAG;
break;
case 'r':
/*
* This flag is noop.. it's only here for
* compatibility with the old implementation.
*/
break;
case 'S':
flags |= ENVSYS_SFLAG;
break;
case 's': /* only show specified sensors */
sensors = strdup(optarg);
if (sensors == NULL)
err(EXIT_FAILURE, "strdup");
break;
case 'T': /* make statistics */
flags |= ENVSYS_TFLAG;
break;
case 'w': /* width value for the lines */
width = (unsigned int)strtoul(optarg, &endptr, 10);
if (*endptr != '\0')
errx(EXIT_FAILURE, "bad width '%s'", optarg);
break;
case 'x': /* print the dictionary in raw format */
flags |= ENVSYS_XFLAG;
break;
case 'W': /* No longer used, retained for campatability */
break;
case '?':
default:
usage();
/* NOTREACHED */
}
}
argc -= optind;
argv += optind;
if (argc > 0)
usage();
/* Check if we want to make statistics */
if (flags & ENVSYS_TFLAG) {
if (!interval)
errx(EXIT_FAILURE,
"-T cannot be used without an interval (-i)");
else
statistics = true;
}
if (mydevname && sensors)
errx(EXIT_FAILURE, "-d flag cannot be used with -s");
/* Open the device in ro mode */
if ((fd = open(_PATH_DEV_SYSMON, O_RDONLY)) == -1)
err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
/* Print dictionary in raw mode */
if (flags & ENVSYS_XFLAG) {
rval = prop_dictionary_recv_ioctl(fd,
ENVSYS_GETDICTIONARY,
&dict);
if (rval)
errx(EXIT_FAILURE, "%s", strerror(rval));
config_dict_dump(dict);
/* Remove all properties set in dictionary */
} else if (flags & ENVSYS_SFLAG) {
/* Close the ro descriptor */
(void)close(fd);
/* open the fd in rw mode */
if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1)
err(EXIT_FAILURE, "%s", _PATH_DEV_SYSMON);
dict = prop_dictionary_create();
if (!dict)
err(EXIT_FAILURE, "prop_dictionary_create");
rval = prop_dictionary_set_bool(dict,
"envsys-remove-props",
true);
if (!rval)
err(EXIT_FAILURE, "prop_dict_set_bool");
/* send the dictionary to the kernel now */
rval = prop_dictionary_send_ioctl(dict, fd, ENVSYS_REMOVEPROPS);
if (rval)
warnx("%s", strerror(rval));
/* Set properties in dictionary */
} else if (configfile) {
/*
* Parse the configuration file.
*/
if ((cf = fopen(configfile, "r")) == NULL) {
syslog(LOG_ERR, "fopen failed: %s", strerror(errno));
errx(EXIT_FAILURE, "%s", strerror(errno));
}
rval = send_dictionary(cf, fd);
(void)fclose(cf);
/* Show sensors with interval */
} else if (interval) {
for (;;) {
rval = parse_dictionary(fd);
if (rval)
break;
(void)fflush(stdout);
(void)sleep(interval);
}
/* Show sensors without interval */
} else {
rval = parse_dictionary(fd);
}
if (sensors)
free(sensors);
if (mydevname)
free(mydevname);
(void)close(fd);
return rval ? EXIT_FAILURE : EXIT_SUCCESS;
}
static int
send_dictionary(FILE *cf, int fd)
{
prop_dictionary_t kdict, udict;
int error = 0;
/* Retrieve dictionary from kernel */
error = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &kdict);
if (error)
return error;
config_parse(cf, kdict);
/*
* Dictionary built by the parser from the configuration file.
*/
udict = config_dict_parsed();
/*
* Close the read only descriptor and open a new one read write.
*/
(void)close(fd);
if ((fd = open(_PATH_DEV_SYSMON, O_RDWR)) == -1) {
error = errno;
warn("%s", _PATH_DEV_SYSMON);
return error;
}
/*
* Send our sensor properties dictionary to the kernel then.
*/
error = prop_dictionary_send_ioctl(udict, fd, ENVSYS_SETDICTIONARY);
if (error)
warnx("%s", strerror(error));
prop_object_release(udict);
return error;
}
static sensor_stats_t
find_stats_sensor(const char *desc)
{
sensor_stats_t stats;
/*
* If we matched a sensor by its description return it, otherwise
* allocate a new one.
*/
SIMPLEQ_FOREACH(stats, &sensor_stats_list, entries)
if (strcmp(stats->desc, desc) == 0)
return stats;
stats = calloc(1, sizeof(*stats));
if (stats == NULL)
return NULL;
(void)strlcpy(stats->desc, desc, sizeof(stats->desc));
SIMPLEQ_INSERT_TAIL(&sensor_stats_list, stats, entries);
return stats;
}
static int
parse_dictionary(int fd)
{
sensor_t sensor = NULL;
dvprops_t edp = NULL;
prop_array_t array;
prop_dictionary_t dict;
prop_object_iterator_t iter;
prop_object_t obj;
const char *dnp = NULL;
int rval = 0;
/* receive dictionary from kernel */
rval = prop_dictionary_recv_ioctl(fd, ENVSYS_GETDICTIONARY, &dict);
if (rval)
return rval;
/* No drivers registered? */
if (prop_dictionary_count(dict) == 0) {
warnx("no drivers registered");
goto out;
}
if (mydevname) {
/* -d flag specified, print sensors only for this device */
obj = prop_dictionary_get(dict, mydevname);
if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
warnx("unknown device `%s'", mydevname);
rval = EINVAL;
goto out;
}
rval = find_sensors(obj, mydevname, NULL);
if (rval)
goto out;
} else {
/* print sensors for all devices registered */
iter = prop_dictionary_iterator(dict);
if (iter == NULL) {
rval = EINVAL;
goto out;
}
/* iterate over the dictionary returned by the kernel */
while ((obj = prop_object_iterator_next(iter)) != NULL) {
array = prop_dictionary_get_keysym(dict, obj);
if (prop_object_type(array) != PROP_TYPE_ARRAY) {
warnx("no sensors found");
rval = EINVAL;
goto out;
}
edp = calloc(1, sizeof(*edp));
if (!edp) {
rval = ENOMEM;
goto out;
}
dnp = prop_dictionary_keysym_cstring_nocopy(obj);
rval = find_sensors(array, dnp, edp);
if (rval)
goto out;
if (((flags & ENVSYS_LFLAG) == 0) &&
(flags & ENVSYS_DFLAG)) {
(void)printf("%s (checking events every ",
dnp);
if (edp->refresh_timo == 1)
(void)printf("second)\n");
else
(void)printf("%d seconds)\n",
(int)edp->refresh_timo);
}
free(edp);
edp = NULL;
}
prop_object_iterator_release(iter);
}
/* print sensors now */
if (sensors) {
char *str = strdup(sensors);
if (!str) {
rval = ENOMEM;
goto out;
}
rval = check_sensors(str);
free(str);
}
if ((flags & ENVSYS_LFLAG) == 0 && (flags & ENVSYS_DFLAG) == 0)
print_sensors();
if (interval)
(void)printf("\n");
out:
while ((sensor = SIMPLEQ_FIRST(&sensors_list))) {
SIMPLEQ_REMOVE_HEAD(&sensors_list, entries);
free(sensor);
}
if (edp)
free(edp);
prop_object_release(dict);
return rval;
}
static int
find_sensors(prop_array_t array, const char *dvname, dvprops_t edp)
{
prop_object_iterator_t iter;
prop_object_t obj, obj1, obj2;
prop_string_t state, desc = NULL;
sensor_t sensor = NULL;
sensor_stats_t stats = NULL;
iter = prop_array_iterator(array);
if (!iter)
return ENOMEM;
/* iterate over the array of dictionaries */
while ((obj = prop_object_iterator_next(iter)) != NULL) {
/* get the refresh-timeout property */
obj2 = prop_dictionary_get(obj, "device-properties");
if (obj2) {
if (!edp)
continue;
if (!prop_dictionary_get_uint64(obj2,
"refresh-timeout",
&edp->refresh_timo))
continue;
}
/* new sensor coming */
sensor = calloc(1, sizeof(*sensor));
if (sensor == NULL) {
prop_object_iterator_release(iter);
return ENOMEM;
}
/* copy device name */
(void)strlcpy(sensor->dvname, dvname, sizeof(sensor->dvname));
/* description string */
desc = prop_dictionary_get(obj, "description");
if (desc) {
/* copy description */
(void)strlcpy(sensor->desc,
prop_string_cstring_nocopy(desc),
sizeof(sensor->desc));
} else {
free(sensor);
continue;
}
/* type string */
obj1 = prop_dictionary_get(obj, "type");
if (obj1) {
/* copy type */
(void)strlcpy(sensor->type,
prop_string_cstring_nocopy(obj1),
sizeof(sensor->type));
} else {
free(sensor);
continue;
}
/* check sensor's state */
state = prop_dictionary_get(obj, "state");
/* mark sensors with invalid/unknown state */
if ((prop_string_equals_cstring(state, "invalid") ||
prop_string_equals_cstring(state, "unknown")))
sensor->invalid = true;
/* get current drive state string */
obj1 = prop_dictionary_get(obj, "drive-state");
if (obj1) {
(void)strlcpy(sensor->drvstate,
prop_string_cstring_nocopy(obj1),
sizeof(sensor->drvstate));
}
/* get current battery capacity string */
obj1 = prop_dictionary_get(obj, "battery-capacity");
if (obj1) {
(void)strlcpy(sensor->battcap,
prop_string_cstring_nocopy(obj1),
sizeof(sensor->battcap));
}
/* get current value */
obj1 = prop_dictionary_get(obj, "cur-value");
if (obj1)
sensor->cur_value = prop_number_integer_value(obj1);
/* get max value */
obj1 = prop_dictionary_get(obj, "max-value");
if (obj1)
sensor->max_value = prop_number_integer_value(obj1);
/* get min value */
obj1 = prop_dictionary_get(obj, "min-value");
if (obj1)
sensor->min_value = prop_number_integer_value(obj1);
/* get avg value */
obj1 = prop_dictionary_get(obj, "avg-value");
if (obj1)
sensor->avg_value = prop_number_integer_value(obj1);
/* get percentage flag */
obj1 = prop_dictionary_get(obj, "want-percentage");
if (obj1)
sensor->percentage = prop_bool_true(obj1);
/* get critical max value if available */
obj1 = prop_dictionary_get(obj, "critical-max");
if (obj1)
sensor->critmax_value = prop_number_integer_value(obj1);
/* get maximum capacity value if available */
obj1 = prop_dictionary_get(obj, "maximum-capacity");
if (obj1)
sensor->critmax_value = prop_number_integer_value(obj1);
/* get critical min value if available */
obj1 = prop_dictionary_get(obj, "critical-min");
if (obj1)
sensor->critmin_value = prop_number_integer_value(obj1);
/* get critical capacity value if available */
obj1 = prop_dictionary_get(obj, "critical-capacity");
if (obj1)
sensor->critmin_value = prop_number_integer_value(obj1);
/* get warning max value if available */
obj1 = prop_dictionary_get(obj, "warning-max");
if (obj1)
sensor->warnmax_value = prop_number_integer_value(obj1);
/* get high capacity value if available */
obj1 = prop_dictionary_get(obj, "high-capacity");
if (obj1)
sensor->warnmax_value = prop_number_integer_value(obj1);
/* get warning min value if available */
obj1 = prop_dictionary_get(obj, "warning-min");
if (obj1)
sensor->warnmin_value = prop_number_integer_value(obj1);
/* get warning capacity value if available */
obj1 = prop_dictionary_get(obj, "warning-capacity");
if (obj1)
sensor->warnmin_value = prop_number_integer_value(obj1);
/* print sensor names if -l was given */
if (flags & ENVSYS_LFLAG) {
if (width)
(void)printf("%*s\n", width,
prop_string_cstring_nocopy(desc));
else
(void)printf("%s\n",
prop_string_cstring_nocopy(desc));
}
/* Add the sensor into the list */
SIMPLEQ_INSERT_TAIL(&sensors_list, sensor, entries);
/* Collect statistics if flag enabled */
if (statistics) {
/* ignore sensors not relevant for statistics */
if ((strcmp(sensor->type, "Indicator") == 0) ||
(strcmp(sensor->type, "Battery charge") == 0) ||
(strcmp(sensor->type, "Drive") == 0))
continue;
/* ignore invalid data */
if (sensor->invalid || !sensor->cur_value)
continue;
/* find or allocate a new statistics sensor */
stats = find_stats_sensor(sensor->desc);
if (stats == NULL) {
free(sensor);
prop_object_iterator_release(iter);
return ENOMEM;
}
/* collect data */
if (!stats->max)
stats->max = sensor->cur_value;
if (!stats->min)
stats->min = sensor->cur_value;
if (sensor->cur_value > stats->max)
stats->max = sensor->cur_value;
if (sensor->cur_value < stats->min)
stats->min = sensor->cur_value;
/* compute avg value */
if (stats->max && stats->min)
stats->avg =
(sensor->cur_value + stats->max +
stats->min) / 3;
}
}
/* free memory */
prop_object_iterator_release(iter);
return 0;
}
static int
check_sensors(char *str)
{
sensor_t sensor = NULL;
char *dvstring, *sstring, *p, *last;
bool sensor_found = false;
/*
* Parse device name and sensor description and find out
* if the sensor is valid.
*/
for ((p = strtok_r(str, ",", &last)); p;
(p = strtok_r(NULL, ",", &last))) {
/* get device name */
dvstring = strtok(p, ":");
if (dvstring == NULL) {
warnx("missing device name");
return EINVAL;
}
/* get sensor description */
sstring = strtok(NULL, ":");
if (sstring == NULL) {
warnx("missing sensor description");
return EINVAL;
}
SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
/* skip until we match device */
if (strcmp(dvstring, sensor->dvname))
continue;
if (strcmp(sstring, sensor->desc) == 0) {
sensor->visible = true;
sensor_found = true;
break;
}
}
if (sensor_found == false) {
warnx("unknown sensor `%s' for device `%s'",
sstring, dvstring);
return EINVAL;
}
sensor_found = false;
}
/* check if all sensors were ok, and error out if not */
SIMPLEQ_FOREACH(sensor, &sensors_list, entries)
if (sensor->visible)
return 0;
warnx("no sensors selected to display");
return EINVAL;
}
static void
print_sensors(void)
{
sensor_t sensor;
sensor_stats_t stats = NULL;
size_t maxlen = 0, ilen;
double temp = 0;
const char *invalid = "N/A", *degrees, *tmpstr, *stype;
const char *a, *b, *c, *d, *e, *units;
tmpstr = stype = d = e = NULL;
/* find the longest description */
SIMPLEQ_FOREACH(sensor, &sensors_list, entries)
if (strlen(sensor->desc) > maxlen)
maxlen = strlen(sensor->desc);
if (width)
maxlen = width;
/*
* Print a header at the bottom only once showing different
* members if the statistics flag is set or not.
*
* As bonus if -s is set, only print this header every 10 iterations
* to avoid redundancy... like vmstat(1).
*/
a = "Current";
units = "Unit";
if (statistics) {
b = "Max";
c = "Min";
d = "Avg";
} else {
b = "CritMax";
c = "WarnMax";
d = "WarnMin";
e = "CritMin";
}
if (!sensors || (!header_passes && sensors) ||
(header_passes == 10 && sensors)) {
if (statistics)
(void)printf("%s%*s %9s %8s %8s %8s %6s\n",
mydevname ? "" : " ", (int)maxlen,
"", a, b, c, d, units);
else
(void)printf("%s%*s %9s %8s %8s %8s %8s %4s\n",
mydevname ? "" : " ", (int)maxlen,
"", a, b, c, d, e, units);
if (sensors && header_passes == 10)
header_passes = 0;
}
if (sensors)
header_passes++;
/* print the sensors */
SIMPLEQ_FOREACH(sensor, &sensors_list, entries) {
/* skip sensors that were not marked as visible */
if (sensors && !sensor->visible)
continue;
/* skip invalid sensors if -I is set */
if ((flags & ENVSYS_IFLAG) && sensor->invalid)
continue;
/* print device name */
if (!mydevname) {
if (tmpstr == NULL || strcmp(tmpstr, sensor->dvname))
printf("[%s]\n", sensor->dvname);
tmpstr = sensor->dvname;
}
/* find out the statistics sensor */
if (statistics) {
stats = find_stats_sensor(sensor->desc);
if (stats == NULL) {
/* No statistics for this sensor */
continue;
}
}
/* print sensor description */
(void)printf("%s%*.*s", mydevname ? "" : " ", (int)maxlen,
(int)maxlen, sensor->desc);
/* print invalid string */
if (sensor->invalid) {
(void)printf(": %9s\n", invalid);
continue;
}
/*
* Indicator and Battery charge sensors.
*/
if ((strcmp(sensor->type, "Indicator") == 0) ||
(strcmp(sensor->type, "Battery charge") == 0)) {
(void)printf(":%10s", sensor->cur_value ? "ON" : "OFF");
/* convert and print a temp value in degC, degF, or Kelvin */
#define PRINTTEMP(a) \
do { \
if (a) { \
temp = ((a) / 1000000.0); \
if (flags & ENVSYS_FFLAG) { \
temp = temp * (9.0 / 5.0) - 459.67; \
degrees = "degF"; \
} else if (flags & ENVSYS_KFLAG) { \
degrees = "K"; \
} else { \
temp = temp - 273.15; \
degrees = "degC"; \
} \
(void)printf("%*.3f ", (int)ilen, temp); \
ilen = 8; \
} else \
ilen += 9; \
} while (/* CONSTCOND */ 0)
/* temperatures */
} else if (strcmp(sensor->type, "Temperature") == 0) {
ilen = 10;
degrees = "";
(void)printf(":");
PRINTTEMP(sensor->cur_value);
stype = degrees;
ilen = 8;
if (statistics) {
/* show statistics if flag set */
PRINTTEMP(stats->max);
PRINTTEMP(stats->min);
PRINTTEMP(stats->avg);
ilen += 2;
} else {
PRINTTEMP(sensor->critmax_value);
PRINTTEMP(sensor->warnmax_value);
PRINTTEMP(sensor->warnmin_value);
PRINTTEMP(sensor->critmin_value);
}
(void)printf("%*s", (int)ilen - 4, stype);
#undef PRINTTEMP
/* fans */
} else if (strcmp(sensor->type, "Fan") == 0) {
stype = "RPM";
(void)printf(":%10u ", sensor->cur_value);
ilen = 8;
if (statistics) {
/* show statistics if flag set */
(void)printf("%8u %8u %8u ",
stats->max, stats->min, stats->avg);
ilen += 2;
} else {
if (sensor->critmax_value) {
(void)printf("%*u ", (int)ilen,
sensor->critmax_value);
ilen = 8;
} else
ilen += 9;
if (sensor->warnmax_value) {
(void)printf("%*u ", (int)ilen,
sensor->warnmax_value);
ilen = 8;
} else
ilen += 9;
if (sensor->warnmin_value) {
(void)printf("%*u ", (int)ilen,
sensor->warnmin_value);
ilen = 8;
} else
ilen += 9;
if (sensor->critmin_value) {
(void)printf( "%*u ", (int)ilen,
sensor->warnmax_value);
ilen = 8;
} else
ilen += 9;
}
(void)printf("%*s", (int)ilen - 4, stype);
/* integers */
} else if (strcmp(sensor->type, "Integer") == 0) {
(void)printf(":%10d", sensor->cur_value);
/* drives */
} else if (strcmp(sensor->type, "Drive") == 0) {
(void)printf(":%10s", sensor->drvstate);
/* Battery capacity */
} else if (strcmp(sensor->type, "Battery capacity") == 0) {
(void)printf(":%10s", sensor->battcap);
/* everything else */
} else {
if (strcmp(sensor->type, "Voltage DC") == 0)
stype = "V";
else if (strcmp(sensor->type, "Voltage AC") == 0)
stype = "VAC";
else if (strcmp(sensor->type, "Ampere") == 0)
stype = "A";
else if (strcmp(sensor->type, "Watts") == 0)
stype = "W";
else if (strcmp(sensor->type, "Ohms") == 0)
stype = "Ohms";
else if (strcmp(sensor->type, "Watt hour") == 0)
stype = "Wh";
else if (strcmp(sensor->type, "Ampere hour") == 0)
stype = "Ah";
(void)printf(":%10.3f ",
sensor->cur_value / 1000000.0);
ilen = 8;
if (!statistics) {
/* Print percentage of max_value */
#define PRINTPCT(a) \
do { \
if (sensor->a && sensor->max_value) { \
(void)printf("%*.3f%%", (int)ilen, \
(sensor->a * 100.0) / sensor->max_value); \
ilen = 8; \
} else \
ilen += 9; \
} while ( /* CONSTCOND*/ 0 )
/* Print a generic sensor value */
#define PRINTVAL(a) \
do { \
if (sensor->a) { \
(void)printf("%*.3f ", (int)ilen, sensor->a / 1000000.0); \
ilen = 8; \
} else \
ilen += 9; \
} while ( /* CONSTCOND*/ 0 )
if (sensor->percentage) {
PRINTPCT(critmax_value);
PRINTPCT(warnmax_value);
PRINTPCT(warnmin_value);
PRINTPCT(critmin_value);
} else {
PRINTVAL(critmax_value);
PRINTVAL(warnmax_value);
PRINTVAL(warnmin_value);
PRINTVAL(critmin_value);
#undef PRINTPCT
#undef PRINTVAL
}
}
if (statistics && !sensor->percentage) {
/* show statistics if flag set */
(void)printf("%8.3f %8.3f %8.3f ",
stats->max / 1000000.0,
stats->min / 1000000.0,
stats->avg / 1000000.0);
ilen += 2;
}
(void)printf("%*s", (int)ilen - 4, stype);
if (sensor->percentage && sensor->max_value) {
(void)printf(" (%5.2f%%)",
(sensor->cur_value * 100.0) /
sensor->max_value);
}
}
(void)printf("\n");
}
}
static int
usage(void)
{
(void)fprintf(stderr, "Usage: %s [-DfIklrSTx] ", getprogname());
(void)fprintf(stderr, "[-c file] [-d device] [-i interval] ");
(void)fprintf(stderr, "[-s device:sensor,...] [-w width]\n");
exit(EXIT_FAILURE);
/* NOTREACHED */
}
|