Re-indent machine* files.

This commit is contained in:
mmdolze
2011-11-20 14:08:05 +00:00
parent e45822c37d
commit 27d1bc1087
7 changed files with 861 additions and 863 deletions
+3 -1
View File
@@ -195,7 +195,9 @@ int machine_get_smpload(load_type *result, int *numcpus);
int machine_get_uptime(double *up, double *idle); int machine_get_uptime(double *up, double *idle);
/** /**
* Get network interface status. * Read network interface statistics for a single interface from system and
* store results in the struct IfaceInfo.
*
* \param interface Pointer where to store interface info. * \param interface Pointer where to store interface info.
* \retval FALSE Error, do not trust the contents of the parameter pointers. * \retval FALSE Error, do not trust the contents of the parameter pointers.
* \retval TRUE OK, parameter pointers are filled with sensible data. * \retval TRUE OK, parameter pointers are filled with sensible data.
+158 -150
View File
@@ -2,8 +2,11 @@
* Collects system information on MacOS / Darwin. * Collects system information on MacOS / Darwin.
*/ */
/* Copyright (c) 2003 Thomas Runge (coto@core.de) /*-
* Mach and Darwin specific code is Copyright (c) 2006 Eric Pooch (epooch@tenon.com) * Copyright (c) 2003 Thomas Runge (coto@core.de)
*
* Mach and Darwin specific code is:
* Copyright (c) 2006 Eric Pooch (epooch@tenon.com)
* *
* All rights reserved. * All rights reserved.
* *
@@ -72,17 +75,20 @@ static int swapmode(int *rettotal, int *retfree);
static mach_port_t lcdproc_port; static mach_port_t lcdproc_port;
int machine_init(void) int
machine_init(void)
{ {
/* get the page size with "getpagesize" and calculate pageshift from it */ /*
* get the page size with "getpagesize" and calculate pageshift from
* it
*/
unsigned int pagesize = 0; unsigned int pagesize = 0;
pageshift = 0; pageshift = 0;
lcdproc_port = mach_host_self(); lcdproc_port = mach_host_self();
host_page_size(lcdproc_port, &pagesize); host_page_size(lcdproc_port, &pagesize);
while (pagesize > 1) while (pagesize > 1) {
{
pageshift++; pageshift++;
pagesize >>= 1; pagesize >>= 1;
} }
@@ -90,15 +96,17 @@ int machine_init(void)
/* we only need the amount of log(2)1024 for our conversion */ /* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10; pageshift -= 10;
return(TRUE); return (TRUE);
} }
int machine_close(void) int
machine_close(void)
{ {
return(TRUE); return (TRUE);
} }
int machine_get_battstat(int *acstat, int *battflag, int *percent) int
machine_get_battstat(int *acstat, int *battflag, int *percent)
{ {
CFTypeRef blob = IOPSCopyPowerSourcesInfo(); CFTypeRef blob = IOPSCopyPowerSourcesInfo();
CFArrayRef sources = IOPSCopyPowerSourcesList(blob); CFArrayRef sources = IOPSCopyPowerSourcesList(blob);
@@ -109,31 +117,32 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent)
*acstat = LCDP_AC_ON; *acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT; *battflag = LCDP_BATT_ABSENT;
*percent = 100; *percent = 100;
if (CFArrayGetCount(sources) == 0) return(FALSE); if (CFArrayGetCount(sources) == 0)
return (FALSE);
for (i = 0; i < CFArrayGetCount(sources); i++) for (i = 0; i < CFArrayGetCount(sources); i++) {
{
pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i)); pSource = IOPSGetPowerSourceDescription(blob, CFArrayGetValueAtIndex(sources, i));
if (!pSource) break; if (!pSource)
break;
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey)); psValue = (CFStringRef) CFDictionaryGetValue(pSource, CFSTR(kIOPSNameKey));
if (CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSIsPresentKey), &psValue) && (CFBooleanGetValue(psValue) > 0)) if (CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSIsPresentKey), &psValue) && (CFBooleanGetValue(psValue) > 0)) {
{ psValue = (CFStringRef) CFDictionaryGetValue(pSource, CFSTR(kIOPSPowerSourceStateKey));
psValue = (CFStringRef)CFDictionaryGetValue(pSource, CFSTR(kIOPSPowerSourceStateKey));
if (CFStringCompare(psValue,CFSTR(kIOPSBatteryPowerValue),0)==kCFCompareEqualTo) if (CFStringCompare(psValue, CFSTR(kIOPSBatteryPowerValue), 0) == kCFCompareEqualTo) {
{
/* We are running on a battery power source. */ /* We are running on a battery power source. */
*battflag = LCDP_BATT_UNKNOWN; *battflag = LCDP_BATT_UNKNOWN;
*acstat = LCDP_AC_OFF; *acstat = LCDP_AC_OFF;
} }
else if (CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSIsChargingKey), &psValue)) else if (CFDictionaryGetValueIfPresent(pSource, CFSTR(kIOPSIsChargingKey), &psValue)) {
{ /*
/* We are running on an AC power source, * We are running on an AC power source, but
but we also have a battery power source present. */ * we also have a battery power source
* present.
*/
if (CFBooleanGetValue(psValue) > 0) if (CFBooleanGetValue(psValue) > 0)
*battflag = LCDP_BATT_CHARGING; *battflag = LCDP_BATT_CHARGING;
@@ -141,8 +150,7 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent)
*battflag = LCDP_BATT_UNKNOWN; *battflag = LCDP_BATT_UNKNOWN;
} }
if (*battflag != LCDP_BATT_ABSENT) if (*battflag != LCDP_BATT_ABSENT) {
{
int curCapacity = 0; int curCapacity = 0;
int maxCapacity = 0; int maxCapacity = 0;
@@ -152,12 +160,14 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent)
psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey)); psValue = CFDictionaryGetValue(pSource, CFSTR(kIOPSMaxCapacityKey));
CFNumberGetValue(psValue, kCFNumberSInt32Type, &maxCapacity); CFNumberGetValue(psValue, kCFNumberSInt32Type, &maxCapacity);
*percent = (int)((double)curCapacity/(double)maxCapacity * 100); *percent = (int)((double)curCapacity / (double)maxCapacity * 100);
/* There is a way to check this through the IOKit, /*
but I am not sure what gets returned for kIOPSLowWarnLevelKey and kIOPSDeadWarnLevelKey, * There is a way to check this through the
and this is easier. * IOKit, but I am not sure what gets
*/ * returned for kIOPSLowWarnLevelKey and
* kIOPSDeadWarnLevelKey, and this is easier.
*/
if (*battflag == LCDP_BATT_UNKNOWN) { if (*battflag == LCDP_BATT_UNKNOWN) {
if (*percent > 50) if (*percent > 50)
*battflag = LCDP_BATT_HIGH; *battflag = LCDP_BATT_HIGH;
@@ -167,7 +177,6 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent)
*battflag = LCDP_BATT_CRITICAL; *battflag = LCDP_BATT_CRITICAL;
} }
/* printf ("powerSource %d of %d: percent: %d/%d %d\n", i, CFArrayGetCount(sources), curCapacity, maxCapacity, *percent);*/ /* printf ("powerSource %d of %d: percent: %d/%d %d\n", i, CFArrayGetCount(sources), curCapacity, maxCapacity, *percent);*/
} }
} }
} }
@@ -175,41 +184,38 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent)
CFRelease(blob); CFRelease(blob);
CFRelease(sources); CFRelease(sources);
return(TRUE); return (TRUE);
} }
int machine_get_fs(mounts_type fs[], int *cnt) int
machine_get_fs(mounts_type fs[], int *cnt)
{ {
struct statfs *mntbuf; struct statfs *mntbuf;
struct statfs *pp; struct statfs *pp;
int statcnt, fscnt, i; int statcnt, fscnt, i;
fscnt = getmntinfo(&mntbuf, MNT_WAIT); fscnt = getmntinfo(&mntbuf, MNT_WAIT);
if (fscnt == 0) if (fscnt == 0) {
{
perror("getmntinfo"); perror("getmntinfo");
return(FALSE); return (FALSE);
} }
for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) {
{ if (strcmp(pp->f_fstypename, "procfs")
if ( strcmp(pp->f_fstypename, "procfs") && strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "kernfs") && strcmp(pp->f_fstypename, "linprocfs")
&& strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS #ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs") && strcmp(pp->f_fstypename, "nfs")
#endif #endif
#ifndef STAT_SMBFS #ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs") && strcmp(pp->f_fstypename, "smbfs")
#endif #endif
) ) {
{ snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname); snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename); snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks; fs[statcnt].blocks = pp->f_blocks;
if (fs[statcnt].blocks > 0) if (fs[statcnt].blocks > 0) {
{
fs[statcnt].bsize = pp->f_bsize; fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree; fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files; fs[statcnt].files = pp->f_files;
@@ -221,43 +227,41 @@ int machine_get_fs(mounts_type fs[], int *cnt)
*cnt = statcnt; *cnt = statcnt;
return(TRUE); return (TRUE);
} }
int machine_get_load(load_type *curr_load) int
machine_get_load(load_type * curr_load)
{ {
static load_type last_load = { 0, 0, 0, 0, 0 }; static load_type last_load = {0, 0, 0, 0, 0};
static load_type last_ret_load; static load_type last_ret_load;
load_type load; load_type load;
host_cpu_load_info_data_t load_info; host_cpu_load_info_data_t load_info;
mach_msg_type_number_t info_count; mach_msg_type_number_t info_count;
info_count = HOST_CPU_LOAD_INFO_COUNT; info_count = HOST_CPU_LOAD_INFO_COUNT;
if (host_statistics(lcdproc_port, HOST_CPU_LOAD_INFO, (host_info_t)&load_info, &info_count)) if (host_statistics(lcdproc_port, HOST_CPU_LOAD_INFO, (host_info_t) & load_info, &info_count)) {
{
perror("host_statistics"); perror("host_statistics");
return(FALSE); return (FALSE);
} }
load.user = (unsigned long) (load_info.cpu_ticks[CPU_STATE_USER]); load.user = (unsigned long)(load_info.cpu_ticks[CPU_STATE_USER]);
load.nice = (unsigned long) (load_info.cpu_ticks[CPU_STATE_NICE]); load.nice = (unsigned long)(load_info.cpu_ticks[CPU_STATE_NICE]);
load.system = (unsigned long) (load_info.cpu_ticks[CPU_STATE_SYSTEM]); load.system = (unsigned long)(load_info.cpu_ticks[CPU_STATE_SYSTEM]);
load.idle = (unsigned long) (load_info.cpu_ticks[CPU_STATE_IDLE]); load.idle = (unsigned long)(load_info.cpu_ticks[CPU_STATE_IDLE]);
load.total = load.user + load.nice + load.system + load.idle; load.total = load.user + load.nice + load.system + load.idle;
if (load.total != last_load.total) if (load.total != last_load.total) {
{ curr_load->user = load.user - last_load.user;
curr_load->user = load.user - last_load.user; curr_load->nice = load.nice - last_load.nice;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system; curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle; curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total; curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load; last_ret_load = *curr_load;
last_load = load; last_load = load;
} }
else else {
{
*curr_load = last_ret_load; *curr_load = last_ret_load;
} }
@@ -265,95 +269,91 @@ int machine_get_load(load_type *curr_load)
} }
int machine_get_loadavg(double *load) int
machine_get_loadavg(double *load)
{ {
double loadavg[LOADAVG_NSTATS]; double loadavg[LOADAVG_NSTATS];
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
return(FALSE); return (FALSE);
*load = loadavg[LOADAVG_1MIN]; *load = loadavg[LOADAVG_1MIN];
return(TRUE); return (TRUE);
} }
int machine_get_meminfo(meminfo_type *result) int
machine_get_meminfo(meminfo_type * result)
{ {
int total_pages, free_pages; int total_pages, free_pages;
vm_statistics_data_t vm_info; vm_statistics_data_t vm_info;
mach_msg_type_number_t info_count; mach_msg_type_number_t info_count;
info_count = HOST_VM_INFO_COUNT; info_count = HOST_VM_INFO_COUNT;
if (host_statistics(lcdproc_port, HOST_VM_INFO, (host_info_t)&vm_info, &info_count)) if (host_statistics(lcdproc_port, HOST_VM_INFO, (host_info_t) & vm_info, &info_count)) {
{
perror("host_statistics"); perror("host_statistics");
return(FALSE); return (FALSE);
} }
result[0].total = pagetok(vm_info.active_count + vm_info.inactive_count + result[0].total = pagetok(vm_info.active_count + vm_info.inactive_count +
vm_info.free_count + vm_info.wire_count); vm_info.free_count + vm_info.wire_count);
result[0].free = pagetok(vm_info.free_count); result[0].free = pagetok(vm_info.free_count);
result[0].buffers = 0; result[0].buffers = 0;
result[0].cache = 0; result[0].cache = 0;
result[0].shared = 0; result[0].shared = 0;
/* swap */ /* swap */
result[1].total = 0; result[1].total = 0;
result[1].free = 0; result[1].free = 0;
if (swapmode(&total_pages, &free_pages) != -1) if (swapmode(&total_pages, &free_pages) != -1) {
{
result[1].total = total_pages; result[1].total = total_pages;
result[1].free = free_pages; result[1].free = free_pages;
} }
return(TRUE); return (TRUE);
} }
int machine_get_procs(LinkedList *procs) int
machine_get_procs(LinkedList * procs)
{ {
struct kinfo_proc *kprocs; struct kinfo_proc *kprocs;
procinfo_type *p; procinfo_type *p;
int nproc, i; int nproc, i;
int mib[] = { CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0 }; int mib[] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
size_t size = 0; size_t size = 0;
/* Call sysctl with a NULL buffer as a dry run. */ /* Call sysctl with a NULL buffer as a dry run. */
if ( sysctl(mib, 4, NULL, &size, NULL, 0 ) < 0) if (sysctl(mib, 4, NULL, &size, NULL, 0) < 0) {
{
perror("Failure calling sysctl"); perror("Failure calling sysctl");
return FALSE; return FALSE;
} }
/* Allocate a buffer based on previous results of sysctl. */ /* Allocate a buffer based on previous results of sysctl. */
kprocs = (struct kinfo_proc *)alloca(size); kprocs = (struct kinfo_proc *)alloca(size);
if (kprocs == NULL) if (kprocs == NULL) {
{
perror("mem_alloca"); perror("mem_alloca");
return FALSE; return FALSE;
} }
/* Call sysctl again with the new buffer. */ /* Call sysctl again with the new buffer. */
if ( sysctl(mib, 4, kprocs, &size, NULL, 0 ) < 0) if (sysctl(mib, 4, kprocs, &size, NULL, 0) < 0) {
{
perror("Failure calling sysctl"); perror("Failure calling sysctl");
return FALSE; return FALSE;
} }
nproc = size / sizeof(struct kinfo_proc); nproc = size / sizeof(struct kinfo_proc);
for (i = 0; i < nproc; i++, kprocs++) for (i = 0; i < nproc; i++, kprocs++) {
{
mach_port_t task; mach_port_t task;
unsigned int status = kprocs->kp_proc.p_stat; unsigned int status = kprocs->kp_proc.p_stat;
if (status == SIDL ||status == SZOMB) if (status == SIDL || status == SZOMB)
continue; continue;
p = malloc(sizeof(procinfo_type)); p = malloc(sizeof(procinfo_type));
if (!p) if (!p) {
{
perror("mem_malloc"); perror("mem_malloc");
continue; continue;
} }
@@ -369,30 +369,33 @@ int machine_get_procs(LinkedList *procs)
continue; continue;
/* Get the memory data for each pid from Mach. */ /* Get the memory data for each pid from Mach. */
if (task_for_pid(mach_task_self(), kprocs->kp_proc.p_pid, &task) == KERN_SUCCESS) if (task_for_pid(mach_task_self(), kprocs->kp_proc.p_pid, &task) == KERN_SUCCESS) {
{
task_basic_info_data_t info; task_basic_info_data_t info;
mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT; mach_msg_type_number_t count = TASK_BASIC_INFO_COUNT;
if (task_info(task, TASK_BASIC_INFO, (task_info_t)&info, &count) == KERN_SUCCESS) { if (task_info(task, TASK_BASIC_INFO, (task_info_t) & info, &count) == KERN_SUCCESS) {
p->totl = (unsigned long)(/*info.virtual_size*/ info.resident_size / 1024); p->totl = (unsigned long)( /* info.virtual_size */ info.resident_size / 1024);
} }
} else { }
/* This error pops up very often because of Mac OS X security fixes. else {
It might pop up all of the time on an intel Mac. /*
Basically, we cannot get many tasks unless we are root. * This error pops up very often because of Mac OS X
perror("task_for_pid"); * security fixes. It might pop up all of the time on
printf("process: %s, owner:%d\n", kprocs->kp_proc.p_comm, kprocs->kp_eproc.e_pcred.p_ruid); */ * an intel Mac. Basically, we cannot get many tasks
* unless we are root.
*/
/* perror("task_for_pid"); */
p->totl = 0; p->totl = 0;
} }
} }
kprocs -= i; kprocs -= i;
return(TRUE); return (TRUE);
} }
int machine_get_smpload(load_type *result, int *numcpus) int
machine_get_smpload(load_type * result, int *numcpus)
{ {
int i, num; int i, num;
size_t size; size_t size;
@@ -401,14 +404,14 @@ int machine_get_smpload(load_type *result, int *numcpus)
size = sizeof(int); size = sizeof(int);
if (sysctlbyname("hw.ncpu", &num, &size, NULL, 0) < 0) { if (sysctlbyname("hw.ncpu", &num, &size, NULL, 0) < 0) {
perror("sysctl hw.ncpu"); perror("sysctl hw.ncpu");
return(FALSE); return (FALSE);
} }
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
return(FALSE); return (FALSE);
if (numcpus == NULL) if (numcpus == NULL)
return(FALSE); return (FALSE);
/* restrict #CPUs to max. *numcpus */ /* restrict #CPUs to max. *numcpus */
num = (*numcpus >= num) ? num : *numcpus; num = (*numcpus >= num) ? num : *numcpus;
@@ -419,10 +422,11 @@ int machine_get_smpload(load_type *result, int *numcpus)
result[i] = curr_load; result[i] = curr_load;
} }
return(TRUE); return (TRUE);
} }
int machine_get_uptime(double *up, double *idle) int
machine_get_uptime(double *up, double *idle)
{ {
size_t size; size_t size;
time_t now; time_t now;
@@ -436,21 +440,21 @@ int machine_get_uptime(double *up, double *idle)
time(&now); time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 && if (sysctl(mib, 2, &boottime, &size, NULL, 0) != -1 &&
boottime.tv_sec != 0) boottime.tv_sec != 0)
*up = (double)(now - boottime.tv_sec); *up = (double)(now - boottime.tv_sec);
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
*idle = 100.; *idle = 100.;
else else
*idle = 100.*curr_load.idle/curr_load.total; *idle = 100. * curr_load.idle / curr_load.total;
return(TRUE); return (TRUE);
} }
static int swapmode(int *rettotal, int *retfree) static int
swapmode(int *rettotal, int *retfree)
{ {
#ifdef VM_SWAPUSAGE #ifdef VM_SWAPUSAGE
size_t size; size_t size;
struct xsw_usage xsu; struct xsw_usage xsu;
@@ -462,45 +466,48 @@ static int swapmode(int *rettotal, int *retfree)
*rettotal = 0; *rettotal = 0;
*retfree = 0; *retfree = 0;
if (sysctl(mib, 2, &xsu, &size, NULL, 0) != 0) if (sysctl(mib, 2, &xsu, &size, NULL, 0) != 0) {
{
perror("sysctl"); perror("sysctl");
return(FALSE); return (FALSE);
} }
*rettotal = (xsu.xsu_total/1024); *rettotal = (xsu.xsu_total / 1024);
*retfree = ((xsu.xsu_total-xsu.xsu_used)/1024); *retfree = ((xsu.xsu_total - xsu.xsu_used) / 1024);
return(TRUE); return (TRUE);
#endif #endif
*rettotal = 0; *rettotal = 0;
*retfree = 0; *retfree = 0;
return(FALSE); return (FALSE);
} }
/* Get network statistics */ /* Get network statistics */
int machine_get_iface_stats (IfaceInfo *interface) int
machine_get_iface_stats(IfaceInfo * interface)
{ {
int rows; int rows;
int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT}; int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_SYSTEM, IFMIB_IFCOUNT};
size_t len; size_t len;
struct ifmibdata ifmd; /* ifmibdata contains the network statistics */ struct ifmibdata ifmd; /* ifmibdata contains the network statistics */
len = sizeof(rows); len = sizeof(rows);
/* get number of interfaces */ /* get number of interfaces */
if (sysctl(name, 5, &rows, &len, 0, 0) == 0) { if (sysctl(name, 5, &rows, &len, 0, 0) == 0) {
interface->status = down; /* set status down by default */ interface->status = down; /* set status down by default */
name[3] = IFMIB_IFDATA; name[3] = IFMIB_IFDATA;
name[4] = 0; name[4] = 0;
name[5] = IFDATA_GENERAL; name[5] = IFDATA_GENERAL;
len = sizeof(ifmd); len = sizeof(ifmd);
/* walk through all interfaces in the ifmib table from last to first */ /*
for ( ; rows > 0; rows--) { * walk through all interfaces in the ifmib table from last
name[4] = rows; /* set the interface index */ * to first
*/
for (; rows > 0; rows--) {
name[4] = rows; /* set the interface index */
/* retrive the ifmibdata for the current index */ /* retrive the ifmibdata for the current index */
if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) { if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) {
perror("read sysctl"); perror("read sysctl");
@@ -522,7 +529,7 @@ int machine_get_iface_stats (IfaceInfo *interface)
} }
if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP) { if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP) {
interface->status = up; /* is up */ interface->status = up; /* is up */
interface->last_online = time(NULL); /* save actual time */ interface->last_online = time(NULL); /* save actual time */
} }
@@ -531,11 +538,12 @@ int machine_get_iface_stats (IfaceInfo *interface)
} }
/* if we are here there is no interface with the given name */ /* if we are here there is no interface with the given name */
return (TRUE); return (TRUE);
} else { }
else {
perror("read sysctl IFMIB_IFCOUNT"); perror("read sysctl IFMIB_IFCOUNT");
return (FALSE); return (FALSE);
} }
} /* get_iface_stats() */ }
#endif /* __APPLE__ */ #endif /* __APPLE__ */
+164 -174
View File
@@ -2,7 +2,8 @@
* Collects system information on FreeBSD. * Collects system information on FreeBSD.
*/ */
/* Copyright (c) 2003 Thomas Runge (coto@core.de) /*-
* Copyright (c) 2003 Thomas Runge (coto@core.de)
* *
* All rights reserved. * All rights reserved.
* *
@@ -76,13 +77,16 @@ static kvm_t *kvmd;
static int swapmode(int *retavail, int *retfree); static int swapmode(int *retavail, int *retfree);
int machine_init(void) int
machine_init(void)
{ {
/* get the page size with "getpagesize" and calculate pageshift from it */ /*
* get the page size with "getpagesize" and calculate pageshift from
* it
*/
int pagesize = getpagesize(); int pagesize = getpagesize();
pageshift = 0; pageshift = 0;
while (pagesize > 1) while (pagesize > 1) {
{
pageshift++; pageshift++;
pagesize >>= 1; pagesize >>= 1;
} }
@@ -91,73 +95,70 @@ int machine_init(void)
pageshift -= 10; pageshift -= 10;
/* open kernel virtual memory */ /* open kernel virtual memory */
if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) {
{
perror("kvm_open"); perror("kvm_open");
return(FALSE); return (FALSE);
} }
return(TRUE); return (TRUE);
} }
int machine_close(void) int
machine_close(void)
{ {
kvm_close(kvmd); kvm_close(kvmd);
return(TRUE); return (TRUE);
} }
int machine_get_battstat(int *acstat, int *battflag, int *percent) int
machine_get_battstat(int *acstat, int *battflag, int *percent)
{ {
*acstat = LCDP_AC_ON; *acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT; *battflag = LCDP_BATT_ABSENT;
*percent = 100; *percent = 100;
#ifdef HAVE_MACHINE_APM_BIOS_H #ifdef HAVE_MACHINE_APM_BIOS_H
int apmd; int apmd;
struct apm_info aip; struct apm_info aip;
if ((apmd = open("/dev/apm", O_RDONLY)) == -1) if ((apmd = open("/dev/apm", O_RDONLY)) == -1) {
{
perror("get_battstat_open"); perror("get_battstat_open");
return(TRUE); return (TRUE);
} }
if (ioctl(apmd, APMIO_GETINFO, &aip) == -1) if (ioctl(apmd, APMIO_GETINFO, &aip) == -1) {
{
perror("get_battstat_ioctl"); perror("get_battstat_ioctl");
return(FALSE); return (FALSE);
} }
switch(aip.ai_acline) switch (aip.ai_acline) {
{ case 0:
case 0: *acstat = LCDP_AC_OFF;
*acstat = LCDP_AC_OFF; break;
break; case 1:
case 1: *acstat = LCDP_AC_ON;
*acstat = LCDP_AC_ON; break;
break; default:
default: *acstat = LCDP_AC_UNKNOWN;
*acstat = LCDP_AC_UNKNOWN; break;
break;
} }
switch(aip.ai_batt_stat) switch (aip.ai_batt_stat) {
{ case 0:
case 0: *battflag = LCDP_BATT_HIGH;
*battflag = LCDP_BATT_HIGH; break;
break; case 1:
case 1: *battflag = LCDP_BATT_LOW;
*battflag = LCDP_BATT_LOW; break;
break; case 2:
case 2: *battflag = LCDP_BATT_CRITICAL;
*battflag = LCDP_BATT_CRITICAL; break;
break; case 3:
case 3: *battflag = LCDP_BATT_CHARGING;
*battflag = LCDP_BATT_CHARGING; break;
break; default:
default: *battflag = LCDP_BATT_UNKNOWN;
*battflag = LCDP_BATT_UNKNOWN; break;
break;
} }
*percent = aip.ai_batt_life; *percent = aip.ai_batt_life;
@@ -167,42 +168,39 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent)
close(apmd); close(apmd);
#endif #endif
return(TRUE); return (TRUE);
} }
int machine_get_fs(mounts_type fs[], int *cnt) int
machine_get_fs(mounts_type fs[], int *cnt)
{ {
struct statfs *mntbuf; struct statfs *mntbuf;
struct statfs *pp; struct statfs *pp;
int statcnt, fscnt, i; int statcnt, fscnt, i;
fscnt = getmntinfo(&mntbuf, MNT_WAIT); fscnt = getmntinfo(&mntbuf, MNT_WAIT);
if (fscnt == 0) if (fscnt == 0) {
{
perror("getmntinfo"); perror("getmntinfo");
return(FALSE); return (FALSE);
} }
for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) {
{ if (strcmp(pp->f_fstypename, "procfs")
if ( strcmp(pp->f_fstypename, "procfs") && strcmp(pp->f_fstypename, "devfs")
&& strcmp(pp->f_fstypename, "devfs") && strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "kernfs") && strcmp(pp->f_fstypename, "linprocfs")
&& strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS #ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs") && strcmp(pp->f_fstypename, "nfs")
#endif #endif
#ifndef STAT_SMBFS #ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs") && strcmp(pp->f_fstypename, "smbfs")
#endif #endif
) ) {
{ snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname); snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename); snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks; fs[statcnt].blocks = pp->f_blocks;
if (fs[statcnt].blocks > 0) if (fs[statcnt].blocks > 0) {
{
fs[statcnt].bsize = pp->f_bsize; fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree; fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files; fs[statcnt].files = pp->f_files;
@@ -214,123 +212,118 @@ int machine_get_fs(mounts_type fs[], int *cnt)
*cnt = statcnt; *cnt = statcnt;
return(TRUE); return (TRUE);
} }
int machine_get_load(load_type *curr_load) int
machine_get_load(load_type * curr_load)
{ {
static load_type last_load = { 0, 0, 0, 0, 0 }; static load_type last_load = {0, 0, 0, 0, 0};
static load_type last_ret_load; static load_type last_ret_load;
load_type load; load_type load;
long cp_time[CPUSTATES]; long cp_time[CPUSTATES];
size_t size; size_t size;
size = sizeof(cp_time); size = sizeof(cp_time);
if (sysctlbyname("kern.cp_time", cp_time, &size, NULL, 0) < 0) if (sysctlbyname("kern.cp_time", cp_time, &size, NULL, 0) < 0) {
{
perror("sysctl kern.cp_time failed"); perror("sysctl kern.cp_time failed");
return(FALSE); return (FALSE);
} }
load.user = (unsigned long) (cp_time[CP_USER]); load.user = (unsigned long)(cp_time[CP_USER]);
load.nice = (unsigned long) (cp_time[CP_NICE]); load.nice = (unsigned long)(cp_time[CP_NICE]);
load.system = (unsigned long) (cp_time[CP_SYS] + cp_time[CP_INTR]); load.system = (unsigned long)(cp_time[CP_SYS] + cp_time[CP_INTR]);
load.idle = (unsigned long) (cp_time[CP_IDLE]); load.idle = (unsigned long)(cp_time[CP_IDLE]);
load.total = load.user + load.nice + load.system + load.idle; load.total = load.user + load.nice + load.system + load.idle;
if (load.total != last_load.total) if (load.total != last_load.total) {
{ curr_load->user = load.user - last_load.user;
curr_load->user = load.user - last_load.user; curr_load->nice = load.nice - last_load.nice;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system; curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle; curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total; curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load; last_ret_load = *curr_load;
last_load = load; last_load = load;
} }
else else {
{
*curr_load = last_ret_load; *curr_load = last_ret_load;
} }
return(TRUE); return (TRUE);
} }
int machine_get_loadavg(double *load) int
machine_get_loadavg(double *load)
{ {
double loadavg[LOADAVG_NSTATS]; double loadavg[LOADAVG_NSTATS];
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
return(FALSE); return (FALSE);
*load = loadavg[LOADAVG_1MIN]; *load = loadavg[LOADAVG_1MIN];
return(TRUE); return (TRUE);
} }
int machine_get_meminfo(meminfo_type *result) int
machine_get_meminfo(meminfo_type * result)
{ {
int total_pages, free_pages; int total_pages, free_pages;
size_t size; size_t size;
size = sizeof(int); size = sizeof(int);
if (sysctlbyname("vm.stats.vm.v_page_count", &total_pages, &size, NULL, 0) < 0) if (sysctlbyname("vm.stats.vm.v_page_count", &total_pages, &size, NULL, 0) < 0) {
{
perror("sysctl vm.stats.vm.v_page_count"); perror("sysctl vm.stats.vm.v_page_count");
return(FALSE); return (FALSE);
} }
if (sysctlbyname("vm.stats.vm.v_free_count", &free_pages, &size, NULL, 0) < 0) if (sysctlbyname("vm.stats.vm.v_free_count", &free_pages, &size, NULL, 0) < 0) {
{
perror("sysctl vm.stats.vm.v_free_count"); perror("sysctl vm.stats.vm.v_free_count");
return(FALSE); return (FALSE);
} }
/* memory */ /* memory */
result[0].total = pagetok(total_pages); result[0].total = pagetok(total_pages);
result[0].free = pagetok(free_pages); result[0].free = pagetok(free_pages);
/* unused anyway */ /* unused anyway */
result[0].shared = 0; result[0].shared = 0;
result[0].buffers = 0; result[0].buffers = 0;
result[0].cache = 0; result[0].cache = 0;
/* swap */ /* swap */
result[1].total = 0; result[1].total = 0;
result[1].free = 0; result[1].free = 0;
if (swapmode(&total_pages, &free_pages) != -1) if (swapmode(&total_pages, &free_pages) != -1) {
{
result[1].total = total_pages; result[1].total = total_pages;
result[1].free = free_pages; result[1].free = free_pages;
} }
return(TRUE); return (TRUE);
} }
int machine_get_procs(LinkedList *procs) int
machine_get_procs(LinkedList * procs)
{ {
struct kinfo_proc *kprocs; struct kinfo_proc *kprocs;
int nproc, i; int nproc, i;
procinfo_type *p; procinfo_type *p;
kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc); kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc);
if (kprocs == NULL) if (kprocs == NULL) {
{
perror("kvm_getprocs"); perror("kvm_getprocs");
kvm_close(kvmd); kvm_close(kvmd);
return(FALSE); return (FALSE);
} }
for (i = 0; i < nproc; i++) for (i = 0; i < nproc; i++) {
{
p = malloc(sizeof(procinfo_type)); p = malloc(sizeof(procinfo_type));
if (!p) if (!p) {
{
perror("mem_top_malloc"); perror("mem_top_malloc");
kvm_close(kvmd); kvm_close(kvmd);
return(FALSE); return (FALSE);
} }
#if (__FreeBSD_version > 500000) #if (__FreeBSD_version > 500000)
strncpy(p->name, kprocs->ki_comm, 15); strncpy(p->name, kprocs->ki_comm, 15);
@@ -350,10 +343,11 @@ int machine_get_procs(LinkedList *procs)
kprocs++; kprocs++;
} }
return(TRUE); return (TRUE);
} }
int machine_get_smpload(load_type *result, int *numcpus) int
machine_get_smpload(load_type * result, int *numcpus)
{ {
int i, num; int i, num;
size_t size; size_t size;
@@ -364,12 +358,12 @@ int machine_get_smpload(load_type *result, int *numcpus)
#endif #endif
if (numcpus == NULL) if (numcpus == NULL)
return(FALSE); return (FALSE);
size = sizeof(int); size = sizeof(int);
if (sysctlbyname("hw.ncpu", &num, &size, NULL, 0) < 0) { if (sysctlbyname("hw.ncpu", &num, &size, NULL, 0) < 0) {
perror("sysctl hw.ncpu"); perror("sysctl hw.ncpu");
return(FALSE); return (FALSE);
} }
/* restrict #CPUs to max. *numcpus */ /* restrict #CPUs to max. *numcpus */
@@ -378,37 +372,37 @@ int machine_get_smpload(load_type *result, int *numcpus)
#ifndef HAVE_SYS_PCPU_H #ifndef HAVE_SYS_PCPU_H
if (machine_get_load(&load) == FALSE) if (machine_get_load(&load) == FALSE)
return(FALSE); return (FALSE);
#endif #endif
for (i = 0; i < num; i++) { for (i = 0; i < num; i++) {
#ifdef HAVE_SYS_PCPU_H #ifdef HAVE_SYS_PCPU_H
pcpudata = kvm_getpcpu(kvmd, i); pcpudata = kvm_getpcpu(kvmd, i);
if (pcpudata == NULL || pcpudata == (void *) -1) if (pcpudata == NULL || pcpudata == (void *)-1)
return(FALSE); return (FALSE);
/* extract the data for single CPU */ /* extract the data for single CPU */
load.user = (unsigned long) (pcpudata->pc_cp_time[CP_USER]); load.user = (unsigned long)(pcpudata->pc_cp_time[CP_USER]);
load.nice = (unsigned long) (pcpudata->pc_cp_time[CP_NICE]); load.nice = (unsigned long)(pcpudata->pc_cp_time[CP_NICE]);
load.system = (unsigned long) (pcpudata->pc_cp_time[CP_SYS] + load.system = (unsigned long)(pcpudata->pc_cp_time[CP_SYS] +
pcpudata->pc_cp_time[CP_INTR]); pcpudata->pc_cp_time[CP_INTR]);
load.idle = (unsigned long) (pcpudata->pc_cp_time[CP_IDLE]); load.idle = (unsigned long)(pcpudata->pc_cp_time[CP_IDLE]);
load.total = load.user + load.nice + load.system + load.idle; load.total = load.user + load.nice + load.system + load.idle;
/* store difference in result */ /* store difference in result */
result[i].user = load.user - last_load[i].user; result[i].user = load.user - last_load[i].user;
result[i].nice = load.nice - last_load[i].nice; result[i].nice = load.nice - last_load[i].nice;
result[i].system = load.system - last_load[i].system; result[i].system = load.system - last_load[i].system;
result[i].idle = load.idle - last_load[i].idle; result[i].idle = load.idle - last_load[i].idle;
result[i].total = load.total - last_load[i].total; result[i].total = load.total - last_load[i].total;
/* store current value for next round */ /* store current value for next round */
last_load[i].user = load.user; last_load[i].user = load.user;
last_load[i].nice = load.nice; last_load[i].nice = load.nice;
last_load[i].system = load.system; last_load[i].system = load.system;
last_load[i].idle = load.idle; last_load[i].idle = load.idle;
last_load[i].total = load.total; last_load[i].total = load.total;
/* free pcpu buffer */ /* free pcpu buffer */
free(pcpudata); free(pcpudata);
@@ -417,10 +411,11 @@ int machine_get_smpload(load_type *result, int *numcpus)
#endif #endif
} }
return(TRUE); return (TRUE);
} }
int machine_get_uptime(double *up, double *idle) int
machine_get_uptime(double *up, double *idle)
{ {
size_t size; size_t size;
time_t now; time_t now;
@@ -429,10 +424,9 @@ int machine_get_uptime(double *up, double *idle)
size = sizeof(boottime); size = sizeof(boottime);
time(&now); time(&now);
if (sysctlbyname("kern.boottime", &boottime, &size, NULL, 0) < 0) if (sysctlbyname("kern.boottime", &boottime, &size, NULL, 0) < 0) {
{
perror("sysctl kern.cp_time failed"); perror("sysctl kern.cp_time failed");
return(FALSE); return (FALSE);
} }
*up = (double)(now - boottime.tv_sec); *up = (double)(now - boottime.tv_sec);
@@ -440,12 +434,13 @@ int machine_get_uptime(double *up, double *idle)
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
*idle = 100.; *idle = 100.;
else else
*idle = 100.*curr_load.idle/curr_load.total; *idle = 100. * curr_load.idle / curr_load.total;
return(TRUE); return (TRUE);
} }
static int swapmode(int *retavail, int *retfree) static int
swapmode(int *retavail, int *retfree)
{ {
int n; int n;
struct kvm_swap swapary[1]; struct kvm_swap swapary[1];
@@ -454,50 +449,45 @@ static int swapmode(int *retavail, int *retfree)
*retavail = 0; *retavail = 0;
*retfree = 0; *retfree = 0;
if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) {
{
perror("read kvm"); perror("read kvm");
return(-1); return (-1);
} }
n = kvm_getswapinfo(kvmd, swapary, 1, 0); n = kvm_getswapinfo(kvmd, swapary, 1, 0);
if (n < 0 || swapary[0].ksw_total == 0) if (n < 0 || swapary[0].ksw_total == 0) {
{
/* strange */ /* strange */
} }
else else {
{
*retavail = pagetok(swapary[0].ksw_total); *retavail = pagetok(swapary[0].ksw_total);
*retfree = pagetok(swapary[0].ksw_total - swapary[0].ksw_used); *retfree = pagetok(swapary[0].ksw_total - swapary[0].ksw_used);
} }
kvm_close(kvmd); kvm_close(kvmd);
return(n); return (n);
} }
/************************************************************************* int
* Read interface statistics from system and store in the struct machine_get_iface_stats(IfaceInfo * interface)
* passed as a pointer. If there are no errors, it returns 1. If errors,
* returns 0.
*************************************************************************
*/
int machine_get_iface_stats (IfaceInfo *interface)
{ {
int rows; int rows;
int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, 0, IFDATA_GENERAL}; int name[6] = {CTL_NET, PF_LINK, NETLINK_GENERIC, IFMIB_IFDATA, 0, IFDATA_GENERAL};
size_t len; size_t len;
struct ifmibdata ifmd; /* ifmibdata contains the network statistics */ struct ifmibdata ifmd; /* ifmibdata contains the network statistics */
len = sizeof(rows); len = sizeof(rows);
/* get number of interfaces */ /* get number of interfaces */
if (sysctlbyname("net.link.generic.system.ifcount", &rows, &len, NULL, 0) == 0) { if (sysctlbyname("net.link.generic.system.ifcount", &rows, &len, NULL, 0) == 0) {
interface->status = down; /* set status down by default */ interface->status = down; /* set status down by default */
len = sizeof(ifmd); len = sizeof(ifmd);
/* walk through all interfaces in the ifmib table from last to first */ /*
for ( ; rows > 0; rows--) { * walk through all interfaces in the ifmib table from last
name[4] = rows; /* set the interface index */ * to first
*/
for (; rows > 0; rows--) {
name[4] = rows; /* set the interface index */
/* retrive the ifmibdata for the current index */ /* retrive the ifmibdata for the current index */
if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) { if (sysctl(name, 6, &ifmd, &len, NULL, 0) == -1) {
perror("read sysctl"); perror("read sysctl");
@@ -519,7 +509,7 @@ int machine_get_iface_stats (IfaceInfo *interface)
} }
if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP) { if ((ifmd.ifmd_flags & IFF_UP) == IFF_UP) {
interface->status = up; /* is up */ interface->status = up; /* is up */
interface->last_online = time(NULL); /* save actual time */ interface->last_online = time(NULL); /* save actual time */
} }
@@ -528,11 +518,11 @@ int machine_get_iface_stats (IfaceInfo *interface)
} }
/* if we are here there is no interface with the given name */ /* if we are here there is no interface with the given name */
return (TRUE); return (TRUE);
} else { }
else {
perror("read sysctlbyname"); perror("read sysctlbyname");
return (FALSE); return (FALSE);
} }
} /* get_iface_stats() */ }
#endif /* __FreeBSD__ */
#endif /* __FreeBSD__ */
+168 -151
View File
@@ -70,24 +70,25 @@ static int loadavg_fd;
static int meminfo_fd; static int meminfo_fd;
static int uptime_fd; static int uptime_fd;
static char procbuf[1024]; //TODO ugly hack! static char procbuf[1024]; /* TODO ugly hack! */
static FILE *mtab_fd; static FILE *mtab_fd;
int machine_init(void) int
machine_init(void)
{ {
uptime_fd = -1; uptime_fd = -1;
batt_fd = -1; batt_fd = -1;
load_fd = -1; load_fd = -1;
loadavg_fd = -1; loadavg_fd = -1;
meminfo_fd = -1; meminfo_fd = -1;
if (uptime_fd < 0) { if (uptime_fd < 0) {
uptime_fd = open("/proc/uptime", O_RDONLY); uptime_fd = open("/proc/uptime", O_RDONLY);
if (uptime_fd < 0) { if (uptime_fd < 0) {
perror("open /proc/uptime"); perror("open /proc/uptime");
return(FALSE); return (FALSE);
} }
} }
@@ -95,7 +96,7 @@ int machine_init(void)
load_fd = open("/proc/stat", O_RDONLY); load_fd = open("/proc/stat", O_RDONLY);
if (load_fd < 0) { if (load_fd < 0) {
perror("open /proc/stat"); perror("open /proc/stat");
return(FALSE); return (FALSE);
} }
} }
@@ -104,7 +105,7 @@ int machine_init(void)
loadavg_fd = open("/proc/loadavg", O_RDONLY); loadavg_fd = open("/proc/loadavg", O_RDONLY);
if (loadavg_fd < 0) { if (loadavg_fd < 0) {
perror("open /proc/loadavg"); perror("open /proc/loadavg");
return(FALSE); return (FALSE);
} }
} }
#endif #endif
@@ -113,23 +114,23 @@ int machine_init(void)
meminfo_fd = open("/proc/meminfo", O_RDONLY); meminfo_fd = open("/proc/meminfo", O_RDONLY);
if (meminfo_fd < 0) { if (meminfo_fd < 0) {
perror("open /proc/meminfo"); perror("open /proc/meminfo");
return(FALSE); return (FALSE);
} }
} }
if (batt_fd < 0) { if (batt_fd < 0) {
batt_fd = open("/proc/apm", O_RDONLY); batt_fd = open("/proc/apm", O_RDONLY);
if (batt_fd < 0) { if (batt_fd < 0) {
//perror("open /proc/apm");
/* allow opening /proc/apm to fail */ /* allow opening /proc/apm to fail */
batt_fd = -1; batt_fd = -1;
} }
} }
return(TRUE); return (TRUE);
} }
int machine_close(void) int
machine_close(void)
{ {
if (batt_fd >= 0) if (batt_fd >= 0)
close(batt_fd); close(batt_fd);
@@ -153,11 +154,11 @@ int machine_close(void)
close(uptime_fd); close(uptime_fd);
uptime_fd = -1; uptime_fd = -1;
return(TRUE); return (TRUE);
} }
static void static void
reread (int f, char *errmsg) reread(int f, char *errmsg)
{ {
if (lseek(f, 0L, 0) == 0 && read(f, procbuf, sizeof(procbuf) - 1) > 0) if (lseek(f, 0L, 0) == 0 && read(f, procbuf, sizeof(procbuf) - 1) > 0)
return; return;
@@ -166,7 +167,7 @@ reread (int f, char *errmsg)
} }
static int static int
getentry (const char *tag, const char *bufptr, long *value) getentry(const char *tag, const char *bufptr, long *value)
{ {
char *tail; char *tail;
int len = strlen(tag); int len = strlen(tag);
@@ -190,27 +191,28 @@ getentry (const char *tag, const char *bufptr, long *value)
return FALSE; return FALSE;
} }
int machine_get_battstat(int *acstat, int *battflag, int *percent) int
machine_get_battstat(int *acstat, int *battflag, int *percent)
{ {
char str[64]; char str[64];
int battstat; int battstat;
/* no battery status available: fake one ;-) */ /* no battery status available: fake one ;-) */
if (batt_fd < 0) { if (batt_fd < 0) {
*acstat = LCDP_AC_ON; *acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT; *battflag = LCDP_BATT_ABSENT;
*percent = 100; *percent = 100;
return(TRUE); return (TRUE);
} }
if (lseek(batt_fd, 0, 0) != 0) if (lseek(batt_fd, 0, 0) != 0)
return(FALSE); return (FALSE);
if (read(batt_fd, str, sizeof(str) - 1) < 0) if (read(batt_fd, str, sizeof(str) - 1) < 0)
return(FALSE); return (FALSE);
if (3 > sscanf(str + 13, "0x%x 0x%x 0x%x %d", acstat, &battstat, battflag, percent)) if (3 > sscanf(str + 13, "0x%x 0x%x 0x%x %d", acstat, &battstat, battflag, percent))
return(FALSE); return (FALSE);
if (*battflag == 0xff) if (*battflag == 0xff)
*battflag = LCDP_BATT_UNKNOWN; *battflag = LCDP_BATT_UNKNOWN;
@@ -227,25 +229,26 @@ int machine_get_battstat(int *acstat, int *battflag, int *percent)
*battflag = LCDP_BATT_ABSENT; *battflag = LCDP_BATT_ABSENT;
} }
switch(*acstat) { switch (*acstat) {
case 0: case 0:
*acstat = LCDP_AC_OFF; *acstat = LCDP_AC_OFF;
break; break;
case 1: case 1:
*acstat = LCDP_AC_ON; *acstat = LCDP_AC_ON;
break; break;
case 2: case 2:
*acstat = LCDP_AC_BACKUP; *acstat = LCDP_AC_BACKUP;
break; break;
default: default:
*acstat = LCDP_AC_UNKNOWN; *acstat = LCDP_AC_UNKNOWN;
break; break;
} }
return(TRUE); return (TRUE);
} }
int machine_get_fs(mounts_type fs[], int *cnt) int
machine_get_fs(mounts_type fs[], int *cnt)
{ {
#ifdef STAT_STATVFS #ifdef STAT_STATVFS
struct statvfs fsinfo; struct statvfs fsinfo;
@@ -258,37 +261,36 @@ int machine_get_fs(mounts_type fs[], int *cnt)
#ifdef MTAB_FILE #ifdef MTAB_FILE
mtab_fd = fopen(MTAB_FILE, "r"); mtab_fd = fopen(MTAB_FILE, "r");
#else #else
# error "Can't find your mounted filesystem table file." #error "Can't find your mounted filesystem table file."
#endif #endif
// Get rid of old, unmounted filesystems... /* Get rid of old, unmounted filesystems... */
memset(fs, 0, sizeof(mounts_type) * 256); memset(fs, 0, sizeof(mounts_type) * 256);
while (x < 256) { while (x < 256) {
if (fgets(line, 256, mtab_fd) == NULL) { if (fgets(line, 256, mtab_fd) == NULL) {
fclose(mtab_fd); fclose(mtab_fd);
*cnt = x; *cnt = x;
return(FALSE); return (FALSE);
} }
sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type); sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type);
if (strcmp(fs[x].type, "proc") if (strcmp(fs[x].type, "proc")
&& strcmp(fs[x].type, "tmpfs") && strcmp(fs[x].type, "tmpfs")
#ifndef STAT_NFS #ifndef STAT_NFS
&& strcmp(fs[x].type, "nfs") && strcmp(fs[x].type, "nfs")
#endif #endif
#ifndef STAT_SMBFS #ifndef STAT_SMBFS
&& strcmp(fs[x].type, "smbfs") && strcmp(fs[x].type, "smbfs")
#endif #endif
) ) {
{
#ifdef STAT_STATVFS #ifdef STAT_STATVFS
y = statvfs(fs[x].mpoint, &fsinfo); y = statvfs(fs[x].mpoint, &fsinfo);
#elif STAT_STATFS2_BSIZE #elif STAT_STATFS2_BSIZE
y = statfs(fs[x].mpoint, &fsinfo); y = statfs(fs[x].mpoint, &fsinfo);
#elif STAT_STATFS4 #elif STAT_STATFS4
y = statfs(fs[x].mpoint, &fsinfo, sizeof (fsinfo), 0); y = statfs(fs[x].mpoint, &fsinfo, sizeof(fsinfo), 0);
#else #else
#error "statfs for this system not yet supported" #error "statfs for this system not yet supported"
#endif #endif
@@ -306,12 +308,13 @@ int machine_get_fs(mounts_type fs[], int *cnt)
fclose(mtab_fd); fclose(mtab_fd);
*cnt = x; *cnt = x;
return(TRUE); return (TRUE);
} }
int machine_get_load(load_type *curr_load) int
machine_get_load(load_type * curr_load)
{ {
static load_type last_load = { 0, 0, 0, 0, 0 }; static load_type last_load = {0, 0, 0, 0, 0};
load_type load; load_type load;
int ret; int ret;
unsigned long load_iowait, load_irq, load_softirq; unsigned long load_iowait, load_irq, load_softirq;
@@ -319,8 +322,8 @@ int machine_get_load(load_type *curr_load)
reread(load_fd, "get_load"); reread(load_fd, "get_load");
ret = sscanf(procbuf, "cpu %lu %lu %lu %lu %lu %lu %lu", ret = sscanf(procbuf, "cpu %lu %lu %lu %lu %lu %lu %lu",
&load.user, &load.nice, &load.system, &load.idle, &load.user, &load.nice, &load.system, &load.idle,
&load_iowait, &load_irq, &load_softirq); &load_iowait, &load_irq, &load_softirq);
if (ret >= 5) if (ret >= 5)
load.idle += load_iowait; load.idle += load_iowait;
@@ -331,79 +334,83 @@ int machine_get_load(load_type *curr_load)
load.total = 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->user = load.user - last_load.user;
curr_load->nice = load.nice - last_load.nice; curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system; curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle; curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total; curr_load->total = load.total - last_load.total;
// struct assignment is legal in C89 /* struct assignment is legal in C89 */
last_load = load; last_load = load;
return(TRUE); return (TRUE);
} }
int machine_get_loadavg(double *load) int
machine_get_loadavg(double *load)
{ {
#ifdef USE_GETLOADAVG #ifdef USE_GETLOADAVG
double loadavg[LOADAVG_NSTATS]; double loadavg[LOADAVG_NSTATS];
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) { if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) {
perror("getloadavg"); /* ToDo: correct error reporting */ perror("getloadavg"); /* ToDo: correct error reporting */
return(FALSE); return (FALSE);
} }
*load = loadavg[LOADAVG_1MIN]; *load = loadavg[LOADAVG_1MIN];
#else #else
reread(loadavg_fd, "get_loadavg"); reread(loadavg_fd, "get_loadavg");
sscanf(procbuf, "%lf", load); sscanf(procbuf, "%lf", load);
#endif #endif
return(TRUE); return (TRUE);
} }
int machine_get_meminfo(meminfo_type *result) int
machine_get_meminfo(meminfo_type * result)
{ {
long tmp; long tmp;
reread(meminfo_fd, "get_meminfo"); reread(meminfo_fd, "get_meminfo");
result[0].total = (getentry("MemTotal:", procbuf, &tmp) == TRUE) ? tmp : 0L; result[0].total = (getentry("MemTotal:", procbuf, &tmp) == TRUE) ? tmp : 0L;
result[0].free = (getentry("MemFree:", procbuf, &tmp) == TRUE) ? tmp : 0L; result[0].free = (getentry("MemFree:", procbuf, &tmp) == TRUE) ? tmp : 0L;
result[0].shared = (getentry("MemShared:", procbuf, &tmp) == TRUE) ? tmp : 0L; result[0].shared = (getentry("MemShared:", procbuf, &tmp) == TRUE) ? tmp : 0L;
result[0].buffers = (getentry("Buffers:", procbuf, &tmp) == TRUE) ? tmp : 0L; result[0].buffers = (getentry("Buffers:", procbuf, &tmp) == TRUE) ? tmp : 0L;
result[0].cache = (getentry("Cached:", procbuf, &tmp) == TRUE) ? tmp : 0L; result[0].cache = (getentry("Cached:", procbuf, &tmp) == TRUE) ? tmp : 0L;
result[1].total = (getentry("SwapTotal:", procbuf, &tmp) == TRUE) ? tmp : 0L; result[1].total = (getentry("SwapTotal:", procbuf, &tmp) == TRUE) ? tmp : 0L;
result[1].free = (getentry("SwapFree:", procbuf, &tmp) == TRUE) ? tmp : 0L; result[1].free = (getentry("SwapFree:", procbuf, &tmp) == TRUE) ? tmp : 0L;
return(TRUE); return (TRUE);
} }
int machine_get_procs(LinkedList *procs) int
machine_get_procs(LinkedList * procs)
{ {
// Much of this code was ripped from "gmemusage" /* Much of this code was ripped from "gmemusage" */
DIR *proc; DIR *proc;
FILE *StatusFile; FILE *StatusFile;
struct dirent *procdir; struct dirent *procdir;
char procName[16]; char procName[16];
int procSize, procRSS, procData, procStk, procExe; int procSize, procRSS, procData, procStk, procExe;
const char const char
*NameLine = "Name:", *NameLine = "Name:",
*VmSizeLine = "VmSize:", *VmSizeLine = "VmSize:",
*VmRSSLine = "VmRSS", *VmRSSLine = "VmRSS",
*VmDataLine = "VmData", *VmDataLine = "VmData",
*VmStkLine = "VmStk", *VmStkLine = "VmStk",
*VmExeLine = "VmExe"; *VmExeLine = "VmExe";
const int const int
NameLineLen = strlen(NameLine), NameLineLen = strlen(NameLine),
VmSizeLineLen = strlen(VmSizeLine), VmSizeLineLen = strlen(VmSizeLine),
VmDataLineLen = strlen(VmDataLine), VmDataLineLen = strlen(VmDataLine),
VmStkLineLen = strlen(VmStkLine), VmStkLineLen = strlen(VmStkLine),
VmExeLineLen = strlen(VmExeLine), VmExeLineLen = strlen(VmExeLine),
VmRSSLineLen = strlen(VmRSSLine); VmRSSLineLen = strlen(VmRSSLine);
int threshold = 400, unique; int threshold = 400, unique;
if ((proc = opendir("/proc")) == NULL) { if ((proc = opendir("/proc")) == NULL) {
perror("mem_top_screen: unable to open /proc"); /* ToDo: correct error reporting */ /* ToDo: correct error reporting */
return(FALSE); perror("mem_top_screen: unable to open /proc");
return (FALSE);
} }
while ((procdir = readdir(proc))) { while ((procdir = readdir(proc))) {
@@ -415,8 +422,10 @@ int machine_get_procs(LinkedList *procs)
sprintf(buf, "/proc/%s/status", procdir->d_name); sprintf(buf, "/proc/%s/status", procdir->d_name);
if ((StatusFile = fopen(buf, "r")) == NULL) { if ((StatusFile = fopen(buf, "r")) == NULL) {
// Not a serious error; process has finished before we could /*
// examine it: * Not a serious error; process has finished before
* we could examine it:
*/
continue; continue;
} }
@@ -425,19 +434,24 @@ int machine_get_procs(LinkedList *procs)
if (!strncmp(buf, NameLine, NameLineLen)) { if (!strncmp(buf, NameLine, NameLineLen)) {
/* Name: procName */ /* Name: procName */
sscanf(buf, "%*s %s", procName); sscanf(buf, "%*s %s", procName);
} else if (!strncmp(buf, VmSizeLine, VmSizeLineLen)) { }
else if (!strncmp(buf, VmSizeLine, VmSizeLineLen)) {
/* VmSize: procSize kB */ /* VmSize: procSize kB */
sscanf(buf, "%*s %d", &procSize); sscanf(buf, "%*s %d", &procSize);
} else if (!strncmp (buf, VmRSSLine, VmRSSLineLen)) { }
else if (!strncmp(buf, VmRSSLine, VmRSSLineLen)) {
/* VmRSS: procRSS kB */ /* VmRSS: procRSS kB */
sscanf(buf, "%*s %d", &procRSS); sscanf(buf, "%*s %d", &procRSS);
} else if (!strncmp(buf, VmDataLine, VmDataLineLen)) { }
else if (!strncmp(buf, VmDataLine, VmDataLineLen)) {
/* VmData: procData kB */ /* VmData: procData kB */
sscanf(buf, "%*s %d", &procData); sscanf(buf, "%*s %d", &procData);
} else if (!strncmp(buf, VmStkLine, VmStkLineLen)) { }
else if (!strncmp(buf, VmStkLine, VmStkLineLen)) {
/* VmStk: procStk kB */ /* VmStk: procStk kB */
sscanf(buf, "%*s %d", &procStk); sscanf(buf, "%*s %d", &procStk);
} else if (!strncmp(buf, VmExeLine, VmExeLineLen)) { }
else if (!strncmp(buf, VmExeLine, VmExeLineLen)) {
/* VmExe: procExe kB */ /* VmExe: procExe kB */
sscanf(buf, "%*s %d", &procExe); sscanf(buf, "%*s %d", &procExe);
} }
@@ -445,7 +459,7 @@ int machine_get_procs(LinkedList *procs)
fclose(StatusFile); fclose(StatusFile);
if (procSize > threshold) { if (procSize > threshold) {
// Figure out if it's sharing any memory... /* Figure out if it's sharing any memory... */
unique = 1; unique = 1;
LL_Rewind(procs); LL_Rewind(procs);
do { do {
@@ -458,7 +472,7 @@ int machine_get_procs(LinkedList *procs)
} }
} while (LL_Next(procs) == 0); } while (LL_Next(procs) == 0);
// If this is the first one by this name... /* If this is the first one by this name... */
if (unique) { if (unique) {
procinfo_type *p = malloc(sizeof(procinfo_type)); procinfo_type *p = malloc(sizeof(procinfo_type));
if (p == NULL) { if (p == NULL) {
@@ -468,17 +482,18 @@ int machine_get_procs(LinkedList *procs)
strcpy(p->name, procName); strcpy(p->name, procName);
p->totl = procData + procStk + procExe; p->totl = procData + procStk + procExe;
p->number = 1; p->number = 1;
// TODO: Check for errors here? /* TODO: Check for errors here? */
LL_Push(procs, (void *) p); LL_Push(procs, (void *)p);
} }
} }
} }
closedir(proc); closedir(proc);
return(TRUE); return (TRUE);
} }
int machine_get_smpload(load_type *result, int *numcpus) int
machine_get_smpload(load_type * result, int *numcpus)
{ {
char *token; char *token;
static load_type last_load[MAX_CPUS]; static load_type last_load[MAX_CPUS];
@@ -489,13 +504,13 @@ int machine_get_smpload(load_type *result, int *numcpus)
reread(load_fd, "get_load"); reread(load_fd, "get_load");
// Look for lines starting with "cpu0", "cpu1", etc. /* Look for lines starting with "cpu0", "cpu1", etc. */
token = strtok(procbuf, "\n"); token = strtok(procbuf, "\n");
while (token != NULL) { while (token != NULL) {
if ((strlen(token) > 3) && (!strncmp(token, "cpu", 3)) && isdigit(token[3])) { if ((strlen(token) > 3) && (!strncmp(token, "cpu", 3)) && isdigit(token[3])) {
ret = sscanf(token, "cpu%*d %lu %lu %lu %lu %lu %lu %lu", ret = sscanf(token, "cpu%*d %lu %lu %lu %lu %lu %lu %lu",
&curr_load[ncpu].user, &curr_load[ncpu].nice, &curr_load[ncpu].system, &curr_load[ncpu].user, &curr_load[ncpu].nice, &curr_load[ncpu].system,
&curr_load[ncpu].idle, &load_iowait, &load_irq, &load_softirq); &curr_load[ncpu].idle, &load_iowait, &load_irq, &load_softirq);
if (ret >= 5) if (ret >= 5)
curr_load[ncpu].idle += load_iowait; curr_load[ncpu].idle += load_iowait;
@@ -505,18 +520,18 @@ int machine_get_smpload(load_type *result, int *numcpus)
curr_load[ncpu].system += load_softirq; curr_load[ncpu].system += load_softirq;
curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice + curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice +
curr_load[ncpu].system + curr_load[ncpu].idle; curr_load[ncpu].system + curr_load[ncpu].idle;
result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total; result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total;
result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user; result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user;
result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice; result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice;
result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system; result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system;
result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle; result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle;
// struct assignment is legal in C89 /* struct assignment is legal in C89 */
last_load[ncpu] = curr_load[ncpu]; last_load[ncpu] = curr_load[ncpu];
// restrict # CPUs to min(*numcpus, MAX_CPUS) /* restrict # CPUs to min(*numcpus, MAX_CPUS) */
ncpu++; ncpu++;
if ((ncpu >= *numcpus) || (ncpu >= MAX_CPUS)) if ((ncpu >= *numcpus) || (ncpu >= MAX_CPUS))
break; break;
@@ -525,41 +540,38 @@ int machine_get_smpload(load_type *result, int *numcpus)
} }
*numcpus = ncpu; *numcpus = ncpu;
return(TRUE); return (TRUE);
} }
int machine_get_uptime(double *up, double *idle) int
machine_get_uptime(double *up, double *idle)
{ {
double local_up, local_idle; double local_up, local_idle;
reread(uptime_fd, "get_uptime"); reread(uptime_fd, "get_uptime");
sscanf(procbuf, "%lf %lf", &local_up, &local_idle); sscanf(procbuf, "%lf %lf", &local_up, &local_idle);
if (up != NULL) if (up != NULL)
*up = local_up; *up = local_up;
if (idle != NULL) if (idle != NULL)
*idle = (local_up != 0) *idle = (local_up != 0)
? 100 * local_idle / local_up ? 100 * local_idle / local_up
: 100; : 100;
return(TRUE); return (TRUE);
} }
/************************************************************************* int
* Read interface statistics from system and store in the struct machine_get_iface_stats(IfaceInfo * interface)
* passed as a pointer. If there are no errors, it returns 1. If errors,
* returns 0.
*************************************************************************
*/
int machine_get_iface_stats (IfaceInfo *interface)
{ {
FILE *file; /* file handler */ FILE *file; /* file handler */
char buffer[1024]; /* buffer to work with the file */ char buffer[1024]; /* buffer to work with the file */
static int first_time = 1; /* is it first time we call this function? */ static int first_time = 1; /* is it first time we call this
char *ch_pointer = NULL; /* pointer to where interface values are in file */ * function? */
char *ch_pointer = NULL;/* pointer to where interface values are in
* file */
/* Open the file in read-only mode and parse */ /* Open the file in read-only mode and parse */
if ((file = fopen("/proc/net/dev", "r")) != NULL) { if ((file = fopen("/proc/net/dev", "r")) != NULL) {
/* Skip first 2 header lines of file */ /* Skip first 2 header lines of file */
fgets(buffer, sizeof(buffer), file); fgets(buffer, sizeof(buffer), file);
@@ -572,43 +584,48 @@ int machine_get_iface_stats (IfaceInfo *interface)
while ((fgets(buffer, sizeof(buffer), file) != NULL)) { while ((fgets(buffer, sizeof(buffer), file) != NULL)) {
if (strstr(buffer, interface->name)) { if (strstr(buffer, interface->name)) {
/* interface exists */ /* interface exists */
interface->status = up; /* is up */ interface->status = up; /* is up */
interface->last_online = time(NULL); /* save actual time */ interface->last_online = time(NULL); /* save actual time */
/* search ':' and skip over it */ /* search ':' and skip over it */
ch_pointer = strchr(buffer, ':'); ch_pointer = strchr(buffer, ':');
ch_pointer++; ch_pointer++;
/* Now ch_pointer points to values of iface_name */ /*
* Now ch_pointer points to values of
* iface_name
*/
/* Scan values from here */ /* Scan values from here */
sscanf(ch_pointer, "%lf %lf %*s %*s %*s %*s %*s %*s %lf %lf", sscanf(ch_pointer, "%lf %lf %*s %*s %*s %*s %*s %*s %lf %lf",
&interface->rc_byte, &interface->rc_byte,
&interface->rc_pkt, &interface->rc_pkt,
&interface->tr_byte, &interface->tr_byte,
&interface->tr_pkt); &interface->tr_pkt);
/* if is the first time we call this function, /*
* old values are the same as new so we don't * if is the first time we call this
* get big speeds when calculating * function, old values are the same as new
* so we don't get big speeds when
* calculating
*/ */
if (first_time) { if (first_time) {
interface->rc_byte_old = interface->rc_byte; interface->rc_byte_old = interface->rc_byte;
interface->tr_byte_old = interface->tr_byte; interface->tr_byte_old = interface->tr_byte;
interface->rc_pkt_old = interface->rc_pkt; interface->rc_pkt_old = interface->rc_pkt;
interface->tr_pkt_old = interface->tr_pkt; interface->tr_pkt_old = interface->tr_pkt;
first_time = 0; /* now it isn't first time */ first_time = 0; /* now it isn't first
* time */
} }
} }
} /* while */ }
fclose(file); /* close file */
return 1; /* everything went OK */
fclose(file);
return (TRUE);
} }
else { /* error when opening the file */ else { /* error when opening the file */
perror("Error: Could not open DEVFILE"); perror("Error: Could not open DEVFILE");
return 0; /* something went wrong */ return (FALSE);
} }
} /* get_iface_stats() */ }
#endif /* linux */ #endif /* linux */
+113 -116
View File
@@ -2,7 +2,8 @@
* Collects system information on NetBSD. * Collects system information on NetBSD.
*/ */
/* Copyright (c) 2003 Thomas Runge (coto@core.de) /*-
* Copyright (c) 2003 Thomas Runge (coto@core.de)
* *
* All rights reserved. * All rights reserved.
* *
@@ -60,7 +61,7 @@
#include <ifaddrs.h> #include <ifaddrs.h>
#if (__NetBSD_Version__ >= 300000000) #if (__NetBSD_Version__ >= 300000000)
#include <sys/statvfs.h> # include <sys/statvfs.h>
#endif #endif
#include "machine.h" #include "machine.h"
@@ -73,13 +74,16 @@ static int pageshift;
#define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize) #define PROCSIZE(pp) ((pp)->p_vm_tsize + (pp)->p_vm_dsize + (pp)->p_vm_ssize)
int machine_init(void) int
machine_init(void)
{ {
/* get the page size with "getpagesize" and calculate pageshift from it */ /*
* get the page size with "getpagesize" and calculate pageshift from
* it
*/
int pagesize = getpagesize(); int pagesize = getpagesize();
pageshift = 0; pageshift = 0;
while (pagesize > 1) while (pagesize > 1) {
{
pageshift++; pageshift++;
pagesize >>= 1; pagesize >>= 1;
} }
@@ -87,59 +91,59 @@ int machine_init(void)
/* we only need the amount of log(2)1024 for our conversion */ /* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10; pageshift -= 10;
return(TRUE); return (TRUE);
} }
int machine_close(void) int
machine_close(void)
{ {
return(TRUE); return (TRUE);
} }
int machine_get_battstat(int *acstat, int *battflag, int *percent) int
machine_get_battstat(int *acstat, int *battflag, int *percent)
{ {
int apmd; int apmd;
struct apm_power_info apmi; struct apm_power_info apmi;
if ((apmd = open("/dev/apm", O_RDONLY)) == -1) if ((apmd = open("/dev/apm", O_RDONLY)) == -1) {
{ *acstat = LCDP_AC_ON;
*acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT; *battflag = LCDP_BATT_ABSENT;
*percent = 100; *percent = 100;
return(TRUE); return (TRUE);
} }
memset(&apmi, 0, sizeof(apmi)); memset(&apmi, 0, sizeof(apmi));
if (ioctl(apmd, APM_IOC_GETPOWER, &apmi) == -1) if (ioctl(apmd, APM_IOC_GETPOWER, &apmi) == -1) {
{
perror("APM_IOC_GETPOWER failed in get_batt_stat()"); perror("APM_IOC_GETPOWER failed in get_batt_stat()");
return(FALSE); return (FALSE);
} }
switch(apmi.ac_state) switch (apmi.ac_state) {
{ case APM_AC_OFF:
case APM_AC_OFF: *acstat = LCDP_AC_OFF;
*acstat = LCDP_AC_OFF; break;
break; case APM_AC_ON:
case APM_AC_ON: *acstat = LCDP_AC_ON;
*acstat = LCDP_AC_ON; break;
break; case APM_AC_BACKUP:
case APM_AC_BACKUP: *acstat = LCDP_AC_BACKUP;
*acstat = LCDP_AC_BACKUP; break;
break; default:
default: *acstat = LCDP_AC_UNKNOWN;
*acstat = LCDP_AC_UNKNOWN; break;
break;
} }
*battflag = apmi.battery_state; *battflag = apmi.battery_state;
*percent = apmi.battery_life; *percent = apmi.battery_life;
close(apmd); close(apmd);
return(TRUE); return (TRUE);
} }
int machine_get_fs(mounts_type fs[], int *cnt) int
machine_get_fs(mounts_type fs[], int *cnt)
{ {
#if (__NetBSD_Version__ >= 300000000) #if (__NetBSD_Version__ >= 300000000)
struct statvfs *mntbuf; struct statvfs *mntbuf;
@@ -151,31 +155,27 @@ int machine_get_fs(mounts_type fs[], int *cnt)
int statcnt, fscnt, i; int statcnt, fscnt, i;
fscnt = getmntinfo(&mntbuf, MNT_WAIT); fscnt = getmntinfo(&mntbuf, MNT_WAIT);
if (fscnt == 0) if (fscnt == 0) {
{
perror("getmntinfo"); perror("getmntinfo");
return(FALSE); return (FALSE);
} }
for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) {
{
if (strcmp(pp->f_fstypename, "procfs") if (strcmp(pp->f_fstypename, "procfs")
&& strcmp(pp->f_fstypename, "kernfs") && strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "linprocfs") && strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS #ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs") && strcmp(pp->f_fstypename, "nfs")
#endif #endif
#ifndef STAT_SMBFS #ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs") && strcmp(pp->f_fstypename, "smbfs")
#endif #endif
) ) {
{ snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname); snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename); snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks; fs[statcnt].blocks = pp->f_blocks;
if (fs[statcnt].blocks > 0) if (fs[statcnt].blocks > 0) {
{
fs[statcnt].bsize = pp->f_bsize; fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree; fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files; fs[statcnt].files = pp->f_files;
@@ -186,12 +186,13 @@ int machine_get_fs(mounts_type fs[], int *cnt)
} }
*cnt = statcnt; *cnt = statcnt;
return(TRUE); return (TRUE);
} }
int machine_get_load(load_type *curr_load) int
machine_get_load(load_type * curr_load)
{ {
static load_type last_load = { 0, 0, 0, 0, 0 }; static load_type last_load = {0, 0, 0, 0, 0};
static load_type last_ret_load; static load_type last_ret_load;
load_type load; load_type load;
u_int64_t cp_time[CPUSTATES]; u_int64_t cp_time[CPUSTATES];
@@ -201,49 +202,48 @@ int machine_get_load(load_type *curr_load)
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_CP_TIME; mib[1] = KERN_CP_TIME;
size = sizeof(cp_time); size = sizeof(cp_time);
if (sysctl(mib, 2, cp_time, &size, NULL, 0) < 0) if (sysctl(mib, 2, cp_time, &size, NULL, 0) < 0) {
{
perror("sysctl kern.cp_time failed"); perror("sysctl kern.cp_time failed");
return(FALSE); return (FALSE);
} }
load.user = (unsigned long) (cp_time[CP_USER]); load.user = (unsigned long)(cp_time[CP_USER]);
load.nice = (unsigned long) (cp_time[CP_NICE]); load.nice = (unsigned long)(cp_time[CP_NICE]);
load.system = (unsigned long) (cp_time[CP_SYS] + cp_time[CP_INTR]); load.system = (unsigned long)(cp_time[CP_SYS] + cp_time[CP_INTR]);
load.idle = (unsigned long) (cp_time[CP_IDLE]); load.idle = (unsigned long)(cp_time[CP_IDLE]);
load.total = load.user + load.nice + load.system + load.idle; load.total = load.user + load.nice + load.system + load.idle;
if (load.total != last_load.total) if (load.total != last_load.total) {
{ curr_load->user = load.user - last_load.user;
curr_load->user = load.user - last_load.user; curr_load->nice = load.nice - last_load.nice;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system; curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle; curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total; curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load; last_ret_load = *curr_load;
last_load = load; last_load = load;
} }
else else {
{
*curr_load = last_ret_load; *curr_load = last_ret_load;
} }
return(TRUE); return (TRUE);
} }
int machine_get_loadavg(double *load) int
machine_get_loadavg(double *load)
{ {
double loadavg[LOADAVG_NSTATS]; double loadavg[LOADAVG_NSTATS];
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
return(FALSE); return (FALSE);
*load = loadavg[LOADAVG_1MIN]; *load = loadavg[LOADAVG_1MIN];
return(TRUE); return (TRUE);
} }
int machine_get_meminfo(meminfo_type *result) int
machine_get_meminfo(meminfo_type * result)
{ {
size_t size; size_t size;
int mib[2]; int mib[2];
@@ -252,30 +252,30 @@ int machine_get_meminfo(meminfo_type *result)
mib[0] = CTL_VM; mib[0] = CTL_VM;
mib[1] = VM_UVMEXP2; mib[1] = VM_UVMEXP2;
size = sizeof(uvmexp); size = sizeof(uvmexp);
if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
{
perror("sysctl vm.uvmexp2 failed"); perror("sysctl vm.uvmexp2 failed");
return(FALSE); return (FALSE);
} }
/* memory */ /* memory */
result[0].total = pagetok(uvmexp.npages); result[0].total = pagetok(uvmexp.npages);
result[0].free = pagetok(uvmexp.free); result[0].free = pagetok(uvmexp.free);
/* not really */ /* not really */
result[0].shared = pagetok(uvmexp.wired); result[0].shared = pagetok(uvmexp.wired);
result[0].buffers = pagetok(uvmexp.execpages); result[0].buffers = pagetok(uvmexp.execpages);
result[0].cache = pagetok(uvmexp.filepages); result[0].cache = pagetok(uvmexp.filepages);
/* swap */ /* swap */
result[1].total = pagetok(uvmexp.swpages); result[1].total = pagetok(uvmexp.swpages);
result[1].free = pagetok(uvmexp.swpginuse); result[1].free = pagetok(uvmexp.swpginuse);
result[1].free = result[1].total - result[1].free; result[1].free = result[1].total - result[1].free;
return(TRUE); return (TRUE);
} }
int machine_get_procs(LinkedList *procs) int
machine_get_procs(LinkedList * procs)
{ {
int nproc, i; int nproc, i;
kvm_t *kvmd; kvm_t *kvmd;
@@ -283,27 +283,23 @@ int machine_get_procs(LinkedList *procs)
procinfo_type *p; procinfo_type *p;
kprocs = NULL; kprocs = NULL;
if ((kvmd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open")) == NULL) if ((kvmd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, "kvm_open")) == NULL) {
{
perror("kvm_openfiles"); perror("kvm_openfiles");
return(FALSE); return (FALSE);
} }
kprocs = kvm_getproc2(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc); kprocs = kvm_getproc2(kvmd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2), &nproc);
if (kprocs == NULL) if (kprocs == NULL) {
{
perror("kvm_getproc2"); perror("kvm_getproc2");
return(FALSE); return (FALSE);
} }
for (i = 0; i < nproc; i++) for (i = 0; i < nproc; i++) {
{
p = malloc(sizeof(procinfo_type)); p = malloc(sizeof(procinfo_type));
if (!p) if (!p) {
{
perror("mem_top_malloc"); perror("mem_top_malloc");
kvm_close(kvmd); kvm_close(kvmd);
return(FALSE); return (FALSE);
} }
strncpy(p->name, kprocs->p_comm, 15); strncpy(p->name, kprocs->p_comm, 15);
p->name[15] = '\0'; p->name[15] = '\0';
@@ -316,10 +312,11 @@ int machine_get_procs(LinkedList *procs)
kvm_close(kvmd); kvm_close(kvmd);
return(TRUE); return (TRUE);
} }
int machine_get_smpload(load_type *result, int *numcpus) int
machine_get_smpload(load_type * result, int *numcpus)
{ {
int mib[2], i, num; int mib[2], i, num;
size_t size; size_t size;
@@ -331,14 +328,14 @@ int machine_get_smpload(load_type *result, int *numcpus)
if (sysctl(mib, 2, &num, &size, NULL, 0) < 0) { if (sysctl(mib, 2, &num, &size, NULL, 0) < 0) {
perror("sysctl hw.ncpu"); perror("sysctl hw.ncpu");
return(FALSE); return (FALSE);
} }
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
return(FALSE); return (FALSE);
if (numcpus == NULL) if (numcpus == NULL)
return(FALSE); return (FALSE);
/* restrict #CPUs to max. *numcpus */ /* restrict #CPUs to max. *numcpus */
num = (*numcpus >= num) ? num : *numcpus; num = (*numcpus >= num) ? num : *numcpus;
@@ -349,10 +346,11 @@ int machine_get_smpload(load_type *result, int *numcpus)
result[i] = curr_load; result[i] = curr_load;
} }
return(TRUE); return (TRUE);
} }
int machine_get_uptime(double *up, double *idle) int
machine_get_uptime(double *up, double *idle)
{ {
size_t size; size_t size;
time_t now; time_t now;
@@ -364,10 +362,9 @@ int machine_get_uptime(double *up, double *idle)
mib[1] = KERN_BOOTTIME; mib[1] = KERN_BOOTTIME;
size = sizeof(boottime); size = sizeof(boottime);
time(&now); time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
{
perror("sysctl kern.boottime failed"); perror("sysctl kern.boottime failed");
return(FALSE); return (FALSE);
} }
*up = (double)(now - boottime.tv_sec); *up = (double)(now - boottime.tv_sec);
@@ -375,13 +372,14 @@ int machine_get_uptime(double *up, double *idle)
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
*idle = 100.; *idle = 100.;
else else
*idle = 100.*curr_load.idle/curr_load.total; *idle = 100. * curr_load.idle / curr_load.total;
return(TRUE); return (TRUE);
} }
/* Get network statistics */ /* Get network statistics */
int machine_get_iface_stats (IfaceInfo *interface) int
machine_get_iface_stats(IfaceInfo * interface)
{ {
struct ifaddrs *ifa, *ifa_ptr; struct ifaddrs *ifa, *ifa_ptr;
struct if_data *ifd; struct if_data *ifd;
@@ -392,11 +390,11 @@ int machine_get_iface_stats (IfaceInfo *interface)
/* loop through all interfaces */ /* loop through all interfaces */
for (ifa_ptr = ifa; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) { for (ifa_ptr = ifa; ifa_ptr != NULL; ifa_ptr = ifa_ptr->ifa_next) {
interface->status = down; /* set status down by default */ interface->status = down; /* set status down by default */
/* check if we got the right interface and if it is Link type */ /* check if we got the right interface and if it is Link type */
if ((strcmp(ifa_ptr->ifa_name, interface->name) == 0) && if ((strcmp(ifa_ptr->ifa_name, interface->name) == 0) &&
(ifa_ptr->ifa_addr->sa_family == AF_LINK)) { (ifa_ptr->ifa_addr->sa_family == AF_LINK)) {
ifd = (struct if_data *)ifa_ptr->ifa_data; ifd = (struct if_data *)ifa_ptr->ifa_data;
@@ -413,7 +411,7 @@ int machine_get_iface_stats (IfaceInfo *interface)
} }
if ((ifa_ptr->ifa_flags & IFF_UP) == IFF_UP) { if ((ifa_ptr->ifa_flags & IFF_UP) == IFF_UP) {
interface->status = up; /* is up */ interface->status = up; /* is up */
interface->last_online = time(NULL); /* save actual time */ interface->last_online = time(NULL); /* save actual time */
} }
@@ -426,5 +424,4 @@ int machine_get_iface_stats (IfaceInfo *interface)
return (TRUE); return (TRUE);
} }
#endif /* __NetBSD__ */ #endif /* __NetBSD__ */
+124 -127
View File
@@ -2,7 +2,8 @@
* Collects system information on OpenBSD. * Collects system information on OpenBSD.
*/ */
/* Copyright (c) 2003 Thomas Runge (coto@core.de) /*-
* Copyright (c) 2003 Thomas Runge (coto@core.de)
* *
* All rights reserved. * All rights reserved.
* *
@@ -63,13 +64,16 @@ static int pageshift;
#define PROCSIZE(pp) ((pp).vm_tsize + (pp).vm_dsize + (pp).vm_ssize) #define PROCSIZE(pp) ((pp).vm_tsize + (pp).vm_dsize + (pp).vm_ssize)
int machine_init(void) int
machine_init(void)
{ {
/* get the page size with "getpagesize" and calculate pageshift from it */ /*
* get the page size with "getpagesize" and calculate pageshift from
* it
*/
int pagesize = getpagesize(); int pagesize = getpagesize();
pageshift = 0; pageshift = 0;
while (pagesize > 1) while (pagesize > 1) {
{
pageshift++; pageshift++;
pagesize >>= 1; pagesize >>= 1;
} }
@@ -77,109 +81,104 @@ int machine_init(void)
/* we only need the amount of log(2)1024 for our conversion */ /* we only need the amount of log(2)1024 for our conversion */
pageshift -= 10; pageshift -= 10;
return(TRUE); return (TRUE);
} }
int machine_close(void) int
machine_close(void)
{ {
return(TRUE); return (TRUE);
} }
int machine_get_battstat(int *acstat, int *battflag, int *percent) int
machine_get_battstat(int *acstat, int *battflag, int *percent)
{ {
int apmd; int apmd;
struct apm_power_info api; struct apm_power_info api;
*acstat = LCDP_AC_ON; *acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT; *battflag = LCDP_BATT_ABSENT;
*percent = 100; *percent = 100;
if ((apmd = open("/dev/apm", O_RDONLY)) == -1) if ((apmd = open("/dev/apm", O_RDONLY)) == -1) {
{
perror("get_battstat_open"); perror("get_battstat_open");
return(TRUE); return (TRUE);
} }
if (ioctl(apmd, APM_IOC_GETPOWER, &api) == -1) if (ioctl(apmd, APM_IOC_GETPOWER, &api) == -1) {
{
perror("get_battstat_ioctl"); perror("get_battstat_ioctl");
return(FALSE); return (FALSE);
} }
switch(api.ac_state) switch (api.ac_state) {
{ case APM_AC_OFF:
case APM_AC_OFF: *acstat = LCDP_AC_OFF;
*acstat = LCDP_AC_OFF; break;
break; case APM_AC_ON:
case APM_AC_ON: *acstat = LCDP_AC_ON;
*acstat = LCDP_AC_ON; break;
break; default:
default: *acstat = LCDP_AC_UNKNOWN;
*acstat = LCDP_AC_UNKNOWN; break;
break;
} }
switch(api.battery_state) switch (api.battery_state) {
{ case APM_BATT_HIGH:
case APM_BATT_HIGH: *battflag = LCDP_BATT_HIGH;
*battflag = LCDP_BATT_HIGH; break;
break; case APM_BATT_LOW:
case APM_BATT_LOW: *battflag = LCDP_BATT_LOW;
*battflag = LCDP_BATT_LOW; break;
break; case APM_BATT_CRITICAL:
case APM_BATT_CRITICAL: *battflag = LCDP_BATT_CRITICAL;
*battflag = LCDP_BATT_CRITICAL; break;
break; case APM_BATT_CHARGING:
case APM_BATT_CHARGING: *battflag = LCDP_BATT_CHARGING;
*battflag = LCDP_BATT_CHARGING; break;
break; case APM_BATTERY_ABSENT:
case APM_BATTERY_ABSENT: *battflag = LCDP_BATT_ABSENT;
*battflag = LCDP_BATT_ABSENT; break;
break; default:
default: *battflag = LCDP_BATT_UNKNOWN;
*battflag = LCDP_BATT_UNKNOWN; break;
break;
} }
*percent = api.battery_life; *percent = api.battery_life;
close(apmd); close(apmd);
return(TRUE); return (TRUE);
} }
int machine_get_fs(mounts_type fs[], int *cnt) int
machine_get_fs(mounts_type fs[], int *cnt)
{ {
struct statfs *mntbuf; struct statfs *mntbuf;
struct statfs *pp; struct statfs *pp;
int statcnt, fscnt, i; int statcnt, fscnt, i;
fscnt = getmntinfo(&mntbuf, MNT_WAIT); fscnt = getmntinfo(&mntbuf, MNT_WAIT);
if (fscnt == 0) if (fscnt == 0) {
{
perror("getmntinfo"); perror("getmntinfo");
return(FALSE); return (FALSE);
} }
for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) for (statcnt = 0, pp = mntbuf, i = 0; i < fscnt; pp++, i++) {
{
if (strcmp(pp->f_fstypename, "procfs") if (strcmp(pp->f_fstypename, "procfs")
&& strcmp(pp->f_fstypename, "kernfs") && strcmp(pp->f_fstypename, "kernfs")
&& strcmp(pp->f_fstypename, "linprocfs") && strcmp(pp->f_fstypename, "linprocfs")
#ifndef STAT_NFS #ifndef STAT_NFS
&& strcmp(pp->f_fstypename, "nfs") && strcmp(pp->f_fstypename, "nfs")
#endif #endif
#ifndef STAT_SMBFS #ifndef STAT_SMBFS
&& strcmp(pp->f_fstypename, "smbfs") && strcmp(pp->f_fstypename, "smbfs")
#endif #endif
) ) {
{ snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].dev, 255, "%s", pp->f_mntfromname);
snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname); snprintf(fs[statcnt].mpoint, 255, "%s", pp->f_mntonname);
snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename); snprintf(fs[statcnt].type, 63, "%s", pp->f_fstypename);
fs[statcnt].blocks = pp->f_blocks; fs[statcnt].blocks = pp->f_blocks;
if (fs[statcnt].blocks > 0) if (fs[statcnt].blocks > 0) {
{
fs[statcnt].bsize = pp->f_bsize; fs[statcnt].bsize = pp->f_bsize;
fs[statcnt].bfree = pp->f_bfree; fs[statcnt].bfree = pp->f_bfree;
fs[statcnt].files = pp->f_files; fs[statcnt].files = pp->f_files;
@@ -190,12 +189,13 @@ int machine_get_fs(mounts_type fs[], int *cnt)
} }
*cnt = statcnt; *cnt = statcnt;
return(TRUE); return (TRUE);
} }
int machine_get_load(load_type *curr_load) int
machine_get_load(load_type * curr_load)
{ {
static load_type last_load = { 0, 0, 0, 0, 0 }; static load_type last_load = {0, 0, 0, 0, 0};
static load_type last_ret_load; static load_type last_ret_load;
load_type load; load_type load;
long cp_time[CPUSTATES]; long cp_time[CPUSTATES];
@@ -205,49 +205,48 @@ int machine_get_load(load_type *curr_load)
mib[0] = CTL_KERN; mib[0] = CTL_KERN;
mib[1] = KERN_CPTIME; mib[1] = KERN_CPTIME;
size = sizeof(cp_time); size = sizeof(cp_time);
if (sysctl(mib, 2, cp_time, &size, NULL, 0) < 0) if (sysctl(mib, 2, cp_time, &size, NULL, 0) < 0) {
{
perror("sysctl kern.cp_time failed"); perror("sysctl kern.cp_time failed");
return(FALSE); return (FALSE);
} }
load.user = (unsigned long) (cp_time[CP_USER]); load.user = (unsigned long)(cp_time[CP_USER]);
load.nice = (unsigned long) (cp_time[CP_NICE]); load.nice = (unsigned long)(cp_time[CP_NICE]);
load.system = (unsigned long) (cp_time[CP_SYS] + cp_time[CP_INTR]); load.system = (unsigned long)(cp_time[CP_SYS] + cp_time[CP_INTR]);
load.idle = (unsigned long) (cp_time[CP_IDLE]); load.idle = (unsigned long)(cp_time[CP_IDLE]);
load.total = load.user + load.nice + load.system + load.idle; load.total = load.user + load.nice + load.system + load.idle;
if (load.total != last_load.total) if (load.total != last_load.total) {
{ curr_load->user = load.user - last_load.user;
curr_load->user = load.user - last_load.user; curr_load->nice = load.nice - last_load.nice;
curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system; curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle; curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total; curr_load->total = load.total - last_load.total;
last_ret_load = *curr_load; last_ret_load = *curr_load;
last_load = load; last_load = load;
} }
else else {
{
*curr_load = last_ret_load; *curr_load = last_ret_load;
} }
return(TRUE); return (TRUE);
} }
int machine_get_loadavg(double *load) int
machine_get_loadavg(double *load)
{ {
double loadavg[LOADAVG_NSTATS]; double loadavg[LOADAVG_NSTATS];
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
return(FALSE); return (FALSE);
*load = loadavg[LOADAVG_1MIN]; *load = loadavg[LOADAVG_1MIN];
return(TRUE); return (TRUE);
} }
int machine_get_meminfo(meminfo_type *result) int
machine_get_meminfo(meminfo_type * result)
{ {
size_t size; size_t size;
int mib[2]; int mib[2];
@@ -257,58 +256,54 @@ int machine_get_meminfo(meminfo_type *result)
mib[1] = VM_UVMEXP; mib[1] = VM_UVMEXP;
size = sizeof(suvm); size = sizeof(suvm);
if (sysctl(mib, 2, &suvm, &size, NULL, 0) < 0) if (sysctl(mib, 2, &suvm, &size, NULL, 0) < 0) {
{
perror("sysctl vm.uvmexp failed"); perror("sysctl vm.uvmexp failed");
return(FALSE); return (FALSE);
} }
/* memory */ /* memory */
result[0].total = pagetok(suvm.npages); result[0].total = pagetok(suvm.npages);
result[0].free = pagetok(suvm.free); result[0].free = pagetok(suvm.free);
/* not used anyway */ /* not used anyway */
result[0].shared = 0; result[0].shared = 0;
result[0].buffers = 0; result[0].buffers = 0;
result[0].cache = 0; result[0].cache = 0;
/* swap */ /* swap */
result[1].total = suvm.pagesize * suvm.swpages; result[1].total = suvm.pagesize * suvm.swpages;
result[1].free = suvm.pagesize * suvm.swpginuse; result[1].free = suvm.pagesize * suvm.swpginuse;
result[1].free = result[1].total - result[1].free; result[1].free = result[1].total - result[1].free;
return(TRUE); return (TRUE);
} }
int machine_get_procs(LinkedList *procs) int
machine_get_procs(LinkedList * procs)
{ {
struct kinfo_proc *kprocs; struct kinfo_proc *kprocs;
int nproc, i; int nproc, i;
procinfo_type *p; procinfo_type *p;
kvm_t *kvmd; kvm_t *kvmd;
if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) if ((kvmd = kvm_open(NULL, NULL, NULL, O_RDONLY, "kvm_open")) == NULL) {
{
perror("kvm_open"); perror("kvm_open");
return(FALSE); return (FALSE);
} }
kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc); kprocs = kvm_getprocs(kvmd, KERN_PROC_ALL, 0, &nproc);
if (kprocs == NULL) if (kprocs == NULL) {
{
perror("kvm_getprocs"); perror("kvm_getprocs");
kvm_close(kvmd); kvm_close(kvmd);
return(FALSE); return (FALSE);
} }
for (i = 0; i < nproc; i++) for (i = 0; i < nproc; i++) {
{
p = malloc(sizeof(procinfo_type)); p = malloc(sizeof(procinfo_type));
if (!p) if (!p) {
{
perror("mem_top_malloc"); perror("mem_top_malloc");
kvm_close(kvmd); kvm_close(kvmd);
return(FALSE); return (FALSE);
} }
strncpy(p->name, kprocs->kp_proc.p_comm, 15); strncpy(p->name, kprocs->kp_proc.p_comm, 15);
p->name[15] = '\0'; p->name[15] = '\0';
@@ -320,10 +315,11 @@ int machine_get_procs(LinkedList *procs)
} }
kvm_close(kvmd); kvm_close(kvmd);
return(TRUE); return (TRUE);
} }
int machine_get_smpload(load_type *result, int *numcpus) int
machine_get_smpload(load_type * result, int *numcpus)
{ {
int mib[2], i, num; int mib[2], i, num;
size_t size; size_t size;
@@ -335,14 +331,14 @@ int machine_get_smpload(load_type *result, int *numcpus)
if (sysctl(mib, 2, &num, &size, NULL, 0) < 0) { if (sysctl(mib, 2, &num, &size, NULL, 0) < 0) {
perror("sysctl hw.ncpu"); perror("sysctl hw.ncpu");
return(FALSE); return (FALSE);
} }
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
return(FALSE); return (FALSE);
if (numcpus == NULL) if (numcpus == NULL)
return(FALSE); return (FALSE);
/* restrict #CPUs to max. *numcpus */ /* restrict #CPUs to max. *numcpus */
num = (*numcpus >= num) ? num : *numcpus; num = (*numcpus >= num) ? num : *numcpus;
@@ -353,10 +349,11 @@ int machine_get_smpload(load_type *result, int *numcpus)
result[i] = curr_load; result[i] = curr_load;
} }
return(TRUE); return (TRUE);
} }
int machine_get_uptime(double *up, double *idle) int
machine_get_uptime(double *up, double *idle)
{ {
size_t size; size_t size;
time_t now; time_t now;
@@ -368,10 +365,9 @@ int machine_get_uptime(double *up, double *idle)
mib[1] = KERN_BOOTTIME; mib[1] = KERN_BOOTTIME;
size = sizeof(boottime); size = sizeof(boottime);
time(&now); time(&now);
if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) if (sysctl(mib, 2, &boottime, &size, NULL, 0) < 0) {
{
perror("sysctl kern.boottime failed"); perror("sysctl kern.boottime failed");
return(FALSE); return (FALSE);
} }
*up = (double)(now - boottime.tv_sec); *up = (double)(now - boottime.tv_sec);
@@ -379,16 +375,17 @@ int machine_get_uptime(double *up, double *idle)
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
*idle = 100.; *idle = 100.;
else else
*idle = 100.*curr_load.idle/curr_load.total; *idle = 100. * curr_load.idle / curr_load.total;
return(TRUE); return (TRUE);
} }
/* Get network statistics */ /* Get network statistics */
int machine_get_iface_stats (IfaceInfo *interface) int
machine_get_iface_stats(IfaceInfo * interface)
{ {
/* Implementation missing */ /* Implementation missing */
return 0; return 0;
} }
#endif /* __OpenBSD__ */ #endif /* __OpenBSD__ */
+131 -144
View File
@@ -41,41 +41,43 @@
static kstat_ctl_t *kc; static kstat_ctl_t *kc;
int machine_init(void) int
machine_init(void)
{ {
kc = NULL; kc = NULL;
kc = kstat_open(); kc = kstat_open();
if (kc == NULL) if (kc == NULL) {
{
perror("kstat_open"); perror("kstat_open");
return(FALSE); return (FALSE);
} }
return(TRUE); return (TRUE);
} }
int machine_close(void) int
machine_close(void)
{ {
if (kc != NULL) if (kc != NULL) {
{
kstat_close(kc); kstat_close(kc);
kc = NULL; kc = NULL;
} }
return(TRUE); return (TRUE);
} }
int machine_get_battstat(int *acstat, int *battflag, int *percent) int
machine_get_battstat(int *acstat, int *battflag, int *percent)
{ {
*acstat = LCDP_AC_ON; *acstat = LCDP_AC_ON;
*battflag = LCDP_BATT_ABSENT; *battflag = LCDP_BATT_ABSENT;
*percent = 100; *percent = 100;
return(TRUE); return (TRUE);
} }
int machine_get_fs(mounts_type fs[], int *cnt) int
machine_get_fs(mounts_type fs[], int *cnt)
{ {
FILE *mtab_fd; FILE *mtab_fd;
char line[256]; char line[256];
@@ -93,13 +95,11 @@ int machine_get_fs(mounts_type fs[], int *cnt)
#error "Can't find your mounted filesystem table file." #error "Can't find your mounted filesystem table file."
#endif #endif
// Get rid of old, unmounted filesystems... /* Get rid of old, unmounted filesystems... */
memset(fs, 0, sizeof(mounts_type) * 256); memset(fs, 0, sizeof(mounts_type) * 256);
while (x < 256) while (x < 256) {
{ if (fgets(line, 256, mtab_fd) == NULL) {
if (fgets(line, 256, mtab_fd) == NULL)
{
fclose(mtab_fd); fclose(mtab_fd);
*cnt = x; *cnt = x;
break; break;
@@ -107,15 +107,14 @@ int machine_get_fs(mounts_type fs[], int *cnt)
sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type); sscanf(line, "%s %s %s", fs[x].dev, fs[x].mpoint, fs[x].type);
if ( strcmp(fs[x].type, "proc") if (strcmp(fs[x].type, "proc")
#ifndef STAT_NFS #ifndef STAT_NFS
&& strcmp(fs[x].type, "nfs") && strcmp(fs[x].type, "nfs")
#endif #endif
#ifndef STAT_SMBFS #ifndef STAT_SMBFS
&& strcmp(fs[x].type, "smbfs") && strcmp(fs[x].type, "smbfs")
#endif #endif
) ) {
{
#ifdef STAT_STATVFS #ifdef STAT_STATVFS
y = statvfs(fs[x].mpoint, &fsinfo); y = statvfs(fs[x].mpoint, &fsinfo);
#elif STAT_STATFS2_BSIZE #elif STAT_STATFS2_BSIZE
@@ -127,8 +126,7 @@ int machine_get_fs(mounts_type fs[], int *cnt)
#endif #endif
fs[x].blocks = fsinfo.f_blocks; fs[x].blocks = fsinfo.f_blocks;
if (fs[x].blocks > 0) if (fs[x].blocks > 0) {
{
fs[x].bsize = fsinfo.f_bsize; fs[x].bsize = fsinfo.f_bsize;
fs[x].bfree = fsinfo.f_bfree; fs[x].bfree = fsinfo.f_bfree;
fs[x].files = fsinfo.f_files; fs[x].files = fsinfo.f_files;
@@ -141,146 +139,138 @@ int machine_get_fs(mounts_type fs[], int *cnt)
fclose(mtab_fd); fclose(mtab_fd);
*cnt = x; *cnt = x;
return(TRUE); return (TRUE);
} }
int machine_get_load(load_type *curr_load) int
machine_get_load(load_type * curr_load)
{ {
static load_type last_load = { 0, 0, 0, 0, 0 }; static load_type last_load = {0, 0, 0, 0, 0};
load_type load; load_type load;
kstat_t *k_space; kstat_t *k_space;
struct cpu_stat cinfo; struct cpu_stat cinfo;
k_space = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0"); k_space = kstat_lookup(kc, "cpu_stat", 0, "cpu_stat0");
if (k_space == NULL) if (k_space == NULL) {
{
fprintf(stderr, "kstat lookup error\n"); fprintf(stderr, "kstat lookup error\n");
return(FALSE); return (FALSE);
} }
if (kstat_read(kc, k_space, NULL) == -1) if (kstat_read(kc, k_space, NULL) == -1) {
{
fprintf(stderr, "kstat read error\n"); fprintf(stderr, "kstat read error\n");
return(FALSE); return (FALSE);
} }
k_space = kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0"); k_space = kstat_lookup(kc, "cpu_stat", -1, "cpu_stat0");
if (k_space == NULL) if (k_space == NULL) {
{
fprintf(stderr, "kstat lookup error\n"); fprintf(stderr, "kstat lookup error\n");
return(FALSE); return (FALSE);
} }
if (kstat_read(kc, k_space, &cinfo) == -1) if (kstat_read(kc, k_space, &cinfo) == -1) {
{
fprintf(stderr, "kstat read error\n"); fprintf(stderr, "kstat read error\n");
return(FALSE); return (FALSE);
} }
load.idle = cinfo.cpu_sysinfo.cpu[CPU_IDLE]; load.idle = cinfo.cpu_sysinfo.cpu[CPU_IDLE];
load.user = cinfo.cpu_sysinfo.cpu[CPU_USER]; load.user = cinfo.cpu_sysinfo.cpu[CPU_USER];
load.system = cinfo.cpu_sysinfo.cpu[CPU_KERNEL]; load.system = cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
load.nice = cinfo.cpu_sysinfo.cpu[CPU_WAIT]; load.nice = cinfo.cpu_sysinfo.cpu[CPU_WAIT];
load.total = 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->user = load.user - last_load.user;
curr_load->nice = load.nice - last_load.nice; curr_load->nice = load.nice - last_load.nice;
curr_load->system = load.system - last_load.system; curr_load->system = load.system - last_load.system;
curr_load->idle = load.idle - last_load.idle; curr_load->idle = load.idle - last_load.idle;
curr_load->total = load.total - last_load.total; curr_load->total = load.total - last_load.total;
// struct assingment is legal in C89 /* struct assingment is legal in C89 */
last_load = load; last_load = load;
return(TRUE); return (TRUE);
} }
int machine_get_loadavg(double *load) int
machine_get_loadavg(double *load)
{ {
double loadavg[LOADAVG_NSTATS]; double loadavg[LOADAVG_NSTATS];
if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN) if (getloadavg(loadavg, LOADAVG_NSTATS) <= LOADAVG_1MIN)
return(FALSE); return (FALSE);
*load = loadavg[LOADAVG_1MIN]; *load = loadavg[LOADAVG_1MIN];
return(TRUE); return (TRUE);
} }
int machine_get_meminfo(meminfo_type *result) int
machine_get_meminfo(meminfo_type * result)
{ {
#define MAXSTRSIZE 80 #define MAXSTRSIZE 80
swaptbl_t *s; swaptbl_t *s;
int i, n, num; int i, n, num;
char *strtab; /* string table for path names */ char *strtab; /* string table for path names */
s = NULL; s = NULL;
result[0].total = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024; result[0].total = sysconf(_SC_PHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].free = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024; result[0].free = sysconf(_SC_AVPHYS_PAGES) * sysconf(_SC_PAGESIZE) / 1024;
result[0].shared = 0; result[0].shared = 0;
result[0].buffers = 0; result[0].buffers = 0;
result[0].cache = 0; result[0].cache = 0;
again: again:
if ((num = swapctl(SC_GETNSWP, 0)) == -1) if ((num = swapctl(SC_GETNSWP, 0)) == -1) {
{
perror("swapctl: GETNSWP"); perror("swapctl: GETNSWP");
return(FALSE); return (FALSE);
} }
if (num == 0) if (num == 0) {
{
fprintf(stderr, "No Swap Devices Configured\n"); fprintf(stderr, "No Swap Devices Configured\n");
return(FALSE); return (FALSE);
} }
/* allocate swaptable for num+1 entries */ /* allocate swaptable for num+1 entries */
if ((s = (swaptbl_t*)malloc(num * sizeof(swapent_t) + sizeof(struct swaptable))) == NULL) if ((s = (swaptbl_t *) malloc(num * sizeof(swapent_t) + sizeof(struct swaptable))) == NULL) {
{
perror("malloc swap"); perror("malloc swap");
return(FALSE); return (FALSE);
} }
/* allocate num+1 string holders */ /* allocate num+1 string holders */
if ((strtab = (char*)malloc((num + 1) * MAXSTRSIZE)) == NULL) if ((strtab = (char *)malloc((num + 1) * MAXSTRSIZE)) == NULL) {
{
perror("malloc string holder"); perror("malloc string holder");
return(FALSE); return (FALSE);
} }
/* initialize string pointers */ /* initialize string pointers */
for (i = 0; i < (num + 1); i++) for (i = 0; i < (num + 1); i++) {
{
s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE); s->swt_ent[i].ste_path = strtab + (i * MAXSTRSIZE);
} }
s->swt_n = num + 1; s->swt_n = num + 1;
if ((n = swapctl(SC_LIST, s)) < 0) if ((n = swapctl(SC_LIST, s)) < 0) {
{
perror("swapctl"); perror("swapctl");
return(FALSE); return (FALSE);
} }
/* more were added */ /* more were added */
if (n > num) if (n > num) {
{
free(s); free(s);
free(strtab); free(strtab);
goto again; goto again;
} }
result[1].total = 0; result[1].total = 0;
result[1].free = 0; result[1].free = 0;
for (i = 0; i < n; i++) for (i = 0; i < n; i++) {
{
result[1].total = result[1].total + s->swt_ent[i].ste_pages * sysconf(_SC_PAGESIZE) / 1024; 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; result[1].free = result[1].free + s->swt_ent[i].ste_free * sysconf(_SC_PAGESIZE) / 1024;
} }
return(TRUE); return (TRUE);
} }
int machine_get_procs(LinkedList *procs) int
machine_get_procs(LinkedList * procs)
{ {
char buf[128]; char buf[128];
DIR *proc; DIR *proc;
@@ -292,24 +282,23 @@ int machine_get_procs(LinkedList *procs)
int procSize, procRSS, procData, procStk, procExe; int procSize, procRSS, procData, procStk, procExe;
int threshold = 400, unique; int threshold = 400, unique;
if ((proc = opendir("/proc")) == NULL) if ((proc = opendir("/proc")) == NULL) {
{
perror("open /proc"); perror("open /proc");
return(FALSE); return (FALSE);
} }
while ((procdir = readdir(proc))) while ((procdir = readdir(proc))) {
{
psinfo_t psinfo; psinfo_t psinfo;
if (!strchr("1234567890", procdir->d_name[0])) if (!strchr("1234567890", procdir->d_name[0]))
continue; continue;
sprintf(buf, "/proc/%s/psinfo", procdir->d_name); sprintf(buf, "/proc/%s/psinfo", procdir->d_name);
if ((StatusFile = fopen(buf, "r")) == NULL) if ((StatusFile = fopen(buf, "r")) == NULL) {
{ /*
// Not a serious error; process has finished before we could * Not a serious error; process has finished before
// examine it: * we could examine it:
*/
continue; continue;
} }
@@ -317,27 +306,26 @@ int machine_get_procs(LinkedList *procs)
fread(&psinfo, sizeof(psinfo), 1, StatusFile); fread(&psinfo, sizeof(psinfo), 1, StatusFile);
strcpy(procName, psinfo.pr_fname); strcpy(procName, psinfo.pr_fname);
procSize = psinfo.pr_size; procSize = psinfo.pr_size;
procRSS = psinfo.pr_rssize; procRSS = psinfo.pr_rssize;
// Following values not accurate, not sure what needs to be set to /*
* Following values not accurate, not sure what needs to be
* set to
*/
procData = psinfo.pr_size; procData = psinfo.pr_size;
procStk = 0; procStk = 0;
procExe = 0; procExe = 0;
fclose(StatusFile); fclose(StatusFile);
if (procSize > threshold) if (procSize > threshold) {
{ /* Figure out if it's sharing any memory... */
// Figure out if it's sharing any memory...
unique = 1; unique = 1;
LL_Rewind(procs); LL_Rewind(procs);
do do {
{
p = LL_Get(procs); p = LL_Get(procs);
if (p) if (p) {
{ if (0 == strcmp(p->name, procName)) {
if (0 == strcmp(p->name, procName))
{
unique = 0; unique = 0;
p->number++; p->number++;
p->totl += procData + procStk + procExe; p->totl += procData + procStk + procExe;
@@ -345,30 +333,29 @@ int machine_get_procs(LinkedList *procs)
} }
} while (LL_Next(procs) == 0); } while (LL_Next(procs) == 0);
// If this is the first one by this name... /* If this is the first one by this name... */
if (unique) if (unique) {
{
p = malloc(sizeof(procinfo_type)); p = malloc(sizeof(procinfo_type));
if (p == NULL) if (p == NULL) {
{
perror("allocating process entry"); perror("allocating process entry");
goto end; // Ack! I hate goto's! goto end;
} }
strcpy(p->name, procName); strcpy(p->name, procName);
p->totl = procData + procStk + procExe; p->totl = procData + procStk + procExe;
p->number = 1; p->number = 1;
// TODO: Check for errors here? /* TODO: Check for errors here? */
LL_Push(procs, (void *) p); LL_Push(procs, (void *)p);
} }
} }
} }
end: end:
closedir(proc); closedir(proc);
return(TRUE); return (TRUE);
} }
int machine_get_smpload(load_type *result, int *numcpus) int
machine_get_smpload(load_type * result, int *numcpus)
{ {
static load_type last_load[MAX_CPUS]; static load_type last_load[MAX_CPUS];
load_type curr_load[MAX_CPUS]; load_type curr_load[MAX_CPUS];
@@ -377,37 +364,36 @@ int machine_get_smpload(load_type *result, int *numcpus)
max_cpu = sysconf(_SC_NPROCESSORS_CONF); max_cpu = sysconf(_SC_NPROCESSORS_CONF);
for (count = 0; count < max_cpu; count++) { for (count = 0; count < max_cpu; count++) {
kstat_t *k_space; kstat_t *k_space;
char buffer[16]; char buffer[16];
sprintf(buffer, "cpu_stat%d", count); sprintf(buffer, "cpu_stat%d", count);
k_space = kstat_lookup(kc, "cpu_stat", count, buffer); k_space = kstat_lookup(kc, "cpu_stat", count, buffer);
if ((k_space != NULL) && (kstat_read(kc, k_space, NULL) != -1)) if ((k_space != NULL) && (kstat_read(kc, k_space, NULL) != -1)) {
{
struct cpu_stat cinfo; struct cpu_stat cinfo;
k_space = kstat_lookup(kc, "cpu_stat", -1, buffer); k_space = kstat_lookup(kc, "cpu_stat", -1, buffer);
kstat_read(kc, k_space, &cinfo); kstat_read(kc, k_space, &cinfo);
curr_load[ncpu].idle = cinfo.cpu_sysinfo.cpu[CPU_IDLE]; curr_load[ncpu].idle = cinfo.cpu_sysinfo.cpu[CPU_IDLE];
curr_load[ncpu].user = cinfo.cpu_sysinfo.cpu[CPU_USER]; curr_load[ncpu].user = cinfo.cpu_sysinfo.cpu[CPU_USER];
curr_load[ncpu].system = cinfo.cpu_sysinfo.cpu[CPU_KERNEL]; curr_load[ncpu].system = cinfo.cpu_sysinfo.cpu[CPU_KERNEL];
curr_load[ncpu].nice = cinfo.cpu_sysinfo.cpu[CPU_WAIT]; curr_load[ncpu].nice = cinfo.cpu_sysinfo.cpu[CPU_WAIT];
curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice + curr_load[ncpu].total = curr_load[ncpu].user + curr_load[ncpu].nice +
curr_load[ncpu].system + curr_load[ncpu].idle; curr_load[ncpu].system + curr_load[ncpu].idle;
result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total; result[ncpu].total = curr_load[ncpu].total - last_load[ncpu].total;
result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user; result[ncpu].user = curr_load[ncpu].user - last_load[ncpu].user;
result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice; result[ncpu].nice = curr_load[ncpu].nice - last_load[ncpu].nice;
result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system; result[ncpu].system = curr_load[ncpu].system - last_load[ncpu].system;
result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle; result[ncpu].idle = curr_load[ncpu].idle - last_load[ncpu].idle;
// struct assignment is legal in C89 /* struct assignment is legal in C89 */
last_load[ncpu] = curr_load[ncpu]; last_load[ncpu] = curr_load[ncpu];
// restrict # CPUs to min(*numcpus, MAX_CPUS) /* restrict # CPUs to min(*numcpus, MAX_CPUS) */
ncpu++; ncpu++;
if ((ncpu >= *numcpus) || (ncpu >= MAX_CPUS)) if ((ncpu >= *numcpus) || (ncpu >= MAX_CPUS))
break; break;
@@ -415,40 +401,41 @@ int machine_get_smpload(load_type *result, int *numcpus)
} }
*numcpus = ncpu; *numcpus = ncpu;
return(TRUE); return (TRUE);
} }
/* TODO get idle time! */ /* TODO get idle time! */
int machine_get_uptime(double *up, double *idle) int
machine_get_uptime(double *up, double *idle)
{ {
struct utmpx *u, id; struct utmpx *u, id;
load_type curr_load; load_type curr_load;
*up = 0; *up = 0;
*idle = 0; *idle = 0;
id.ut_type = BOOT_TIME; id.ut_type = BOOT_TIME;
u = getutxid(&id); u = getutxid(&id);
if (u == NULL) if (u == NULL)
return(FALSE); return (FALSE);
*up = time(0) - u->ut_xtime; *up = time(0) - u->ut_xtime;
if (machine_get_load(&curr_load) == FALSE) if (machine_get_load(&curr_load) == FALSE)
*idle = 100.; *idle = 100.;
else else
*idle = 100.*curr_load.idle/curr_load.total; *idle = 100. * curr_load.idle / curr_load.total;
return(TRUE); return (TRUE);
} }
/* Get network statistics */ /* Get network statistics */
int machine_get_iface_stats (IfaceInfo *interface) int
machine_get_iface_stats(IfaceInfo * interface)
{ {
/* Implementation missing */ /* Implementation missing */
return 0; return 0;
} }
#endif /* sun */ #endif /* sun */