/* $Id: system.c,v 1.29 2003/08/24 05:17:58 reinelt Exp $ * * system status retreivement * * Copyright 1999, 2000 by Michael Reinelt (reinelt@eunet.at) * * This program 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. * * This program 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.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 * * 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 * * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #ifdef HAVE_NET_IF_PPP_H #include #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)); } }
/* $Id: plugin_ppp.c,v 1.6 2004/06/17 06:23:43 reinelt Exp $
 *
 * plugin for ppp throughput
 *
 * Copyright 2003 Michael Reinelt <reinelt@eunet.at>
 * Copyright 2004 The LCD4Linux Team <lcd4linux-devel@users.sourceforge.net>
 *
 * 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: plugin_ppp.c,v $
 * Revision 1.6  2004/06/17 06:23:43  reinelt
 *
 * hash handling rewritten to solve performance issues
 *
 * Revision 1.5  2004/05/29 15:53:28  reinelt
 *
 * M50530: reset parport signals on exit
 * plugin_ppp: ppp() has two parameters, not three
 * lcd4linux.conf.sample: diskstats() corrected
 *
 * Revision 1.4  2004/03/03 04:44:16  reinelt
 * changes (cosmetics?) to the big patch from Martin
 * hash patch un-applied
 *
 * Revision 1.3  2004/03/03 03:47:04  reinelt
 * big patch from Martin Hejl:
 * - use qprintf() where appropriate
 * - save CPU cycles on gettimeofday()
 * - add quit() functions to free allocated memory
 * - fixed lots of memory leaks
 *
 * Revision 1.2  2004/01/28 06:43:31  reinelt
 * plugin_ppp finished.
 *
 * Revision 1.1  2004/01/27 08:13:39  reinelt
 * ported PPP token to plugin_ppp
 *
 */

/* 
 * exported functions:
 *
 * int plugin_init_ppp (void)
 *  adds ppp() function
 *
 */

#include "config.h"

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <sys/ioctl.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 "plugin.h"
#include "qprintf.h"
#include "hash.h"

#ifdef HAVE_NET_IF_PPP_H

static HASH PPP;

static int get_ppp_stats (void)
{
  int age;
  int unit;
  unsigned ibytes, obytes;
  static int fd=-2;
  struct ifpppstatsreq req;
  char key[16], val[16];

  // reread every 10 msec only
  age=hash_age(&PPP, NULL);
  if (age>0 && age<=10) return 0;
  
  // open socket only once
  if (fd==-2) {
    fd = socket(AF_INET, SOCK_DGRAM, 0);
    if (fd==-1) {
      error ("socket() failed: %s", strerror(errno));
      return -1;
    }
  }
  
  for (unit=0; unit<8; unit++) {
    memset (&req, 0, sizeof (req));
    req.stats_ptr = (caddr_t) &req.stats;
    qprintf(req.ifr__name, sizeof(req.ifr__name), "ppp%d", unit);
    
    if (ioctl(fd, SIOCGPPPSTATS, &req) == 0) {
      ibytes=req.stats.p.ppp_ibytes;
      obytes=req.stats.p.ppp_obytes;
    } else {
      ibytes=obytes=0;
    }
    qprintf(key, sizeof(key), "Rx:%d", unit);
    qprintf(val, sizeof(val), "%d", ibytes);
    hash_put_delta (&PPP, key, val);
    qprintf(key, sizeof(key), "Tx:%d", unit);
    qprintf(val, sizeof(val), "%d", obytes);
    hash_put_delta (&PPP, key, val);

  }
  return 0;
}


static void my_ppp (RESULT *result, RESULT *arg1, RESULT *arg2)
{
  double value;
  
  if (get_ppp_stats()<0) {
    SetResult(&result, R_STRING, ""); 
    return;
  }
  value=hash_get_delta(&PPP, R2S(arg1), NULL, R2N(arg2));
  SetResult(&result, R_NUMBER, &value); 
}

#endif


int plugin_init_ppp (void)
{
  hash_create(&PPP);
#ifdef HAVE_NET_IF_PPP_H
  AddFunction ("ppp", 2, my_ppp);
#endif
  return 0;
}

void plugin_exit_ppp(void) 
{
  hash_destroy(&PPP);
}