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