aboutsummaryrefslogtreecommitdiffstats
path: root/plugin_math.c (follow)
AgeCommit message (Expand)AuthorFilesLines
2005-05-08[lcd4linux @ 2005-05-08 04:32:43 by reinelt]reinelt1-62/+65
2005-04-05[lcd4linux @ 2005-04-05 04:46:06 by reinelt]reinelt1-1/+20
2005-01-18[lcd4linux @ 2005-01-18 06:30:21 by reinelt]reinelt1-2/+6
2004-06-26[lcd4linux @ 2004-06-26 12:04:59 by reinelt]reinelt1-2/+6
2004-06-26[lcd4linux @ 2004-06-26 09:27:20 by reinelt]reinelt1-4/+10
2004-03-06[lcd4linux @ 2004-03-06 20:31:16 by reinelt]reinelt1-3/+9
2004-03-03[lcd4linux @ 2004-03-03 03:47:04 by reinelt]reinelt1-1/+11
2004-01-29[lcd4linux @ 2004-01-29 04:40:02 by reinelt]reinelt1-1/+6
2003-12-19[lcd4linux @ 2003-12-19 05:50:34 by reinelt]reinelt1-0/+128
a id='n116' href='#n116'>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
/* $Id: system.c,v 1.31 2003/12/01 07:08:51 reinelt Exp $
 *
 * system status retreivement
 *
 * Copyright 1999, 2000 Michael Reinelt <reinelt@eunet.at>
 *
 * This file is part of LCD4Linux.
 *
 * LCD4Linux is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * LCD4Linux is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 * $Log: system.c,v $
 * Revision 1.31  2003/12/01 07:08:51  reinelt
 *
 * Patches from Xavier:
 *  - WiFi: make interface configurable
 *  - "quiet" as an option from the config file
 *  - ignore missing "MemShared" on Linux 2.6
 *
 * Revision 1.30  2003/10/05 17:58:50  reinelt
 * libtool junk; copyright messages cleaned up
 *
 * Revision 1.29  2003/08/24 05:17:58  reinelt
 * liblcd4linux patch from Patrick Schemitz
 *
 * Revision 1.28  2003/06/26 05:31:16  reinelt
 * bug in /proc/net/dev parsing fixed
 *
 * Revision 1.27  2003/06/13 05:11:10  reinelt
 * error message cosmetics
 *
 * Revision 1.26  2003/02/22 07:53:10  reinelt
 * cfg_get(key,defval)
 *
 * Revision 1.25  2002/12/05 19:12:47  reinelt
 * sensors factor and offset patch from Petri Damsten <petri.damsten@raketti.net>
 *
 * Revision 1.24  2001/08/05 17:13:29  reinelt
 *
 * cleaned up inlude of sys/time.h and time.h
 *
 * Revision 1.23  2001/03/13 08:34:15  reinelt
 *
 * corrected a off-by-one bug with sensors
 *
 * Revision 1.22  2001/03/12 12:39:36  reinelt
 *
 * reworked autoconf a lot: drivers may be excluded, #define's went to config.h
 *
 * Revision 1.21  2001/03/09 12:14:24  reinelt
 *
 * minor cleanups
 *
 * Revision 1.20  2001/03/02 20:18:12  reinelt
 *
 * allow compile on systems without net/if_ppp.h
 *
 * Revision 1.19  2001/02/16 08:23:09  reinelt
 *
 * new token 'ic' (ISDN connected) by Carsten Nau <info@cnau.de>
 *
 * Revision 1.18  2000/11/17 10:36:23  reinelt
 *
 * fixed parsing of /proc/net/dev for 2.0 kernels
 *
 * Revision 1.17  2000/10/08 09:16:40  reinelt
 *
 *
 * Linux-2.4.0-test9 changed the layout of /proc/stat (especially the disk_io line)
 * rearranged parsing of some /proc files and (hopefully) made it more robust in concerns of format changes
 *
 * Revision 1.16  2000/08/10 09:44:09  reinelt
 *
 * new debugging scheme: error(), info(), debug()
 * uses syslog if in daemon mode
 *
 * Revision 1.15  2000/08/09 11:03:07  reinelt
 *
 * fixed a bug in system.c where the format of /proc/net/dev was not correctly
 * detected and parsed with different kernels
 *
 * Revision 1.14  2000/08/09 09:50:29  reinelt
 *
 * opened 0.98 development
 * removed driver-specific signal-handlers
 * added 'quit'-function to driver structure
 * added global signal-handler
 *
 * Revision 1.13  2000/07/31 10:43:44  reinelt
 *
 * some changes to support kernel-2.4 (different layout of various files in /proc)
 *
 * Revision 1.12  2000/05/21 06:20:35  reinelt
 *
 * added ppp throughput
 * token is '%t[iomt]' at the moment, but this will change in the near future
 *
 * Revision 1.11  2000/04/15 11:56:35  reinelt
 *
 * more debug messages
 *
 * Revision 1.10  2000/04/13 06:09:52  reinelt
 *
 * added BogoMips() to system.c (not used by now, maybe sometimes we can
 * calibrate our delay loop with this value)
 *
 * added delay loop to HD44780 driver. It seems to be quite fast now. Hopefully
 * no compiler will optimize away the delay loop!
 *
 * Revision 1.9  2000/03/28 07:22:15  reinelt
 *
 * version 0.95 released
 * X11 driver up and running
 * minor bugs fixed
 *
 * Revision 1.8  2000/03/23 07:24:48  reinelt
 *
 * PPM driver up and running (but slow!)
 *
 * Revision 1.7  2000/03/18 08:07:04  reinelt
 *
 * vertical bars implemented
 * bar compaction improved
 * memory information implemented
 *
 * Revision 1.6  2000/03/17 09:21:42  reinelt
 *
 * various memory statistics added
 *
 * Revision 1.5  2000/03/10 17:36:02  reinelt
 *
 * first unstable but running release
 *
 * Revision 1.4  2000/03/10 10:49:53  reinelt
 *
 * MatrixOrbital driver finished
 *
 * Revision 1.3  2000/03/07 11:01:34  reinelt
 *
 * system.c cleanup
 *
 * Revision 1.2  2000/03/06 06:04:06  reinelt
 *
 * minor cleanups
 *
 *
 */

