2009-07-28 21:44:22 +00:00
|
|
|
/* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
|
|
|
|
*
|
|
|
|
* Conky, a system monitor, based on torsmo
|
2007-03-01 01:46:11 +00:00
|
|
|
*
|
2007-08-10 19:53:44 +00:00
|
|
|
* Any original torsmo code is licensed under the BSD license
|
|
|
|
*
|
|
|
|
* All code written since the fork of torsmo is licensed under the GPL
|
|
|
|
*
|
|
|
|
* Please see COPYING for details
|
|
|
|
*
|
|
|
|
* Copyright (c) 2007 Toni Spets
|
2009-03-30 04:55:30 +00:00
|
|
|
* Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
|
2008-02-20 20:30:45 +00:00
|
|
|
* (see AUTHORS)
|
2007-08-10 19:53:44 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2008-02-20 20:30:45 +00:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2007-08-10 19:53:44 +00:00
|
|
|
*
|
2009-07-27 20:47:19 +00:00
|
|
|
* vim: ts=4 sw=4 noet ai cindent syntax=c
|
|
|
|
*
|
2008-12-09 23:35:49 +00:00
|
|
|
*/
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
#include <sys/dkstat.h>
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/resource.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <sys/sysctl.h>
|
|
|
|
#include <sys/time.h>
|
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/vmmeter.h>
|
|
|
|
#include <sys/user.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/sensors.h>
|
|
|
|
#include <sys/malloc.h>
|
|
|
|
#include <sys/swap.h>
|
|
|
|
#include <kvm.h>
|
|
|
|
|
|
|
|
#include <net/if.h>
|
|
|
|
#include <net/if_media.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
|
|
|
|
#include <err.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <ifaddrs.h>
|
|
|
|
#include <limits.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <machine/apmvar.h>
|
|
|
|
|
|
|
|
#include <net80211/ieee80211.h>
|
|
|
|
#include <net80211/ieee80211_ioctl.h>
|
|
|
|
|
|
|
|
#include "conky.h"
|
2009-06-03 06:16:23 +00:00
|
|
|
#include "diskio.h"
|
|
|
|
#include "logging.h"
|
|
|
|
#include "openbsd.h"
|
|
|
|
#include "top.h"
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
#define MAXSHOWDEVS 16
|
|
|
|
|
|
|
|
#define LOG1024 10
|
|
|
|
#define pagetok(size) ((size) << pageshift)
|
|
|
|
|
|
|
|
inline void proc_find_top(struct process **cpu, struct process **mem);
|
|
|
|
|
|
|
|
static short cpu_setup = 0;
|
|
|
|
static kvm_t *kd = 0;
|
|
|
|
|
|
|
|
struct ifmibdata *data = NULL;
|
|
|
|
size_t len = 0;
|
|
|
|
|
|
|
|
int init_kvm = 0;
|
|
|
|
int init_sensors = 0;
|
|
|
|
|
|
|
|
static int kvm_init()
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (init_kvm) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
kd = kvm_open(NULL, NULL, NULL, KVM_NO_FILES, NULL);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (kd == NULL) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("error opening kvm");
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
|
|
|
init_kvm = 1;
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* note: swapmode taken from 'top' source */
|
2008-02-20 20:30:45 +00:00
|
|
|
/* swapmode is rewritten by Tobias Weingartner <weingart@openbsd.org>
|
|
|
|
* to be based on the new swapctl(2) system call. */
|
|
|
|
static int swapmode(int *used, int *total)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
struct swapent *swdev;
|
|
|
|
int nswap, rnswap, i;
|
|
|
|
|
|
|
|
nswap = swapctl(SWAP_NSWAP, 0, 0);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (nswap == 0) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
swdev = malloc(nswap * sizeof(*swdev));
|
2008-02-20 20:30:45 +00:00
|
|
|
if (swdev == NULL) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
rnswap = swapctl(SWAP_STATS, swdev, nswap);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (rnswap == -1) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
/* if rnswap != nswap, then what? */
|
|
|
|
|
|
|
|
/* Total things up */
|
|
|
|
*total = *used = 0;
|
|
|
|
for (i = 0; i < nswap; i++) {
|
|
|
|
if (swdev[i].se_flags & SWF_ENABLE) {
|
|
|
|
*used += (swdev[i].se_inuse / (1024 / DEV_BSIZE));
|
|
|
|
*total += (swdev[i].se_nblks / (1024 / DEV_BSIZE));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
free(swdev);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
int check_mount(char *s)
|
|
|
|
{
|
|
|
|
/* stub */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_uptime()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
int mib[2] = { CTL_KERN, KERN_BOOTTIME };
|
|
|
|
struct timeval boottime;
|
|
|
|
time_t now;
|
2008-02-20 20:30:45 +00:00
|
|
|
size_t size = sizeof(boottime);
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
|
|
|
|
&& (boottime.tv_sec != 0)) {
|
2007-03-01 01:46:11 +00:00
|
|
|
time(&now);
|
|
|
|
info.uptime = now - boottime.tv_sec;
|
|
|
|
} else {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("Could not get uptime");
|
2007-03-01 01:46:11 +00:00
|
|
|
info.uptime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_meminfo()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
static int mib[2] = { CTL_VM, VM_METER };
|
2007-03-01 01:46:11 +00:00
|
|
|
struct vmtotal vmtotal;
|
|
|
|
size_t size;
|
|
|
|
int pagesize, pageshift, swap_avail, swap_used;
|
|
|
|
|
|
|
|
pagesize = getpagesize();
|
|
|
|
pageshift = 0;
|
|
|
|
while (pagesize > 1) {
|
|
|
|
pageshift++;
|
|
|
|
pagesize >>= 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* we only need the amount of log(2)1024 for our conversion */
|
|
|
|
pageshift -= LOG1024;
|
|
|
|
|
|
|
|
/* get total -- systemwide main memory usage structure */
|
|
|
|
size = sizeof(vmtotal);
|
|
|
|
if (sysctl(mib, 2, &vmtotal, &size, NULL, 0) < 0) {
|
|
|
|
warn("sysctl failed");
|
|
|
|
bzero(&vmtotal, sizeof(vmtotal));
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
info.memmax = pagetok(vmtotal.t_rm) + pagetok(vmtotal.t_free);
|
2007-03-01 01:46:11 +00:00
|
|
|
info.mem = pagetok(vmtotal.t_rm);
|
2008-06-21 20:37:58 +00:00
|
|
|
info.memeasyfree = info.memfree = info.memmax - info.mem;
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
if ((swapmode(&swap_used, &swap_avail)) >= 0) {
|
|
|
|
info.swapmax = swap_avail;
|
|
|
|
info.swap = swap_used;
|
2009-07-09 18:12:52 +00:00
|
|
|
info.swapfree = swap_avail - swap_used;
|
2007-03-01 01:46:11 +00:00
|
|
|
} else {
|
|
|
|
info.swapmax = 0;
|
|
|
|
info.swap = 0;
|
2009-07-09 18:12:52 +00:00
|
|
|
info.swapfree = 0;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_net_stats()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
struct net_stat *ns;
|
|
|
|
double delta;
|
|
|
|
long long r, t, last_recv, last_trans;
|
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct if_data *ifd;
|
|
|
|
|
|
|
|
/* get delta */
|
|
|
|
delta = current_update_time - last_update_time;
|
2009-06-06 16:14:06 +00:00
|
|
|
if (delta <= 0.0001) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (getifaddrs(&ifap) < 0) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
2009-07-16 18:28:23 +00:00
|
|
|
ns = get_net_stat((const char *) ifa->ifa_name, NULL, NULL);
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
if (ifa->ifa_flags & IFF_UP) {
|
|
|
|
struct ifaddrs *iftmp;
|
|
|
|
|
|
|
|
ns->up = 1;
|
|
|
|
last_recv = ns->recv;
|
|
|
|
last_trans = ns->trans;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ifa->ifa_addr->sa_family != AF_LINK) {
|
2007-03-01 01:46:11 +00:00
|
|
|
continue;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (iftmp = ifa->ifa_next;
|
|
|
|
iftmp != NULL && strcmp(ifa->ifa_name, iftmp->ifa_name) == 0;
|
|
|
|
iftmp = iftmp->ifa_next) {
|
|
|
|
if (iftmp->ifa_addr->sa_family == AF_INET) {
|
2007-03-01 01:46:11 +00:00
|
|
|
memcpy(&(ns->addr), iftmp->ifa_addr,
|
|
|
|
iftmp->ifa_addr->sa_len);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
ifd = (struct if_data *) ifa->ifa_data;
|
|
|
|
r = ifd->ifi_ibytes;
|
|
|
|
t = ifd->ifi_obytes;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (r < ns->last_read_recv) {
|
|
|
|
ns->recv += ((long long) 4294967295U - ns->last_read_recv) + r;
|
|
|
|
} else {
|
2007-03-01 01:46:11 +00:00
|
|
|
ns->recv += (r - ns->last_read_recv);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
ns->last_read_recv = r;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (t < ns->last_read_trans) {
|
|
|
|
ns->trans += (long long) 4294967295U - ns->last_read_trans + t;
|
|
|
|
} else {
|
2007-03-01 01:46:11 +00:00
|
|
|
ns->trans += (t - ns->last_read_trans);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
ns->last_read_trans = t;
|
|
|
|
|
|
|
|
/* calculate speeds */
|
|
|
|
ns->recv_speed = (ns->recv - last_recv) / delta;
|
|
|
|
ns->trans_speed = (ns->trans - last_trans) / delta;
|
|
|
|
} else {
|
|
|
|
ns->up = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
freeifaddrs(ifap);
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_total_processes()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
int n_processes;
|
|
|
|
|
|
|
|
kvm_init();
|
2008-02-20 20:30:45 +00:00
|
|
|
kvm_getprocs(kd, KERN_PROC_ALL, 0, &n_processes);
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
info.procs = n_processes;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_running_processes()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
struct kinfo_proc2 *p;
|
|
|
|
int n_processes;
|
|
|
|
int i, cnt = 0;
|
|
|
|
|
|
|
|
kvm_init();
|
|
|
|
int max_size = sizeof(struct kinfo_proc2);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-03-01 01:46:11 +00:00
|
|
|
p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
|
|
|
|
for (i = 0; i < n_processes; i++) {
|
2008-02-20 20:30:45 +00:00
|
|
|
if (p[i].p_stat == SRUN) {
|
2007-03-01 01:46:11 +00:00
|
|
|
cnt++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info.run_procs = cnt;
|
|
|
|
}
|
|
|
|
|
2007-11-04 11:20:55 +00:00
|
|
|
/* new SMP code can be enabled by commenting the following line */
|
|
|
|
#define OLDCPU
|
|
|
|
|
2007-11-04 13:14:55 +00:00
|
|
|
#ifdef OLDCPU
|
2007-03-01 01:46:11 +00:00
|
|
|
struct cpu_load_struct {
|
|
|
|
unsigned long load[5];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cpu_load_struct fresh = { {0, 0, 0, 0, 0} };
|
|
|
|
long cpu_used, oldtotal, oldused;
|
2007-11-04 11:20:55 +00:00
|
|
|
#else
|
2007-11-04 13:14:55 +00:00
|
|
|
#include <assert.h>
|
2008-02-20 20:30:45 +00:00
|
|
|
int64_t *fresh = NULL;
|
|
|
|
|
|
|
|
/* XXX is 8 enough? - What's the constant for MAXCPU? */
|
|
|
|
/* allocate this with malloc would be better */
|
2007-11-04 11:20:55 +00:00
|
|
|
int64_t oldtotal[8], oldused[8];
|
|
|
|
#endif
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void get_cpu_count()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
int cpu_count = 1; /* default to 1 cpu */
|
2007-11-04 11:20:55 +00:00
|
|
|
#ifndef OLDCPU
|
2007-03-01 01:46:11 +00:00
|
|
|
int mib[2] = { CTL_HW, HW_NCPU };
|
|
|
|
size_t len = sizeof(cpu_count);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (sysctl(mib, 2, &cpu_count, &len, NULL, 0) != 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("error getting cpu count, defaulting to 1");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
#endif
|
2007-11-04 13:14:55 +00:00
|
|
|
info.cpu_count = cpu_count;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
info.cpu_usage = malloc(info.cpu_count * sizeof(float));
|
|
|
|
if (info.cpu_usage == NULL) {
|
2009-07-23 18:30:53 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "malloc");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-11-04 11:20:55 +00:00
|
|
|
|
|
|
|
#ifndef OLDCPU
|
2008-02-20 20:30:45 +00:00
|
|
|
assert(fresh == NULL); /* XXX Is this leaking memory? */
|
|
|
|
/* XXX Where shall I free this? */
|
|
|
|
if (NULL == (fresh = calloc(cpu_count, sizeof(int64_t) * CPUSTATES))) {
|
2009-07-23 18:30:53 +00:00
|
|
|
CRIT_ERR(NULL, NULL, "calloc");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-11-04 11:20:55 +00:00
|
|
|
#endif
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_cpu_usage()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2007-11-04 11:20:55 +00:00
|
|
|
#ifdef OLDCPU
|
2007-03-01 01:46:11 +00:00
|
|
|
int mib[2] = { CTL_KERN, KERN_CPTIME };
|
|
|
|
long used, total;
|
|
|
|
long cp_time[CPUSTATES];
|
2008-02-20 20:30:45 +00:00
|
|
|
size_t len = sizeof(cp_time);
|
2007-11-04 11:20:55 +00:00
|
|
|
#else
|
|
|
|
size_t size;
|
|
|
|
unsigned int i;
|
|
|
|
#endif
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
/* add check for !info.cpu_usage since that mem is freed on a SIGUSR1 */
|
|
|
|
if ((cpu_setup == 0) || (!info.cpu_usage)) {
|
|
|
|
get_cpu_count();
|
|
|
|
cpu_setup = 1;
|
|
|
|
}
|
|
|
|
|
2007-11-04 11:20:55 +00:00
|
|
|
#ifdef OLDCPU
|
2007-03-01 01:46:11 +00:00
|
|
|
if (sysctl(mib, 2, &cp_time, &len, NULL, 0) < 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("Cannot get kern.cp_time");
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fresh.load[0] = cp_time[CP_USER];
|
|
|
|
fresh.load[1] = cp_time[CP_NICE];
|
|
|
|
fresh.load[2] = cp_time[CP_SYS];
|
|
|
|
fresh.load[3] = cp_time[CP_IDLE];
|
|
|
|
fresh.load[4] = cp_time[CP_IDLE];
|
|
|
|
|
|
|
|
used = fresh.load[0] + fresh.load[1] + fresh.load[2];
|
2008-02-20 20:30:45 +00:00
|
|
|
total = fresh.load[0] + fresh.load[1] + fresh.load[2] + fresh.load[3];
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
if ((total - oldtotal) != 0) {
|
|
|
|
info.cpu_usage[0] = ((double) (used - oldused)) /
|
|
|
|
(double) (total - oldtotal);
|
|
|
|
} else {
|
|
|
|
info.cpu_usage[0] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldused = used;
|
|
|
|
oldtotal = total;
|
2007-11-04 11:20:55 +00:00
|
|
|
#else
|
|
|
|
if (info.cpu_count > 1) {
|
|
|
|
size = CPUSTATES * sizeof(int64_t);
|
|
|
|
for (i = 0; i < info.cpu_count; i++) {
|
2008-02-20 20:30:45 +00:00
|
|
|
int cp_time_mib[] = { CTL_KERN, KERN_CPTIME2, i };
|
|
|
|
if (sysctl(cp_time_mib, 3, &(fresh[i * CPUSTATES]), &size, NULL, 0)
|
|
|
|
< 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("sysctl kern.cp_time2 failed");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-11-04 11:20:55 +00:00
|
|
|
}
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
int cp_time_mib[] = { CTL_KERN, KERN_CPTIME };
|
2007-11-04 11:20:55 +00:00
|
|
|
long cp_time_tmp[CPUSTATES];
|
|
|
|
|
|
|
|
size = sizeof(cp_time_tmp);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (sysctl(cp_time_mib, 2, cp_time_tmp, &size, NULL, 0) < 0) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("sysctl kern.cp_time failed");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-11-04 11:20:55 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (i = 0; i < CPUSTATES; i++) {
|
|
|
|
fresh[i] = (int64_t) cp_time_tmp[i];
|
|
|
|
}
|
2007-11-04 11:20:55 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* XXX Do sg with this int64_t => long => double ? float hell. */
|
2007-11-04 11:20:55 +00:00
|
|
|
for (i = 0; i < info.cpu_count; i++) {
|
|
|
|
int64_t used, total;
|
|
|
|
int at = i * CPUSTATES;
|
|
|
|
|
|
|
|
used = fresh[at + CP_USER] + fresh[at + CP_NICE] + fresh[at + CP_SYS];
|
|
|
|
total = used + fresh[at + CP_IDLE];
|
|
|
|
|
|
|
|
if ((total - oldtotal[i]) != 0) {
|
|
|
|
info.cpu_usage[i] = ((double) (used - oldused[i])) /
|
2008-02-20 20:30:45 +00:00
|
|
|
(double) (total - oldtotal[i]);
|
2007-11-04 11:20:55 +00:00
|
|
|
} else {
|
|
|
|
info.cpu_usage[i] = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
oldused[i] = used;
|
|
|
|
oldtotal[i] = total;
|
|
|
|
}
|
|
|
|
#endif
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_load_average()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
double v[3];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-03-01 01:46:11 +00:00
|
|
|
getloadavg(v, 3);
|
|
|
|
|
|
|
|
info.loadavg[0] = (float) v[0];
|
|
|
|
info.loadavg[1] = (float) v[1];
|
|
|
|
info.loadavg[2] = (float) v[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/* read sensors from sysctl */
|
|
|
|
void update_obsd_sensors()
|
|
|
|
{
|
|
|
|
int sensor_cnt, dev, numt, mib[5] = { CTL_HW, HW_SENSORS, 0, 0, 0 };
|
|
|
|
struct sensor sensor;
|
|
|
|
struct sensordev sensordev;
|
2008-02-20 20:30:45 +00:00
|
|
|
size_t slen, sdlen;
|
2007-03-01 01:46:11 +00:00
|
|
|
enum sensor_type type;
|
|
|
|
|
|
|
|
slen = sizeof(sensor);
|
|
|
|
sdlen = sizeof(sensordev);
|
|
|
|
|
|
|
|
sensor_cnt = 0;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
dev = obsd_sensors.device; // FIXME: read more than one device
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
/* for (dev = 0; dev < MAXSENSORDEVICES; dev++) { */
|
|
|
|
mib[2] = dev;
|
2008-02-20 20:30:45 +00:00
|
|
|
if (sysctl(mib, 3, &sensordev, &sdlen, NULL, 0) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2007-03-01 01:46:11 +00:00
|
|
|
warn("sysctl");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
// continue;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
for (type = 0; type < SENSOR_MAX_TYPES; type++) {
|
|
|
|
mib[3] = type;
|
|
|
|
for (numt = 0; numt < sensordev.maxnumt[type]; numt++) {
|
|
|
|
mib[4] = numt;
|
2008-02-20 20:30:45 +00:00
|
|
|
if (sysctl(mib, 5, &sensor, &slen, NULL, 0) == -1) {
|
|
|
|
if (errno != ENOENT) {
|
2007-03-01 01:46:11 +00:00
|
|
|
warn("sysctl");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
continue;
|
|
|
|
}
|
2008-02-20 20:30:45 +00:00
|
|
|
if (sensor.flags & SENSOR_FINVALID) {
|
2007-03-01 01:46:11 +00:00
|
|
|
continue;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
switch (type) {
|
|
|
|
case SENSOR_TEMP:
|
2008-02-20 20:30:45 +00:00
|
|
|
obsd_sensors.temp[dev][sensor.numt] =
|
|
|
|
(sensor.value - 273150000) / 1000000.0;
|
2007-03-01 01:46:11 +00:00
|
|
|
break;
|
|
|
|
case SENSOR_FANRPM:
|
|
|
|
obsd_sensors.fan[dev][sensor.numt] = sensor.value;
|
|
|
|
break;
|
|
|
|
case SENSOR_VOLTS_DC:
|
2008-02-20 20:30:45 +00:00
|
|
|
obsd_sensors.volt[dev][sensor.numt] =
|
|
|
|
sensor.value / 1000000.0;
|
2007-03-01 01:46:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
sensor_cnt++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* } */
|
|
|
|
|
|
|
|
init_sensors = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* chipset vendor */
|
|
|
|
void get_obsd_vendor(char *buf, size_t client_buffer_size)
|
|
|
|
{
|
|
|
|
int mib[2];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-03-01 01:46:11 +00:00
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_VENDOR;
|
|
|
|
char vendor[64];
|
|
|
|
size_t size = sizeof(vendor);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (sysctl(mib, 2, vendor, &size, NULL, 0) == -1) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("error reading vendor");
|
2007-03-01 01:46:11 +00:00
|
|
|
snprintf(buf, client_buffer_size, "unknown");
|
|
|
|
} else {
|
|
|
|
snprintf(buf, client_buffer_size, "%s", vendor);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* chipset name */
|
|
|
|
void get_obsd_product(char *buf, size_t client_buffer_size)
|
|
|
|
{
|
|
|
|
int mib[2];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-03-01 01:46:11 +00:00
|
|
|
mib[0] = CTL_HW;
|
|
|
|
mib[1] = HW_PRODUCT;
|
|
|
|
char product[64];
|
|
|
|
size_t size = sizeof(product);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (sysctl(mib, 2, product, &size, NULL, 0) == -1) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("error reading product");
|
2007-03-01 01:46:11 +00:00
|
|
|
snprintf(buf, client_buffer_size, "unknown");
|
|
|
|
} else {
|
|
|
|
snprintf(buf, client_buffer_size, "%s", product);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* rdtsc() and get_freq_dynamic() copied from linux.c */
|
|
|
|
|
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2008-02-20 20:30:45 +00:00
|
|
|
__inline__ unsigned long long int rdtsc()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
unsigned long long int x;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2007-03-01 01:46:11 +00:00
|
|
|
__asm__ volatile(".byte 0x0f, 0x31":"=A" (x));
|
2008-02-20 20:30:45 +00:00
|
|
|
return x;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* return system frequency in MHz (use divisor=1) or GHz (use divisor=1000) */
|
2008-02-20 20:30:45 +00:00
|
|
|
void get_freq_dynamic(char *p_client_buffer, size_t client_buffer_size,
|
2008-03-29 03:45:36 +00:00
|
|
|
const char *p_format, int divisor)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
#if defined(__i386) || defined(__x86_64)
|
2007-03-01 01:46:11 +00:00
|
|
|
struct timezone tz;
|
|
|
|
struct timeval tvstart, tvstop;
|
|
|
|
unsigned long long cycles[2]; /* gotta be 64 bit */
|
|
|
|
unsigned int microseconds; /* total time taken */
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
memset(&tz, 0, sizeof(tz));
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
/* get this function in cached memory */
|
|
|
|
gettimeofday(&tvstart, &tz);
|
|
|
|
cycles[0] = rdtsc();
|
|
|
|
gettimeofday(&tvstart, &tz);
|
|
|
|
|
|
|
|
/* we don't trust that this is any specific length of time */
|
|
|
|
usleep(100);
|
|
|
|
cycles[1] = rdtsc();
|
|
|
|
gettimeofday(&tvstop, &tz);
|
|
|
|
microseconds = ((tvstop.tv_sec - tvstart.tv_sec) * 1000000) +
|
|
|
|
(tvstop.tv_usec - tvstart.tv_usec);
|
|
|
|
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
2008-02-20 20:30:45 +00:00
|
|
|
(float) ((cycles[1] - cycles[0]) / microseconds) / divisor);
|
2007-03-01 01:46:11 +00:00
|
|
|
#else
|
2007-09-17 04:40:13 +00:00
|
|
|
get_freq(p_client_buffer, client_buffer_size, p_format, divisor, 1);
|
2007-03-01 01:46:11 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* void */
|
2008-03-29 03:45:36 +00:00
|
|
|
char get_freq(char *p_client_buffer, size_t client_buffer_size,
|
|
|
|
const char *p_format, int divisor, unsigned int cpu)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
int freq = cpu;
|
|
|
|
int mib[2] = { CTL_HW, HW_CPUSPEED };
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (!p_client_buffer || client_buffer_size <= 0 || !p_format
|
|
|
|
|| divisor <= 0) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
size_t size = sizeof(freq);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
|
|
|
if (sysctl(mib, 2, &freq, &size, NULL, 0) == 0) {
|
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format,
|
|
|
|
(float) freq / divisor);
|
|
|
|
} else {
|
2007-03-01 01:46:11 +00:00
|
|
|
snprintf(p_client_buffer, client_buffer_size, p_format, 0.0f);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_top()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-10-08 11:41:16 +00:00
|
|
|
kvm_init();
|
2007-03-01 01:46:11 +00:00
|
|
|
proc_find_top(info.cpu, info.memu);
|
|
|
|
}
|
|
|
|
|
2007-08-10 20:51:36 +00:00
|
|
|
#if 0
|
|
|
|
/* deprecated, will rewrite this soon in update_net_stats() -hifi */
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_wifi_stats()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
struct net_stat *ns;
|
2007-03-01 01:46:11 +00:00
|
|
|
struct ifaddrs *ifap, *ifa;
|
|
|
|
struct ifmediareq ifmr;
|
|
|
|
struct ieee80211_nodereq nr;
|
|
|
|
struct ieee80211_bssid bssid;
|
2008-02-20 20:30:45 +00:00
|
|
|
int s, ibssid;
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* Get iface table */
|
|
|
|
if (getifaddrs(&ifap) < 0) {
|
2007-03-01 01:46:11 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
|
|
|
|
ns = get_net_stat((const char *) ifa->ifa_name);
|
|
|
|
|
|
|
|
s = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
|
|
|
|
|
|
|
/* Get media type */
|
|
|
|
bzero(&ifmr, sizeof(ifmr));
|
|
|
|
strlcpy(ifmr.ifm_name, ifa->ifa_name, IFNAMSIZ);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ioctl(s, SIOCGIFMEDIA, (caddr_t) &ifmr) < 0) {
|
2008-04-10 22:45:45 +00:00
|
|
|
close(s);
|
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* We can monitor only wireless interfaces
|
|
|
|
* which are not in hostap mode */
|
|
|
|
if ((ifmr.ifm_active & IFM_IEEE80211)
|
|
|
|
&& !(ifmr.ifm_active & IFM_IEEE80211_HOSTAP)) {
|
2007-03-01 01:46:11 +00:00
|
|
|
/* Get wi status */
|
|
|
|
|
|
|
|
memset(&bssid, 0, sizeof(bssid));
|
|
|
|
strlcpy(bssid.i_name, ifa->ifa_name, sizeof(bssid.i_name));
|
|
|
|
ibssid = ioctl(s, SIOCG80211BSSID, &bssid);
|
|
|
|
|
|
|
|
bzero(&nr, sizeof(nr));
|
|
|
|
bcopy(bssid.i_bssid, &nr.nr_macaddr, sizeof(nr.nr_macaddr));
|
|
|
|
strlcpy(nr.nr_ifname, ifa->ifa_name, sizeof(nr.nr_ifname));
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ioctl(s, SIOCG80211NODE, &nr) == 0 && nr.nr_rssi) {
|
2007-05-06 13:00:35 +00:00
|
|
|
ns->linkstatus = nr.nr_rssi;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
cleanup:
|
|
|
|
close(s);
|
|
|
|
}
|
|
|
|
}
|
2007-08-10 20:51:36 +00:00
|
|
|
#endif
|
|
|
|
|
2008-10-08 11:41:16 +00:00
|
|
|
void clear_diskio_stats()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2009-06-03 06:16:23 +00:00
|
|
|
struct diskio_stat *prepare_diskio_stat(const char *s)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_diskio()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
return; /* XXX: implement? hifi: not sure how */
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* While topless is obviously better, top is also not bad. */
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int comparecpu(const void *a, const void *b)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (((struct process *) a)->amount > ((struct process *) b)->amount) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (((struct process *) a)->amount < ((struct process *) b)->amount) {
|
|
|
|
return 1;
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return 0;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int comparemem(const void *a, const void *b)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (((struct process *) a)->totalmem > ((struct process *) b)->totalmem) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (((struct process *) a)->totalmem < ((struct process *) b)->totalmem) {
|
|
|
|
return 1;
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return 0;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
inline void proc_find_top(struct process **cpu, struct process **mem)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
struct kinfo_proc2 *p;
|
|
|
|
int n_processes;
|
|
|
|
int i, j = 0;
|
|
|
|
struct process *processes;
|
|
|
|
int mib[2];
|
|
|
|
|
2008-10-08 11:41:16 +00:00
|
|
|
u_int total_pages;
|
|
|
|
int64_t usermem;
|
2007-03-01 01:46:11 +00:00
|
|
|
int pagesize = getpagesize();
|
|
|
|
|
|
|
|
/* we get total pages count again to be sure it is up to date */
|
|
|
|
mib[0] = CTL_HW;
|
2008-10-08 11:41:16 +00:00
|
|
|
mib[1] = HW_USERMEM64;
|
|
|
|
size_t size = sizeof(usermem);
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2008-10-08 11:41:16 +00:00
|
|
|
if (sysctl(mib, 2, &usermem, &size, NULL, 0) == -1) {
|
2009-08-01 18:45:43 +00:00
|
|
|
NORM_ERR("error reading usermem");
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-10-08 11:41:16 +00:00
|
|
|
/* translate bytes into page count */
|
|
|
|
total_pages = usermem / pagesize;
|
|
|
|
|
2007-03-01 01:46:11 +00:00
|
|
|
int max_size = sizeof(struct kinfo_proc2);
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
p = kvm_getproc2(kd, KERN_PROC_ALL, 0, max_size, &n_processes);
|
|
|
|
processes = malloc(n_processes * sizeof(struct process));
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
for (i = 0; i < n_processes; i++) {
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!((p[i].p_flag & P_SYSTEM)) && p[i].p_comm != NULL) {
|
2007-03-01 01:46:11 +00:00
|
|
|
processes[j].pid = p[i].p_pid;
|
2008-04-02 18:44:49 +00:00
|
|
|
processes[j].name = strndup(p[i].p_comm, text_buffer_size);
|
2008-02-20 20:30:45 +00:00
|
|
|
processes[j].amount = 100.0 * p[i].p_pctcpu / FSCALE;
|
2008-10-08 11:41:16 +00:00
|
|
|
processes[j].totalmem = (float) (p[i].p_vm_rssize /
|
|
|
|
(float) total_pages) * 100.0;
|
2007-03-01 01:46:11 +00:00
|
|
|
j++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
qsort(processes, j - 1, sizeof(struct process), comparemem);
|
2007-03-01 01:46:11 +00:00
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
struct process *tmp, *ttmp;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
tmp = malloc(sizeof(struct process));
|
2007-03-01 01:46:11 +00:00
|
|
|
tmp->pid = processes[i].pid;
|
|
|
|
tmp->amount = processes[i].amount;
|
|
|
|
tmp->totalmem = processes[i].totalmem;
|
2008-04-02 18:44:49 +00:00
|
|
|
tmp->name = strndup(processes[i].name, text_buffer_size);
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
ttmp = mem[i];
|
|
|
|
mem[i] = tmp;
|
|
|
|
if (ttmp != NULL) {
|
|
|
|
free(ttmp->name);
|
|
|
|
free(ttmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
qsort(processes, j - 1, sizeof(struct process), comparecpu);
|
2007-03-01 01:46:11 +00:00
|
|
|
for (i = 0; i < 10; i++) {
|
|
|
|
struct process *tmp, *ttmp;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
tmp = malloc(sizeof(struct process));
|
2007-03-01 01:46:11 +00:00
|
|
|
tmp->pid = processes[i].pid;
|
|
|
|
tmp->amount = processes[i].amount;
|
|
|
|
tmp->totalmem = processes[i].totalmem;
|
2008-04-02 18:44:49 +00:00
|
|
|
tmp->name = strndup(processes[i].name, text_buffer_size);
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
ttmp = cpu[i];
|
|
|
|
cpu[i] = tmp;
|
|
|
|
if (ttmp != NULL) {
|
|
|
|
free(ttmp->name);
|
|
|
|
free(ttmp);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
for (i = 0; i < j; i++) {
|
|
|
|
free(processes[i].name);
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
free(processes);
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
#if defined(i386) || defined(__i386__)
|
2007-03-01 01:46:11 +00:00
|
|
|
#define APMDEV "/dev/apm"
|
|
|
|
#define APM_UNKNOWN 255
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int apm_getinfo(int fd, apm_info_t aip)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ioctl(fd, APM_IOC_GETPOWER, aip) == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return 0;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
char *get_apm_adapter()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
struct apm_power_info info;
|
2007-05-06 09:32:21 +00:00
|
|
|
char *out;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
out = (char *) calloc(16, sizeof(char));
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
fd = open(APMDEV, O_RDONLY);
|
2007-05-06 09:32:21 +00:00
|
|
|
if (fd < 0) {
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-05-06 09:32:21 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
if (apm_getinfo(fd, &info) != 0) {
|
|
|
|
close(fd);
|
2007-05-06 09:32:21 +00:00
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
switch (info.ac_state) {
|
|
|
|
case APM_AC_OFF:
|
2007-05-06 09:32:21 +00:00
|
|
|
strncpy(out, "off-line", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
break;
|
|
|
|
case APM_AC_ON:
|
2007-05-06 09:32:21 +00:00
|
|
|
if (info.battery_state == APM_BATT_CHARGING) {
|
|
|
|
strncpy(out, "charging", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-05-06 09:32:21 +00:00
|
|
|
} else {
|
|
|
|
strncpy(out, "on-line", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-05-06 09:32:21 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
break;
|
|
|
|
default:
|
2007-05-06 09:32:21 +00:00
|
|
|
strncpy(out, "unknown", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
char *get_apm_battery_life()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
u_int batt_life;
|
|
|
|
struct apm_power_info info;
|
|
|
|
char *out;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
out = (char *) calloc(16, sizeof(char));
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
fd = open(APMDEV, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (apm_getinfo(fd, &info) != 0) {
|
|
|
|
close(fd);
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
batt_life = info.battery_life;
|
|
|
|
if (batt_life <= 100) {
|
|
|
|
snprintf(out, 16, "%d%%", batt_life);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
|
|
|
} else {
|
2007-03-01 01:46:11 +00:00
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2007-03-01 01:46:11 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
char *get_apm_battery_time()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
int fd;
|
|
|
|
int batt_time;
|
|
|
|
int h, m;
|
|
|
|
struct apm_power_info info;
|
|
|
|
char *out;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
out = (char *) calloc(16, sizeof(char));
|
2007-03-01 01:46:11 +00:00
|
|
|
|
|
|
|
fd = open(APMDEV, O_RDONLY);
|
|
|
|
if (fd < 0) {
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (apm_getinfo(fd, &info) != 0) {
|
|
|
|
close(fd);
|
|
|
|
strncpy(out, "ERR", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
close(fd);
|
|
|
|
|
|
|
|
batt_time = info.minutes_left;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (batt_time == -1) {
|
2007-03-01 01:46:11 +00:00
|
|
|
strncpy(out, "unknown", 16);
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2007-03-01 01:46:11 +00:00
|
|
|
h = batt_time / 60;
|
|
|
|
m = batt_time % 60;
|
|
|
|
snprintf(out, 16, "%2d:%02d", h, m);
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
return out;
|
2007-03-01 01:46:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* empty stubs so conky links */
|
2008-02-20 20:30:45 +00:00
|
|
|
void prepare_update()
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_entropy(void)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void free_all_processes(void)
|
2007-03-01 01:46:11 +00:00
|
|
|
{
|
|
|
|
}
|