New lcdproc client by Thomas Runge. Works on Solaris, Linux, NetBSD, OpenBSD and FreeBSD.

This commit is contained in:
gfk
2003-12-24 19:47:55 +00:00
parent 56db57f518
commit 1372677298
30 changed files with 2921 additions and 2275 deletions
+1 -1
View File
@@ -1,4 +1,4 @@
bin_PROGRAMS = lcdproc
lcdproc_SOURCES = main.c main.h mode.c mode.h batt.c batt.h chrono.c chrono.h cpu.c cpu.h cpu_smp.c cpu_smp.h disk.c disk.h load.c load.h mem.c mem.h
lcdproc_SOURCES = main.c main.h mode.c mode.h batt.c batt.h chrono.c chrono.h cpu.c cpu.h cpu_smp.c cpu_smp.h disk.c disk.h load.c load.h mem.c mem.h machine_Linux.c machine_OpenBSD.c machine_FreeBSD.c machine_NetBSD.c machine_SunOS.c
lcdproc_LDADD = ../../shared/libLCDstuff.a
INCLUDES = -I$(top_srcdir)
+86 -187
View File
@@ -7,106 +7,12 @@
#include "shared/sockets.h"
#ifdef __NetBSD__
#include <errno.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <machine/apmvar.h>
#endif
#include "main.h"
#include "mode.h"
#include "batt.h"
#include "machine.h"
int batt_fd = 0;
static int get_batt_stat (int *acstat, int *battstat, int *battflag, int *percent);
static int
get_batt_stat (int *acstat, int *battstat, int *battflag, int *percent)
{
#ifdef __NetBSD__
struct apm_power_info apmi;
int apmd;
if((apmd = open("/dev/apm", O_RDONLY)) == -1)
return -1;
memset(&apmi, 0, sizeof(apmi));
if(ioctl(apmd, APM_IOC_GETPOWER, &apmi) == -1)
{
fprintf(stderr, "APM_IOC_GETPOWER failed in get_batt_stat(): %s\n",
strerror(errno));
return -1;
}
/* don't know, what the Linux counterparts exactly mean */
switch(apmi.ac_state)
{
case APM_AC_OFF:
*acstat = 0;
break;
case APM_AC_ON:
*acstat = 1;
break;
case APM_AC_BACKUP:
*acstat = 2;
break;
default:
*acstat = apmi.ac_state;
break;
}
*battstat = 0;
*battflag = apmi.battery_state;
*percent = apmi.battery_life;
close(apmd);
#else
char str[64];
if (!batt_fd)
batt_fd = open ("/proc/apm", O_RDONLY);
if (batt_fd <= 0)
return -1;
if (lseek (batt_fd, 0, 0) != 0)
return -1;
if (read (batt_fd, str, sizeof (str) - 1) < 0)
return -1;
if (3 > sscanf (str + 13, "0x%x 0x%x 0x%x %d", acstat, battstat, battflag, percent))
return -1;
#endif
return 0;
}
int
batt_init ()
{
static int first = 1;
if (first) {
first = 0;
sock_send_string (sock, "screen_add B\n");
}
return 0;
}
int
batt_close ()
{
if (batt_fd)
close (batt_fd);
batt_fd = 0;
return 0;
}
static int first = TRUE;
////////////////////////////////////////////////////////////////////////
// Battery Screen shows apm battery status...
@@ -121,18 +27,17 @@ batt_close ()
int
battery_screen (int rep, int display)
{
static int first = 1;
int acstat = 0, battflag = 0, percent = 0;
int acstat = 0, battstat = 0, battflag = 0, percent = 0;
if (first == TRUE) {
first = FALSE;
if (first) {
first = 0;
sprintf (buffer, "screen_set B -name {APM stats: %s}\n", host);
sock_send_string (sock, "screen_add B\n");
sprintf (buffer, "screen_set B -name {APM stats: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add B title title\n");
sprintf (tmp, "widget_set B title {LCDPROC %s}\n", version);
sock_send_string (sock, tmp);
sprintf (buffer, "widget_set B title {LCDPROC %s}\n", version);
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add B one string\n");
if (lcd_hgt >= 4) {
sock_send_string (sock, "widget_add B two string\n");
@@ -147,111 +52,105 @@ battery_screen (int rep, int display)
// Only run once every 16 frames...
//if(rep&0x0f) return 0;
get_batt_stat (&acstat, &battstat, &battflag, &percent);
machine_get_battstat(&acstat, &battflag, &percent);
if (percent >= 0)
sprintf (buffer, "%i%%", percent);
if(percent >= 0)
sprintf(tmp, "%d%%", percent);
else
sprintf (buffer, "??%%");
sprintf (tmp, "widget_set B title {Batt: %s: %s}\n", buffer, host);
sprintf(tmp, "??%%");
sprintf(buffer, "widget_set B title {Batt: %s: %s}\n", tmp, get_hostname());
if(display)
sock_send_string (sock, buffer);
if (display)
sock_send_string (sock, tmp);
if (lcd_hgt >= 4) // 4-line version of the screen
if(lcd_hgt >= 4) // 4-line version of the screen
{
sprintf (tmp, "widget_set B one 1 2 {");
switch (acstat) {
case 0:
sprintf (tmp + strlen (tmp), "AC: Off}\n");
break;
case 1:
sprintf (tmp + strlen (tmp), "AC: On}\n");
break;
case 2:
sprintf (tmp + strlen (tmp), "AC: Backup}\n");
break;
default:
sprintf (tmp + strlen (tmp), "AC: Unknown}\n");
break;
strcpy(buffer, "widget_set B one 1 2 {AC: ");
switch(acstat)
{
case LCDP_AC_OFF:
strcat(buffer, "Off");
break;
case LCDP_AC_ON:
strcat(buffer, "On");
break;
case LCDP_AC_BACKUP:
strcat(buffer, "Backup");
break;
case LCDP_AC_UNKNOWN:
default:
strcat(buffer, "Unknown");
break;
}
strcat(buffer, "}\n");
if(display)
sock_send_string (sock, buffer);
if (display)
sock_send_string (sock, tmp);
sprintf (tmp, "widget_set B two 1 3 {");
#ifdef __NetBSD__
strcpy(buffer, "widget_set B two 1 3 {Batt: ");
switch(battflag)
{
case APM_BATT_HIGH:
sprintf (tmp + strlen (tmp), "Batt: High");
case LCDP_BATT_HIGH:
strcat(buffer, "High");
break;
case APM_BATT_LOW:
sprintf (tmp + strlen (tmp), "Batt: Low");
case LCDP_BATT_LOW:
strcat(buffer, "Low");
break;
case APM_BATT_CRITICAL:
sprintf (tmp + strlen (tmp), "Batt: Critical");
case LCDP_BATT_CRITICAL:
strcat(buffer, "Critical");
break;
case APM_BATT_CHARGING:
sprintf (tmp + strlen (tmp), "Batt: Charging");
case LCDP_BATT_CHARGING:
strcat(buffer, "Charging");
break;
case APM_BATT_UNKNOWN:
case LCDP_BATT_ABSENT:
strcat(buffer, "Absent");
break;
case LCDP_BATT_UNKNOWN:
default:
sprintf (tmp + strlen (tmp), "Batt: Unknown");
strcat(buffer, "Unknown");
break;
}
#else
if (battflag == 0xff) {
sprintf (tmp + strlen (tmp), "Battery Stat Unknown");
} else {
sprintf (tmp + strlen (tmp), "Batt:");
if (battflag & 1)
sprintf (tmp + strlen (tmp), " High");
if (battflag & 2)
sprintf (tmp + strlen (tmp), " Low");
if (battflag & 4)
sprintf (tmp + strlen (tmp), " Critical");
if (battflag & 8 || battstat == 3)
sprintf (tmp + strlen (tmp), " Charging");
if (battflag & 128)
sprintf (tmp + strlen (tmp), " (NONE)");
}
#endif
sprintf (tmp + strlen (tmp), "}\n");
if (display)
sock_send_string (sock, tmp);
strcat(buffer, "}\n");
if(display)
sock_send_string(sock, buffer);
if (percent > 0) {
sprintf (tmp, "widget_set B gauge 2 4 %i\n", (percent * ((lcd_wid - 2) * lcd_cellwid) / 100));
if (display)
sock_send_string (sock, tmp);
if(percent > 0)
{
sprintf(buffer, "widget_set B gauge 2 4 %d\n", (percent * ((lcd_wid - 2) * lcd_cellwid) / 100));
if(display)
sock_send_string(sock, buffer);
}
} // end if(lcd_hgt >= 4)
else // two-line version of the screen
{
sprintf (tmp, "widget_set B one 1 2 {");
if (acstat == 1)
sprintf (tmp + strlen (tmp), "AC, ");
strcpy(buffer, "widget_set B one 1 2 {");
if(acstat == LCDP_AC_ON)
strcat(buffer, "AC, ");
strcat(buffer, "Batt: ");
if (battflag == 0xff) {
sprintf (tmp + strlen (tmp), "No battery???");
} else {
sprintf (tmp + strlen (tmp), "Batt:");
if (battflag & 1)
sprintf (tmp + strlen (tmp), " High");
if (battflag & 2)
sprintf (tmp + strlen (tmp), " Low");
if (battflag & 4)
sprintf (tmp + strlen (tmp), " Critical");
if (battflag & 8 || battstat == 3)
sprintf (tmp + strlen (tmp), " Charging");
if (battflag & 128)
sprintf (tmp + strlen (tmp), " (NONE)");
switch(battflag)
{
case LCDP_BATT_HIGH:
strcat(buffer, "High");
break;
case LCDP_BATT_LOW:
strcat(buffer, "Low");
break;
case LCDP_BATT_CRITICAL:
strcat(buffer, "Critical");
break;
case LCDP_BATT_CHARGING:
strcat(buffer, "Charging");
break;
case LCDP_BATT_ABSENT:
strcat(buffer, "Absent");
break;
case LCDP_BATT_UNKNOWN:
default:
strcat(buffer, "Unknown");
break;
}
sprintf (tmp + strlen (tmp), "}\n");
if (display)
sock_send_string (sock, tmp);
sprintf(buffer, "}\n");
if(display)
sock_send_string(sock, buffer);
}
return 0;
-3
View File
@@ -1,9 +1,6 @@
#ifndef BATT_H
#define BATT_H
int batt_init ();
int batt_close ();
int battery_screen (int rep, int display);
#endif
+19 -307
View File
@@ -1,9 +1,14 @@
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/utsname.h>
#include <limits.h>
#include <sys/param.h>
#include <errno.h>
#ifdef HAVE_CONFIG_H
# include "config.h"
@@ -24,257 +29,10 @@
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "chrono.h"
#if HAVE_UTMPX_H
#include <utmpx.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_LIMITS_H
#include <limits.h>
#endif
#if HAVE_KVM_H
#include <kvm.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#if HAVE_SYS_SCHED_H
#include <sys/sched.h>
#endif
#if HAVE_SYS_DKSTAT_H
#include <sys/dkstat.h>
#endif
#if FREEBSD
/* definitions for indices in the nlist array */
/* from /usr/src/src.bin/top/machine.c */
static struct nlist nlst[] = {
#define X_CCPU 0
{ "_ccpu" },
#define X_CP_TIME 1
{ "_cp_time" },
{ 0 }
};
#elif OPENBSD
#define X_CP_TIME 0
static struct nlist nlst[] = {
{ "_cp_time" }, /* 0 */
{ 0 }
};
#endif
int TwentyFourHour = 1;
int uptime_fd = 0;
#ifdef SYS_NMLN
static char kver[SYS_NMLN];
static char sysname[SYS_NMLN];
#else
# ifdef _SYS_NAMELEN
static char kver[_SYS_NAMELEN];
static char sysname[_SYS_NAMELEN];
# else
// Last hope! May segfault!
static char *kver;
static char *sysname;
# endif
#endif
#if FREEBSD
static double freebsd_get_uptime(double *up, double *idle);
#define get_uptime freebsd_get_uptime
#elif (__NetBSD__ || OPENBSD)
static double opennetbsd_get_uptime(double *up, double *idle);
#define get_uptime opennetbsd_get_uptime
#elif LINUX
static double linux_get_uptime(double *up, double *idle);
#define get_uptime linux_get_uptime
#elif SOLARIS
static double solaris_get_uptime(double *up, double *idle);
#define get_uptime solaris_get_uptime
#else
#error "Does not know how to get the uptime."
#endif
#if FREEBSD
static double freebsd_get_uptime(double *up, double *idle) {
double uptime = 0;
struct timeval boottime;
time_t now, uuptime;
size_t size;
long cp_time[CPUSTATES];
long iidle = 0;
int mib[2];
char errbuf[_POSIX2_LINE_MAX];
kvm_t *kvmd = NULL;
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
time(&now);
if(sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
boottime.tv_sec != 0)
{
uuptime = now - boottime.tv_sec;
uuptime += 30;
uptime = (double) uuptime;
}
/* open kernel virtual memory */
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
if(kvmd==NULL)
{
fprintf(stderr, "kvm_openfiles: %s\n", errbuf);
exit(EXIT_FAILURE);
}
else
{
/* ask for specified values */
if(kvm_nlist(kvmd, nlst) >= 0)
{
if(nlst[X_CCPU].n_type != 0)
{
/* read values for cpu time (user, nice, sys, intr, idle) */
if(kvm_read(kvmd, nlst[X_CP_TIME].n_value, (char *)&cp_time,
sizeof(cp_time))==sizeof(cp_time))
{
//ZZZ oder besser alle anderen zusammenaddieren und durch
// idle dividieren?
iidle = cp_time[CP_IDLE];
}
}
}
kvm_close(kvmd);
*idle = (double) (iidle / 100.);
}
*up = uptime;
return uptime;
}
#endif
#if OPENBSD
static long get_openbsd_idle_time() {
kvm_t *kvmd;
long cp_time[CPUSTATES];
char errbuf[_POSIX2_LINE_MAX];
cp_time[CP_IDLE] = 0;
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
if(kvmd==NULL) {
fprintf(stderr, "kvm_openfiles: %s\n", errbuf);
return(0);
} else{
if(kvm_nlist(kvmd, nlst) >= 0) {
if(kvm_read(kvmd, nlst[X_CP_TIME].n_value, (char *)cp_time, sizeof(cp_time)) != sizeof(cp_time)) {
fprintf(stderr, "kvm_read: %s\n", errbuf);
}
}
kvm_close(kvmd);
}
return(cp_time[CP_IDLE]);
}
#endif
#if (__NetBSD__ || OPENBSD)
static double opennetbsd_get_uptime(double *up, double *idle) {
double uptime = 0;
struct timeval boottime;
u_int64_t iidle = 0;
time_t now, uuptime;
size_t size;
int mib[2];
#if __NetBSD__
u_int64_t cp_time[CPUSTATES];
#endif
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
time(&now);
if(sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
fprintf(stderr, "sysctl kern.boottime failed: %s\n",
strerror(errno));
}
else
if(boottime.tv_sec != 0) {
uuptime = now - boottime.tv_sec;
uuptime += 30;
uptime = (double) uuptime;
}
#if OPENBSD
iidle = get_openbsd_idle_time();
#else
mib[0] = CTL_KERN;
mib[1] = KERN_CP_TIME;
size = sizeof(cp_time);
if(sysctl(mib, 2, cp_time, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl kern.cp_time failed: %s\n",
strerror(errno));
}
else
iidle = cp_time[CP_IDLE];
#endif // __OpenBSD__
*up = uptime;
*idle = (double) (iidle / 100.);
return uptime;
}
#endif // NetBSD or OpenBSD
#if LINUX
static double linux_get_uptime (double *up, double *idle) {
double uptime;
reread (uptime_fd, "get_uptime:");
sscanf (buffer, "%lf %lf", &uptime, idle);
*up = uptime;
return uptime;
}
#endif
#if SOLARIS
static double solaris_get_uptime (double *up, double *idle) {
double uptime;
struct utmpx *u, id;
id.ut_type = BOOT_TIME;
u = getutxid(&id);
uptime=time(0) - u->ut_xtime;
*up = uptime;
return uptime;
}
#endif
static int TwentyFourHour = 1;
// A couple of tables for later use...
static char *days[] = {
@@ -325,51 +83,6 @@ static char *shortmonths[] = {
"Dec",
};
int
chrono_init ()
{
struct utsname *unamebuf;
if (!uptime_fd) {
unamebuf = (struct utsname *) malloc (sizeof (struct utsname));
uptime_fd = open ("/proc/uptime", O_RDONLY);
#if 0
kversion_fd = open ("/proc/sys/kernel/osrelease", O_RDONLY);
reread (kversion_fd, "main:");
sscanf (buffer, "%s", kver);
close (kversion_fd);
#endif
/* Get OS name and version from uname() */
/* Changed to check if eq -1 instead of non-zero */
/* since uname may return any non-negative value */
if (uname (unamebuf) == -1) {
perror ("Error calling uname:");
}
strcpy (kver, unamebuf->release);
strcpy (sysname, unamebuf->sysname);
free (unamebuf);
}
return 0;
}
int
chrono_close ()
{
if (uptime_fd)
close (uptime_fd);
uptime_fd = 0;
return 0;
}
//////////////////////////////////////////////////////////////////////
// Time Screen displays current time and date, uptime, OS ver...
//
@@ -398,7 +111,7 @@ time_screen (int rep, int display)
first = 0;
sock_send_string (sock, "screen_add T\n");
sprintf (buffer, "screen_set T -name {Time Screen: %s}\n", host);
sprintf (buffer, "screen_set T -name {Time Screen: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add T title title\n");
sock_send_string (sock, "widget_set T title {Time Screen}\n");
@@ -407,7 +120,7 @@ time_screen (int rep, int display)
sock_send_string (sock, "widget_add T two string\n");
sock_send_string (sock, "widget_add T three string\n");
} else {
sprintf (buffer, "widget_set T title {TIME: %s}\n", host);
sprintf (buffer, "widget_set T title {TIME: %s}\n", get_hostname());
sock_send_string (sock, buffer);
}
}
@@ -449,14 +162,13 @@ time_screen (int rep, int display)
if (lcd_hgt >= 4) {
sprintf (tmp, "widget_set T title {");
sprintf (tmp + strlen (tmp), "%s %s: %s}\n", sysname, kver, host);
sprintf (tmp + strlen (tmp), "%s %s: %s}\n", get_sysname(), get_sysrelease(), get_hostname());
if (display)
sock_send_string (sock, tmp);
}
/////////////////////// Display the time...
if (lcd_hgt >= 4) {
get_uptime (&uptime, &idle);
idle = (idle * 100) / uptime;
machine_get_uptime(&uptime, &idle);
if (TwentyFourHour)
sprintf (tmp, "%s%c%s%c%s %2i%% idle", hr, colon, min, colon, sec, (int) idle);
@@ -534,7 +246,7 @@ clock_screen (int rep, int display)
first = 0;
sock_send_string (sock, "screen_add O\n");
sprintf (buffer, "screen_set O -name {Clock Screen: %s}\n", host);
sprintf (buffer, "screen_set O -name {Clock Screen: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add O title title\n");
sock_send_string (sock, "widget_add O one string\n");
@@ -542,10 +254,10 @@ clock_screen (int rep, int display)
sock_send_string (sock, "widget_set O title {DATE & TIME}\n");
sock_send_string (sock, "widget_add O two string\n");
sock_send_string (sock, "widget_add O three string\n");
sprintf (buffer, "widget_set O one 3 2 {%s}\n", host);
sprintf (buffer, "widget_set O one 3 2 {%s}\n", get_hostname());
sock_send_string (sock, buffer);
} else {
sprintf (buffer, "widget_set O title {TIME: %s}\n", host);
sprintf (buffer, "widget_set O title {TIME: %s}\n", get_hostname());
sock_send_string (sock, buffer);
}
}
@@ -633,7 +345,7 @@ uptime_screen (int rep, int display)
first = 0;
sock_send_string (sock, "screen_add U\n");
sprintf (buffer, "screen_set U -name {Uptime Screen: %s}\n", host);
sprintf (buffer, "screen_set U -name {Uptime Screen: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add U title title\n");
if (lcd_hgt >= 4) {
@@ -642,12 +354,12 @@ uptime_screen (int rep, int display)
sock_send_string (sock, "widget_add U two string\n");
sock_send_string (sock, "widget_add U three string\n");
sprintf (buffer, "widget_set U one 3 2 {%s}\n", host);
sprintf (buffer, "widget_set U one 3 2 {%s}\n", get_hostname());
sock_send_string (sock, buffer);
sprintf (tmp, "widget_set U three 5 4 {%s %s}\n", sysname, kver);
sprintf (tmp, "widget_set U three 5 4 {%s %s}\n", get_sysname(), get_sysrelease());
sock_send_string (sock, tmp);
} else {
sprintf (tmp, "widget_set U title {%s %s: %s}\n", sysname, kver, host);
sprintf (tmp, "widget_set U title {%s %s: %s}\n", get_sysname(), get_sysrelease(), get_hostname());
sock_send_string (sock, tmp);
sock_send_string (sock, "widget_add U one string\n");
}
@@ -658,7 +370,7 @@ uptime_screen (int rep, int display)
else
colon = ' ';
get_uptime (&uptime, &idle);
machine_get_uptime(&uptime, &idle);
i = (int) uptime / 86400;
sprintf (date, "%d day%s,", i, (i != 1 ? "s" : ""));
i = ((int) uptime % 86400) / 60 / 60;
-3
View File
@@ -1,9 +1,6 @@
#ifndef CHRONO_H
#define CHRONO_H
int chrono_init ();
int chrono_close ();
int clock_screen (int rep, int display);
int uptime_screen (int rep, int display);
int time_screen (int rep, int display);
+192 -433
View File
@@ -1,322 +1,90 @@
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <fcntl.h>
#include <unistd.h>
#include <limits.h>
#include <errno.h>
#include "shared/sockets.h"
#include "shared/debug.h"
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "cpu.h"
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_CPUVAR_H
#include <sys/cpuvar.h>
#endif
#ifdef HAVE_LIBKSTAT
#include <kstat.h>
#endif
#if HAVE_ERRNO_H
#include <errno.h>
#endif
#if HAVE_LIMITS_H
#include <limits.h>
#endif
#if HAVE_KVM_H
#include <kvm.h>
#endif
#if HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#if HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#if HAVE_SYS_SYSCTL_H
#include <sys/sysctl.h>
#endif
#if HAVE_SYS_SCHED_H
#include <sys/sched.h>
#endif
#if HAVE_SYS_DKSTAT_H
#include <sys/dkstat.h>
#endif
#if FREEBSD
/* definitions for indices in the nlist array */
/* from /usr/src/src.bin/top/machine.c */
static struct nlist nlst[] = {
#define X_CCPU 0
{ "_ccpu" },
#define X_CP_TIME 1
{ "_cp_time" },
{ 0 }
};
#elif OPENBSD
#define X_CP_TIME 0
static struct nlist nlst[] = {
{ "_cp_time" }, /* 0 */
{ 0 }
};
#endif
struct load {
unsigned long total, user, system, nice, idle;
};
#ifdef HAVE_LIBKSTAT
kstat_ctl_t *kc;
#endif
int load_fd = 0;
#ifdef __NetBSD__
static void netbsd_get_load (struct load * result, struct load * last_load );
#define get_load netbsd_get_load
#elif defined OPENBSD || FREEBSD
static void openfreebsd_get_load (struct load * result, struct load * last_load );
#define get_load openfreebsd_get_load
#elif defined SOLARIS || LINUX
static void linuxsolaris_get_load (struct load * result, struct load * last_load );
#define get_load linuxsolaris_get_load
#else
#error "Does not know how to get the CPU load on your system."
#endif
#if defined LINUX || SOLARIS
static void linuxsolaris_get_load (struct load *result, struct load * last_load ) {
struct load curr_load;
#ifndef HAVE_LIBKSTAT
reread (load_fd, "get_load:");
sscanf (buffer, "%*s %lu %lu %lu %lu\n", &curr_load.user, &curr_load.nice, &curr_load.system, &curr_load.idle);
#else
kstat_t *k_space;
void *val_ptr;
k_space = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0");
if (k_space == NULL) {
printf("\nkstat lookup error");
exit(1);
} else if (kstat_read(kc, k_space, NULL) == -1) {
printf("\nkstat read error");
exit(1);
}
k_space=kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0");
{
struct cpu_stat cinfo;
kstat_read(kc,k_space,&cinfo);
curr_load.idle=cinfo.cpu_sysinfo.cpu[CPU_IDLE];
curr_load.user=cinfo.cpu_sysinfo.cpu[CPU_USER];
curr_load.system=cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
curr_load.nice=cinfo.cpu_sysinfo.cpu[CPU_WAIT];
}
curr_load.nice=0;
#endif
curr_load.total = curr_load.user + curr_load.nice + curr_load.system + curr_load.idle;
result->total = curr_load.total - last_load->total;
result->user = curr_load.user - last_load->user;
result->nice = curr_load.nice - last_load->nice;
result->system = curr_load.system - last_load->system;
result->idle = curr_load.idle - last_load->idle;
*last_load = curr_load ;
}
#endif
#ifdef __NetBSD__
static void netbsd_get_load(struct load * result, struct load * last_load) {
struct load curr_load;
u_int64_t cp_time[CPUSTATES];
size_t size;
int mib[2];
mib[0] = CTL_KERN;
mib[1] = KERN_CP_TIME;
size = sizeof(cp_time);
if(sysctl(mib, 2, cp_time, &size, NULL, 0) < 0) {
fprintf(stderr, "sysctl kern.cp_time failed: %s\n", strerror(errno));
curr_load.user = 1;
curr_load.nice = 1;
curr_load.system = 1;
curr_load.idle = 1;
} else {
curr_load.user = cp_time[CP_USER];
curr_load.nice = cp_time[CP_NICE];
curr_load.system = cp_time[CP_SYS] + cp_time[CP_INTR];
curr_load.idle = cp_time[CP_IDLE];
}
curr_load.total = curr_load.user + curr_load.nice + curr_load.system + curr_load.idle;
result->total = curr_load.total - last_load->total;
result->user = curr_load.user - last_load->user;
result->nice = curr_load.nice - last_load->nice;
result->system = curr_load.system - last_load->system;
result->idle = curr_load.idle - last_load->idle;
*last_load = curr_load ;
}
#endif
#if defined OPENBSD || FREEBSD
static void openfreebsd_get_load(struct load * result, struct load * last_load) {
struct load curr_load;
long cp_time[CPUSTATES];
char errbuf[_POSIX2_LINE_MAX];
kvm_t *kvmd = NULL;
/* open kernel virtual memory */
kvmd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
if(kvmd==NULL) {
//fprintf(stderr, "kvm_openfiles: %s\n", errbuf);
curr_load.user = 1;
curr_load.nice = 1;
curr_load.system = 1;
curr_load.idle = 1;
} else {
/* ask for specified values */
if(kvm_nlist(kvmd, nlst) >= 0) {
/* read values for cpu time (user, nice, sys, intr, idle) */
if(kvm_read(kvmd, nlst[X_CP_TIME].n_value, (char *)&cp_time, sizeof(cp_time))==sizeof(cp_time)) {
curr_load.user = cp_time[CP_USER];
curr_load.nice = cp_time[CP_NICE];
curr_load.system = cp_time[CP_SYS] + cp_time[CP_INTR];
curr_load.idle = cp_time[CP_IDLE];
}
}
kvm_close(kvmd);
}
curr_load.total = curr_load.user + curr_load.nice + curr_load.system + curr_load.idle;
result->total = curr_load.total - last_load->total;
result->user = curr_load.user - last_load->user;
result->nice = curr_load.nice - last_load->nice;
result->system = curr_load.system - last_load->system;
result->idle = curr_load.idle - last_load->idle;
*last_load = curr_load ;
}
#endif
int
cpu_init ()
{
#ifndef HAVE_LIBKSTAT
if (!load_fd) {
# if defined LINUX || SOLARIS
load_fd = open ("/proc/stat", O_RDONLY);
# else
load_fd=1;
# endif
}
#else
kc = kstat_open();
if (kc == NULL) {
exit(1);
}
#endif
return 0;
}
int
cpu_close ()
{
#ifndef HAVE_LIBKSTAT
# if defined LINUX || SOLARIS
if (load_fd)
close (load_fd);
# endif
#else
kstat_close(kc);
#endif
load_fd = 0;
return 0;
}
//////////////////////////////////////////////////////////////////////////
// CPU screen shows info about percentage of the CPU being used
//
int
cpu_screen (int rep, int display)
cpu_screen(int rep, int display)
{
#undef CPU_BUF_SIZE
#define CPU_BUF_SIZE 4
int i, j, n;
static int first = 1;
float value;
static int first = TRUE;
static float cpu[CPU_BUF_SIZE + 1][5]; // last buffer is scratch
static struct load last_load = { 0, 0, 0, 0, 0 };
struct load load;
if (first) {
first = 0;
int i, j, n;
float value;
load_type load;
sock_send_string (sock, "screen_add C\n");
sprintf (buffer, "screen_set C -name {CPU Use: %s}\n", host);
sock_send_string (sock, buffer);
if (lcd_hgt >= 4) {
sock_send_string (sock, "widget_add C title title\n");
sock_send_string (sock, "widget_set C title {CPU LOAD}\n");
sock_send_string (sock, "widget_add C one string\n");
sock_send_string (sock, "widget_set C one 1 2 {0 1 Sys 0.0%}\n");
sock_send_string (sock, "widget_add C two string\n");
sock_send_string (sock, "widget_add C three string\n");
sock_send_string (sock, "widget_set C one 1 2 {Usr 0.0% Nice 0.0%}\n");
sock_send_string (sock, "widget_set C two 1 3 {Sys 0.0% Idle 0.0%}\n");
sock_send_string (sock, "widget_set C three 1 4 {0% 100%}\n");
sock_send_string (sock, "widget_add C usr string\n");
sock_send_string (sock, "widget_add C nice string\n");
sock_send_string (sock, "widget_add C idle string\n");
sock_send_string (sock, "widget_add C sys string\n");
sock_send_string (sock, "widget_add C bar hbar\n");
sock_send_string (sock, "widget_set C bar 3 4 0\n");
} else {
sock_send_string (sock, "widget_add C cpu0 string\n");
if (lcd_wid >= 20)
sock_send_string (sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n");
if(first)
{
first = FALSE;
sock_send_string(sock, "screen_add C\n");
sprintf(buffer, "screen_set C -name {CPU Use: %s}\n", get_hostname());
sock_send_string(sock, buffer);
if(lcd_hgt >= 4)
{
sock_send_string(sock, "widget_add C title title\n");
sock_send_string(sock, "widget_set C title {CPU LOAD}\n");
sock_send_string(sock, "widget_add C one string\n");
sock_send_string(sock, "widget_set C one 1 2 {0 1 Sys 0.0%}\n");
sock_send_string(sock, "widget_add C two string\n");
sock_send_string(sock, "widget_add C three string\n");
sock_send_string(sock, "widget_set C one 1 2 {Usr 0.0% Nice 0.0%}\n");
sock_send_string(sock, "widget_set C two 1 3 {Sys 0.0% Idle 0.0%}\n");
sock_send_string(sock, "widget_set C three 1 4 {0% 100%}\n");
sock_send_string(sock, "widget_add C usr string\n");
sock_send_string(sock, "widget_add C nice string\n");
sock_send_string(sock, "widget_add C idle string\n");
sock_send_string(sock, "widget_add C sys string\n");
sock_send_string(sock, "widget_add C bar hbar\n");
sock_send_string(sock, "widget_set C bar 3 4 0\n");
}
else
{
sock_send_string(sock, "widget_add C cpu0 string\n");
if(lcd_wid >= 20)
sock_send_string(sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n");
else
sock_send_string (sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n");
sock_send_string (sock, "widget_add C cpu0% string\n");
sprintf (buffer, "widget_set C cpu0%% 1 %i { 0.0%%}\n", lcd_wid - 4);
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add C usni string\n");
sock_send_string (sock, "widget_set C usni 1 2 { U S N I}\n");
sock_send_string (sock, "widget_add C usr hbar\n");
sock_send_string (sock, "widget_add C sys hbar\n");
sock_send_string (sock, "widget_add C nice hbar\n");
sock_send_string (sock, "widget_add C idle hbar\n");
sock_send_string (sock, "widget_add C total hbar\n");
sock_send_string (sock, "widget_set C total 6 1 0\n");
sock_send_string(sock, "widget_set C cpu0 1 1 {CPU0[ ]}\n");
sock_send_string(sock, "widget_add C cpu0% string\n");
sprintf(buffer, "widget_set C cpu0%% 1 %d { 0.0%%}\n", lcd_wid - 4);
sock_send_string(sock, buffer);
sock_send_string(sock, "widget_add C usni string\n");
sock_send_string(sock, "widget_set C usni 1 2 { U S N I}\n");
sock_send_string(sock, "widget_add C usr hbar\n");
sock_send_string(sock, "widget_add C sys hbar\n");
sock_send_string(sock, "widget_add C nice hbar\n");
sock_send_string(sock, "widget_add C idle hbar\n");
sock_send_string(sock, "widget_add C total hbar\n");
sock_send_string(sock, "widget_set C total 6 1 0\n");
}
return 0;
return(0);
}
//else return 0;
get_load (&load, &last_load);
machine_get_load(&load);
// Shift values over by one
for (i = 0; i < (CPU_BUF_SIZE - 1); i++)
for (j = 0; j < 5; j++)
for(i = 0; i < (CPU_BUF_SIZE - 1); i++)
for(j = 0; j < 5; j++)
cpu[i][j] = cpu[i + 1][j];
// Read new data
@@ -326,213 +94,204 @@ cpu_screen (int rep, int display)
cpu[CPU_BUF_SIZE - 1][3] = ((float) load.idle / (float) load.total) * 100.0;
cpu[CPU_BUF_SIZE - 1][4] = (((float) load.user + (float) load.system + (float) load.nice) / (float) load.total) * 100.0;
/*
// Only clear on first display...
if (!rep) {
/*
if(!rep){
// Make all the same, if this is the first time...
for(i=0; i<CPU_BUF_SIZE-1; i++)
for(j=0; j<5; j++)
cpu[i][j] = cpu[CPU_BUF_SIZE-1][j];
*/
}
*/
// Average values for final result
for (i = 0; i < 5; i++) {
for(i = 0; i < 5; i++)
{
value = 0;
for (j = 0; j < CPU_BUF_SIZE; j++) {
for(j = 0; j < CPU_BUF_SIZE; j++)
value += cpu[j][i];
}
value /= CPU_BUF_SIZE;
cpu[CPU_BUF_SIZE][i] = value;
}
if (lcd_hgt >= 4) {
value = cpu[CPU_BUF_SIZE][4];
n = (int) (value * 70.0);
if (value >= 99.9) {
sprintf (tmp, "100%%");
} else {
sprintf (tmp, "%4.1f%%", value);
}
sprintf (buffer, "widget_set C title {CPU %s: %s}\n", tmp, host);
if (display)
sock_send_string (sock, buffer);
if(!display)
return(0);
value = cpu[CPU_BUF_SIZE][0];
if (value >= 99.9) {
sprintf (tmp, " 100%%");
} else {
sprintf (tmp, "%4.1f%%", value);
}
sprintf (buffer, "widget_set C usr 5 2 {%s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
value = cpu[CPU_BUF_SIZE][1];
if (value >= 99.9) {
sprintf (tmp, " 100%%");
} else {
sprintf (tmp, "%4.1f%%", value);
}
sprintf (buffer, "widget_set C sys 5 3 {%s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
value = cpu[CPU_BUF_SIZE][2];
if (value >= 99.9) {
sprintf (tmp, " 100%%");
} else {
sprintf (tmp, "%4.1f%%", value);
}
sprintf (buffer, "widget_set C nice 16 2 {%s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
value = cpu[CPU_BUF_SIZE][3];
if (value >= 99.9) {
sprintf (tmp, " 100%%");
} else {
sprintf (tmp, "%4.1f%%", value);
}
sprintf (buffer, "widget_set C idle 16 3 {%s}\n", tmp);
if (display)
sock_send_string (sock, buffer);
value = cpu[CPU_BUF_SIZE][4];
n = (int) (value * (lcd_cellwid * 14) / 100.0);
sprintf (buffer, "widget_set C bar 3 4 %i\n", n);
if (display)
sock_send_string (sock, buffer);
} // end if(lcd_hgt >= 4)
else // 20x2 version
if(lcd_hgt >= 4)
{
value = cpu[CPU_BUF_SIZE][4];
if (value >= 99.9) {
sprintf (tmp, "100%%");
} else {
sprintf (tmp, "%4.1f%%", value);
}
sprintf (buffer, "widget_set C cpu0%% %i 1 {%s}\n", lcd_wid - 4, tmp);
if (display)
sock_send_string (sock, buffer);
n = (float) (value * lcd_cellwid * (float) (lcd_wid - 11)) / 100.0;
sprintf (buffer, "widget_set C total 6 1 %i\n", n);
if (display)
sock_send_string (sock, buffer);
if(value >= 99.9)
sprintf(tmp, "100%%");
else
sprintf(tmp, "%4.1f%%", value);
sprintf(buffer, "widget_set C title {CPU %s: %s}\n", tmp, get_hostname());
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][0];
n = (float) (value * lcd_cellwid * 4.0) / 100.0;
sprintf (buffer, "widget_set C usr 1 2 %i\n", n);
if (display)
sock_send_string (sock, buffer);
if(value >= 99.9)
sprintf(tmp, " 100%%");
else
sprintf(tmp, "%4.1f%%", value);
sprintf(buffer, "widget_set C usr 5 2 {%s}\n", tmp);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][1];
n = (float) (value * lcd_cellwid * 3.0) / 100.0;
sprintf (buffer, "widget_set C sys 7 2 %i\n", n);
if (display)
sock_send_string (sock, buffer);
if(value >= 99.9)
sprintf(tmp, " 100%%");
else
sprintf(tmp, "%4.1f%%", value);
sprintf(buffer, "widget_set C sys 5 3 {%s}\n", tmp);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][2];
n = (float) (value * lcd_cellwid * 3.0) / 100.0;
sprintf (buffer, "widget_set C nice 12 2 %i\n", n);
if (display)
sock_send_string (sock, buffer);
if(value >= 99.9)
sprintf(tmp, " 100%%");
else
sprintf(tmp, "%4.1f%%", value);
sprintf(buffer, "widget_set C nice 16 2 {%s}\n", tmp);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][3];
n = (float) (value * lcd_cellwid * 3.0) / 100.0;
sprintf (buffer, "widget_set C idle 17 2 %i\n", n);
if (display)
sock_send_string (sock, buffer);
if(value >= 99.9)
sprintf(tmp, " 100%%");
else
sprintf(tmp, "%4.1f%%", value);
sprintf(buffer, "widget_set C idle 16 3 {%s}\n", tmp);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][4];
n = (int)(value * (lcd_cellwid * 14) / 100.0);
sprintf(buffer, "widget_set C bar 3 4 %d\n", n);
sock_send_string(sock, buffer);
}
else // 20x2 version
{
value = cpu[CPU_BUF_SIZE][4];
if(value >= 99.9)
sprintf(tmp, "100%%");
else
sprintf(tmp, "%4.1f%%", value);
sprintf(buffer, "widget_set C cpu0%% %d 1 {%s}\n", lcd_wid - 4, tmp);
sock_send_string(sock, buffer);
n = (float)(value * lcd_cellwid * (float) (lcd_wid - 11)) / 100.0;
sprintf(buffer, "widget_set C total 6 1 %d\n", n);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][0];
n = (float)(value * lcd_cellwid * 4.0) / 100.0;
sprintf(buffer, "widget_set C usr 1 2 %d\n", n);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][1];
n = (float)(value * lcd_cellwid * 3.0) / 100.0;
sprintf(buffer, "widget_set C sys 7 2 %d\n", n);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][2];
n = (float)(value * lcd_cellwid * 3.0) / 100.0;
sprintf(buffer, "widget_set C nice 12 2 %d\n", n);
sock_send_string(sock, buffer);
value = cpu[CPU_BUF_SIZE][3];
n = (float)(value * lcd_cellwid * 3.0) / 100.0;
sprintf(buffer, "widget_set C idle 17 2 %d\n", n);
sock_send_string(sock, buffer);
}
return 0;
return(0);
} // End cpu_screen()
//////////////////////////////////////////////////////////////////////////
// Cpu Graph Screen shows a quick-moving histogram of CPU use.
//
int
cpu_graph_screen (int rep, int display)
cpu_graph_screen(int rep, int display)
{
int i, j, n = 0;
static int first = 1;
float value ;
#undef CPU_BUF_SIZE
#define CPU_BUF_SIZE 2
static int first = TRUE;
static float cpu[CPU_BUF_SIZE];
static int cpu_past[LCD_MAX_WIDTH];
static struct load last_load;
struct load load;
if (first) {
first = 0;
sock_send_string (sock, "screen_add G\n");
sprintf (buffer, "screen_set G -name {CPU Graph: %s}\n", host);
sock_send_string (sock, buffer);
if (lcd_hgt >= 4) {
sock_send_string (sock, "widget_add G title title\n");
sprintf (buffer, "widget_set G title {CPU: %s}\n", host);
} else {
sock_send_string (sock, "widget_add G title string\n");
sprintf (buffer, "widget_set G title 1 1 {CPU: %s}\n", host);
int i, j, n = 0;
float value ;
load_type load;
if(first)
{
first = FALSE;
sock_send_string(sock, "screen_add G\n");
sprintf(buffer, "screen_set G -name {CPU Graph: %s}\n", get_hostname());
sock_send_string(sock, buffer);
if(lcd_hgt >= 4)
{
sock_send_string(sock, "widget_add G title title\n");
sprintf(buffer, "widget_set G title {CPU: %s}\n", get_hostname());
}
sock_send_string (sock, buffer);
else
{
sock_send_string(sock, "widget_add G title string\n");
sprintf(buffer, "widget_set G title 1 1 {CPU: %s}\n", get_hostname());
}
sock_send_string(sock, buffer);
for (i = 1; i <= lcd_wid; i++) {
sprintf (tmp, "widget_add G %i vbar\n", i);
sock_send_string (sock, tmp);
sprintf (tmp, "widget_set G %i %i %i 0\n", i, i, lcd_hgt);
sock_send_string (sock, tmp);
cpu_past[i - 1] = 0 ;
for(i = 1; i <= lcd_wid; i++)
{
sprintf(buffer, "widget_add G %d vbar\n", i);
sock_send_string(sock, buffer);
sprintf(buffer, "widget_set G %d %d %d 0\n", i, i, lcd_hgt);
sock_send_string(sock, buffer);
cpu_past[i-1] = 0;
};
// Clear out CPU averaging array
for (i = 0 ; i < CPU_BUF_SIZE ; ++i) {
cpu[i] = 0 ;
};
for(i = 0; i < CPU_BUF_SIZE ; i++)
cpu[i] = 0.;
}
// Shift values over by one
for (i = 0; i < (CPU_BUF_SIZE - 1); i++)
cpu[i] = cpu[i + 1];
for(i = 0; i < (CPU_BUF_SIZE-1); i++)
cpu[i] = cpu[i+1];
// Read and save new data
get_load (&load, &last_load);
cpu[CPU_BUF_SIZE - 1] = ((float) load.user + (float) load.system + (float) load.nice) / (float) load.total;
machine_get_load(&load);
cpu[CPU_BUF_SIZE-1] = ((float) load.user + (float) load.system + (float) load.nice) / (float) load.total;
// Average values for final result
value = 0;
for (j = 0; j < CPU_BUF_SIZE; j++) {
for(j = 0; j < CPU_BUF_SIZE; j++)
value += cpu[j];
}
value /= (float) CPU_BUF_SIZE;
// Scale result to available height
if (lcd_hgt > 2)
n = (int) (value * (float) (lcd_cellhgt) * (float) (lcd_hgt - 1));
if(lcd_hgt > 2)
n = (int)(value * (float)(lcd_cellhgt) * (float)(lcd_hgt - 1));
else
n = (int) (value * (float) (lcd_cellhgt) * (float) (lcd_hgt));
n = (int)(value * (float)(lcd_cellhgt) * (float)(lcd_hgt));
// Shift and update display the graph
for (i = 0; i < lcd_wid - 1; i++) {
for(i = 0; i < lcd_wid - 1; i++)
{
cpu_past[i] = cpu_past[i + 1];
if (display) {
sprintf (tmp, "widget_set G %i %i %i %i\n",
i + 1, i + 1, lcd_hgt, cpu_past[i] );
sock_send_string (sock, tmp);
};
};
if(display)
{
sprintf(buffer, "widget_set G %d %d %d %d\n",
i + 1, i + 1, lcd_hgt, cpu_past[i]);
sock_send_string(sock, buffer);
}
}
// Save the newest entry and display it
cpu_past[lcd_wid - 1] = n;
if (display) {
sprintf (tmp, "widget_set G %i %i %i %i\n", lcd_wid, lcd_wid, lcd_hgt, n);
sock_send_string (sock, tmp);
};
if(display)
{
sprintf(buffer, "widget_set G %d %d %d %d\n", lcd_wid, lcd_wid, lcd_hgt, n);
sock_send_string(sock, buffer);
}
return(0);
} // End cpu_graph_screen()
return 0;
} // End cpu_graph_screen()
-3
View File
@@ -1,9 +1,6 @@
#ifndef CPU_H
#define CPU_H
int cpu_init ();
int cpu_close ();
int cpu_screen (int rep, int display);
int cpu_graph_screen (int rep, int display);
+5 -195
View File
@@ -54,203 +54,13 @@
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "cpu_smp.h"
#ifdef SOLARIS
#include <sys/types.h>
#include <sys/param.h>
#include <sys/cpuvar.h>
#endif
#ifdef HAVE_LIBKSTAT
#include <kstat.h>
#endif
#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <sys/sched.h>
#include <errno.h>
#endif
#define MAX_CPUS 8
struct load {
unsigned long total, user, system, nice, idle;
};
#ifdef HAVE_LIBKSTAT
kstat_ctl_t *kc;
#endif
static int load_fd = 0;
static char *numnames[MAX_CPUS] = { "one", "two", "three", "four", "five", "six", "seven", "eight" };
#ifdef __NetBSD__
static void
get_netbsd_load (struct load *result, int *numcpus)
{
int mib[2], i, num;
size_t size;
struct load curr_load;
u_int64_t cp_time[CPUSTATES];
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
size = sizeof(int);
if(sysctl(mib, 2, &num, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl kern.boottime failed: %s\n", strerror(errno));
}
mib[0] = CTL_KERN;
mib[1] = KERN_CP_TIME;
size = sizeof(cp_time);
if(sysctl(mib, 2, cp_time, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl kern.cp_time failed: %s\n", strerror(errno));
curr_load.user = 1;
curr_load.nice = 1;
curr_load.system = 1;
curr_load.idle = 1;
}
else
{
curr_load.user = cp_time[CP_USER];
curr_load.nice = cp_time[CP_NICE];
curr_load.system = cp_time[CP_SYS] + cp_time[CP_INTR];
curr_load.idle = cp_time[CP_IDLE];
}
curr_load.total = curr_load.user + curr_load.nice + curr_load.system + curr_load.idle;
*numcpus = num;
num = num > 8 ? 8 : num;
/* we can not get per-cpu-load values */
for(i = 0; i < num; i++)
{
result[i] = curr_load;
}
}
#endif /* __NetBSD__ */
static void
get_load (struct load *result, int *numcpus)
{
#ifdef __NetBSD__
get_netbsd_load(result, numcpus);
#else
char *token;
static struct load last_load[MAX_CPUS];
struct load curr_load[MAX_CPUS];
#ifndef HAVE_LIBKSTAT
*numcpus = 0;
reread (load_fd, "get_load");
// Look for lines starting with "cpu0", "cpu1", etc.
token = strtok (buffer, "\n");
while (token) {
if (token[0] == 'c') {
if (isdigit (token[3])) {
sscanf (token, "%*s %lu %lu %lu %lu", &curr_load[*numcpus].user, &curr_load[*numcpus].nice, &curr_load[*numcpus].system, &curr_load[*numcpus].idle);
curr_load[*numcpus].total = curr_load[*numcpus].user + curr_load[*numcpus].nice + curr_load[*numcpus].system + curr_load[*numcpus].idle;
result[*numcpus].total = curr_load[*numcpus].total - last_load[*numcpus].total;
result[*numcpus].user = curr_load[*numcpus].user - last_load[*numcpus].user;
result[*numcpus].nice = curr_load[*numcpus].nice - last_load[*numcpus].nice;
result[*numcpus].system = curr_load[*numcpus].system - last_load[*numcpus].system;
result[*numcpus].idle = curr_load[*numcpus].idle - last_load[*numcpus].idle;
last_load[*numcpus].total = curr_load[*numcpus].total;
last_load[*numcpus].user = curr_load[*numcpus].user;
last_load[*numcpus].nice = curr_load[*numcpus].nice;
last_load[*numcpus].system = curr_load[*numcpus].system;
last_load[*numcpus].idle = curr_load[*numcpus].idle;
(*numcpus)++;
}
} else {
break;
}
token = strtok (NULL, "\n");
}
#else
kstat_t *k_space;
void *val_ptr;
int ncpu=0,count;
char buffer[10];
*numcpus=sysconf(_SC_NPROCESSORS_CONF);
for (count = 0; count < MAX_CPUS;count++) {
sprintf (buffer, "cpu_stat%d",count);
k_space = kstat_lookup(kc, "cpu_stat", count, buffer);
if (k_space == NULL) {
} else if (kstat_read(kc, k_space, NULL) == -1) {
} else {
struct cpu_stat cinfo;
k_space=kstat_lookup(kc, "cpu_stat", -1, buffer);
kstat_read(kc,k_space,&cinfo);
curr_load[ncpu].idle=cinfo.cpu_sysinfo.cpu[CPU_IDLE];
curr_load[ncpu].user=cinfo.cpu_sysinfo.cpu[CPU_USER];
curr_load[ncpu].system=cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
curr_load[ncpu].nice=cinfo.cpu_sysinfo.cpu[CPU_WAIT];
curr_load[ncpu].nice=0;
curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice + curr_load[ncpu].system + curr_load[ncpu].idle;
result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total;
result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user;
result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice;
result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system;
result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle;
last_load[ncpu].total = curr_load[ncpu].total;
last_load[ncpu].user = curr_load[ncpu].user;
last_load[ncpu].nice = curr_load[ncpu].nice;
last_load[ncpu].system = curr_load[ncpu].system;
last_load[ncpu].idle = curr_load[ncpu].idle;
ncpu=ncpu++;
}
}
#endif
#endif /* __NetBSD__ */
}
int
cpu_smp_init ()
{
#ifndef __NetBSD__
#ifndef HAVE_LIBKSTAT
if (!load_fd) {
load_fd = open ("/proc/stat", O_RDONLY);
}
#else
kc = kstat_open();
if (kc == NULL) {
exit(1);
}
#endif
#endif /* __NetBSD__ */
return 0;
}
int
cpu_smp_close ()
{
#ifndef __NetBSD__
#ifndef HAVE_LIBKSTAT
if (load_fd)
close (load_fd);
#else
kstat_close(kc);
#endif
load_fd = 0;
#endif /* __NetBSD__ */
return 0;
}
//////////////////////////////////////////////////////////////////////////
// CPU screen shows info about percentage of the CPU being used
//
@@ -263,7 +73,7 @@ cpu_smp_screen (int rep, int display)
static int first = 1;
float value;
static float cpu[MAX_CPUS][CPU_BUF_SIZE + 1][5]; // last buffer is scratch
struct load load[MAX_CPUS];
load_type load[MAX_CPUS];
int numprocs;
char buf[256];
char *graphsize;
@@ -271,12 +81,12 @@ cpu_smp_screen (int rep, int display)
if (first) {
first = 0;
get_load (load, &numprocs);
machine_get_smpload (load, &numprocs);
sock_send_string (sock, "screen_add P\n");
sock_send_string (sock, "widget_del P heartbeat\n");
sprintf (buffer, "screen_set P -name {CPU Use: %s}\n", host);
sprintf (buffer, "screen_set P -name {CPU Use: %s}\n", get_hostname());
sock_send_string (sock, buffer);
graphsize = calloc (sizeof (char), lcd_wid);
@@ -313,7 +123,7 @@ cpu_smp_screen (int rep, int display)
return 0;
}
get_load (load, &numprocs);
machine_get_smpload(load, &numprocs);
for (z = 0; z < numprocs; z++) {
-3
View File
@@ -1,9 +1,6 @@
#ifndef CPU_SMP_H
#define CPU_SMP_H
int cpu_smp_init ();
int cpu_smp_close ();
int cpu_smp_screen (int rep, int display);
#endif
+8 -165
View File
@@ -1,3 +1,5 @@
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
@@ -8,172 +10,13 @@
#include "config.h"
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_TYPES_H
#include <sys/types.h>
#endif
#ifdef HAVE_SYS_MOUNT_H
#include <sys/mount.h>
#endif
#ifdef HAVE_SYS_VFS_H
#include <sys/vfs.h>
#endif
#ifdef HAVE_SYS_STATVFS_H
#include <sys/statvfs.h>
#endif
#ifdef HAVE_SYS_STATFS_H
#include <sys/statfs.h>
#endif
#include "shared/sockets.h"
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "disk.h"
#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#else
FILE *mtab_fd;
#endif
typedef struct mounts {
char dev[256], type[64], mpoint[256];
long bsize, blocks, bfree, files, ffree;
} mounts;
static int
get_fs (mounts fs[])
{
#ifdef __NetBSD__
struct statfs *mntbuf;
struct statfs *pp;
int statcnt, cnt, i;
cnt = getmntinfo(&mntbuf, MNT_WAIT);
if(cnt == 0)
{
perror("getmntinfo");
return(0);
}
for(statcnt = 0, pp = mntbuf, i = 0; i < cnt; pp++, i++)
{
if(strcmp(pp->f_fstypename, "procfs")
&& strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs")
#endif
)
{
/*
printf("mount point: %s\n", pp->f_mntonname);
printf("fs name : %s\n", pp->f_mntfromname);
printf("fs type : %s\n", pp->f_fstypename);
*/
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 255, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks;
if(fs[statcnt].blocks > 0)
{
fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files;
fs[statcnt].ffree = pp->f_ffree;
/*
printf("bsize : %ld\n", pp->f_bsize);
printf("blocks: %ld\n", pp->f_blocks);
printf("free : %ld\n", pp->f_bfree);
*/
}
statcnt++;
}
}
return(statcnt);
#else
#ifdef STAT_STATVFS
struct statvfs fsinfo;
#else
struct statfs fsinfo;
#endif
char line[256];
int x = 0, y;
#ifdef MTAB_FILE
mtab_fd = fopen(MTAB_FILE, "r");
#else
#error "Can't find your mounted filesystem table file."
#endif
// Get rid of old, unmounted filesystems...
memset (fs, 0, sizeof (mounts) * 256);
while (x < 256) {
if (fgets (line, 256, mtab_fd) == NULL) {
fclose (mtab_fd);
return x;
}
sscanf (line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type);
if (strcmp (fs[x].type, "proc")
#ifndef STAT_NFS
&& strcmp (fs[x].type, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp (fs[x].type, "smbfs")
#endif
) {
#ifdef STAT_STATVFS
y = statvfs (fs[x].mpoint, &fsinfo);
#elif STAT_STATFS2_BSIZE
y = statfs (fs[x].mpoint, &fsinfo);
#elif STAT_STATFS4
y = statfs (fs[x].mpoint, &fsinfo, sizeof (fsinfo), 0);
#else
#error "statfs for this system noy yet supported"
#endif
fs[x].blocks = fsinfo.f_blocks;
if (fs[x].blocks > 0) {
fs[x].bsize = fsinfo.f_bsize;
fs[x].bfree = fsinfo.f_bfree;
fs[x].files = fsinfo.f_files;
fs[x].ffree = fsinfo.f_ffree;
x++;
}
}
}
fclose (mtab_fd);
return x;
#endif
}
int
disk_init ()
{
return 0;
}
int
disk_close ()
{
return 0;
}
///////////////////////////////////////////////////////////////////////////
// Gives disk stats.
//
@@ -185,7 +28,7 @@ disk_close ()
int
disk_screen (int rep, int display)
{
static mounts mnt[256];
static mounts_type mnt[256];
static int count = 0;
// Holds info to display (avoid recalculating it)
@@ -205,10 +48,10 @@ disk_screen (int rep, int display)
first = 0;
sock_send_string (sock, "screen_add D\n");
sprintf (buffer, "screen_set D -name {Disk Use: %s}\n", host);
sprintf (buffer, "screen_set D -name {Disk Use: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add D title title\n");
sprintf (buffer, "widget_set D title {DISKS: %s}\n", host);
sprintf (buffer, "widget_set D title {DISKS: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add D f frame\n");
//sock_send_string(sock, "widget_set D f 1 2 20 4 20 3 v 8\n");
@@ -223,7 +66,7 @@ disk_screen (int rep, int display)
// Get rid of old, unmounted filesystems...
memset (table, 0, sizeof (struct disp) * 256);
count = get_fs (mnt);
machine_get_fs(mnt, &count);
first = 0;
// Fill the display structure...
@@ -232,7 +75,7 @@ disk_screen (int rep, int display)
sock_send_string (sock, "widget_set D err2 30 5 .\n");
for (i = 0; i < count; i++) {
if (strlen (mnt[i].mpoint) > 6) {
sprintf (table[i].dev, "%c%s", ELLIPSIS, (mnt[i].mpoint) + (strlen (mnt[i].mpoint) - 5));
sprintf (table[i].dev, "-%s", (mnt[i].mpoint) + (strlen (mnt[i].mpoint) - 5));
} else {
sprintf (table[i].dev, "%s", mnt[i].mpoint);
}
-3
View File
@@ -1,9 +1,6 @@
#ifndef DISK_H
#define DISK_H
int disk_init ();
int disk_close ();
int disk_screen (int rep, int display);
#endif
+10 -69
View File
@@ -8,71 +8,12 @@
# include "config.h"
#endif
#if defined( HAVE_GETLOADAVG ) && defined( HAVE_SYS_LOADAVG_H )
# define USE_GETLOADAVG
#else
# undef USE_GETLOADAVG
#endif
#include "shared/sockets.h"
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "load.h"
#ifdef USE_GETLOADAVG
# include <sys/loadavg.h>
#endif
#ifdef __NetBSD__
#define USE_GETLOADAVG
#define LOADAVG_NSTATS 1
#define LOADAVG_1MIN 1
#endif
int loadavg_fd = 0;
static double get_loadavg (void);
static double
get_loadavg (void)
{
double load;
#ifdef USE_GETLOADAVG
double loadavg[LOADAVG_NSTATS];
#endif
#ifndef USE_GETLOADAVG
reread (loadavg_fd, "get_load:");
sscanf (buffer, "%lf", &load);
#else
getloadavg(loadavg,LOADAVG_NSTATS);
load = loadavg[LOADAVG_1MIN];
#endif
return load;
}
int
load_init ()
{
#ifndef USE_GETLOADAVG
if (!loadavg_fd)
loadavg_fd = open ("/proc/loadavg", O_RDONLY);
#endif
return 0;
}
int
load_close ()
{
#ifndef USE_GETLOADAVG
if (loadavg_fd)
close (loadavg_fd);
loadavg_fd = 0;
#endif
return 0;
}
///////////////////////////////////////////////////////////////////////////
// Shows a display very similar to "xload"'s histogram.
//
@@ -81,18 +22,18 @@ int
xload_screen (int rep, int display)
{
static int first = 1;
static float loads[LCD_MAX_WIDTH];
static double loads[LCD_MAX_WIDTH];
int n, i;
float loadmax = 0, factor, x;
double loadmax = 0, factor, x;
int status = 0;
if (first) // Only the first time this is ever called...
{
first = 0;
memset (loads, 0, sizeof (float) * LCD_MAX_WIDTH);
memset (loads, 0, sizeof (double) * LCD_MAX_WIDTH);
sock_send_string (sock, "screen_add X\n");
sprintf (buffer, "screen_set X -name {X-Load: %s}\n", host);
sprintf (buffer, "screen_set X -name {X-Load: %s}\n", get_hostname());
sock_send_string (sock, buffer);
// Add the vbars...
for (i = 1; i < lcd_wid; i++) {
@@ -120,21 +61,21 @@ xload_screen (int rep, int display)
for (n = 0; n < (lcd_wid - 2); n++)
loads[n] = loads[n + 1];
loads[lcd_wid - 2] = get_loadavg ();
machine_get_loadavg(&(loads[lcd_wid - 2]));
for (n = 0; n < lcd_wid - 1; n++)
if (loads[n] > loadmax)
loadmax = loads[n];
n = (int) loadmax;
if ((float) n < loadmax) {
if ((double) n < loadmax) {
n++;
}
sprintf (tmp, "widget_set X top %i %i %i\n", lcd_wid, (lcd_hgt <= 2) ? 1 : 2, n);
//if(display) sock_send_string(sock, tmp);
sock_send_string (sock, tmp);
factor = (float) (lcd_cellhgt) * ((lcd_hgt <= 2) ? 2.0 : 1.0 * (lcd_hgt-1)) / (float) n;
factor = (double) (lcd_cellhgt) * ((lcd_hgt <= 2) ? 2.0 : 1.0 * (lcd_hgt-1)) / (double) n;
for (n = 0; n < lcd_wid - 1; n++) {
x = (loads[n] * factor);
@@ -155,9 +96,9 @@ xload_screen (int rep, int display)
}
// And now the title...
if (lcd_hgt > 2)
sprintf (tmp, "widget_set X title {LOAD %2.2f: %s}\n", loads[lcd_wid - 2], host);
sprintf (tmp, "widget_set X title {LOAD %2.2f: %s}\n", loads[lcd_wid - 2], get_hostname());
else
sprintf (tmp, "widget_set X title 1 1 {%s %2.2f}\n", host, loads[lcd_wid - 2]);
sprintf (tmp, "widget_set X title 1 1 {%s %2.2f}\n", get_hostname(), loads[lcd_wid - 2]);
//if(display) sock_send_string(sock, tmp);
sock_send_string (sock, tmp);
-3
View File
@@ -8,9 +8,6 @@
#define LOAD_MIN 0.5
#endif
int load_init ();
int load_close ();
int xload_screen (int rep, int display);
#endif
+42
View File
@@ -0,0 +1,42 @@
#ifndef _lcdproc_machine_h_
#define _lcdproc_machine_h_
#include "shared/LL.h"
typedef struct
{
unsigned long total, user, system, nice, idle;
} load_type;
typedef struct
{
char dev[256], type[64], mpoint[256];
long bsize, blocks, bfree, files, ffree;
} mounts_type;
typedef struct
{
int total, cache, buffers, free, shared;
} meminfo_type;
typedef struct
{
char name[16];
int totl;
int number;
} procinfo_type;
int machine_init();
int machine_close();
int machine_get_battstat(int *acstat, int *battflag, int *percent);
int machine_get_fs(mounts_type fs[], int *cnt);
int machine_get_load(load_type *cur_load);
int machine_get_loadavg(double *load);
int machine_get_meminfo(meminfo_type *result);
int machine_get_procs(LinkedList *procs);
int machine_get_smpload(load_type *result, int *numcpus);
int machine_get_uptime(double *up, double *idle);
#endif /* _lcdproc_machine_h_ */
+410
View File
@@ -0,0 +1,410 @@
/* Copyright (c) 2003 Thomas Runge (coto@core.de)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __FreeBSD__
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/sysctl.h>
#include <sys/dkstat.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <sys/time.h>
#include <sys/user.h>
#include <machine/apm_bios.h>
#include <kvm.h>
#include <errno.h>
#include "main.h"
#include "machine.h"
#include "config.h"
#include "shared/LL.h"
static int pageshift;
#define pagetok(size) ((size) << pageshift)
static int swapmode(int *retavail, int *retfree);
int machine_init()
{
/* get the page size with "getpagesize" and calculate pageshift from it */
int pagesize = getpagesize();
pageshift = 0;
while(pagesize > 1)
{
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
return(TRUE);
}
int machine_close()
{
return(TRUE);
}
int machine_get_battstat(int *acstat, int *battflag, int *percent)
{
int apmd;
apm_info_t aip;
*acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT;
*percent = 100;
if((apmd = open("/dev/apm", O_RDONLY)) == -1)
{
perror("get_battstat_open");
return(TRUE);
}
if(ioctl(apmd, APMIO_GETINFO, &aip) == -1)
{
perror("get_battstat_ioctl");
return(FALSE);
}
switch(aip->ai_acline)
{
case 0:
*acstat = LCDP_AC_OFF;
break;
case 1:
*acstat = LCDP_AC_ON;
break;
default:
*acstat = LCDP_AC_UNKNOWN;
break;
}
switch(aip->ai_batt_stat)
{
case 0:
*battflag = LCDP_BATT_HIGH;
break;
case 1:
*battflag = LCDP_BATT_LOW;
break;
case 2:
*battflag = LCDP_BATT_CRITICAL;
break;
case 3:
*battflag = LCDP_BATT_CHARGING;
break;
default:
*battflag = LCDP_BATT_UNKNOWN;
break;
}
*percent = aip->ai_batt_life;
if(*percent == 255)
*percent = -1;
close(apmd);
return(TRUE);
}
int machine_get_fs(mounts_type fs[], int *cnt)
{
struct statfs *mntbuf;
struct statfs *pp;
int statcnt, fscnt, i;
fscnt = getmntinfo(&mntbuf, MNT_WAIT);
if(fscnt == 0)
{
perror("getmntinfo");
return(FALSE);
}
for(statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++)
{
if( strcmp(pp->f_fstypename, "procfs")
&& strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs")
#endif
)
{
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 255, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks;
if(fs[statcnt].blocks > 0)
{
fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files;
fs[statcnt].ffree = pp->f_ffree;
}
statcnt++;
}
}
*cnt = statcnt;
return(TRUE);
}
int machine_get_load(load_type *curr_load)
{
static load_type last_load = { 0, 0, 0, 0, 0 };
static load_type last_ret_load;
load_type load;
u_int32_t cp_time[CPUSTATES];
size_t size;
size = sizeof(cp_time);
if(sysctlbyname("kern.cp_time", cp_time, &size, NULL, 0) < 0)
{
perror("sysctl kern.cp_time failed");
return(FALSE);
}
load.user = (unsigned long) (cp_time[CP_USER]);
load.nice = (unsigned long) (cp_time[CP_NICE]);
load.system = (unsigned long) (cp_time[CP_SYS] + cp_time[CP_INTR]);
load.idle = (unsigned long) (cp_time[CP_IDLE]);
load.total = load.user + load.nice + load.system + load.idle;
if(load.total != last_load.total)
{
curr_load->user = load.user - last_load.user;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load;
last_load = load;
}
else
{
*curr_load = last_ret_load;
}
return(TRUE);
}
int machine_get_loadavg(double *load)
{
double loadavg[1];
if(getloadavg(loadavg, 1) == -1)
return(FALSE);
*load = loadavg[0];
return(TRUE);
}
int machine_get_meminfo(meminfo_type *result)
{
int total_pages, free_pages;
size_t size;
size = sizeof(int);
if(sysctlbyname("vm.stats.vm.v_page_count", &total_pages, &size, NULL, 0) < 0)
{
perror("sysctl vm.stats.vm.v_page_count");
return(FALSE);
}
if(sysctlbyname("vm.stats.vm.v_free_count", &free_pages, &size, NULL, 0) < 0)
{
perror("sysctl vm.stats.vm.v_free_count");
return(FALSE);
}
/* memory */
result[0].total = pagetok(total_pages);
result[0].free = pagetok(free_pages);
/* unused anyway */
result[0].shared = 0;
result[0].buffers = 0;
result[0].cache = 0;
/* swap */
result[1].total = 0;
result[1].free = 0;
if(swapmode(&total_pages, &free_pages) != -1)
{
result[1].total = total_pages;
result[1].free = free_pages;
}
return(TRUE);
}
int machine_get_procs(LinkedList *procs)
{
struct kinfo_proc *kprocs;
int nproc, i;
procinfo_type *p;
kvm_t *kvmd;
if((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
{
perror("kvm_open");
return(FALSE);
}
kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc);
if(kprocs == NULL)
{
perror("kvm_getprocs");
kvm_close(kvmd);
return(FALSE);
}
for(i = 0; i < nproc; i++)
{
p = malloc(sizeof(procinfo_type));
if(!p)
{
perror("mem_top_malloc");
kvm_close(kvmd);
return(FALSE);
}
strncpy(p->name, kprocs->kp_proc.p_comm, 15);
p->name[15] = '\0';
p->totl = kprocs->kp_eproc.e_vm.vm_map.size / 1024;
p->number = kprocs->kp_proc.p_pid;
LL_Push(procs, (void *)p);
kprocs++;
}
kvm_close(kvmd);
return(TRUE);
}
int machine_get_smpload(load_type *result, int *numcpus)
{
int i, num;
size_t size;
load_type curr_load;
size = sizeof(int);
if(sysctlbyname("hw.ncpu", &num, &size, NULL, 0) < 0)
{
perror("sysctl hw.ncpu");
return(FALSE);
}
if(machine_get_load(&curr_load) == FALSE)
return(FALSE);
*numcpus = num;
num = num > 8 ? 8 : num;
/* Don't know how to get per-cpu-load values */
for(i = 0; i < num; i++)
{
result[i] = curr_load;
}
return(TRUE);
}
int machine_get_uptime(double *up, double *idle)
{
size_t size;
time_t now;
struct timeval boottime;
load_type curr_load;
size = sizeof(boottime);
time(&now);
if(sysctlbyname("kern.boottime", &boottime, &size, NULL, 0) < 0)
{
perror("sysctl kern.cp_time failed");
return(FALSE);
}
*up = (double)(now - boottime.tv_sec);
if(machine_get_load(&curr_load) == FALSE)
*idle = 100.;
else
*idle = 100.*curr_load.idle/curr_load.total;
return(TRUE);
}
static int swapmode(int *retavail, int *retfree)
{
int n;
struct kvm_swap swapary[1];
kvm_t *kvmd;
*retavail = 0;
*retfree = 0;
if((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
{
perror("read kvm");
return(-1);
}
n = kvm_getswapinfo(kvmd, swapary, 1, 0);
if(n < 0 || swapary[0].ksw_total == 0)
{
/* strange */
}
else
{
*retavail = pagetok(swapary[0].ksw_total);
*retfree = pagetok(swapary[0].ksw_total - swapary[0].ksw_used);
}
kvm_close(kvmd);
return(n);
}
#endif /* __FreeBSD__ */
Binary file not shown.
+506
View File
@@ -0,0 +1,506 @@
#ifdef linux
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <string.h>
#include <fcntl.h>
#include <dirent.h>
#include <sys/param.h>
#include <sys/utsname.h>
#include <sys/stat.h>
#include <sys/statvfs.h>
#include <errno.h>
#ifdef USE_GETLOADAVG
# include <sys/loadavg.h>
#endif
#ifdef HAVE_PROCFS_H
# include <procfs.h>
#endif
#ifdef HAVE_SYS_PROCFS_H
# include <sys/procfs.h>
#endif
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "config.h"
#include "shared/LL.h"
#define MAX_CPUS 8
static int batt_fd;
static int load_fd;
static int loadavg_fd;
static int meminfo_fd;
static int uptime_fd;
static char procbuf[1024]; //TODO ugly hack!
static FILE *mtab_fd;
int machine_init()
{
uptime_fd = -1;
batt_fd = -1;
load_fd = -1;
loadavg_fd = -1;
meminfo_fd = -1;
if(uptime_fd == -1)
{
uptime_fd = open("/proc/uptime", O_RDONLY);
if(uptime_fd < 0)
{
perror("open /proc/uptime");
return(FALSE);
}
}
if(load_fd == -1)
{
load_fd = open("/proc/stat", O_RDONLY);
if(load_fd < 0)
{
perror("open /proc/stat");
return(FALSE);
}
}
#ifndef USE_GETLOADAVG
if(loadavg_fd == -1)
{
loadavg_fd = open("/proc/loadavg", O_RDONLY);
if(loadavg_fd < 0)
{
perror("open /proc/loadavg");
return(FALSE);
}
}
#endif
if(meminfo_fd == -1)
{
meminfo_fd = open("/proc/meminfo", O_RDONLY);
if(meminfo_fd < 0)
{
perror("open /proc/meminfo");
return(FALSE);
}
}
if(batt_fd == -1)
{
batt_fd = open("/proc/apm", O_RDONLY);
if(batt_fd < 0)
{
//perror("open /proc/apm");
batt_fd = -1;
}
}
return(TRUE);
}
int machine_close()
{
if(batt_fd != -1)
close(batt_fd);
batt_fd = -1;
if(load_fd != -1)
close(load_fd);
load_fd = -1;
#ifndef USE_GETLOADAVG
if(loadavg_fd != -1)
close(loadavg_fd);
loadavg_fd = -1;
#endif
if(meminfo_fd != -1)
close(meminfo_fd);
meminfo_fd = -1;
if(uptime_fd != -1)
close(uptime_fd);
uptime_fd = -1;
return(TRUE);
}
static void
reread (int f, char *errmsg)
{
if (lseek (f, 0L, 0) == 0 && read (f, procbuf, sizeof(procbuf) - 1) > 0)
return;
perror (errmsg);
exit (1);
}
int
getentry (const char *tag, const char *bufptr)
{
char *tail;
int retval, len = strlen (tag);
while (bufptr) {
if (*bufptr == '\n')
bufptr++;
if (!strncmp (tag, bufptr, len)) {
retval = strtol (bufptr + len, &tail, 10);
if (tail == bufptr + len)
return -1;
else
return retval;
}
bufptr = strchr (bufptr, '\n');
}
return -1;
}
int machine_get_battstat(int *acstat, int *battflag, int *percent)
{
char str[64];
int battstat;
if(batt_fd == -1)
{
*acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT;
*percent = 100;
return(TRUE);
}
if(lseek(batt_fd, 0, 0) != 0)
return(FALSE);
if(read(batt_fd, str, sizeof(str) - 1) < 0)
return(FALSE);
if(3 > sscanf(str + 13, "0x%x 0x%x 0x%x %d", acstat, &battstat, battflag, percent))
return(FALSE);
if(*battflag == 0xff)
*battflag = LCDP_BATT_UNKNOWN;
else
{
if(*battflag & 1)
*battflag = LCDP_BATT_HIGH;
if(*battflag & 2)
*battflag = LCDP_BATT_LOW;
if(*battflag & 4)
*battflag = LCDP_BATT_CRITICAL;
if(*battflag & 8 || battstat == 3)
*battflag = LCDP_BATT_CHARGING;
if(*battflag & 128)
*battflag = LCDP_BATT_ABSENT;
}
switch(*acstat)
{
case 0:
*acstat = LCDP_AC_OFF;
break;
case 1:
*acstat = LCDP_AC_ON;
break;
case 2:
*acstat = LCDP_AC_BACKUP;
break;
default:
*acstat = LCDP_AC_UNKNOWN;
break;
}
return(TRUE);
}
int machine_get_fs(mounts_type fs[], int *cnt)
{
#ifdef STAT_STATVFS
struct statvfs fsinfo;
#else
struct statfs fsinfo;
#endif
char line[256];
int x = 0, y;
#ifdef MTAB_FILE
mtab_fd = fopen(MTAB_FILE, "r");
#else
#error "Can't find your mounted filesystem table file."
#endif
// Get rid of old, unmounted filesystems...
memset(fs, 0, sizeof(mounts_type) * 256);
while(x < 256)
{
if(fgets(line, 256, mtab_fd) == NULL)
{
fclose (mtab_fd);
*cnt = x;
return(FALSE);
}
sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type);
if(strcmp(fs[x].type, "proc")
#ifndef STAT_NFS
&& strcmp(fs[x].type, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp(fs[x].type, "smbfs")
#endif
)
{
#ifdef STAT_STATVFS
y = statvfs(fs[x].mpoint, &fsinfo);
#elif STAT_STATFS2_BSIZE
y = statfs(fs[x].mpoint, &fsinfo);
#elif STAT_STATFS4
y = statfs(fs[x].mpoint, &fsinfo, sizeof (fsinfo), 0);
#else
#error "statfs for this system not yet supported"
#endif
fs[x].blocks = fsinfo.f_blocks;
if(fs[x].blocks > 0)
{
fs[x].bsize = fsinfo.f_bsize;
fs[x].bfree = fsinfo.f_bfree;
fs[x].files = fsinfo.f_files;
fs[x].ffree = fsinfo.f_ffree;
x++;
}
}
}
fclose(mtab_fd);
*cnt = x;
return(TRUE);
}
int machine_get_load(load_type *curr_load)
{
static load_type last_load = { 0, 0, 0, 0, 0 };
load_type load;
reread(load_fd, "get_load:");
sscanf(procbuf, "%*s %lu %lu %lu %lu\n", &load.user, &load.nice, &load.system, &load.idle);
load.total = load.user + load.nice + load.system + load.idle;
curr_load->user = load.user - last_load.user;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total;
last_load = load;
return(TRUE);
}
int machine_get_loadavg(double *load)
{
#ifdef USE_GETLOADAVG
double loadavg[LOADAVG_NSTATS];
if(getloadavg(loadavg, LOADAVG_NSTATS) < 0)
{
perror("getloadavg");
*load = 1.;
return(FALSE);
}
*load = loadavg[LOADAVG_1MIN];
#else
reread(loadavg_fd, "get_load:");
sscanf(procbuf, "%lf", load);
#endif
return(TRUE);
}
int machine_get_meminfo(meminfo_type *result)
{
reread(meminfo_fd, "get_meminfo:");
result[0].total = getentry("MemTotal:", procbuf);
result[0].free = getentry("MemFree:", procbuf);
result[0].shared = getentry("MemShared:", procbuf);
result[0].buffers = getentry("Buffers:", procbuf);
result[0].cache = getentry("Cached:", procbuf);
result[1].total = getentry("SwapTotal:", procbuf);
result[1].free = getentry("SwapFree:", procbuf);
return(TRUE);
}
int machine_get_procs(LinkedList *procs)
{
// Much of this code was ripped from "gmemusage"
char buf[128];
DIR *proc;
FILE *StatusFile;
struct dirent *procdir;
procinfo_type *p;
char procName[16];
int procSize, procRSS, procData, procStk, procExe;
const char
*NameLine = "Name:",
*VmSizeLine = "VmSize:",
*VmRSSLine = "VmRSS",
*VmDataLine = "VmData",
*VmStkLine = "VmStk",
*VmExeLine = "VmExe";
const int
NameLineLen = strlen(NameLine),
VmSizeLineLen = strlen(VmSizeLine),
VmDataLineLen = strlen(VmDataLine),
VmStkLineLen = strlen(VmStkLine),
VmExeLineLen = strlen(VmExeLine),
VmRSSLineLen = strlen(VmRSSLine);
int threshold = 400, unique;
if((proc = opendir("/proc")) == NULL)
{
perror("mem_top_screen: unable to open /proc");
return(FALSE);
}
while((procdir = readdir(proc)))
{
if(!index("1234567890", procdir->d_name[0]))
continue;
sprintf(buf, "/proc/%s/status", procdir->d_name);
if((StatusFile = fopen(buf, "r")) == NULL)
{
// Not a serious error; process has finished before we could
// examine it:
continue;
}
procRSS = procSize = procData = procStk = procExe = 0;
while(fgets(buf, sizeof(buf), StatusFile))
{
if(!strncmp(buf, NameLine, NameLineLen))
{
/* Name: procName */
sscanf(buf, "%*s %s", procName);
} else if(!strncmp(buf, VmSizeLine, VmSizeLineLen))
{
/* VmSize: procSize kB */
sscanf(buf, "%*s %d", &procSize);
} else if(!strncmp (buf, VmRSSLine, VmRSSLineLen))
{
/* VmRSS: procRSS kB */
sscanf(buf, "%*s %d", &procRSS);
} else if(!strncmp(buf, VmDataLine, VmDataLineLen))
{
/* VmData: procData kB */
sscanf(buf, "%*s %d", &procData);
} else if(!strncmp(buf, VmStkLine, VmStkLineLen))
{
/* VmStk: procStk kB */
sscanf(buf, "%*s %d", &procStk);
} else if(!strncmp(buf, VmExeLine, VmExeLineLen))
{
/* VmExe: procExe kB */
sscanf(buf, "%*s %d", &procExe);
}
}
fclose(StatusFile);
if(procSize > threshold)
{
// Figure out if it's sharing any memory...
unique = 1;
LL_Rewind(procs);
do
{
p = LL_Get (procs);
if(p)
{
if(0 == strcmp(p->name, procName))
{
unique = 0;
p->number++;
p->totl += procData + procStk + procExe;
}
}
} while(LL_Next(procs) == 0);
// If this is the first one by this name...
if(unique)
{
p = malloc(sizeof(procinfo_type));
if(!p)
{
perror("mem_top_screen: Error allocating process entry");
break;
}
strcpy(p->name, procName);
p->totl = procData + procStk + procExe;
p->number = 1;
// TODO: Check for errors here?
LL_Push(procs, (void *) p);
}
}
}
closedir(proc);
return(TRUE);
}
int machine_get_smpload(load_type *result, int *numcpus)
{
char *token;
static load_type last_load[MAX_CPUS];
load_type curr_load[MAX_CPUS];
*numcpus = 0;
reread(load_fd, "get_load");
// Look for lines starting with "cpu0", "cpu1", etc.
token = strtok(procbuf, "\n");
while(token)
{
if((strlen(token) > 3) && (!strncmp(token, "cpu", 3)) && isdigit(token[3]))
{
sscanf(token, "%*s %lu %lu %lu %lu", &curr_load[*numcpus].user, &curr_load[*numcpus].nice, &curr_load[*numcpus].system, &curr_load[*numcpus].idle);
curr_load[*numcpus].total = curr_load[*numcpus].user + curr_load[*numcpus].nice + curr_load[*numcpus].system + curr_load[*numcpus].idle;
result[*numcpus].total = curr_load[*numcpus].total - last_load[*numcpus].total;
result[*numcpus].user = curr_load[*numcpus].user - last_load[*numcpus].user;
result[*numcpus].nice = curr_load[*numcpus].nice - last_load[*numcpus].nice;
result[*numcpus].system = curr_load[*numcpus].system - last_load[*numcpus].system;
result[*numcpus].idle = curr_load[*numcpus].idle - last_load[*numcpus].idle;
last_load[*numcpus].total = curr_load[*numcpus].total;
last_load[*numcpus].user = curr_load[*numcpus].user;
last_load[*numcpus].nice = curr_load[*numcpus].nice;
last_load[*numcpus].system = curr_load[*numcpus].system;
last_load[*numcpus].idle = curr_load[*numcpus].idle;
(*numcpus)++;
}
token = strtok(NULL, "\n");
}
return(TRUE);
}
int machine_get_uptime(double *up, double *idle)
{
reread(uptime_fd, "get_uptime:");
sscanf(procbuf, "%lf %lf", up, idle);
return(TRUE);
}
#endif /* linux */
Binary file not shown.
+365
View File
@@ -0,0 +1,365 @@
/* Copyright (c) 2003 Thomas Runge (coto@core.de)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __NetBSD__
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
#include <kvm.h>
#include <sys/utsname.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <sys/stat.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <sys/dkstat.h>
#include <sys/sched.h>
#include <uvm/uvm_extern.h>
#include <machine/apmvar.h>
#include <errno.h>
#include "machine.h"
#include "main.h"
#include "config.h"
#include "shared/LL.h"
static int pageshift;
#define pagetok(size) ((size) << pageshift)
#define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize)
int machine_init()
{
/* get the page size with "getpagesize" and calculate pageshift from it */
int pagesize = getpagesize();
pageshift = 0;
while(pagesize > 1)
{
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
return(TRUE);
}
int machine_close()
{
return(TRUE);
}
int machine_get_battstat(int *acstat, int *battflag, int *percent)
{
int apmd;
struct apm_power_info apmi;
if((apmd = open("/dev/apm", O_RDONLY)) == -1)
{
*acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT;
*percent = 100;
return(TRUE);
}
memset(&apmi, 0, sizeof(apmi));
if(ioctl(apmd, APM_IOC_GETPOWER, &apmi) == -1)
{
perror("APM_IOC_GETPOWER failed in get_batt_stat()");
return(FALSE);
}
switch(apmi.ac_state)
{
case APM_AC_OFF:
*acstat = LCDP_AC_OFF;
break;
case APM_AC_ON:
*acstat = LCDP_AC_ON;
break;
case APM_AC_BACKUP:
*acstat = LCDP_AC_BACKUP;
break;
default:
*acstat = LCDP_AC_UNKNOWN;
break;
}
*battflag = apmi.battery_state;
*percent = apmi.battery_life;
close(apmd);
return(TRUE);
}
int machine_get_fs(mounts_type fs[], int *cnt)
{
struct statfs *mntbuf;
struct statfs *pp;
int statcnt, fscnt, i;
fscnt = getmntinfo(&mntbuf, MNT_WAIT);
if(fscnt == 0)
{
perror("getmntinfo");
return(FALSE);
}
for(statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++)
{
if(strcmp(pp->f_fstypename, "procfs")
&& strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs")
#endif
)
{
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 255, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks;
if(fs[statcnt].blocks > 0)
{
fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files;
fs[statcnt].ffree = pp->f_ffree;
}
statcnt++;
}
}
*cnt = statcnt;
return(TRUE);
}
int machine_get_load(load_type *curr_load)
{
static load_type last_load = { 0, 0, 0, 0, 0 };
static load_type last_ret_load;
load_type load;
u_int64_t cp_time[CPUSTATES];
size_t size;
int mib[2];
mib[0] = CTL_KERN;
mib[1] = KERN_CP_TIME;
size = sizeof(cp_time);
if(sysctl(mib, 2, cp_time, &size, NULL, 0) < 0)
{
perror("sysctl kern.cp_time failed");
return(FALSE);
}
load.user = (unsigned long) (cp_time[CP_USER]);
load.nice = (unsigned long) (cp_time[CP_NICE]);
load.system = (unsigned long) (cp_time[CP_SYS] + cp_time[CP_INTR]);
load.idle = (unsigned long) (cp_time[CP_IDLE]);
load.total = load.user + load.nice + load.system + load.idle;
if(load.total != last_load.total)
{
curr_load->user = load.user - last_load.user;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load;
last_load = load;
}
else
{
*curr_load = last_ret_load;
}
return(TRUE);
}
int machine_get_loadavg(double *load)
{
double loadavg[1];
if(getloadavg(loadavg, 1) == -1)
return(FALSE);
*load = loadavg[0];
return(TRUE);
}
int machine_get_meminfo(meminfo_type *result)
{
size_t size;
int mib[2];
struct uvmexp_sysctl uvmexp;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP2;
size = sizeof(uvmexp);
if(sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0)
{
perror("sysctl vm.uvmexp2 failed");
return(FALSE);
}
/* memory */
result[0].total = pagetok(uvmexp.npages);
result[0].free = pagetok(uvmexp.free);
/* not really */
result[0].shared = pagetok(uvmexp.wired);
result[0].buffers = pagetok(uvmexp.execpages);
result[0].cache = pagetok(uvmexp.filepages);
/* swap */
result[1].total = uvmexp.pagesize * uvmexp.swpages;
result[1].free = uvmexp.pagesize * uvmexp.swpginuse;
result[1].free = result[1].total - result[1].free;
return(TRUE);
}
int machine_get_procs(LinkedList *procs)
{
int nproc, i;
kvm_t *kvmd;
struct kinfo_proc2 *kprocs;
procinfo_type *p;
kprocs = NULL;
if((kvmd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open")) == NULL)
{
perror("kvm_openfiles");
return(FALSE);
}
kprocs = kvm_getproc2(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc);
if(kprocs == NULL)
{
perror("kvm_getproc2");
return(FALSE);
}
for(i = 0; i < nproc; i++)
{
p = malloc(sizeof(procinfo_type));
if(!p)
{
perror("mem_top_malloc");
kvm_close(kvmd);
return(FALSE);
}
strncpy(p->name, kprocs->p_comm, 15);
p->name[15] = '\0';
p->totl = pagetok(PROCSIZE(kprocs));
p->number = kprocs->p_pid;
LL_Push(procs, (void *)p);
kprocs++;
}
kvm_close(kvmd);
return(TRUE);
}
int machine_get_smpload(load_type *result, int *numcpus)
{
int mib[2], i, num;
size_t size;
load_type curr_load;
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
size = sizeof(int);
if(sysctl(mib, 2, &num, &size, NULL, 0) < 0)
{
perror("sysctl hw.ncpu");
return(FALSE);
}
if(machine_get_load(&curr_load) == FALSE)
return(FALSE);
*numcpus = num;
num = num > 8 ? 8 : num;
/* Don't know how to get per-cpu-load values */
for(i = 0; i < num; i++)
{
result[i] = curr_load;
}
return(TRUE);
}
int machine_get_uptime(double *up, double *idle)
{
size_t size;
time_t now;
struct timeval boottime;
load_type curr_load;
int mib[2];
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
time(&now);
if(sysctl(mib, 2, &boottime, &size, NULL, 0) < 0)
{
perror("sysctl kern.boottime failed");
return(FALSE);
}
*up = (double)(now - boottime.tv_sec);
if(machine_get_load(&curr_load) == FALSE)
*idle = 100.;
else
*idle = 100.*curr_load.idle/curr_load.total;
return(TRUE);
}
#endif /* __NetBSD__ */
Binary file not shown.
+381
View File
@@ -0,0 +1,381 @@
/* Copyright (c) 2003 Thomas Runge (coto@core.de)
*
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. Neither the name of the author nor the names of its contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifdef __OpenBSD__
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/sysctl.h>
#include <sys/dkstat.h>
#include <sys/ucred.h>
#include <sys/mount.h>
#include <sys/time.h>
#include <sys/user.h>
/* take care if $ARCH != i386! */
#include <i386/apmvar.h>
#include <kvm.h>
#include <errno.h>
#include "main.h"
#include "machine.h"
#include "config.h"
#include "shared/LL.h"
static int pageshift;
#define pagetok(size) ((size) << pageshift)
#define PROCSIZE(pp) ((pp).vm_tsize + (pp).vm_dsize + (pp).vm_ssize)
int machine_init()
{
/* get the page size with "getpagesize" and calculate pageshift from it */
int pagesize = getpagesize();
pageshift = 0;
while(pagesize > 1)
{
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
return(TRUE);
}
int machine_close()
{
return(TRUE);
}
int machine_get_battstat(int *acstat, int *battflag, int *percent)
{
int apmd;
struct apm_power_info api;
*acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT;
*percent = 100;
if((apmd = open("/dev/apm", O_RDONLY)) == -1)
{
perror("get_battstat_open");
return(TRUE);
}
if(ioctl(apmd, APM_IOC_GETPOWER, &api) == -1)
{
perror("get_battstat_ioctl");
return(FALSE);
}
switch(api.ac_state)
{
case APM_AC_OFF:
*acstat = LCDP_AC_OFF;
break;
case APM_AC_ON:
*acstat = LCDP_AC_ON;
break;
default:
*acstat = LCDP_AC_UNKNOWN;
break;
}
switch(api.battery_state)
{
case APM_BATT_HIGH:
*battflag = LCDP_BATT_HIGH;
break;
case APM_BATT_LOW:
*battflag = LCDP_BATT_LOW;
break;
case APM_BATT_CRITICAL:
*battflag = LCDP_BATT_CRITICAL;
break;
case APM_BATT_CHARGING:
*battflag = LCDP_BATT_CHARGING;
break;
case APM_BATTERY_ABSENT:
*battflag = LCDP_BATT_ABSENT;
break;
default:
*battflag = LCDP_BATT_UNKNOWN;
break;
}
*percent = api.battery_life;
close(apmd);
return(TRUE);
}
int machine_get_fs(mounts_type fs[], int *cnt)
{
struct statfs *mntbuf;
struct statfs *pp;
int statcnt, fscnt, i;
fscnt = getmntinfo(&mntbuf, MNT_WAIT);
if(fscnt == 0)
{
perror("getmntinfo");
return(FALSE);
}
for(statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++)
{
if(strcmp(pp->f_fstypename, "procfs")
&& strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs")
#endif
)
{
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 255, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks;
if(fs[statcnt].blocks > 0)
{
fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files;
fs[statcnt].ffree = pp->f_ffree;
}
statcnt++;
}
}
*cnt = statcnt;
return(TRUE);
}
int machine_get_load(load_type *curr_load)
{
static load_type last_load = { 0, 0, 0, 0, 0 };
static load_type last_ret_load;
load_type load;
long cp_time[CPUSTATES];
size_t size;
int mib[2];
mib[0] = CTL_KERN;
mib[1] = KERN_CPTIME;
size = sizeof(cp_time);
if(sysctl(mib, 2, cp_time, &size, NULL, 0) < 0)
{
perror("sysctl kern.cp_time failed");
return(FALSE);
}
load.user = (unsigned long) (cp_time[CP_USER]);
load.nice = (unsigned long) (cp_time[CP_NICE]);
load.system = (unsigned long) (cp_time[CP_SYS] + cp_time[CP_INTR]);
load.idle = (unsigned long) (cp_time[CP_IDLE]);
load.total = load.user + load.nice + load.system + load.idle;
if(load.total != last_load.total)
{
curr_load->user = load.user - last_load.user;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load;
last_load = load;
}
else
{
*curr_load = last_ret_load;
}
return(TRUE);
}
int machine_get_loadavg(double *load)
{
double loadavg[1];
if(getloadavg(loadavg, 1) == -1)
return(FALSE);
*load = loadavg[0];
return(TRUE);
}
int machine_get_meminfo(meminfo_type *result)
{
size_t size;
int mib[2];
struct uvmexp suvm;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP;
size = sizeof(suvm);
if(sysctl(mib, 2, &suvm, &size, NULL, 0) < 0)
{
perror("sysctl vm.uvmexp failed");
return(FALSE);
}
/* memory */
result[0].total = pagetok(suvm.npages);
result[0].free = pagetok(suvm.free);
/* not used anyway */
result[0].shared = 0;
result[0].buffers = 0;
result[0].cache = 0;
/* swap */
result[1].total = suvm.pagesize * suvm.swpages;
result[1].free = suvm.pagesize * suvm.swpginuse;
result[1].free = result[1].total - result[1].free;
return(TRUE);
}
int machine_get_procs(LinkedList *procs)
{
struct kinfo_proc *kprocs;
int nproc, i;
procinfo_type *p;
kvm_t *kvmd;
if((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL)
{
perror("kvm_open");
return(FALSE);
}
kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc);
if(kprocs == NULL)
{
perror("kvm_getprocs");
kvm_close(kvmd);
return(FALSE);
}
for(i = 0; i < nproc; i++)
{
p = malloc(sizeof(procinfo_type));
if(!p)
{
perror("mem_top_malloc");
kvm_close(kvmd);
return(FALSE);
}
strncpy(p->name, kprocs->kp_proc.p_comm, 15);
p->name[15] = '\0';
p->totl = pagetok(PROCSIZE(kprocs->kp_eproc.e_vm));
p->number = kprocs->kp_proc.p_pid;
LL_Push(procs, (void *)p);
kprocs++;
}
kvm_close(kvmd);
return(TRUE);
}
int machine_get_smpload(load_type *result, int *numcpus)
{
int mib[2], i, num;
size_t size;
load_type curr_load;
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
size = sizeof(int);
if(sysctl(mib, 2, &num, &size, NULL, 0) < 0)
{
perror("sysctl hw.ncpu");
return(FALSE);
}
if(machine_get_load(&curr_load) == FALSE)
return(FALSE);
*numcpus = num;
num = num > 8 ? 8 : num;
/* Don't know how to get per-cpu-load values */
for(i = 0; i < num; i++)
{
result[i] = curr_load;
}
return(TRUE);
}
int machine_get_uptime(double *up, double *idle)
{
size_t size;
time_t now;
struct timeval boottime;
int mib[2];
load_type curr_load;
mib[0] = CTL_KERN;
mib[1] = KERN_BOOTTIME;
size = sizeof(boottime);
time(&now);
if(sysctl(mib, 2, &boottime, &size, NULL, 0) < 0)
{
perror("sysctl kern.boottime failed");
return(FALSE);
}
*up = (double)(now - boottime.tv_sec);
if(machine_get_load(&curr_load) == FALSE)
*idle = 100.;
else
*idle = 100.*curr_load.idle/curr_load.total;
return(TRUE);
}
#endif /* __OpenBSD__ */
Binary file not shown.
+443
View File
@@ -0,0 +1,443 @@
#ifdef sun
#include <sys/types.h>
#include <sys/param.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include <fcntl.h>
#include <dirent.h>
#include <utmpx.h>
#include <procfs.h>
#include <kstat.h>
#include <errno.h>
#include <sys/utsname.h>
#include <sys/fcntl.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/loadavg.h>
#include <sys/mount.h>
#include <sys/cpuvar.h>
#include <sys/swap.h>
#include <sys/statvfs.h>
#include "machine.h"
#include "main.h"
#include "config.h"
#include "shared/LL.h"
#define MAX_CPUS 8
static kstat_ctl_t *kc;
int machine_init()
{
kc = NULL;
kc = kstat_open();
if(kc == NULL)
{
perror("kstat_open");
return(FALSE);
}
return(TRUE);
}
int machine_close()
{
if(kc != NULL)
{
kstat_close(kc);
kc = NULL;
}
return(TRUE);
}
int machine_get_battstat(int *acstat, int *battflag, int *percent)
{
*acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT;
*percent = 100;
return(TRUE);
}
int machine_get_fs(mounts_type fs[], int *cnt)
{
FILE *mtab_fd;
char line[256];
int x = 0, y;
#ifdef STAT_STATVFS
struct statvfs fsinfo;
#else
struct statfs fsinfo;
#endif
#ifdef MTAB_FILE
mtab_fd = fopen(MTAB_FILE, "r");
#else
#error "Can't find your mounted filesystem table file."
#endif
// Get rid of old, unmounted filesystems...
memset(fs, 0, sizeof(mounts_type) * 256);
while(x < 256)
{
if(fgets(line, 256, mtab_fd) == NULL)
{
fclose(mtab_fd);
*cnt = x;
break;
}
sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type);
if( strcmp(fs[x].type, "proc")
#ifndef STAT_NFS
&& strcmp(fs[x].type, "nfs")
#endif
#ifndef STAT_SMBFS
&& strcmp(fs[x].type, "smbfs")
#endif
)
{
#ifdef STAT_STATVFS
y = statvfs(fs[x].mpoint, &fsinfo);
#elif STAT_STATFS2_BSIZE
y = statfs(fs[x].mpoint, &fsinfo);
#elif STAT_STATFS4
y = statfs(fs[x].mpoint, &fsinfo, sizeof(fsinfo), 0);
#else
#error "statfs for this system noy yet supported"
#endif
fs[x].blocks = fsinfo.f_blocks;
if(fs[x].blocks > 0)
{
fs[x].bsize = fsinfo.f_bsize;
fs[x].bfree = fsinfo.f_bfree;
fs[x].files = fsinfo.f_files;
fs[x].ffree = fsinfo.f_ffree;
x++;
}
}
}
fclose(mtab_fd);
*cnt = x;
return(TRUE);
}
int machine_get_load(load_type *curr_load)
{
static load_type last_load = { 0, 0, 0, 0, 0 };
static load_type last_ret_load;
load_type load;
kstat_t *k_space;
struct cpu_stat cinfo;
k_space = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0");
if(k_space == NULL)
{
fprintf(stderr, "kstat lookup error\n");
return(FALSE);
}
if(kstat_read(kc, k_space, NULL) == -1)
{
fprintf(stderr, "kstat read error\n");
return(FALSE);
}
k_space = kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0");
if(k_space == NULL)
{
fprintf(stderr, "kstat lookup error\n");
return(FALSE);
}
if(kstat_read(kc, k_space, &cinfo) == -1)
{
fprintf(stderr, "kstat read error\n");
return(FALSE);
}
load.idle = cinfo.cpu_sysinfo.cpu[CPU_IDLE];
load.user = cinfo.cpu_sysinfo.cpu[CPU_USER];
load.system = cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
load.nice = cinfo.cpu_sysinfo.cpu[CPU_WAIT];
load.total = load.user + load.nice + load.system + load.idle;
if(load.total != last_load.total)
{
curr_load->user = load.user - last_load.user;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load;
last_load = load;
}
else
{
*curr_load = last_ret_load;
}
return(TRUE);
}
int machine_get_loadavg(double *load)
{
double loadavg[1];
if(getloadavg(loadavg, 1) == -1)
return(FALSE);
*load = loadavg[0];
return(TRUE);
}
int machine_get_meminfo(meminfo_type *result)
{
#define MAXSTRSIZE 80
swaptbl_t *s;
int i, n, num;
char *strtab; /* string table for path names */
s = NULL;
result[0].total = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].free = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].shared = 0;
result[0].buffers = 0;
result[0].cache = 0;
again:
if((num = swapctl(SC_GETNSWP, 0)) == -1)
{
perror("swapctl: GETNSWP");
return(FALSE);
}
if(num == 0)
{
fprintf(stderr, "No Swap Devices Configured\n");
return(FALSE);
}
/* allocate swaptable for num+1 entries */
if((s = (swaptbl_t*)malloc(num * sizeof(swapent_t) + sizeof(struct swaptable))) == NULL)
{
perror("malloc swap");
return(FALSE);
}
/* allocate num+1 string holders */
if((strtab = (char*)malloc((num + 1) * MAXSTRSIZE)) == NULL)
{
perror("malloc string holder");
return(FALSE);
}
/* initialize string pointers */
for(i = 0; i < (num + 1); i++)
{
s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE);
}
s->swt_n = num + 1;
if((n = swapctl(SC_LIST, s)) < 0)
{
perror("swapctl");
return(FALSE);
}
/* more were added */
if(n > num)
{
free(s);
free(strtab);
goto again;
}
result[1].total = 0;
result[1].free = 0;
for(i = 0; i < n; i++)
{
result[1].total = result[1].total + s->swt_ent[i].ste_pages * sysconf(_SC_PAGESIZE) / 1024;
result[1].free = result[1].free + s->swt_ent[i].ste_free * sysconf(_SC_PAGESIZE) / 1024;
}
return(TRUE);
}
int machine_get_procs(LinkedList *procs)
{
char buf[128];
DIR *proc;
FILE *StatusFile;
struct dirent *procdir;
procinfo_type *p;
char procName[16];
int procSize, procRSS, procData, procStk, procExe;
int threshold = 400, unique;
if((proc = opendir("/proc")) == NULL)
{
perror("open /proc");
return(FALSE);
}
while((procdir = readdir(proc)))
{
psinfo_t psinfo;
if(!index("1234567890", procdir->d_name[0]))
continue;
sprintf(buf, "/proc/%s/psinfo", procdir->d_name);
if((StatusFile = fopen(buf, "r")) == NULL)
{
// Not a serious error; process has finished before we could
// examine it:
continue;
}
procRSS = procSize = procData = procStk = procExe = 0;
fread(&psinfo, sizeof(psinfo), 1, StatusFile);
strcpy(procName, psinfo.pr_fname);
procSize = psinfo.pr_size;
procRSS = psinfo.pr_rssize;
// Following values not accurate, not sure what needs to be set to
procData = psinfo.pr_size;
procStk = 0;
procExe = 0;
fclose(StatusFile);
if(procSize > threshold)
{
// Figure out if it's sharing any memory...
unique = 1;
LL_Rewind(procs);
do
{
p = LL_Get(procs);
if(p)
{
if(0 == strcmp(p->name, procName))
{
unique = 0;
p->number++;
p->totl += procData + procStk + procExe;
}
}
}while(LL_Next(procs) == 0);
// If this is the first one by this name...
if(unique)
{
p = malloc(sizeof(procinfo_type));
if(p == NULL)
{
perror("allocating process entry");
goto end; // Ack! I hate goto's!
}
strcpy(p->name, procName);
p->totl = procData + procStk + procExe;
p->number = 1;
// TODO: Check for errors here?
LL_Push(procs, (void *) p);
}
}
}
end:
closedir(proc);
return(TRUE);
}
int machine_get_smpload(load_type *result, int *numcpus)
{
static load_type last_load[MAX_CPUS];
load_type curr_load[MAX_CPUS];
kstat_t *k_space;
int ncpu = 0, count;
char buffer[16];
*numcpus = sysconf(_SC_NPROCESSORS_CONF);
for(count = 0; count < MAX_CPUS; count++)
{
sprintf(buffer, "cpu_stat%d", count);
k_space = kstat_lookup(kc, "cpu_stat", count, buffer);
if((k_space != NULL) && (kstat_read(kc, k_space, NULL) != -1))
{
struct cpu_stat cinfo;
k_space = kstat_lookup(kc, "cpu_stat", -1, buffer);
kstat_read(kc,k_space,&cinfo);
curr_load[ncpu].idle = cinfo.cpu_sysinfo.cpu[CPU_IDLE];
curr_load[ncpu].user = cinfo.cpu_sysinfo.cpu[CPU_USER];
curr_load[ncpu].system = cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
curr_load[ncpu].nice = cinfo.cpu_sysinfo.cpu[CPU_WAIT];
//Hu? Why? curr_load[ncpu].nice = 0;
curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice + curr_load[ncpu].system + curr_load[ncpu].idle;
result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total;
result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user;
result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice;
result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system;
result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle;
last_load[ncpu].total = curr_load[ncpu].total;
last_load[ncpu].user = curr_load[ncpu].user;
last_load[ncpu].nice = curr_load[ncpu].nice;
last_load[ncpu].system = curr_load[ncpu].system;
last_load[ncpu].idle = curr_load[ncpu].idle;
ncpu++;
}
}
return(TRUE);
}
/* TODO get idle time! */
int machine_get_uptime(double *up, double *idle)
{
struct utmpx *u, id;
load_type curr_load;
*up = 0;
*idle = 0;
id.ut_type = BOOT_TIME;
u = getutxid(&id);
if(u == NULL)
return(FALSE);
*up = time(0) - u->ut_xtime;
if(machine_get_load(&curr_load) == FALSE)
*idle = 100.;
else
*idle = 100.*curr_load.idle/curr_load.total;
return(TRUE);
}
#endif /* sun */
Binary file not shown.
+274 -260
View File
@@ -1,12 +1,15 @@
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <termios.h>
#include <errno.h>
#include <stdlib.h>
#include <signal.h>
#include <netdb.h>
#include <ctype.h>
#include <sys/utsname.h>
#include <sys/param.h>
#ifdef HAVE_CONFIG_H
#include "config.h"
@@ -29,140 +32,192 @@ int lcd_wid;
int lcd_hgt;
int lcd_cellwid;
int lcd_cellhgt;
static struct utsname unamebuf;
/*
Mode List:
See below... (default_sequence[])
*/
void HelpScreen ();
void exit_program (int val);
void main_loop (mode * sequence);
static void HelpScreen();
static void exit_program(int val);
static void main_loop();
static mode *sequence = NULL;
static int islow = -1;
#define MAX_SEQUENCE 256
// 1/8th second is a single time unit...
#define TIME_UNIT 125000
// Contains a list of modes to run
mode default_sequence[] = {
static mode default_sequence[] =
{
// which on off inv timer/visible
{'C', 1, 2, 0, 0xffff, 0,}, // [C]PU
{'M', 4, 16, 0, 0xffff, 0,}, // [M]emory
{'X', 64, 128, 1, 0xffff, 0,}, // [X]-load (load histogram)
{'T', 4, 64, 0, 0xffff, 0,}, // [T]ime/Date
{'C', 1, 2, 0, 0xffff, 0,}, // [C]PU
{'M', 4, 16, 0, 0xffff, 0,}, // [M]emory
{'X', 64, 128, 1, 0xffff, 0,}, // [X]-load (load histogram)
{'T', 4, 64, 0, 0xffff, 0,}, // [T]ime/Date
{'A', 999, 9999, 0, 0xffff, 0,}, // [A]bout (credits)
{1, 0, 0, 0, 0, 0,}, // Modes after this line will not be run by default...
{1, 0, 0, 0, 0, 0,}, // Modes after this line will not be run by default...
// ... all non-default modes must be in here!
// ... they will not show up otherwise.
{'P', 1, 2, 0, 0xffff, 0,}, // [O]ld Timescreen
{'O', 4, 64, 0, 0xffff, 0,}, // [O]ld Timescreen
{'K', 4, 64, 0, 0xffff, 0,}, // big cloc[K]
{'U', 4, 128, 0, 0xffff, 0,}, // Old [U]ptime Screen
{'B', 32, 256, 1, 0xffff, 0,}, // [B]attery Status
{'G', 1, 2, 0, 0xffff, 0,}, // Cpu histogram [G]raph
{'S', 16, 256, 1, 0xffff, 0,}, // [S]ize of biggest programs
{'D', 256, 256, 1, 0xffff, 0,}, // [D]isk stats
{0, 0, 0, 0, 0,}, // No more.. all done.
{'P', 1, 2, 0, 0xffff, 0,}, // [O]ld Timescreen
{'O', 4, 64, 0, 0xffff, 0,}, // [O]ld Timescreen
{'K', 4, 64, 0, 0xffff, 0,}, // big cloc[K]
{'U', 4, 128, 0, 0xffff, 0,}, // Old [U]ptime Screen
{'B', 32, 256, 1, 0xffff, 0,}, // [B]attery Status
{'G', 1, 2, 0, 0xffff, 0,}, // Cpu histogram [G]raph
{'S', 16, 256, 1, 0xffff, 0,}, // [S]ize of biggest programs
{'D', 256, 256, 1, 0xffff, 0,}, // [D]isk stats
{0, 0, 0, 0, 0,}, // No more.. all done.
};
// TODO: Clean up main()... It is still way too big.
// TODO: Config file; not just command line
int
main (int argc, char **argv)
const char *get_hostname()
{
mode sequence[MAX_SEQUENCE];
char cfgfile[256] = "/etc/lcdproc.cf";
char server[256] = "localhost";
int port = LCDPORT;
int i, j, k;
int tmp;
int daemonize=0;
return(unamebuf.nodename);
}
memset (sequence, 0, sizeof (mode) * MAX_SEQUENCE);
const char *get_sysname()
{
return(unamebuf.sysname);
}
const char *get_sysrelease()
{
return(unamebuf.release);
}
int
main(int argc, char **argv)
{
char *server = NULL;
int i, j, seqlen;
int port = LCDPORT;
int daemonize = FALSE;
if(uname(&unamebuf) == -1)
{
perror("uname");
return(EXIT_FAILURE);
}
seqlen = 0;
while(default_sequence[seqlen].which != 0)
seqlen++;
seqlen++;
sequence = (mode*)malloc(seqlen*sizeof(mode));
if(sequence == NULL)
{
perror("sequence malloc");
return(EXIT_FAILURE);
}
for(i = 0; i < seqlen; i++)
{
//sequence[i] = default_sequence[i];
memcpy(&sequence[i], &default_sequence[i], sizeof(mode));
}
// Ctrl-C will cause a clean exit...
signal (SIGINT, exit_program);
signal(SIGINT, exit_program);
// and "kill"...
signal (SIGTERM, exit_program);
signal(SIGTERM, exit_program);
// and "kill -HUP" (hangup)...
signal (SIGHUP, exit_program);
signal(SIGHUP, exit_program);
// and just in case, "kill -KILL" (which cannot be trapped; but oh well)
signal (SIGKILL, exit_program);
signal(SIGKILL, exit_program);
// Command line
memcpy (sequence, default_sequence, sizeof (default_sequence));
for (i = 1, j = 0; i < argc; i++) {
if (argv[i][0] == '-')
switch (argv[i][1]) {
for(i = 1, j = 0; i < argc; i++)
{
if(argv[i][0] == '-')
{
switch(argv[i][1])
{
// s is for server
case 'S':
case 's':
if (argc < i + 1)
HelpScreen ();
strcpy (server, argv[++i]);
break;
// p is for port
case 'P':
case 'p':
if (argc < i + 1)
HelpScreen ();
port = atoi (argv[++i]);
if (port < 1 && port > 0xffff)
fprintf (stderr, "Warning: Port %i outside of standard range\n", port);
break;
case 'd':
case 'D':
daemonize=1;
break;
// otherwise... Get help!
default:
HelpScreen ();
break;
case 'S':
case 's':
if(argc < i + 1)
HelpScreen();
if(server == NULL)
server = argv[++i];
else
fprintf(stderr, "Ignoring additional server: %s\n", argv[++i]);
break;
// p is for port
case 'P':
case 'p':
if(argc < i + 1)
HelpScreen();
port = atoi(argv[++i]);
if(port < 1 && port > 0xffff)
fprintf(stderr, "Warning: Port %d outside of standard range\n", port);
break;
case 'e':
case 'E':
if(argc < i + 1)
HelpScreen();
islow = atoi(argv[++i]);
break;
case 'd':
case 'D':
daemonize = TRUE;
break;
// otherwise... Get help!
default:
HelpScreen();
break;
}
// Parse command line here... read the man page.
else if (strlen (argv[i]) == 1) {
}
else if(strlen(argv[i]) == 1)
{
int k, found = FALSE;
if(j >= seqlen)
continue;
// Grab the mode letter...
sequence[j].which = argv[i][0];
// If we have just a letter with no numbers...
// Set the defaults...
for (tmp = 0, k = 0; default_sequence[k].which; k++) {
if (toupper (sequence[j].which) == default_sequence[k].which) {
memcpy (&sequence[j], &default_sequence[k], sizeof (mode));
tmp = 1;
for(k = 0; k < seqlen; k++)
{
if(toupper(sequence[j].which) == default_sequence[k].which)
{
//sequence[j] = default_sequence[k];
memcpy(&sequence[j], &default_sequence[k], sizeof(mode));
found = TRUE;
break;
}
}
if (!tmp) {
printf ("Invalid Mode: %c\n", argv[i][0]);
exit (0);
}
j++;
// Set the last element to 0...
memset (sequence + j, 0, sizeof (mode));
} // End if(strlen(argv == 1))
else {
// A multicharacter parameter by itself
// is assumed to be a config file..
strcpy (cfgfile, argv[i]);
printf ("Ignoring config file: %s\n", cfgfile);
if(!found)
{
fprintf(stderr, "Invalid Mode: %c, ignoring\n", argv[i][0]);
}
else
{
j++;
memcpy(&sequence[j], &default_sequence[seqlen-1], sizeof(mode));
//sequence[j] = default_sequence[seqlen-1];
}
}
}
if(server == NULL)
server = "localhost";
// Connect to the server...
usleep (500000); // wait for the server to start up
sock = sock_connect (server, port);
if (sock <= 0) {
printf ("Error connecting to LCD server %s on port %i.\nCheck to see that the server is running and operating normally.\n", server, port);
usleep(500000); // wait for the server to start up
sock = sock_connect(server, port);
if(sock <= 0)
{
printf("Error connecting to LCD server %s on port %i.\nCheck to see that the server is running and operating normally.\n", server, port);
return 0;
}
sock_send_string (sock, "hello\n");
usleep (500000); // wait for the server to say hi.
sock_send_string(sock, "hello\n");
usleep(500000); // wait for the server to say hi.
// We grab the real values below, from the "connect" line.
lcd_wid = 20;
@@ -170,55 +225,57 @@ main (int argc, char **argv)
lcd_cellwid = 5;
lcd_cellhgt = 8;
if(daemonize) {
if (daemon(1,0)!=0)
fprintf(stderr, "Error: daemonize failed");;
}
if(daemonize)
if(daemon(1,0) != 0)
fprintf(stderr, "Error: daemonize failed");
// Init the status gatherers...
mode_init (sequence);
mode_init();
// And spew stuff!
main_loop (sequence);
main_loop();
// Clean up
exit_program (0);
return 0;
exit_program(EXIT_SUCCESS);
return(0);
}
void
HelpScreen ()
{
printf ("LCDproc, %s\n", version);
printf ("Usage: lcdproc [-s server] [-p port] [-d] [modelist]\n");
printf ("\tOptions in []'s are optional.\n");
printf ("\tmodelist is \"mode [mode mode ...]\"\n");
printf ("\tMode letters: \t[C]pu [G]raph [T]ime [M]emory [X]load [D]isk [B]attery\n\t\t\tproc_[S]izes [O]ld_time big_cloc[K] [U]ptime CPU_SM[P]\n\t\t\t[A]bout\n");
printf ("\n");
printf ("\tUse \"man lcdproc\" for more info.\n");
printf ("Example:\n");
printf ("\tlcdproc -s my.lcdproc.server.com C M X -p 13666\n");
printf ("\n");
exit (0);
printf("LCDproc, %s\n", version);
printf("Usage: lcdproc [-s server] [-p port] [-e islow] [-d] [modelist]\n");
printf("\tOptions in []'s are optional.\n");
printf("\tmodelist is \"mode [mode mode ...]\"\n");
printf("\tMode letters: \t[C]pu [G]raph [T]ime [M]emory [X]load [D]isk [B]attery\n\t\t\tproc_[S]izes [O]ld_time big_cloc[K] [U]ptime CPU_SM[P]\n\t\t\t[A]bout\n");
printf("\n");
printf("\tislow is to slow down initial announcement of modes (in 1/100 sec)\n");
printf("\tUse \"man lcdproc\" for more info.\n");
printf("Example:\n");
printf("\tlcdproc -s my.lcdproc.server.com C M X -p 13666\n");
printf("\n");
exit(EXIT_FAILURE);
}
///////////////////////////////////////////////////////////////////
// Called upon TERM and INTR signals...
//
void
exit_program (int val)
exit_program(int val)
{
Quit = 1;
sock_close (sock);
mode_close ();
exit (0);
sock_close(sock);
mode_close();
free(sequence);
exit(val);
}
///////////////////////////////////////////////////////////////////
// Main program loop...
//
void
main_loop (mode * sequence)
main_loop()
{
int i = 0, j;
//int status=0;
@@ -229,182 +286,139 @@ main_loop (mode * sequence)
// Main loop
// Run whatever screen we want, then wait. Woo-hoo!
while (!Quit) {
while(!Quit)
{
// Check for server input...
len = sock_recv (sock, buf, 8000);
len = sock_recv(sock, buf, 8000);
// Handle server input...
while (len > 0) {
while(len > 0)
{
// Now split the string into tokens...
//for(i=0; i<argc; i++) argv[i]=NULL; // Get rid of old tokens
argc = 0;
newtoken = 1;
for (i = 0; i < len; i++) {
switch (buf[i]) {
case ' ':
newtoken = 1;
buf[i] = 0;
for(i = 0; i < len; i++)
{
switch(buf[i])
{
case ' ':
newtoken = 1;
buf[i] = 0;
break;
default: // regular chars, keep tokenizing
if (newtoken) {
argv[argc] = buf + i;
argc++;
}
newtoken = 0;
default: // regular chars, keep tokenizing
if(newtoken)
argv[argc++] = buf + i;
newtoken = 0;
break;
case '\0':
case '\n':
buf[i] = 0;
if (argc > 0) {
//printf("%s %s\n", argv[0], argv[1]);
if (0 == strcmp (argv[0], "listen")) {
for (j = 0; sequence[j].which; j++) {
if (sequence[j].which == argv[1][0]) {
sequence[j].visible = 1;
//debug("Listen %s\n", argv[1]);
case '\0':
case '\n':
buf[i] = 0;
if(argc > 0)
{
//printf("%s %s\n", argv[0], argv[1]);
if(0 == strcmp(argv[0], "listen"))
{
for(j = 0; sequence[j].which; j++)
{
if(sequence[j].which == argv[1][0])
{
sequence[j].visible = 1;
//debug("Listen %s\n", argv[1]);
}
}
}
} else if (0 == strcmp (argv[0], "ignore")) {
for (j = 0; sequence[j].which; j++) {
if (sequence[j].which == argv[1][0]) {
sequence[j].visible = 0;
//debug("Ignore %s\n", argv[1]);
else if(0 == strcmp(argv[0], "ignore"))
{
for(j = 0; sequence[j].which; j++)
{
if(sequence[j].which == argv[1][0])
{
sequence[j].visible = 0;
//debug("Ignore %s\n", argv[1]);
}
}
}
} else if (0 == strcmp (argv[0], "key")) {
debug ("Key %s\n", argv[1]);
} else if (0 == strcmp (argv[0], "menu")) {
} else if (0 == strcmp (argv[0], "connect")) {
int a;
for (a = 1; a < argc; a++) {
if (0 == strcmp (argv[a], "wid"))
lcd_wid = atoi (argv[++a]);
else if (0 == strcmp (argv[a], "hgt"))
lcd_hgt = atoi (argv[++a]);
else if (0 == strcmp (argv[a], "cellwid"))
lcd_cellwid = atoi (argv[++a]);
else if (0 == strcmp (argv[a], "cellhgt"))
lcd_cellhgt = atoi (argv[++a]);
else if(0 == strcmp(argv[0], "key"))
{
debug("Key %s\n", argv[1]);
}
else if(0 == strcmp(argv[0], "menu"))
{
}
else if(0 == strcmp(argv[0], "connect"))
{
int a;
for(a = 1; a < argc; a++)
{
if(0 == strcmp(argv[a], "wid"))
lcd_wid = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "hgt"))
lcd_hgt = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "cellwid"))
lcd_cellwid = atoi(argv[++a]);
else if(0 == strcmp(argv[a], "cellhgt"))
lcd_cellhgt = atoi(argv[++a]);
}
sock_send_string(sock, "client_set -name LCDproc\n");
}
else if(0 == strcmp(argv[0], "bye"))
{
//printf("Exiting LCDproc\n");
exit_program(EXIT_SUCCESS);
}
else if(0 == strcmp(argv[0], "success"))
{
}
else
{
int j;
for(j = 0; j < argc; j++)
printf("%s ", argv[j]);
printf("\n");
}
sock_send_string (sock, "client_set -name LCDproc\n");
} else if (0 == strcmp (argv[0], "bye")) {
//printf("Exiting LCDproc\n");
exit_program (0);
} else if (0 == strcmp (argv[0], "success")) {
} else {
int j;
for (j = 0; j < argc; j++)
printf ("%s ", argv[j]);
printf ("\n");
}
}
// Restart tokenizing
argc = 0;
newtoken = 1;
break ;
// Restart tokenizing
argc = 0;
newtoken = 1;
break;
} // switch( buf[i] )
}
len = sock_recv (sock, buf, 8000);
len = sock_recv(sock, buf, 8000);
//debug("\n");
}
// Gather stats...
// Update screens...
for (i = 0; sequence[i].which > 1; i++) {
for(i = 0; sequence[i].which > 1; i++)
{
sequence[i].timer++;
if (sequence[i].visible) {
if (sequence[i].timer >= sequence[i].on_time) {
if(sequence[i].visible)
{
if(sequence[i].timer >= sequence[i].on_time)
{
sequence[i].timer = 0;
// Now, update the screen...
update_screen (sequence + i, 1);
}
} else {
if (sequence[i].show_invisible) {
if (sequence[i].timer >= sequence[i].off_time) {
sequence[i].timer = 0;
// Now, update the screen...
update_screen (sequence + i, 1);
}
} else {
if (sequence[i].timer >= sequence[i].off_time) {
sequence[i].timer = 0;
// Now, update the screen...
update_screen (sequence + i, 0);
}
update_screen(&sequence[i], 1);
}
}
else
{
if(sequence[i].timer >= sequence[i].off_time)
{
sequence[i].timer = 0;
// Now, update the screen...
update_screen(&sequence[i], sequence[i].show_invisible);
}
}
if(islow > 0)
usleep(islow * 10000);
}
// Now sleep...
usleep (TIME_UNIT);
/* Old stuff
timer=0;
for(j=0; hold || (j<sequence[i].num_times && !Quit); j++)
{
switch(sequence[i].which)
{
case 'g':
case 'G': status = cpu_graph_screen(j); break;
case 'c':
case 'C': status = cpu_screen(j); break;
case 'o':
case 'O': status = clock_screen(j); break;
case 'k':
case 'K': status = big_clock_screen(j); break;
case 'm':
case 'M': status = mem_screen(j); break;
case 'u':
case 'U': status = uptime_screen(j); break;
case 't':
case 'T': status = time_screen(j); break;
case 'd':
case 'D': status = disk_screen(j); break;
case 'x':
case 'X': status = xload_screen(j); break;
case 'b':
case 'B': status = battery_screen(j); break;
case 'a':
case 'A': status = credit_screen(j); break;
default: status = dumbass_screen(j); break;
}
for(k=0; k<sequence[i].delay_time; k++)
{
usleep(TIME_UNIT); timer++;
// Modify lcd status here... (blinking, for example)
switch(status)
{
case BLINK_ON : blink=1; break;
case BLINK_OFF: blink=0; lcd.backlight(1); break;
case BACKLIGHT_OFF: blink=0; lcd.backlight(0); break;
case BACKLIGHT_ON : blink=0; lcd.backlight(1); break;
case CONTINUE : hold=0; break;
}
status=0;
if(blink) lcd.backlight(! ((timer&15) == 15));
if(heartbeat)
{
// Set this to pulsate like a real heart beat...
// (binary is fun... :)
lcd.icon(!((timer+4)&5), 0);
lcd.chr(lcd.wid, 1, 0);
}
lcd.flush();
PollSockets();
}
}
i++;
if(sequence[i].which < 2) i=0;
*/
usleep(TIME_UNIT);
}
}
+38 -14
View File
@@ -1,6 +1,25 @@
#ifndef MAIN_H
#define MAIN_H
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
#define LCDP_BATT_HIGH 0x00
#define LCDP_BATT_LOW 0x01
#define LCDP_BATT_CRITICAL 0x02
#define LCDP_BATT_CHARGING 0x03
#define LCDP_BATT_ABSENT 0x04
#define LCDP_BATT_UNKNOWN 0xFF
#define LCDP_AC_OFF 0x00
#define LCDP_AC_ON 0x01
#define LCDP_AC_BACKUP 0x02
#define LCDP_AC_UNKNOWN 0xFF
extern int Quit;
extern int sock;
extern char *version;
@@ -11,23 +30,28 @@ extern int lcd_hgt;
extern int lcd_cellwid;
extern int lcd_cellhgt;
typedef struct mode {
char which; // Which screen is it?
int on_time; // How often to update while visible?
int off_time; // How often to get stats while not visible?
typedef struct mode
{
char which; // Which screen is it?
int on_time; // How often to update while visible?
int off_time; // How often to get stats while not visible?
int show_invisible; // Send stats while not visible?
int timer; // Time since last update
int visible; // Can we be seen right now?
int timer; // Time since last update
int visible; // Can we be seen right now?
} mode;
#define BLINK_ON 0x10
#define BLINK_OFF 0x11
#define BACKLIGHT_OFF 0x20
#define BACKLIGHT_ON 0x21
#define HOLD_SCREEN 0x30
#define CONTINUE 0x31
#define BLINK_ON 0x10
#define BLINK_OFF 0x11
#define BACKLIGHT_OFF 0x20
#define BACKLIGHT_ON 0x21
#define HOLD_SCREEN 0x30
#define CONTINUE 0x31
#define LCD_MAX_WIDTH 80
#define LCD_MAX_HEIGHT 80
#define LCD_MAX_WIDTH 80
#define LCD_MAX_HEIGHT 80
const char *get_hostname();
const char *get_sysname();
const char *get_sysrelease();
#endif
+13 -374
View File
@@ -13,188 +13,9 @@
#include "main.h"
#include "mode.h"
#include "machine.h"
#include "mem.h"
#ifdef SOLARIS
#include <strings.h>
#include <sys/stat.h>
#include <sys/swap.h>
#ifdef HAVE_PROCFS_H
# include <procfs.h>
#endif
#ifdef HAVE_SYS_PROCFS_H
# include <sys/procfs.h>
#endif
#endif
#ifdef __NetBSD__
#include <sys/param.h>
#include <sys/sysctl.h>
#include <uvm/uvm_extern.h>
#include <errno.h>
#include <kvm.h>
static int pageshift;
#define pagetok(size) ((size) << pageshift)
#define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize)
static void netbsd_get_procs(LinkedList *procs);
#endif
struct meminfo {
int total, cache, buffers, free, shared;
};
int meminfo_fd = 0;
static void get_mem_info (struct meminfo *result);
static void
get_mem_info (struct meminfo *result)
{
#ifdef SOLARIS
#define MAXSTRSIZE 80
swaptbl_t *s=NULL;
int i, n, num;
char *strtab; /* string table for path names */
result[0].total = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].free = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].shared = 0;
result[0].buffers = 0;
result[0].cache = 0;
again:
if ((num = swapctl(SC_GETNSWP, 0)) == -1) {
perror("swapctl: GETNSWP");
exit(1);
}
if (num == 0) {
fprintf(stderr, "No Swap Devices Configured\n");
exit(2);
}
/* allocate swaptable for num+1 entries */
if ((s = (swaptbl_t *)
malloc(num * sizeof(swapent_t) +
sizeof(struct swaptable))) ==
(void *) 0) {
fprintf(stderr, "Malloc Failed\n");
exit(3);
}
/* allocate num+1 string holders */
if ((strtab = (char *)
malloc((num + 1) * MAXSTRSIZE)) == (void *) 0) {
fprintf(stderr, "Malloc Failed\n");
exit(3);
}
/* initialize string pointers */
for (i = 0; i < (num + 1); i++) {
s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE);
}
s->swt_n = num + 1;
if ((n = swapctl(SC_LIST, s)) < 0) {
perror("swapctl");
exit(1);
}
if (n > num) { /* more were added */
free(s);
free(strtab);
goto again;
}
result[1].total = 0;
result[1].free = 0;
for (i = 0; i < n; i++) {
result[1].total = result[1].total + s->swt_ent[i].ste_pages * sysconf(_SC_PAGESIZE) / 1024;
result[1].free = result[1].free + s->swt_ent[i].ste_free * sysconf(_SC_PAGESIZE) / 1024;
}
#else
#ifdef __NetBSD__
size_t size;
int mib[2];
struct uvmexp_sysctl uvmexp;
mib[0] = CTL_VM;
mib[1] = VM_UVMEXP2;
size = sizeof(uvmexp);
if(sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0)
{
fprintf(stderr, "sysctl vm.uvmexp2 failed: %s\n",
strerror(errno));
exit(EXIT_FAILURE);
}
/* memory */
result[0].total = pagetok(uvmexp.npages);
result[0].free = pagetok(uvmexp.free);
/* not really */
result[0].shared = pagetok(uvmexp.wired);
result[0].buffers = pagetok(uvmexp.execpages);
result[0].cache = pagetok(uvmexp.filepages);
/* swap */
result[1].total = uvmexp.pagesize * uvmexp.swpages;
result[1].free = uvmexp.pagesize * uvmexp.swpginuse;
result[1].free = result[1].total - result[1].free;
#else
reread (meminfo_fd, "get_meminfo:");
result[0].total = getentry ("MemTotal:", buffer);
result[0].free = getentry ("MemFree:", buffer);
result[0].shared = getentry ("MemShared:", buffer);
result[0].buffers = getentry ("Buffers:", buffer);
result[0].cache = getentry ("Cached:", buffer);
result[1].total = getentry ("SwapTotal:", buffer);
result[1].free = getentry ("SwapFree:", buffer);
#endif
#endif
}
int
mem_init ()
{
#ifdef SOLARIS
#else
#ifdef __NetBSD__
/* get the page size with "getpagesize" and calculate pageshift from it */
int pagesize = getpagesize();
pageshift = 0;
while(pagesize > 1)
{
pageshift++;
pagesize >>= 1;
}
/* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10;
#else
if (!meminfo_fd) {
meminfo_fd = open ("/proc/meminfo", O_RDONLY);
if (meminfo_fd < 0) {
perror ("Can't open /proc/meminfo");
exit (1);
}
}
#endif
#endif
return 0;
}
int
mem_close ()
{
#ifdef SOLARIS
#else
#ifdef __NetBSD__
#else
if (meminfo_fd)
meminfo_fd = open ("/proc/meminfo", O_RDONLY);
meminfo_fd = 0;
#endif
#endif
return 0;
}
/////////////////////////////////////////////////////////////////////////
// Mem Screen displays info about memory and swap usage...
//
@@ -202,7 +23,7 @@ int
mem_screen (int rep, int display)
{
int n;
struct meminfo mem[2];
meminfo_type mem[2];
static int first = 1;
static int which_title = 0;
float value;
@@ -211,7 +32,7 @@ mem_screen (int rep, int display)
first = 0;
sock_send_string (sock, "screen_add M\n");
sprintf (buffer, "screen_set M -name {Memory & Swap: %s}\n", host);
sprintf (buffer, "screen_set M -name {Memory & Swap: %s}\n", get_hostname());
sock_send_string (sock, buffer);
if (lcd_hgt >= 4) {
@@ -255,12 +76,12 @@ mem_screen (int rep, int display)
//sock_send_string(sock, "\n");
}
get_mem_info (mem);
machine_get_meminfo(mem);
// flip the title back and forth...
if (lcd_hgt >= 4) {
if (which_title & 4) {
sprintf (buffer, "widget_set M title {%s}\n", host);
sprintf (buffer, "widget_set M title {%s}\n", get_hostname());
sock_send_string (sock, buffer);
} else
sock_send_string (sock, "widget_set M title { MEM -==- SWAP}\n");
@@ -361,26 +182,18 @@ mem_screen (int rep, int display)
return 0;
} // End mem_screen()
typedef struct proc_mem_info {
char name[16]; // Is this really long enough?
// Size isn't used any more...
// Totl stores the "size" of the program now...
int size, totl;
int number;
} proc_mem_info;
static int
sort_procs (void *a, void *b)
{
proc_mem_info *one, *two;
procinfo_type *one, *two;
if (!a)
return 0;
if (!b)
return 0;
one = (proc_mem_info *) a;
two = (proc_mem_info *) b;
one = (procinfo_type *) a;
two = (procinfo_type *) b;
return (two->totl > one->totl);
}
@@ -388,36 +201,18 @@ sort_procs (void *a, void *b)
int
mem_top_screen (int rep, int display)
{
#ifndef __NetBSD__
// Much of this code was ripped from "gmemusage"
char buf[128];
DIR *proc;
FILE *StatusFile;
struct dirent *procdir;
char procName[16];
int
procSize, procRSS, procData, procStk, procExe;
const char
*NameLine = "Name:", *VmSizeLine = "VmSize:", *VmRSSLine = "VmRSS", *VmDataLine = "VmData", *VmStkLine = "VmStk", *VmExeLine = "VmExe";
const int
NameLineLen = strlen (NameLine), VmSizeLineLen = strlen (VmSizeLine), VmDataLineLen = strlen (VmDataLine), VmStkLineLen = strlen (VmStkLine), VmExeLineLen = strlen (VmExeLine), VmRSSLineLen = strlen (VmRSSLine);
int threshold = 400, unique;
#endif /* __NetBSD__ */
int i;
proc_mem_info *p;
procinfo_type *p;
LinkedList *procs;
static int first = 1;
if (first) {
first = 0;
sock_send_string (sock, "screen_add S\n");
sprintf (buffer, "screen_set S -name {Top Memory Use: %s}\n", host);
sprintf (buffer, "screen_set S -name {Top Memory Use: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add S title title\n");
sprintf (buffer, "widget_set S title {TOP MEM: %s}\n", host);
sprintf (buffer, "widget_set S title {TOP MEM: %s}\n", get_hostname());
sock_send_string (sock, buffer);
sock_send_string (sock, "widget_add S f frame\n");
if (lcd_hgt >= 4)
@@ -437,109 +232,7 @@ mem_top_screen (int rep, int display)
return -1;
}
#ifdef __NetBSD__
netbsd_get_procs(procs);
#else
if ((proc = opendir ("/proc")) == NULL) {
fprintf (stderr, "mem_top_screen: unable to open /proc");
perror ("");
return -1;
}
while ((procdir = readdir (proc))) {
if (!index ("1234567890", procdir->d_name[0])) {
continue;
}
#ifndef SOLARIS
sprintf (buf, "/proc/%s/status", procdir->d_name);
if ((StatusFile = fopen (buf, "r")) == NULL) {
// Not a serious error; process has finished before we could
// examine it:
//fprintf ( stderr , "mem_top_screen: cannot open %s for reading" ,
// buf ) ;
//perror ( "" ) ;
continue;
}
procRSS = procSize = procData = procStk = procExe = 0;
while (fgets (buf, sizeof (buf), StatusFile)) {
if (!strncmp (buf, NameLine, NameLineLen)) {
/* Name: procName */
sscanf (buf, "%*s %s", procName);
} else if (!strncmp (buf, VmSizeLine, VmSizeLineLen)) {
/* VmSize: procSize kB */
sscanf (buf, "%*s %d", &procSize);
} else if (!strncmp (buf, VmRSSLine, VmRSSLineLen)) {
/* VmRSS: procRSS kB */
sscanf (buf, "%*s %d", &procRSS);
} else if (!strncmp (buf, VmDataLine, VmDataLineLen)) {
/* VmData: procData kB */
sscanf (buf, "%*s %d", &procData);
} else if (!strncmp (buf, VmStkLine, VmStkLineLen)) {
/* VmStk: procStk kB */
sscanf (buf, "%*s %d", &procStk);
} else if (!strncmp (buf, VmExeLine, VmExeLineLen)) {
/* VmExe: procExe kB */
sscanf (buf, "%*s %d", &procExe);
}
}
#else
sprintf (buf, "/proc/%s/psinfo", procdir->d_name);
if ((StatusFile = fopen (buf, "r")) == NULL) {
// Not a serious error; process has finished before we could
// examine it:
//fprintf ( stderr , "mem_top_screen: cannot open %s for reading" ,
// buf ) ;
//perror ( "" ) ;
continue;
}
{
psinfo_t psinfo;
fread(&psinfo,sizeof(psinfo),1,StatusFile);
procRSS = procSize = procData = procStk = procExe = 0;
strcpy(procName,psinfo.pr_fname);
procSize=psinfo.pr_size;
procRSS=psinfo.pr_rssize;
// Following values not accurate, not sure what needs to be set to
procData=psinfo.pr_size;
procStk=0;
procExe=0;
}
#endif
fclose (StatusFile);
if (procSize > threshold) {
// Figure out if it's sharing any memory...
unique = 1;
LL_Rewind (procs);
do {
p = LL_Get (procs);
if (p) {
if (0 == strcmp (p->name, procName)) {
unique = 0;
p->number++;
p->totl += procData + procStk + procExe;
}
}
} while (LL_Next (procs) == 0);
// If this is the first one by this name...
if (unique) {
p = malloc (sizeof (proc_mem_info));
if (!p) {
fprintf (stderr, "mem_top_screen: Error allocating process entry\n");
goto end; // Ack! I hate goto's!
}
strcpy (p->name, procName);
p->size = procSize;
p->totl = procData + procStk + procExe;
p->number = 1;
// TODO: Check for errors here?
LL_Push (procs, (void *) p);
}
}
}
closedir (proc);
#endif /* __NetBSD__ */
machine_get_procs(procs);
// Now, print some info...
LL_Rewind (procs);
@@ -566,13 +259,10 @@ mem_top_screen (int rep, int display)
LL_Next (procs);
}
#ifndef __NetBSD__
end: // Ack! I hate using labels!
#endif
// Now clean it all up...
LL_Rewind (procs);
do {
p = (proc_mem_info *) LL_Get (procs);
p = (procinfo_type *) LL_Get (procs);
if (p) {
//printf("Proc: %6ik %s\n", p->size, p->name);
free (p);
@@ -584,54 +274,3 @@ mem_top_screen (int rep, int display)
}
#ifdef __NetBSD__
static void netbsd_get_procs(LinkedList *procs)
{
kvm_t *kvmd = NULL;
struct kinfo_proc2 *kprocs = NULL;
struct kinfo_proc2 *pp;
int nproc, i;
proc_mem_info *p;
if((kvmd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open")) == NULL
)
{
fprintf(stderr, "kvm_openfiles\n");
exit(EXIT_FAILURE);
}
kprocs = kvm_getproc2(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc);
if(kprocs == NULL)
{
fprintf(stderr, "kvm_getproc2\n");
exit(EXIT_FAILURE);
}
for(pp = kprocs, i = 0; i < nproc; pp++, i++)
{
/*
printf("proc: %s (%ld) size: %ld K\n", pp->p_comm,
(long) (pp->p_pid),
(long) (PROCSIZE(pp) << pageshift));
*/
p = malloc(sizeof(proc_mem_info));
if(!p)
{
fprintf(stderr, "mem_top_screen: Error allocating process entry\n");
kvm_close(kvmd);
exit(EXIT_FAILURE);
}
strncpy(p->name, pp->p_comm, 15);
p->name[15] = '\0';
p->totl = (PROCSIZE(pp) << pageshift);
p->number = pp->p_pid;
/* TODO: Check for errors here? */
LL_Push(procs, (void *)p);
kprocs++;
}
kvm_close(kvmd);
}
#endif /* __NetBSD__ */
-3
View File
@@ -1,9 +1,6 @@
#ifndef MEM_H
#define MEM_H
int mem_init ();
int mem_close ();
int mem_screen (int rep, int display);
int mem_top_screen (int rep, int display);
+124 -231
View File
@@ -1,8 +1,3 @@
/*
* This file contains status-gathering code *and* modescreen functions.
* It's long, and messy.
*
*/
#include <stdlib.h>
#include <stdio.h>
@@ -16,7 +11,7 @@
#endif
#if defined( IRIX ) || defined( SOLARIS )
# include <strings.h>
#include <strings.h>
#endif
#include "shared/sockets.h"
@@ -31,271 +26,169 @@
#include "disk.h"
#include "load.h"
#include "mem.h"
int ELLIPSIS = '-';
#include "machine.h"
// TODO: Clean this up... Support multiple display sizes..
char buffer[1024];
char host[16];
void reread (int f, char *errmsg);
int getentry (const char *tag, const char *bufptr);
char *tmp = NULL;
char *buffer = NULL;
int
mode_init (mode * sequence)
mode_init()
{
int i;
struct utsname Uname;
// Grab the host name
if (0 > uname (&Uname)) {
perror ("Can't get hostname");
strcpy (host, "host");
} else {
strncpy (host, Uname.nodename, 15);
host[16] = 0;
if (index (host, '.')) // truncate at the first dot
*(index (host, '.')) = '\0';
for (i = 0; i < 16; i++)
if (host[i] == '\n')
host[i] = 0;
if((buffer = malloc(1024)) == NULL)
{
perror("malloc buffer");
return(0);
}
// Init all the modescreens we need
for (i = 0; sequence[i].which > 1; i++) {
switch (sequence[i].which) {
case 'g':
case 'G': // cpu_graph_screen
cpu_init ();
break;
case 'c':
case 'C': // cpu_screen
cpu_init ();
break;
case 'o':
case 'O': // clock_screen
chrono_init ();
break;
case 'k':
case 'K': // big_clock_screen
chrono_init ();
break;
case 'm':
case 'M': // mem_screen
mem_init ();
break;
case 's':
case 'S': // mem_info_screen
mem_init ();
break;
case 'u':
case 'U': // uptime_screen
chrono_init ();
break;
case 't':
case 'T': // time_screen
chrono_init ();
break;
case 'd':
case 'D': // disk_screen
disk_init ();
break;
case 'x':
case 'X': // xload_screen
load_init ();
break;
case 'b':
case 'B': // battery_screen
batt_init ();
break;
case 'a':
case 'A': // credit_screen
break;
case 'p':
case 'P': // cpu_smp_screen
cpu_smp_init ();
break;
default:
break;
}
if((tmp = malloc(1024)) == NULL)
{
perror("malloc tmp");
return(0);
}
return 0;
machine_init();
return(0);
}
void
mode_close ()
mode_close()
{
batt_close ();
chrono_close ();
cpu_close ();
disk_close ();
load_close ();
mem_close ();
cpu_smp_close ();
machine_close();
if(tmp != NULL)
{
free(tmp);
tmp = NULL;
}
if(buffer != NULL)
{
free(buffer);
buffer = NULL;
}
}
int
update_screen (mode * m, int display)
update_screen(mode *m, int display)
{
static int status = -1;
int old_status = status;
if (m) {
switch (m->which) {
case 'g':
case 'G':
status = cpu_graph_screen (m->timer, display);
break;
case 'c':
case 'C':
status = cpu_screen (m->timer, display);
break;
case 'o':
case 'O':
status = clock_screen (m->timer, display);
break;
case 'k':
case 'K':
status = big_clock_screen (m->timer, display);
break;
case 'm':
case 'M':
status = mem_screen (m->timer, display);
break;
case 's':
case 'S':
status = mem_top_screen (m->timer, display);
break;
case 'u':
case 'U':
status = uptime_screen (m->timer, display);
break;
case 't':
case 'T':
status = time_screen (m->timer, display);
break;
case 'd':
case 'D':
status = disk_screen (m->timer, display);
break;
case 'x':
case 'X':
status = xload_screen (m->timer, display);
break;
case 'b':
case 'B':
status = battery_screen (m->timer, display);
break;
case 'a':
case 'A':
status = credit_screen (m->timer, display);
break;
case 'p':
case 'P':
status = cpu_smp_screen (m->timer, display);
break;
default:
break;
}
/* Debugging tool...
if(display)
{
printf("Displayed Mode %c\n", m->which);
}
*/
}
if (status != old_status) {
if (status == BACKLIGHT_OFF) {
sock_send_string (sock, "backlight off\n");
}
if (status == BACKLIGHT_ON) {
sock_send_string (sock, "backlight on\n");
}
if (status == BLINK_ON) {
sock_send_string (sock, "backlight blink\n");
if(m)
{
switch(m->which)
{
case 'g':
case 'G':
status = cpu_graph_screen(m->timer, display);
break;
case 'c':
case 'C':
status = cpu_screen(m->timer, display);
break;
case 'o':
case 'O':
status = clock_screen(m->timer, display);
break;
case 'k':
case 'K':
status = big_clock_screen(m->timer, display);
break;
case 'm':
case 'M':
status = mem_screen(m->timer, display);
break;
case 's':
case 'S':
status = mem_top_screen(m->timer, display);
break;
case 'u':
case 'U':
status = uptime_screen(m->timer, display);
break;
case 't':
case 'T':
status = time_screen(m->timer, display);
break;
case 'd':
case 'D':
status = disk_screen(m->timer, display);
break;
case 'x':
case 'X':
status = xload_screen(m->timer, display);
break;
case 'b':
case 'B':
status = battery_screen(m->timer, display);
break;
case 'a':
case 'A':
status = credit_screen(m->timer, display);
break;
case 'p':
case 'P':
status = cpu_smp_screen(m->timer, display);
break;
default:
break;
}
}
return status;
}
void
reread (int f, char *errmsg)
{
if (lseek (f, 0L, 0) == 0 && read (f, buffer, sizeof (buffer) - 1) > 0)
return;
perror (errmsg);
exit (1);
}
int
getentry (const char *tag, const char *bufptr)
{
char *tail;
int retval, len = strlen (tag);
while (bufptr) {
if (*bufptr == '\n')
bufptr++;
if (!strncmp (tag, bufptr, len)) {
retval = strtol (bufptr + len, &tail, 10);
if (tail == bufptr + len)
return -1;
else
return retval;
}
bufptr = strchr (bufptr, '\n');
if(status != old_status)
{
if(status == BACKLIGHT_OFF)
sock_send_string(sock, "backlight off\n");
if(status == BACKLIGHT_ON)
sock_send_string(sock, "backlight on\n");
if(status == BLINK_ON)
sock_send_string(sock, "backlight blink\n");
}
return -1;
return(status);
}
///////////////////////////////////////////////////////////////////////////
//////////////////////// Let the Modes Begin! /////////////////////////////
///////////////////////////////////////////////////////////////////////////
// Er, these have been moved.. :)
char tmp[1024];
////////////////////////////////////////////////////////////////////////
// Credit Screen shows who wrote this...
//
int
credit_screen (int rep, int display)
credit_screen(int rep, int display)
{
static int first = 1;
if (first) {
if(first)
{
first = 0;
sock_send_string (sock, "screen_add A\n");
sock_send_string (sock, "screen_set A -name {Credits for LCDproc}\n");
sock_send_string (sock, "widget_add A title title\n");
sprintf (tmp, "widget_set A title {LCDPROC %s}\n", version);
sock_send_string (sock, tmp);
if (lcd_hgt >= 4) {
sock_send_string (sock, "widget_add A one string\n");
sock_send_string (sock, "widget_add A two string\n");
sock_send_string (sock, "widget_add A three string\n");
#ifdef __NetBSD__
sock_send_string (sock, "widget_set A one 1 2 { for Linux & NetBSD}\n");
#else
sock_send_string (sock, "widget_set A one 1 2 { for Linux }\n");
#endif
sock_send_string (sock, "widget_set A two 1 3 { by William Ferrell}\n");
sock_send_string (sock, "widget_set A three 1 4 { and Scott Scriven}\n");
} else {
sock_send_string (sock, "widget_add A text scroller\n");
#ifdef __NetBSD__
sock_send_string (sock, "widget_set A text 1 2 20 2 v 8 { for Linux & NetBSD by William Ferrell and Scott Scriven}\n");
#else
sock_send_string (sock, "widget_set A text 1 2 20 2 v 8 { for Linux by William Ferrell and Scott Scriven}\n");
#endif
sock_send_string(sock, "screen_add A\n");
sock_send_string(sock, "screen_set A -name {Credits for LCDproc}\n");
sock_send_string(sock, "widget_add A title title\n");
sprintf(buffer, "widget_set A title {LCDPROC %s}\n", version);
sock_send_string(sock, buffer);
if(lcd_hgt >= 4)
{
sock_send_string(sock, "widget_add A one string\n");
sock_send_string(sock, "widget_add A two string\n");
sock_send_string(sock, "widget_add A three string\n");
sock_send_string(sock, "widget_set A one 1 2 { for Linux & BSD }\n");
sock_send_string(sock, "widget_set A two 1 3 { by William Ferrell}\n");
sock_send_string(sock, "widget_set A three 1 4 { and Scott Scriven}\n");
}
else
{
sock_send_string(sock, "widget_add A text scroller\n");
sock_send_string(sock, "widget_set A text 1 2 20 2 v 8 { for Linux & *BSD by William Ferrell, Scott Scriven}\n");
}
}
return 0;
} // End credit_screen()
return(0);
} // End credit_screen()
+4 -18
View File
@@ -3,28 +3,14 @@
#include "main.h"
//TODO: Net stats screen...
//TODO: "Who"
//TODO: biff / mail checking, etc...
//TODO: get rid of these global variables!
extern char *tmp;
extern char *buffer;
extern char tmp[];
extern char buffer[];
extern char host[];
// Character to use for padding title bars, etc...
extern int PAD;
// Character for the "..." symbol.
extern int ELLIPSIS;
int mode_init (mode * sequence);
int mode_init ();
void mode_close ();
int update_screen (mode * m, int display);
int credit_screen (int rep, int display);
// These are for the modescreens' use only..
void reread (int f, char *errmsg);
int getentry (const char *tag, const char *bufptr);
#endif