Home | History | Annotate | Line # | Download | only in envstat
envstat.c revision 1.22.2.1
      1 /*	$NetBSD: envstat.c,v 1.22.2.1 2007/10/15 05:09:58 riz 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.22.2.1 2007/10/15 05:09:58 riz 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 
    200 		sleep(interval);
    201 
    202 		fillsensors(fd, etds, ebis, ns);
    203 	}
    204 
    205 	/* NOTREACHED */
    206 	return (0);
    207 }
    208 
    209 /*
    210  * row wise processing
    211  */
    212 void
    213 printrow(int *cetds, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
    214 	 int ns, int celsius, size_t width)
    215 {
    216 	int i;
    217 
    218 	for (i = 0 ; i < ns ; i++) {
    219 		if (cetds[i] == 0)
    220 			continue;
    221 
    222 		if ((etds[i].validflags & ENVSYS_FCURVALID) == 0)
    223 			continue;
    224 
    225 		if (ebis[i].units == ENVSYS_INDICATOR &&
    226 		    etds[i].cur.data_s == 0)
    227 			continue;
    228 
    229 		printf("%*.*s", (int)width, (int)width, ebis[i].desc);
    230 		/* different units need some magic */
    231 		switch (ebis[i].units)
    232 		{
    233 		case ENVSYS_DRIVE:
    234 			printf(": drive %s",
    235 			    envsysdrivestatus[etds[i].cur.data_us]);
    236 		case ENVSYS_INDICATOR:
    237 			break;
    238 		case ENVSYS_INTEGER:
    239 			printf(": %10d", etds[i].cur.data_s);
    240 			break;
    241 		case ENVSYS_STEMP: {
    242 			double temp = (etds[i].cur.data_s / 1000000.0)
    243 			    - 273.15;
    244 			if (celsius)
    245 				printf(": %10.3f degC", temp);
    246 			else {
    247 				temp = (9.0 / 5.0) * temp + 32.0;
    248 				printf(": %10.3f degF", temp);
    249 			}
    250 			break;
    251 		}
    252 		case ENVSYS_SFANRPM:
    253 			printf(": %10u RPM", etds[i].cur.data_us);
    254 			break;
    255 		default:
    256 			printf(": %10.3f %s",
    257 			    etds[i].cur.data_s / 1000000.0,
    258 			    envsysunitnames[ebis[i].units]);
    259 			break;
    260 		}
    261 
    262 		if (etds[i].validflags & ENVSYS_FFRACVALID) {
    263 			printf(" (%5.2f%%)",
    264 			    (etds[i].cur.data_s * 100.0) /
    265 			    etds[i].max.data_s);
    266 		}
    267 
    268 		printf("\n");
    269 	}
    270 
    271 }
    272 
    273 /*
    274  * pre:  cetds[i] != 0 iff sensor i should appear in the output
    275  * post: a column header line is displayed on stdout
    276  */
    277 void
    278 header(size_t width, int celsius, envsys_basic_info_t *ebis,
    279        const int * const cetds, size_t ns)
    280 {
    281 	int i;
    282 	const char *s;
    283 
    284 	/* sensor names */
    285 	for (i = 0; i < ns; ++i)
    286 		if (cetds[i])
    287 			printf(" %*.*s", (int)width, (int)width, ebis[i].desc);
    288 
    289 	printf("\n");
    290 
    291 	/* units */
    292 	for (i = 0; i < ns; ++i)
    293 		if (cetds[i]) {
    294 			if ((ebis[i].units == ENVSYS_STEMP) &&
    295 			    !celsius)
    296 				s = "degF";
    297 			else if (ebis[i].units >= ENVSYS_NSENSORS)
    298 				s = envsysunitnames[ENVSYS_NSENSORS];
    299 			else
    300 				s = envsysunitnames[ebis[i].units];
    301 
    302 			printf(" %*.*s", (int)width, (int)width, s);
    303 		}
    304 	printf("\n");
    305 }
    306 
    307 void
    308 values(size_t width, int celsius, envsys_tre_data_t *etds,
    309        const int * const cetds, size_t ns)
    310 {
    311 	int i;
    312 	double temp;
    313 
    314 	for (i = 0; i < ns; ++i)
    315 		if (cetds[i]) {
    316 
    317 			/* * sensors without valid data */
    318 			if ((etds[i].validflags & ENVSYS_FCURVALID) == 0) {
    319 				printf(" %*.*s", (int)width, (int)width, "*");
    320 				continue;
    321 			}
    322 
    323 			switch(etds[i].units) {
    324 			case ENVSYS_DRIVE:
    325 				printf(" %*.*s", (int)width, (int)width,
    326 				    envsysdrivestatus[etds[i].cur.data_us]);
    327 				break;
    328 			case ENVSYS_INDICATOR:
    329 				printf(" %*.*s", (int)width, (int)width,
    330 				    etds[i].cur.data_us ? "ON" : "OFF");
    331 				break;
    332 			case ENVSYS_STEMP:
    333 				temp = (etds[i].cur.data_us / 1000000.0) -
    334 				    273.15;
    335 				if (!celsius)
    336 					temp = (9.0 / 5.0) * temp + 32.0;
    337 				printf(" %*.2f", (int)width, temp);
    338 				break;
    339 			case ENVSYS_SFANRPM:
    340 				printf(" %*u", (int)width, etds[i].cur.data_us);
    341 				break;
    342 			default:
    343 				printf(" %*.2f", (int)width, etds[i].cur.data_s /
    344 				       1000000.0);
    345 				break;
    346 			}
    347 		}
    348 	printf("\n");
    349 }
    350 
    351 
    352 /*
    353  * post: displays usage on stderr
    354  */
    355 void
    356 usage(void)
    357 {
    358 
    359 	(void)fprintf(stderr, "usage: %s [-fr] [-s s1,s2,...]", getprogname());
    360 	(void)fprintf(stderr, " [-i interval] [-n headrep] [-w width]");
    361 	(void)fprintf(stderr, " [device]\n");
    362 	(void)fprintf(stderr, "       %s -l [device]\n", getprogname());
    363 	exit(1);
    364 }
    365 
    366 
    367 /*
    368  * post: a list of sensor names supported by the device is displayed on stdout
    369  */
    370 void
    371 listsensors(envsys_basic_info_t *ebis, size_t ns)
    372 {
    373 	int i;
    374 
    375 	for (i = 0; i < ns; ++i)
    376 		if (ebis[i].validflags & ENVSYS_FVALID)
    377 			printf("%s\n", ebis[i].desc);
    378 }
    379 
    380 
    381 /*
    382  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    383  * post: returns the number of valid sensors provided by the device
    384  *       or -1 on error
    385  */
    386 size_t
    387 numsensors(int fd)
    388 {
    389 	int count = 0, valid = 1;
    390 	envsys_tre_data_t etd;
    391 	etd.sensor = 0;
    392 
    393 	while (valid) {
    394 		if (ioctl(fd, ENVSYS_GTREDATA, &etd) == -1)
    395 			err(1, "Can't get sensor data");
    396 
    397 		valid = etd.validflags & ENVSYS_FVALID;
    398 		if (valid)
    399 			++count;
    400 
    401 		++etd.sensor;
    402 	}
    403 
    404 	return count;
    405 }
    406 
    407 /*
    408  * pre:  fd contains a valid file descriptor of an envsys(4) supporting device
    409  *       && ns is the number of sensors
    410  *       && etds and ebis are arrays of sufficient size
    411  * post: returns 0 and etds and ebis arrays are filled with sensor info
    412  *       or returns -1 on failure
    413  */
    414 void
    415 fillsensors(int fd, envsys_tre_data_t *etds, envsys_basic_info_t *ebis,
    416     size_t ns)
    417 {
    418 	int i;
    419 
    420 	for (i = 0; i < ns; ++i) {
    421 		ebis[i].sensor = i;
    422 		if (ioctl(fd, ENVSYS_GTREINFO, &ebis[i]) == -1)
    423 			err(1, "Can't get sensor info for sensor %d", i);
    424 
    425 		etds[i].sensor = i;
    426 		if (ioctl(fd, ENVSYS_GTREDATA, &etds[i]) == -1)
    427 			err(1, "Can't get sensor data for sensor %d", i);
    428 	}
    429 }
    430 
    431 
    432 /*
    433  * post: returns the strlen() of the longest sensor name
    434  */
    435 size_t
    436 longestname(envsys_basic_info_t *ebis, size_t ns)
    437 {
    438 	size_t i, maxlen, cur;
    439 
    440 	maxlen = 0;
    441 
    442 	for (i = 0; i < ns; ++i) {
    443 		cur = strlen(ebis[i].desc);
    444 		if (cur > maxlen)
    445 			maxlen = cur;
    446 	}
    447 
    448 	return maxlen;
    449 }
    450 
    451 /*
    452  * post: returns 0 and cetds[i] != 0 iff sensor i should appear in the output
    453  *       or returns -1
    454  */
    455 int
    456 marksensors(envsys_basic_info_t *ebis, int *cetds, char *sensors, size_t ns)
    457 {
    458 	size_t i;
    459 	char *s;
    460 
    461 	if (sensors == NULL) {
    462 		/* No sensors specified, include them all */
    463 		for (i = 0; i < ns; ++i)
    464 			cetds[i] = 1;
    465 
    466 		return 0;
    467 	}
    468 
    469 	/* Assume no sensors in display */
    470 	memset(cetds, 0, ns * sizeof(int));
    471 
    472 	s = strtok(sensors, ",");
    473 	while (s != NULL) {
    474 		int snum;
    475 		if ((snum = strtosnum(ebis, s, ns)) != -1)
    476 			cetds[snum] = 1;
    477 		else {
    478 			warnx("Unknown sensor %s", s);
    479 			return (-1);
    480 		}
    481 
    482 		s = strtok(NULL, ",");
    483 	}
    484 
    485 	/* Check if we have at least one sensor selected for output */
    486 	for (i = 0; i < ns; ++i)
    487 		if (cetds[i] == 1)
    488 			return (0);
    489 
    490 	warnx("No sensors selected for display");
    491 	return (-1);
    492 }
    493 
    494 
    495 /*
    496  * returns -1 if s is not a valid sensor name for the device
    497  *       or the sensor number of a sensor which has that name
    498  */
    499 int
    500 strtosnum(envsys_basic_info_t *ebis, const char *s, size_t ns)
    501 {
    502 	size_t i;
    503 
    504 	for (i = 0; i < ns; ++i) {
    505 		if((ebis[i].validflags & ENVSYS_FVALID) &&
    506 		   !strcmp(s, ebis[i].desc))
    507 			return ebis[i].sensor;
    508 	}
    509 
    510 	return -1;
    511 }
    512