Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.18
      1 /*	$NetBSD: envstat.c,v 1.18 2004/03/21 10:52:26 mrg Exp $ */
      2 
      3 /*-
      4  * Copyright (c) 2000 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Bill Squier.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 #include <sys/cdefs.h>
     40 #ifndef lint
     41 __RCSID("$NetBSD: envstat.c,v 1.18 2004/03/21 10:52:26 mrg Exp $");
     42 #endif
     43 
     44 #include <fcntl.h>
     45 #include <stdio.h>
     46 #include <stdlib.h>
     47 #include <string.h>
     48 #include <unistd.h>
     49 #include <err.h>
     50 #include <paths.h>
     51 
     52 #define ENVSYSUNITNAMES
     53 #include <sys/envsys.h>
     54 #include <sys/ioctl.h>
     55 
     56 int main(int, char **);
     57 void listsensors(envsys_basic_info_t *, size_t);
     58 size_t numsensors(int);
     59 void fillsensors(int, envsys_tre_data_t *, envsys_basic_info_t *, size_t);
     60 size_t longestname(envsys_basic_info_t *, size_t);
     61 int marksensors(envsys_basic_info_t *, int *, char *, size_t);
     62 int strtosnum(envsys_basic_info_t *, const char *, size_t);
     63 void header(size_t, int, envsys_basic_info_t *, const int * const, size_t);
     64 void values(size_t, int, envsys_tre_data_t *, const int * const, size_t);
     65 void usage(void);
     66 
     67 int rflag = 0;
     68 
     69 int
     70 main(int argc, char **argv)
     71 {
     72 	int c, fd, ls, celsius;
     73 	size_t ns;
     74 	size_t interval, width, headrep, headcnt;
     75 	envsys_tre_data_t *etds;
     76 	envsys_basic_info_t *ebis;
     77 	int *cetds;
     78 	char *sensors;
     79 	const char *dev;
     80 
     81 	fd = -1;
     82 	ls = 0;
     83 	celsius = 1;
     84 	interval = 0;
     85 	width = 0;
     86 	sensors = NULL;
     87 	headrep = 22;
     88 
     89 	while ((c = getopt(argc, argv, "cfi:ln:rs:w:r")) != -1) {
     90 		switch(c) {
     91 		case 'r':
     92 			rflag = 1;
     93 			break;
     94 		case 'i':	/* wait time between displays */
     95 			interval = atoi(optarg);
     96 			break;
     97 		case 'w':	/* minimum column width */
     98 			width = atoi(optarg);
     99 			break;
    100 		case 'l':	/* list sensor names */
    101 			ls = 1;
    102 			break;
    103 		case 'n':	/* repeat header every headrep lines */
    104 			headrep = atoi(optarg);
    105 			break;
    106 		case 's':	/* restrict display to named sensors */
    107 			sensors = (char *)malloc(strlen(optarg) + 1);
    108 			if (sensors == NULL)
    109 				exit(1);
    110 			strcpy(sensors, optarg);
    111 			break;
    112 		case 'f':	/* display temp in degF */
    113 			celsius = 0;
    114 			break;
    115 		case '?':
    116 		default:
    117 			usage();
    118 			/* NOTREACHED */
    119 		}
    120 	}
    121 
    122 	if (optind < argc)
    123 		dev = argv[optind];
    124 	else
    125 		dev = _PATH_SYSMON;
    126 
    127 	if ((fd = open(dev, O_RDONLY)) == -1)
    128 		err(1, "unable to open %s", dev);
    129 
    130 	/*
    131 	 * Determine number of sensors, allocate and fill arrays with
    132 	 * initial information.  Determine column width
    133 	 */
    134 	if ((ns = numsensors(fd)) == 0)
    135 		errx(1, "No sensors found");
    136 
    137 	cetds = (int *)malloc(ns * sizeof(int));
    138 	etds = (envsys_tre_data_t *)malloc(ns * sizeof(envsys_tre_data_t));
    139 	ebis = (envsys_basic_info_t *)malloc(ns * sizeof(envsys_basic_info_t));
    140 
    141 	if ((cetds == NULL) || (etds == NULL) || (ebis == NULL))
    142 		err(1, "Out of memory");
    143 
    144 	fillsensors(fd, etds, ebis, ns);
    145 
    146 	if (ls) {
    147 		listsensors(ebis, ns);
    148 		exit(0);
    149 	}
    150 
    151 
    152 #define MAX(x, y)  (((x) > (y)) ? (x) : (y))
    153 	if (!width) {
    154 		width = longestname(ebis, ns);
    155 		width = MAX(((79 - ns) / ns), width);
    156 	}
    157 
    158 	/* Mark which sensor numbers are to be displayed */
    159 	if (marksensors(ebis, cetds, sensors, ns) == -1)
    160 		exit(1);
    161 
    162 	if (rflag) {
    163 		int i;
    164 
    165 		for (i = 0 ; i < ns ; i++) {
    166 			if (cetds[i] == 0)
    167 				continue;
    168 
    169 			if ((etds[i].validflags & ENVSYS_FCURVALID) == 0)
    170 				continue;
    171 
    172 			if (ebis[i].units == ENVSYS_INDICATOR &&
    173 			    etds[i].cur.data_s == 0)
    174 				continue;
    175 
    176 			printf("%*.*s", (int)width, (int)width, ebis[i].desc);
    177 			/* different units need some magic */
    178 			switch (ebis[i].units)
    179 			{
    180 			case ENVSYS_INDICATOR:
    181 				break;
    182 			case ENVSYS_INTEGER:
    183 				printf(": %10d", etds[i].cur.data_s);
    184 				break;
    185 			case ENVSYS_STEMP: {
    186 			     	double temp = (etds[i].cur.data_s / 1000000.0)
    187 				    - 273.15;
    188 				if (celsius)
    189 					printf(": %10.3f degC", temp);
    190 				else {
    191 					temp = (9.0 / 5.0) * temp + 32.0;
    192 					printf(": %10.3f degF", temp);
    193 				}
    194 				break;
    195 			}
    196 			case ENVSYS_SFANRPM:
    197 				printf(": %10u RPM", etds[i].cur.data_us);
    198 				break;
    199 			default:
    200 				printf(": %10.3f %s",
    201 				    etds[i].cur.data_s / 1000000.0,
    202 				    envsysunitnames[ebis[i].units]);
    203 				break;
    204 			}
    205 
    206 			if (etds[i].validflags & ENVSYS_FFRACVALID) {
    207 				printf(" (%5.2f%%)",
    208 				    (etds[i].cur.data_s * 100.0) /
    209 				    etds[i].max.data_s);
    210 			}
    211 
    212 			printf("\n");
    213 		}
    214 		exit(0);
    215 	}
    216 
    217 
    218 
    219 	/* If we didn't specify an interval value, print the sensors once */
    220 	if (!interval) {
    221 		if (headrep)
    222 			header(width, celsius, ebis, cetds, ns);
    223 		values(width, celsius, etds, cetds, ns);
    224 
    225 		exit (0);
    226 	}
    227 
    228 	headcnt = 0;
    229 	if (headrep)
    230 		header(width, celsius, ebis, cetds, ns);
    231 
    232         for (;;) {
    233 		values(width, celsius, etds, cetds, ns);
    234 		if (headrep && (++headcnt == headrep)) {
    235 			headcnt = 0;
    236 			header(width, celsius, ebis, cetds, ns);
    237 		}
    238 
    239 		sleep(interval);
    240 
    241 		fillsensors(fd, etds, ebis, ns);
    242 	}
    243 
    244 	/* NOTREACHED */
    245 	return (0);
    246 }
    247 
    248 
    249 /*
    250  * pre:  cetds[i] != 0 iff sensor i should appear in the output
    251  * post: a column header line is displayed on stdout
    252  */
    253 void
    254 header(size_t width, int celsius, envsys_basic_info_t *ebis,
    255        const int * const cetds, size_t ns)
    256 {
    257 	int i;
    258 	const char *s;
    259 
    260 	/* sensor names */
    261 	for (i = 0; i < ns; ++i)
    262 		if (cetds[i])
    263 			printf(" %*.*s", (int)width, (int)width, ebis[i].desc);
    264 
    265 	printf("\n");
    266 
    267 	/* units */
    268 	for (i = 0; i < ns; ++i)
    269 		if (cetds[i]) {
    270 			if ((ebis[i].units == ENVSYS_STEMP) &&
    271 			    !celsius)
    272 				s = "degF";
    273 			else if (ebis[i].units >= ENVSYS_NSENSORS)
    274 				s = envsysunitnames[ENVSYS_NSENSORS];
    275 			else
    276 				s = envsysunitnames[ebis[i].units];
    277 
    278 			printf(" %*.*s", (int)width, (int)width, s);
    279 		}
    280 	printf("\n");
    281 }
    282 
    283 void
    284 values(size_t width, int celsius, envsys_tre_data_t *etds,
    285        const int * const cetds, size_t ns)
    286 {
    287 	int i;
    288 	double temp;
    289 
    290 	for (i = 0; i < ns; ++i)
    291 		if (cetds[i]) {
    292 
    293 			/* * sensors without valid data */
    294 			if ((etds[i].validflags & ENVSYS_FCURVALID) == 0) {
    295 				printf(" %*.*s", (int)width, (int)width, "*");
    296 				continue;
    297 			}
    298 
    299 			switch(etds[i].units) {
    300 			case ENVSYS_INDICATOR:
    301 				printf(" %*.*s", (int)width, (int)width,
    302 				    etds[i].cur.data_us ? "ON" : "OFF");
    303 				break;
    304 			case ENVSYS_STEMP:
    305 				temp = (etds[i].cur.data_us / 1000000.0) -
    306 				    273.15;
    307 				if (!celsius)
    308 					temp = (9.0 / 5.0) * temp + 32.0;
    309 				printf(" %*.2f", (int)width, temp);
    310 				break;
    311 			case ENVSYS_SFANRPM:
    312 				printf(" %*u", (int)width, etds[i].cur.data_us);
    313 				break;
    314 			default:
    315 				printf(" %*.2f", (int)width, etds[i].cur.data_s /
    316 				       1000000.0);
    317 				break;
    318 			}
    319 		}
    320 	printf("\n");
    321 }
    322 
    323 
    324 /*
    325  * post: displays usage on stderr
    326  */
    327 void
    328 usage(void)
    329 {
    330 
    331 	(void)fprintf(stderr, "usage: %s [-cr] [-s s1,s2,...]", getprogname());
    332 	(void)fprintf(stderr, " [-i interval] [-n headrep] [-w width]");
    333 	(void)fprintf(stderr, " [device]\n");
    334 	(void)fprintf(stderr, "       %s -l [device]\n", getprogname());
    335 	exit(1);
    336 }
    337 
    338 
    339 /*
    340  * post: a list of sensor names supported by the device is displayed on stdout
    341  */
    342 void
    343 listsensors(envsys_basic_info_t *ebis, size_t ns)
    344 {
    345 	int i;
    346 
    347 	for (i = 0; i < ns; ++i)
    348 		if (ebis[i].validflags & ENVSYS_FVALID)
    349 			printf("%s\n", ebis[i].desc);
    350 }
    351 
    352 
    353 /*
    354  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    355  * post: returns the number of valid sensors provided by the device
    356  *       or -1 on error
    357  */
    358 size_t
    359 numsensors(int fd)
    360 {
    361 	int count = 0, valid = 1;
    362 	envsys_tre_data_t etd;
    363 	etd.sensor = 0;
    364 
    365 	while (valid) {
    366 		if (ioctl(fd, ENVSYS_GTREDATA, &etd) == -1)
    367 			err(1, "Can't get sensor data");
    368 
    369 		valid = etd.validflags & ENVSYS_FVALID;
    370 		if (valid)
    371 			++count;
    372 
    373 		++etd.sensor;
    374 	}
    375 
    376 	return count;
    377 }
    378 
    379 /*
    380  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    381  *       && ns is the number of sensors
    382  *       && etds and ebis are arrays of sufficient size
    383  * post: returns 0 and etds and ebis arrays are filled with sensor info
    384  *       or returns -1 on failure
    385  */
    386 void
    387 fillsensors(int fd, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
    388     size_t ns)
    389 {
    390 	int i;
    391 
    392 	for (i = 0; i < ns; ++i) {
    393 		ebis[i].sensor = i;
    394 		if (ioctl(fd, ENVSYS_GTREINFO, &ebis[i]) == -1)
    395 			err(1, "Can't get sensor info for sensor %d", i);
    396 
    397 		etds[i].sensor = i;
    398 		if (ioctl(fd, ENVSYS_GTREDATA, &etds[i]) == -1)
    399 			err(1, "Can't get sensor data for sensor %d", i);
    400 	}
    401 }
    402 
    403 
    404 /*
    405  * post: returns the strlen() of the longest sensor name
    406  */
    407 size_t
    408 longestname(envsys_basic_info_t *ebis, size_t ns)
    409 {
    410 	size_t i, maxlen, cur;
    411 
    412 	maxlen = 0;
    413 
    414 	for (i = 0; i < ns; ++i) {
    415 		cur = strlen(ebis[i].desc);
    416 		if (cur > maxlen)
    417 			maxlen = cur;
    418 	}
    419 
    420 	return maxlen;
    421 }
    422 
    423 /*
    424  * post: returns 0 and cetds[i] != 0 iff sensor i should appear in the output
    425  *       or returns -1
    426  */
    427 int
    428 marksensors(envsys_basic_info_t *ebis, int *cetds, char *sensors, size_t ns)
    429 {
    430 	size_t i;
    431 	char *s;
    432 
    433 	if (sensors == NULL) {
    434 		/* No sensors specified, include them all */
    435 		for (i = 0; i < ns; ++i)
    436 			cetds[i] = 1;
    437 
    438 		return 0;
    439 	}
    440 
    441 	/* Assume no sensors in display */
    442 	memset(cetds, 0, ns * sizeof(int));
    443 
    444 	s = strtok(sensors, ",");
    445 	while (s != NULL) {
    446 		int snum;
    447 		if ((snum = strtosnum(ebis, s, ns)) != -1)
    448 			cetds[snum] = 1;
    449 		else {
    450 			warnx("Unknown sensor %s", s);
    451 			return (-1);
    452 		}
    453 
    454 		s = strtok(NULL, ",");
    455 	}
    456 
    457 	/* Check if we have at least one sensor selected for output */
    458 	for (i = 0; i < ns; ++i)
    459 		if (cetds[i] == 1)
    460 			return (0);
    461 
    462 	warnx("No sensors selected for display");
    463 	return (-1);
    464 }
    465 
    466 
    467 /*
    468  * returns -1 if s is not a valid sensor name for the device
    469  *       or the sensor number of a sensor which has that name
    470  */
    471 int
    472 strtosnum(envsys_basic_info_t *ebis, const char *s, size_t ns)
    473 {
    474 	size_t i;
    475 
    476 	for (i = 0; i < ns; ++i) {
    477 		if((ebis[i].validflags & ENVSYS_FVALID) &&
    478 		   !strcmp(s, ebis[i].desc))
    479 			return ebis[i].sensor;
    480 	}
    481 
    482 	return -1;
    483 }
    484