📄 system.c
字号:
/* $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 <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;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -