aboutsummaryrefslogtreecommitdiffstats
path: root/config.h.in (follow)
AgeCommit message (Collapse)AuthorFilesLines
2006-06-25[lcd4linux @ 2006-06-25 15:13:00 by reinelt]reinelt1-3/+6
automake-1.9 cleanups
2006-06-20[lcd4linux @ 2006-06-20 08:50:58 by reinelt]reinelt1-2/+5
widget_image linker error hopefully finally fixed
2006-06-19[lcd4linux @ 2006-06-19 15:12:54 by reinelt]reinelt1-2/+2
bootstrapped
2006-04-15[lcd4linux @ 2006-04-15 05:22:52 by reinelt]reinelt1-0/+6
mpd plugin from Stefan Kuhne
2006-02-19[lcd4linux @ 2006-02-19 15:42:18 by reinelt]reinelt1-0/+3
file plugin from Chris Maj
2006-01-26[lcd4linux @ 2006-01-26 19:26:26 by harbaum]harbaum1-0/+3
Added LCD2USB support
2006-01-21[lcd4linux @ 2006-01-21 13:26:43 by reinelt]reinelt1-0/+3
Logitech G-15 keyboard LCD driver from Dave Ingram
2006-01-18[lcd4linux @ 2006-01-18 11:49:48 by reinelt]reinelt1-0/+3
adopted to lcd-linux-0.9.2
2006-01-03[lcd4linux @ 2006-01-03 13:20:05 by reinelt]reinelt1-0/+6
LUIse driver added
2005-11-04[lcd4linux @ 2005-11-04 14:10:38 by reinelt]reinelt1-0/+3
drv_Sample and drv_LPH7508
2005-11-04[lcd4linux @ 2005-11-04 04:53:08 by reinelt]reinelt1-0/+3
sample plugin activated
2005-11-04[lcd4linux @ 2005-11-04 04:44:52 by reinelt]reinelt1-0/+3
LPH7508 driver (not yet finished)
2005-08-27[lcd4linux @ 2005-08-27 07:02:25 by reinelt]reinelt1-0/+3
LCD-Linux updated to 0.9.0
2005-08-22[lcd4linux @ 2005-08-22 05:44:43 by reinelt]reinelt1-0/+3
new driver 'WincorNixdorf' some fixes to the bar code
2005-06-10[lcd4linux @ 2005-06-10 11:22:28 by reinelt]reinelt1-0/+6
double-check for X11 headers (AC_PATH_XTRA is buggy)
2005-06-01[lcd4linux @ 2005-06-01 12:09:11 by reinelt]reinelt1-3/+0
removed ^M from lcd4linux_i2c.h; indent.sh run
2005-05-31[lcd4linux @ 2005-05-31 20:42:54 by lfcorreia]lfcorreia1-6/+0
new file: lcd4linux_i2c.h avoid the problems detecting the proper I2C kernel include files rearrange all the other autoconf stuff to remove I2C detection new method by Paul Kamphuis to write to the I2C device
2005-05-13[lcd4linux @ 2005-05-13 05:43:25 by reinelt]reinelt1-0/+3
added drv_LCDLinux.h to extra_sources
2005-05-10[lcd4linux @ 2005-05-10 13:20:10 by reinelt]reinelt1-0/+6
added serdisplib driver
2005-05-04[lcd4linux @ 2005-05-04 05:42:37 by reinelt]reinelt1-0/+3
Noritake driver added
2005-05-02[lcd4linux @ 2005-05-02 10:29:20 by reinelt]reinelt1-0/+3
preparations for python bindings and python plugin
2005-04-24[lcd4linux @ 2005-04-24 04:33:46 by reinelt]reinelt1-0/+3
driver for TREFON USB LCD's added
2005-04-03[lcd4linux @ 2005-04-03 07:07:43 by reinelt]reinelt1-0/+6
added statfs plugin
2005-03-28[lcd4linux @ 2005-03-28 19:39:14 by reinelt]reinelt1-0/+15
HD44780/I2C patch from Luis merged (still does not work for me)
2005-02-24[lcd4linux @ 2005-02-24 07:06:45 by reinelt]reinelt1-0/+3
SimpleLCD driver added
2005-01-22[lcd4linux @ 2005-01-22 22:57:57 by reinelt]reinelt1-0/+3
LCD-Linux driver added
2005-01-15[lcd4linux @ 2005-01-15 13:10:13 by reinelt]reinelt1-0/+3
LCDTerm driver added
2004-09-24[lcd4linux @ 2004-09-24 21:40:52 by reinelt]reinelt1-0/+6
new driver for the BWCT USB LCD interface board.
2004-08-29[lcd4linux @ 2004-08-29 13:03:40 by reinelt]reinelt1-0/+3
added RouterBoard driver
2004-06-08[lcd4linux @ 2004-06-08 12:35:24 by reinelt]reinelt1-21/+6
autoconf/automake updates
2004-05-31[lcd4linux @ 2004-05-31 16:39:05 by reinelt]reinelt1-0/+3
added NULL display driver (for debugging/profiling purposes) added backlight/contrast initialisation for matrixOrbital added Backlight initialisation for Cwlinux
2004-05-29[lcd4linux @ 2004-05-29 00:27:14 by reinelt]reinelt1-0/+3
added plugin_diskstats.c
2004-05-26[lcd4linux @ 2004-05-26 11:37:35 by reinelt]reinelt1-3/+3
Curses driver ported.
2004-05-25[lcd4linux @ 2004-05-25 19:54:11 by reinelt]reinelt1-3/+0
'make distcheck' bugs fixed release number changed to 0.10.0-RC1
2004-05-22[lcd4linux @ 2004-05-22 18:30:01 by reinelt]reinelt1-0/+3
added plugin 'uptime'
2004-04-12[lcd4linux @ 2004-04-12 11:12:24 by reinelt]reinelt1-0/+6
added plugin_isdn, removed old ISDN client fixed some real bad bugs in the evaluator
2004-04-09[lcd4linux @ 2004-04-09 06:09:54 by reinelt]reinelt1-3/+51
big configure rework from Xavier
2004-03-19[lcd4linux @ 2004-03-19 06:37:47 by reinelt]reinelt1-3/+0
asynchronous thread handling started
2004-03-10[lcd4linux @ 2004-03-10 07:16:15 by reinelt]reinelt1-9/+3
MySQL plugin from Javier added
2004-02-10[lcd4linux @ 2004-02-10 07:42:35 by reinelt]reinelt1-20/+14
cut off all old-style files which are no longer used with NextGeneration
2004-01-30[lcd4linux @ 2004-01-30 20:57:55 by reinelt]reinelt1-0/+3
HD44780 patch from Martin Hejl dmalloc integrated
2004-01-29[lcd4linux @ 2004-01-29 05:53:47 by reinelt]reinelt1-0/+30
uClibc compatibility issues from Martin Hejl
2004-01-20[lcd4linux @ 2004-01-20 15:32:48 by reinelt]reinelt1-283/+105
first version of Next Generation HD44780 (untested! but it compiles...) some cleanup in the other drivers
2004-01-07[lcd4linux @ 2004-01-07 10:15:41 by reinelt]reinelt1-107/+287
small glitch in evaluator fixed made config table sorted and access with bsearch(), which should be much faster
2003-10-07[lcd4linux @ 2003-10-07 04:12:38 by reinelt]reinelt1-3/+12
AM_PROG_LIBTOOL removed
2003-10-04[lcd4linux @ 2003-10-04 07:54:17 by reinelt]reinelt1-0/+3
autoconf/automake/libtool fixes from Ronald Landheer-Cieslak
2003-09-29[lcd4linux @ 2003-09-29 06:58:36 by reinelt]reinelt1-0/+3
new driver for Milford Instruments MI420 by Andy Baxter
2003-09-13[lcd4linux @ 2003-09-13 06:20:39 by reinelt]reinelt1-3/+0
HD44780 timings changed; deactivated libtool
2003-09-01[lcd4linux @ 2003-09-01 07:07:03 by reinelt]reinelt1-0/+6
shared liblcd4linux
2003-08-12[lcd4linux @ 2003-08-12 05:10:31 by reinelt]reinelt1-7/+0
first version of HD44780 4Bit-Mode patch
'n758' href='#n758'>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
/* $Id: system.c,v 1.30 2003/10/05 17:58:50 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.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) {
    fd=-1;
    return -1;
  }
  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;
}