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