Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.23.2.1
      1 /*	$NetBSD: envstat.c,v 1.23.2.1 2007/05/08 10:45:17 pavel 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.23.2.1 2007/05/08 10:45:17 pavel 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 void printrow(int *, envsys_tre_data_t *, envsys_basic_info_t *, int, int,
     67 	      size_t);
     68 
     69 int rflag = 0;
     70 
     71 int
     72 main(int argc, char **argv)
     73 {
     74 	int c, fd, ls, celsius;
     75 	size_t ns;
     76 	size_t interval, width, headrep, headcnt;
     77 	envsys_tre_data_t *etds;
     78 	envsys_basic_info_t *ebis;
     79 	int *cetds;
     80 	char *sensors;
     81 	const char *dev;
     82 
     83 	fd = -1;
     84 	ls = 0;
     85 	celsius = 1;
     86 	interval = 0;
     87 	width = 0;
     88 	sensors = NULL;
     89 	headrep = 22;
     90 
     91 	while ((c = getopt(argc, argv, "fi:ln:rs:w:")) != -1) {
     92 		switch(c) {
     93 		case 'r':
     94 			rflag = 1;
     95 			break;
     96 		case 'i':	/* wait time between displays */
     97 			interval = atoi(optarg);
     98 			break;
     99 		case 'w':	/* minimum column width */
    100 			width = atoi(optarg);
    101 			break;
    102 		case 'l':	/* list sensor names */
    103 			ls = 1;
    104 			break;
    105 		case 'n':	/* repeat header every headrep lines */
    106 			headrep = atoi(optarg);
    107 			break;
    108 		case 's':	/* restrict display to named sensors */
    109 			sensors = (char *)malloc(strlen(optarg) + 1);
    110 			if (sensors == NULL)
    111 				exit(1);
    112 			strcpy(sensors, optarg);
    113 			break;
    114 		case 'f':	/* display temp in degF */
    115 			celsius = 0;
    116 			break;
    117 		case '?':
    118 		default:
    119 			usage();
    120 			/* NOTREACHED */
    121 		}
    122 	}
    123 
    124 	if (optind < argc)
    125 		dev = argv[optind];
    126 	else
    127 		dev = _PATH_SYSMON;
    128 
    129 	if ((fd = open(dev, O_RDONLY)) == -1)
    130 		err(1, "unable to open %s", dev);
    131 
    132 	/*
    133 	 * Determine number of sensors, allocate and fill arrays with
    134 	 * initial information.  Determine column width
    135 	 */
    136 	if ((ns = numsensors(fd)) == 0)
    137 		errx(1, "No sensors found");
    138 
    139 	cetds = (int *)malloc(ns * sizeof(int));
    140 	etds = (envsys_tre_data_t *)malloc(ns * sizeof(envsys_tre_data_t));
    141 	ebis = (envsys_basic_info_t *)malloc(ns * sizeof(envsys_basic_info_t));
    142 
    143 	if ((cetds == NULL) || (etds == NULL) || (ebis == NULL))
    144 		err(1, "Out of memory");
    145 
    146 	fillsensors(fd, etds, ebis, ns);
    147 
    148 	if (ls) {
    149 		listsensors(ebis, ns);
    150 		exit(0);
    151 	}
    152 
    153 
    154 #define MAX(x, y)  (((x) > (y)) ? (x) : (y))
    155 	if (!width) {
    156 		width = longestname(ebis, ns);
    157 		width = MAX(((79 - ns) / ns), width);
    158 	}
    159 
    160 	/* Mark which sensor numbers are to be displayed */
    161 	if (marksensors(ebis, cetds, sensors, ns) == -1)
    162 		exit(1);
    163 
    164 	if (rflag) {
    165 		if (interval == 0) {
    166 			printrow(cetds, etds, ebis, ns, celsius, width);
    167 			exit(0);
    168 		}
    169 
    170 		while (1) {
    171 			printrow(cetds, etds, ebis, ns, celsius, width);
    172 			sleep(interval);
    173 			fillsensors(fd, etds, ebis, ns);
    174 			printf("\n");
    175 		}
    176 	}
    177 
    178 
    179 
    180 	/* If we didn't specify an interval value, print the sensors once */
    181 	if (!interval) {
    182 		if (headrep)
    183 			header(width, celsius, ebis, cetds, ns);
    184 		values(width, celsius, etds, cetds, ns);
    185 
    186 		exit (0);
    187 	}
    188 
    189 	headcnt = 0;
    190 	if (headrep)
    191 		header(width, celsius, ebis, cetds, ns);
    192 
    193         for (;;) {
    194 		values(width, celsius, etds, cetds, ns);
    195 		if (headrep && (++headcnt == headrep)) {
    196 			headcnt = 0;
    197 			header(width, celsius, ebis, cetds, ns);
    198 		}
    199 		fflush(stdout);
    200 
    201 		sleep(interval);
    202 
    203 		fillsensors(fd, etds, ebis, ns);
    204 	}
    205 
    206 	/* NOTREACHED */
    207 	return (0);
    208 }
    209 
    210 /*
    211  * row wise processing
    212  */
    213 void
    214 printrow(int *cetds, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
    215 	 int ns, int celsius, size_t width)
    216 {
    217 	int i;
    218 
    219 	for (i = 0 ; i < ns ; i++) {
    220 		if (cetds[i] == 0)
    221 			continue;
    222 
    223 		if ((etds[i].validflags & ENVSYS_FCURVALID) == 0)
    224 			continue;
    225 
    226 		if (ebis[i].units == ENVSYS_INDICATOR &&
    227 		    etds[i].cur.data_s == 0)
    228 			continue;
    229 
    230 		printf("%*.*s", (int)width, (int)width, ebis[i].desc);
    231 		/* different units need some magic */
    232 		switch (ebis[i].units)
    233 		{
    234 		case ENVSYS_DRIVE:
    235 			printf(": drive %s",
    236 			    envsysdrivestatus[etds[i].cur.data_us]);
    237 		case ENVSYS_INDICATOR:
    238 			break;
    239 		case ENVSYS_INTEGER:
    240 			printf(": %10d", etds[i].cur.data_s);
    241 			break;
    242 		case ENVSYS_STEMP: {
    243 			double temp = (etds[i].cur.data_s / 1000000.0)
    244 			    - 273.15;
    245 			if (celsius)
    246 				printf(": %10.3f degC", temp);
    247 			else {
    248 				temp = (9.0 / 5.0) * temp + 32.0;
    249 				printf(": %10.3f degF", temp);
    250 			}
    251 			break;
    252 		}
    253 		case ENVSYS_SFANRPM:
    254 			printf(": %10u RPM", etds[i].cur.data_us);
    255 			break;
    256 		default:
    257 			printf(": %10.3f %s",
    258 			    etds[i].cur.data_s / 1000000.0,
    259 			    envsysunitnames[ebis[i].units]);
    260 			break;
    261 		}
    262 
    263 		if (etds[i].validflags & ENVSYS_FFRACVALID) {
    264 			printf(" (%5.2f%%)",
    265 			    (etds[i].cur.data_s * 100.0) /
    266 			    etds[i].max.data_s);
    267 		}
    268 
    269 		printf("\n");
    270 	}
    271 
    272 }
    273 
    274 /*
    275  * pre:  cetds[i] != 0 iff sensor i should appear in the output
    276  * post: a column header line is displayed on stdout
    277  */
    278 void
    279 header(size_t width, int celsius, envsys_basic_info_t *ebis,
    280        const int * const cetds, size_t ns)
    281 {
    282 	int i;
    283 	const char *s;
    284 
    285 	/* sensor names */
    286 	for (i = 0; i < ns; ++i)
    287 		if (cetds[i])
    288 			printf(" %*.*s", (int)width, (int)width, ebis[i].desc);
    289 
    290 	printf("\n");
    291 
    292 	/* units */
    293 	for (i = 0; i < ns; ++i)
    294 		if (cetds[i]) {
    295 			if ((ebis[i].units == ENVSYS_STEMP) &&
    296 			    !celsius)
    297 				s = "degF";
    298 			else if (ebis[i].units >= ENVSYS_NSENSORS)
    299 				s = envsysunitnames[ENVSYS_NSENSORS];
    300 			else
    301 				s = envsysunitnames[ebis[i].units];
    302 
    303 			printf(" %*.*s", (int)width, (int)width, s);
    304 		}
    305 	printf("\n");
    306 }
    307 
    308 void
    309 values(size_t width, int celsius, envsys_tre_data_t *etds,
    310        const int * const cetds, size_t ns)
    311 {
    312 	int i;
    313 	double temp;
    314 
    315 	for (i = 0; i < ns; ++i)
    316 		if (cetds[i]) {
    317 
    318 			/* * sensors without valid data */
    319 			if ((etds[i].validflags & ENVSYS_FCURVALID) == 0) {
    320 				printf(" %*.*s", (int)width, (int)width, "*");
    321 				continue;
    322 			}
    323 
    324 			switch(etds[i].units) {
    325 			case ENVSYS_DRIVE:
    326 				printf(" %*.*s", (int)width, (int)width,
    327 				    envsysdrivestatus[etds[i].cur.data_us]);
    328 				break;
    329 			case ENVSYS_INDICATOR:
    330 				printf(" %*.*s", (int)width, (int)width,
    331 				    etds[i].cur.data_us ? "ON" : "OFF");
    332 				break;
    333 			case ENVSYS_STEMP:
    334 				temp = (etds[i].cur.data_us / 1000000.0) -
    335 				    273.15;
    336 				if (!celsius)
    337 					temp = (9.0 / 5.0) * temp + 32.0;
    338 				printf(" %*.2f", (int)width, temp);
    339 				break;
    340 			case ENVSYS_SFANRPM:
    341 				printf(" %*u", (int)width, etds[i].cur.data_us);
    342 				break;
    343 			default:
    344 				printf(" %*.2f", (int)width, etds[i].cur.data_s /
    345 				       1000000.0);
    346 				break;
    347 			}
    348 		}
    349 	printf("\n");
    350 }
    351 
    352 
    353 /*
    354  * post: displays usage on stderr
    355  */
    356 void
    357 usage(void)
    358 {
    359 
    360 	(void)fprintf(stderr, "usage: %s [-fr] [-s s1,s2,...]", getprogname());
    361 	(void)fprintf(stderr, " [-i interval] [-n headrep] [-w width]");
    362 	(void)fprintf(stderr, " [device]\n");
    363 	(void)fprintf(stderr, "       %s -l [device]\n", getprogname());
    364 	exit(1);
    365 }
    366 
    367 
    368 /*
    369  * post: a list of sensor names supported by the device is displayed on stdout
    370  */
    371 void
    372 listsensors(envsys_basic_info_t *ebis, size_t ns)
    373 {
    374 	int i;
    375 
    376 	for (i = 0; i < ns; ++i)
    377 		if (ebis[i].validflags & ENVSYS_FVALID)
    378 			printf("%s\n", ebis[i].desc);
    379 }
    380 
    381 
    382 /*
    383  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    384  * post: returns the number of valid sensors provided by the device
    385  *       or -1 on error
    386  */
    387 size_t
    388 numsensors(int fd)
    389 {
    390 	int count = 0, valid = 1;
    391 	envsys_tre_data_t etd;
    392 	etd.sensor = 0;
    393 
    394 	while (valid) {
    395 		if (ioctl(fd, ENVSYS_GTREDATA, &etd) == -1)
    396 			err(1, "Can't get sensor data");
    397 
    398 		valid = etd.validflags & ENVSYS_FVALID;
    399 		if (valid)
    400 			++count;
    401 
    402 		++etd.sensor;
    403 	}
    404 
    405 	return count;
    406 }
    407 
    408 /*
    409  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    410  *       && ns is the number of sensors
    411  *       && etds and ebis are arrays of sufficient size
    412  * post: returns 0 and etds and ebis arrays are filled with sensor info
    413  *       or returns -1 on failure
    414  */
    415 void
    416 fillsensors(int fd, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
    417     size_t ns)
    418 {
    419 	int i;
    420 
    421 	for (i = 0; i < ns; ++i) {
    422 		ebis[i].sensor = i;
    423 		if (ioctl(fd, ENVSYS_GTREINFO, &ebis[i]) == -1)
    424 			err(1, "Can't get sensor info for sensor %d", i);
    425 
    426 		etds[i].sensor = i;
    427 		if (ioctl(fd, ENVSYS_GTREDATA, &etds[i]) == -1)
    428 			err(1, "Can't get sensor data for sensor %d", i);
    429 	}
    430 }
    431 
    432 
    433 /*
    434  * post: returns the strlen() of the longest sensor name
    435  */
    436 size_t
    437 longestname(envsys_basic_info_t *ebis, size_t ns)
    438 {
    439 	size_t i, maxlen, cur;
    440 
    441 	maxlen = 0;
    442 
    443 	for (i = 0; i < ns; ++i) {
    444 		cur = strlen(ebis[i].desc);
    445 		if (cur > maxlen)
    446 			maxlen = cur;
    447 	}
    448 
    449 	return maxlen;
    450 }
    451 
    452 /*
    453  * post: returns 0 and cetds[i] != 0 iff sensor i should appear in the output
    454  *       or returns -1
    455  */
    456 int
    457 marksensors(envsys_basic_info_t *ebis, int *cetds, char *sensors, size_t ns)
    458 {
    459 	size_t i;
    460 	char *s;
    461 
    462 	if (sensors == NULL) {
    463 		/* No sensors specified, include them all */
    464 		for (i = 0; i < ns; ++i)
    465 			cetds[i] = 1;
    466 
    467 		return 0;
    468 	}
    469 
    470 	/* Assume no sensors in display */
    471 	memset(cetds, 0, ns * sizeof(int));
    472 
    473 	s = strtok(sensors, ",");
    474 	while (s != NULL) {
    475 		int snum;
    476 		if ((snum = strtosnum(ebis, s, ns)) != -1)
    477 			cetds[snum] = 1;
    478 		else {
    479 			warnx("Unknown sensor %s", s);
    480 			return (-1);
    481 		}
    482 
    483 		s = strtok(NULL, ",");
    484 	}
    485 
    486 	/* Check if we have at least one sensor selected for output */
    487 	for (i = 0; i < ns; ++i)
    488 		if (cetds[i] == 1)
    489 			return (0);
    490 
    491 	warnx("No sensors selected for display");
    492 	return (-1);
    493 }
    494 
    495 
    496 /*
    497  * returns -1 if s is not a valid sensor name for the device
    498  *       or the sensor number of a sensor which has that name
    499  */
    500 int
    501 strtosnum(envsys_basic_info_t *ebis, const char *s, size_t ns)
    502 {
    503 	size_t i;
    504 
    505 	for (i = 0; i < ns; ++i) {
    506 		if((ebis[i].validflags & ENVSYS_FVALID) &&
    507 		   !strcmp(s, ebis[i].desc))
    508 			return ebis[i].sensor;
    509 	}
    510 
    511 	return -1;
    512 }
    513