/* 
 * exported functions:
 *
 * char *System (void);
 *   returns OS name ('Linux')
 *
 * char *Release (void);
 *   returns OS release ('2.0.38')
 * 
 * char *Processor (void);
 *   returns processor type ('i686')
 *
 * double BogoMips (void);
 *   returns BogoMips from /proc/cpuinfo
 *
 * int   Memory (void);
 *   returns main memory (Megabytes)
 *
 * int Ram (int *total, int *free, int *shared, int *buffer, int *cached)
 *   sets various usage of ram
 *   retuns 0 if ok, -1 on error
 *
 * int Load (double *load1, double *load2, double *load3)
 *   sets load average during thwe last 1, 5 and 15 minutes
 *   retuns 0 if ok, -1 on error
 *
 * int Busy (double *user, double *nice, double *system, double *idle)
 *   sets percentage of CPU time spent in user processes, nice'd processes
 *   system calls and idle state
 *   returns 0 if ok, -1 on error
 *
 * int Disk (int *r, int *w);
 *   sets number of blocks read and written from/to all disks 
 *   returns 0 if ok, -1 on error
 *
 * int Net (int *rx, int *tx, int *bytes);
 *   sets number of packets or bytes received and transmitted
 *   *bytes ist set to 0 if rx/tx are packets
 *   *bytes ist set to 1 if rx/tx are bytes
 *   returns 0 if ok, -1 on error
 *
 * int PPP (int unit, int *rx, int *tx);
 *   sets number of packets received and transmitted
 *   returns 0 if ok, -1 on error
 *
 * int Sensor (int index, double *val, double *min, double *max)
 *   sets the current value of the index'th sensor and
 *   the minimum and maximum values from the config file
 *   returns 0 if ok, -1 on error
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <ctype.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <time.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/utsname.h>
#include <sys/param.h>
#include <sys/ioctl.h>
#include <sys/socket.h>

#ifdef HAVE_NET_IF_PPP_H
#include <net/if_ppp.h>
#else
#warning if_ppp.h not found. PPP support deactivated.
#endif

#include "debug.h"
#include "cfg.h"
#include "system.h"
#include "filter.h"


static int parse_meminfo (char *tag, char *buffer)
{
  char *p;
  unsigned long val;
  
  p=strstr(buffer, tag);
  if (p==NULL) {
    error ("parse(/proc/meminfo) failed: no '%s' line", tag);
    return -1;
  }
  if (sscanf(p+strlen(tag), "%lu", &val)!=1) {
    error ("parse(/proc/meminfo) failed: unknown '%s' format", tag);
    return -1;
  }
  return val;
}


char *System(void)
{
  static char buffer[32]="";
  struct utsname ubuf;

  if (*buffer=='\0') {
    if (uname(&ubuf)==-1) {
      error ("uname() failed: %s", strerror(errno));
      strcpy (buffer, "unknown");
    } else {
      debug ("uname(sysname)=%s", ubuf.sysname);
      strncpy (buffer, ubuf.sysname, sizeof(buffer));
    }
  }
  return buffer;
}


char *Release(void)
{
  static char buffer[32]="";
  struct utsname ubuf;

  if (*buffer=='\0') {
    if (uname(&ubuf)==-1) {
      error ("uname() failed: %s", strerror(errno));
      strcpy (buffer, "unknown");
    } else {
      debug ("uname(release)=%s", ubuf.release);
      strncpy (buffer, ubuf.release, sizeof(buffer));
    }
  }
  return buffer;
}


char *Processor(void)
{
  static char buffer[16]="";
  struct utsname ubuf;

  if (*buffer=='\0') {
    if (uname(&ubuf)==-1) {
      error ("uname() failed: %s", strerror(errno));
      strcpy (buffer, "unknown");
    } else {
      debug ("uname(machine)=%s", ubuf.machine);
      strncpy (buffer, ubuf.machine, sizeof(buffer));
    }
  }
  return buffer;
}


double BogoMips (void)
{
  static double val=-2;
  char buffer[4096];

  if (val==-1) return -1;

  if (val==-2) {
    char *p;
    int fd=open("/proc/cpuinfo", O_RDONLY);
    if (fd==-1) {
      error ("open(/proc/cpuinfo) failed: %s", strerror(errno));
      val=-1;
      return -1;
    }
    debug ("open(proc/cpuinfo)=%d", fd);
    if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
      error ("read(/proc/cpuinfo) failed: %s", strerror(errno));
      close (fd);
      val=-1;
      return -1;
    }
    close (fd);
    p=strstr(buffer, "bogomips");
    if (p==NULL) {
      error ("parse(/proc/cpuinfo) failed: no 'bogomips' line");
      val=-1;
      return -1;
    }
    if (sscanf(p+8, " : %lf", &val)!=1) {
      error ("parse(/proc/cpuinfo) failed: unknown 'bogomips' format");
      val=-1;
      return -1;
    }
    debug ("BogoMips=%f", val);
  }
  return val;
}


int Memory(void)
{
  static int value=-1;
  struct stat buf;

  if (value==-1) {
    if (stat("/proc/kcore", &buf)==-1) {
      error ("stat(/proc/kcore) failed: %s", strerror(errno));
      value=0;
    } else {
      debug ("sizeof(/proc/kcore)=%ld bytes", buf.st_size);
      value=buf.st_size>>20;
    }
  }
  return value;
}

int Ram (int *total, int *free, int *shared, int *buffered, int *cached)
{
  static time_t now=0;
  static int fd=-2;
  static int v1=0;
  static int v2=0;
  static int v3=0;
  static int v4=0;
  static int v5=0;
  char buffer[4096];
  
  *total=v1;
  *free=v2;
  *shared=v3;
  *buffered=v4;
  *cached=v5;

  if (fd==-1) return -1;
  
  if (time(NULL)==now) return 0;
  time(&now);
  
  if (fd==-2) {
    fd = open("/proc/meminfo", O_RDONLY | O_NDELAY);
    if (fd==-1) {
      error ("open(/proc/meminfo) failed: %s", strerror(errno));
      return -1;
    }
    debug ("open(/proc/meminfo)=%d", fd);
  }
  
  if (lseek(fd, 0L, SEEK_SET)!=0) {
    error ("lseek(/proc/meminfo) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }
  if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
    error ("read(/proc/meminfo) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }
  
  if ((v1=parse_meminfo ("MemTotal:", buffer))<0) {
    fd=-1;
    return -1;
  }
  if ((v2=parse_meminfo ("MemFree:", buffer))<0) {
    fd=-1;
    return -1;
  }
  if ((v3=parse_meminfo ("MemShared:", buffer))<0) {
    // The "MemShared" entriy disappeared in Kernel 2.6...
#if 0
    fd=-1;
    return -1;
#else
    v3=0;
#endif
  }
  if ((v4=parse_meminfo ("Buffers:", buffer))<0) {
    fd=-1;
    return -1;
  }
  if ((v5=parse_meminfo ("Cached:", buffer))<0) {
    fd=-1;
    return -1;
  }

  *total=v1;
  *free=v2;
  *shared=v3;
  *buffered=v4;
  *cached=v5;

  return 0;

}


int Load (double *load1, double *load2, double *load3)
{
  static int fd=-2;
  char buffer[16];
  static double val1=0;
  static double val2=0;
  static double val3=0;
  static time_t now=0;
  
  *load1=val1;
  *load2=val2;
  *load3=val3;

  if (fd==-1) return -1;
  
  if (time(NULL)==now) return 0;
  time(&now);
  
  if (fd==-2) {
    fd=open("/proc/loadavg", O_RDONLY);
    if (fd==-1) {
      error ("open(/proc/loadavg) failed: %s", strerror(errno));
      return -1;
    }
    debug ("open(/proc/loadavg)=%d", fd);
  }

  if (lseek(fd, 0L, SEEK_SET)!=0) {
    error("lseek(/proc/loadavg) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
    error("read(/proc/loadavg) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  if (sscanf(buffer, "%lf %lf %lf", &val1, &val2, &val3)!=3) {
    error ("parse(/proc/loadavg) failed: unknown format");
    fd=-1;
    return -1;
  }

  *load1=val1;
  *load2=val2;
  *load3=val3;

  return 0;
}


int Busy (double *user, double *nice, double *system, double *idle)
{
  static int fd=-2;
  char buffer[64], *p;
  unsigned long v1, v2, v3, v4;
  double d1, d2, d3, d4, d5;

  *user=0.0;
  *nice=0.0;
  *system=0.0;
  *idle=0.0;

  if (fd==-1) return -1;

  if (fd==-2) {
    fd=open("/proc/stat", O_RDONLY);
    if (fd==-1) {
      error ("open(proc/stat) failed: %s", strerror(errno));
      return -1;
    }
    debug ("open (/proc/stat)=%d", fd);
  }

  if (lseek(fd, 0L, SEEK_SET)!=0) {
    error ("lseek(/proc/stat) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
    error ("read(/proc/stat) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  p=strstr(buffer, "cpu ");
  if (p==NULL) {
    error ("parse(/proc/stat) failed: no 'cpu' line");
    fd=-1;
    return -1;
  }

  if (sscanf(p+4, " %lu %lu %lu %lu", &v1, &v2, &v3, &v4)!=4) {
    error ("parse(/proc/stat) failed: unknown 'cpu' format");
    fd=-1;
    return -1;
  }

  d1=smooth("cpu_user", 500, v1);
  d2=smooth("cpu_nice", 500, v2);
  d3=smooth("cpu_sys",  500, v3);
  d4=smooth("cpu_idle", 500, v4);
  d5=d1+d2+d3+d4;
 
  if (d5!=0.0) {
    *user=(d1+d2)/d5;
    *nice=d2/d5;
    *system=d3/d5;
    *idle=d4/d5;
  }
  return 0;
}


int Disk (int *r, int *w)
{
  char buffer[4096], *p;
  static int fd=-2;
  
  *r=0;
  *w=0;

  if (fd==-1) return -1;
  
  if (fd==-2) {
    fd = open("/proc/stat", O_RDONLY | O_NDELAY);
    if (fd==-1) {
      error ("open(/proc/stat) failed: %s", strerror(errno));
      return -1;
    }
    debug ("open (/proc/stat)=%d", fd);
  }
  
  if (lseek(fd, 0L, SEEK_SET)!=0) {
    error ("lseek(/proc/stat) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }
  if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
    error ("read(/proc/stat) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  p=strstr(buffer, "disk_io:");
  if (p!=NULL) {
    unsigned long rsum=0, wsum=0;
    p+=8; while (*p==' ') p++;
    while (*p && *p!='\n') {
      int i, n;
      unsigned long dummy;
      unsigned long rblk, wblk;
      i=sscanf(p, "(%lu,%lu):(%lu,%lu,%lu,%lu,%lu)%n", 
	       &dummy, &dummy, &dummy, &dummy, &rblk, &dummy, &wblk, &n);
      if (n==0 || i!= 7) {
	i=sscanf(p, "(%lu,%lu):(%lu,%lu,%lu,%lu)%n",
		 &dummy, &dummy, &dummy, &rblk, &dummy, &wblk, &n)+1;
      }
      if (n>0 && i==7) {
	rsum+=rblk;
	wsum+=wblk;
	p+=n; while (*p==' ') p++;
      } else {
	error ("parse(/proc/stat) failed: unknown 'disk_io' format");
	fd=-1;
	return -1;
      }
    }
    // assume that we got the number of sectors, and a sector size of 512 bytes.
    // to get te number in kilobytes/sec, we calculate as follows:
    // kb=blocks*512/1024, which is blocks/2
    *r=smooth ("disk_r", 500, rsum/2);
    *w=smooth ("disk_w", 500, wsum/2);

  } else {

    unsigned long r1, r2, r3, r4;
    unsigned long w1, w2, w3, w4;
    p=strstr(buffer, "disk_rblk");
    if (p==NULL) {
      error ("parse(/proc/stat) failed: neither 'disk_io' nor 'disk_rblk' found");
      fd=-1;
      return -1;
    }
    if (sscanf(p+9, "%lu %lu %lu %lu", &r1, &r2, &r3, &r4)!=4) {
      error ("parse(/proc/stat) failed: unknown 'disk_rblk' format");
      fd=-1;
      return -1;
    }
    p=strstr(buffer, "disk_wblk");
    if (p==NULL) {
      error ("parse(/proc/stat) failed: no 'disk_wblk' line");
      fd=-1;
      return -1;
    }
    if (sscanf(p+9, "%lu %lu %lu %lu", &w1, &w2, &w3, &w4)!=4) {
      error ("parse(/proc/stat) failed: unknown 'disk_wblk' format");
      fd=-1;
      return -1;
    }
    *r=smooth ("disk_r", 500, r1+r2+r3+r4);
    *w=smooth ("disk_w", 500, w1+w2+w3+w4);
  }
  
  return 0;
}


int Net (int *rx, int *tx, int *bytes)
{
  char buffer[4096], *p, *s;
  static int fd=-2;
  unsigned long pkg_rx, pkg_tx;
  
  *rx=0;
  *tx=0;
  *bytes=0;

  if (fd==-1) return -1;
  
  if (fd==-2) {
    fd = open("/proc/net/dev", O_RDONLY | O_NDELAY);
    if (fd==-1) {
      error ("open(/proc/net/dev) failed: %s", strerror(errno));
      return -1;
    }
    debug ("open (/proc/net/dev)=%d", fd);
  }
  
  if (lseek(fd, 0L, SEEK_SET)!=0) {
    error ("lseek(/proc/net/dev) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  if (read (fd, &buffer, sizeof(buffer)-1)==-1) {
    error ("read(/proc/net/dev) failed: %s", strerror(errno));
    fd=-1;
    return -1;
  }

  pkg_rx=0;
  pkg_tx=0;
  p=buffer;
  while ((s=strsep(&p, "\n"))) {
    int n, u;
    unsigned long v[16]={0};
    n=sscanf (s, " eth%d:%lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu",
	      &u, &v[0], &v[1], &v[2], &v[3], &v[4], &v[5], &v[6], &v[7],
	      &v[8], &v[9], &v[10], &v[11], &v[12], &v[13], &v[14], &v[15]);

    if (n==17) {
      pkg_rx+=v[0];
      pkg_tx+=v[8];
      *bytes=1;
    } else if (n==12) {
      pkg_rx+=v[0];
      pkg_tx+=v[5];
      *bytes=0;
    } else if (n>0) {
      error ("parse(/proc/net/dev) failed: unknown format");
      fd=-1;
      return -1;
    }
  }

  *rx=smooth("net_rx", 500, pkg_rx);
  *tx=smooth("net_tx", 500, pkg_tx);

  return 0;
}


int PPP (int unit, int *rx, int *tx)
{
  static int fd=-2;
  char buffer[16];
  
#ifdef HAVE_NET_IF_PPP_H
  struct ifpppstatsreq req;
#endif

  *rx=0;
  *tx=0;

#ifdef HAVE_NET_IF_PPP_H

  if (fd==-1) return -1;
  
  if (fd==-2) {
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd==-1) {
      error ("socket() failed: %s", strerror(errno));
      return -1;
    }
    debug ("socket()=%d", fd);
  }

  memset (&req, 0, sizeof (req));
  req.stats_ptr = (caddr_t) &req.stats;
  snprintf (req.ifr__name, sizeof(req.ifr__name), "ppp%d", unit);
  
  if (ioctl(fd, SIOCGPPPSTATS, &req) < 0)
    return 0;
  
  snprintf (buffer, sizeof(buffer), "ppp%d_rx", unit);
  *rx=smooth(buffer, 500, req.stats.p.ppp_ibytes);
  snprintf (buffer, sizeof(buffer), "ppp%d_tx", unit);
  *tx=smooth(buffer, 500, req.stats.p.ppp_obytes);

#endif

  return 0;
}


int Sensor (int index, double *val, double *min, double *max)
{
  char buffer[32];
  double dummy, value;
  static int fd[SENSORS+1]={[0 ... SENSORS]=-2,};
  static char *sensor[SENSORS+1]={NULL,};
  static double val_buf[SENSORS+1]={0.0,};
  static double min_buf[SENSORS+1]={0.0,};
  static double max_buf[SENSORS+1]={0.0,};
  static double factor_buf[SENSORS+1]={0.0,};
  static double offset_buf[SENSORS+1]={0.0,};
  static time_t now[SENSORS+1]={0,};

  if (index<0 || index>SENSORS) return -1;

  *val=val_buf[index];
  *min=min_buf[index];
  *max=max_buf[index];

  if (fd[index]==-1) return -1;

  if (time(NULL)==now[index]) return 0;
  time(&now[index]);

  if (fd[index]==-2) {
    snprintf(buffer, 32, "Sensor%d", index);
    sensor[index]=cfg_get(buffer, NULL);
    if (sensor[index]==NULL || *sensor[index]=='\0') {
      error ("no entry for '%s' in %s", buffer, cfg_source());
      fd[index]=-1;
      return -1;
    }

    snprintf(buffer, 32, "Sensor%d_min", index);
    min_buf[index]=atof(cfg_get(buffer,"0"));
    *min=min_buf[index];

    snprintf(buffer, 32, "Sensor%d_max", index);
    max_buf[index]=atof(cfg_get(buffer,"100"));
    *max=max_buf[index];

    snprintf(buffer, 32, "Sensor%d_factor", index);
    factor_buf[index]=atof(cfg_get(buffer,"1"));

    snprintf(buffer, 32, "Sensor%d_offset", index);
    offset_buf[index]=atof(cfg_get(buffer,"0"));

    fd[index]=open(sensor[index], O_RDONLY);
    if (fd[index]==-1) {
      error ("open(%s) failed: %s", sensor[index], strerror(errno));
      return -1;
    }
    debug ("open (%s)=%d", sensor[index], fd[index]);
  }

  if (lseek(fd[index], 0L, SEEK_SET)!=0) {
    error ("lseek(%s) failed: %s", sensor[index], strerror(errno));
    fd[index]=-1;
    return -1;
  }

  if (read (fd[index], &buffer, sizeof(buffer)-1)==-1) {
    error ("read(%s) failed: %s", sensor[index], strerror(errno));
    fd[index]=-1;
    return -1;
  }

  if (sscanf(buffer, "%lf %lf %lf", &dummy, &dummy, &value)!=3) {
    error ("parse(%s) failed: unknown format", sensor[index]);
    fd[index]=-1;
    return -1;
  }
  value *= factor_buf[index];
  value += offset_buf[index];
  val_buf[index]=value;
  *val=value;
  return 0;
}