2008-02-20 20:30:45 +00:00
|
|
|
/* Conky, a system monitor, based on torsmo
|
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) 2004, Hannu Saransaari and Lauri Hakkarainen
|
2008-03-31 04:56:39 +00:00
|
|
|
* Copyright (c) 2005-2008 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
|
|
|
*
|
2008-02-20 20:30:45 +00:00
|
|
|
* $Id$ */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-06-14 18:41:12 +00:00
|
|
|
#include "netbsd.h"
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
static kvm_t *kd = NULL;
|
|
|
|
int kd_init = 0, nkd_init = 0;
|
|
|
|
u_int32_t sensvalue;
|
|
|
|
char errbuf[_POSIX2_LINE_MAX];
|
|
|
|
|
|
|
|
static int init_kvm(void)
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (kd_init) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
kd = kvm_openfiles(NULL, NULL, NULL, KVM_NO_FILES, errbuf);
|
|
|
|
if (kd == NULL) {
|
2008-02-20 20:30:45 +00:00
|
|
|
warnx("cannot kvm_openfiles: %s", errbuf);
|
2005-07-20 00:30:40 +00:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
kd_init = 1;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int swapmode(int *retavail, int *retfree)
|
|
|
|
{
|
|
|
|
int n;
|
|
|
|
struct swapent *sep;
|
|
|
|
|
|
|
|
*retavail = 0;
|
|
|
|
*retfree = 0;
|
|
|
|
|
|
|
|
n = swapctl(SWAP_NSWAP, 0, 0);
|
|
|
|
|
|
|
|
if (n < 1) {
|
2008-02-20 20:30:45 +00:00
|
|
|
warn("could not get swap information");
|
2005-07-20 00:30:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
sep = (struct swapent *) malloc(n * (sizeof(*sep)));
|
|
|
|
|
|
|
|
if (sep == NULL) {
|
2008-02-20 20:30:45 +00:00
|
|
|
warn("memory allocation failed");
|
2005-07-20 00:30:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (swapctl(SWAP_STATS, (void *) sep, n) < n) {
|
2008-02-20 20:30:45 +00:00
|
|
|
warn("could not get swap stats");
|
2005-07-20 00:30:40 +00:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
for (; n > 0; n--) {
|
|
|
|
*retavail += (int) dbtob(sep[n - 1].se_nblks);
|
2008-02-20 20:30:45 +00:00
|
|
|
*retfree += (int) dbtob(sep[n - 1].se_nblks - sep[n - 1].se_inuse);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
*retavail = (int) (*retavail / 1024);
|
|
|
|
*retfree = (int) (*retfree / 1024);
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void prepare_update()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_uptime()
|
|
|
|
{
|
|
|
|
int mib[2] = { CTL_KERN, KERN_BOOTTIME };
|
|
|
|
struct timeval boottime;
|
|
|
|
time_t now;
|
|
|
|
int size = sizeof(boottime);
|
|
|
|
|
|
|
|
if ((sysctl(mib, 2, &boottime, &size, NULL, 0) != -1)
|
2008-02-20 20:30:45 +00:00
|
|
|
&& (boottime.tv_sec != 0)) {
|
|
|
|
time(&now);
|
2005-07-20 00:30:40 +00:00
|
|
|
info.uptime = now - boottime.tv_sec;
|
|
|
|
} else {
|
2008-02-20 20:30:45 +00:00
|
|
|
warn("could not get uptime");
|
2005-07-20 00:30:40 +00:00
|
|
|
info.uptime = 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-08-05 04:47:21 +00:00
|
|
|
int check_mount(char *s)
|
|
|
|
{
|
|
|
|
/* stub */
|
|
|
|
return 0;
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
void update_meminfo()
|
|
|
|
{
|
|
|
|
int mib[] = { CTL_VM, VM_UVMEXP2 };
|
|
|
|
int total_pages, inactive_pages, free_pages;
|
|
|
|
int swap_avail, swap_free;
|
|
|
|
const int pagesize = getpagesize();
|
|
|
|
struct uvmexp_sysctl uvmexp;
|
|
|
|
size_t size = sizeof(uvmexp);
|
|
|
|
|
|
|
|
if (sysctl(mib, 2, &uvmexp, &size, NULL, 0) < 0) {
|
|
|
|
warn("could not get memory info");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
total_pages = uvmexp.npages;
|
|
|
|
free_pages = uvmexp.free;
|
|
|
|
inactive_pages = uvmexp.inactive;
|
|
|
|
|
|
|
|
info.memmax = (total_pages * pagesize) >> 10;
|
2008-02-20 20:30:45 +00:00
|
|
|
info.mem = ((total_pages - free_pages - inactive_pages) * pagesize) >> 10;
|
2008-06-21 20:37:58 +00:00
|
|
|
info.memeasyfree = info.memfree = info.memmax - info.mem;
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
if (swapmode(&swap_avail, &swap_free) >= 0) {
|
|
|
|
info.swapmax = swap_avail;
|
|
|
|
info.swap = (swap_avail - swap_free);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_net_stats()
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
double delta;
|
|
|
|
struct ifnet ifnet;
|
|
|
|
struct ifnet_head ifhead; /* interfaces are in a tail queue */
|
|
|
|
u_long ifnetaddr;
|
|
|
|
static struct nlist namelist[] = {
|
2008-02-20 20:30:45 +00:00
|
|
|
{ "_ifnet" },
|
|
|
|
{ NULL }
|
2005-07-20 00:30:40 +00:00
|
|
|
};
|
|
|
|
static kvm_t *nkd;
|
|
|
|
|
|
|
|
if (!nkd_init) {
|
|
|
|
nkd = kvm_openfiles(NULL, NULL, NULL, O_RDONLY, errbuf);
|
|
|
|
if (nkd == NULL) {
|
2008-02-20 20:30:45 +00:00
|
|
|
warnx("cannot kvm_openfiles: %s", errbuf);
|
|
|
|
warnx("maybe you need to setgid kmem this program?");
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
|
|
|
} else if (kvm_nlist(nkd, namelist) != 0) {
|
2008-02-20 20:30:45 +00:00
|
|
|
warn("cannot kvm_nlist");
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
nkd_init = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if (kvm_read(nkd, (u_long) namelist[0].n_value, (void *) &ifhead,
|
2008-02-20 20:30:45 +00:00
|
|
|
sizeof(ifhead)) < 0) {
|
|
|
|
warn("cannot kvm_read");
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get delta */
|
|
|
|
delta = current_update_time - last_update_time;
|
2008-02-20 20:30:45 +00:00
|
|
|
if (delta <= 0.0001) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
for (i = 0, ifnetaddr = (u_long) ifhead.tqh_first;
|
2008-02-20 20:30:45 +00:00
|
|
|
ifnet.if_list.tqe_next && i < 16;
|
|
|
|
ifnetaddr = (u_long) ifnet.if_list.tqe_next, i++) {
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
struct net_stat *ns;
|
|
|
|
long long last_recv, last_trans;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
kvm_read(nkd, (u_long) ifnetaddr, (void *) &ifnet, sizeof(ifnet));
|
2005-07-20 00:30:40 +00:00
|
|
|
ns = get_net_stat(ifnet.if_xname);
|
|
|
|
ns->up = 1;
|
|
|
|
last_recv = ns->recv;
|
|
|
|
last_trans = ns->trans;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ifnet.if_ibytes < ns->last_read_recv) {
|
|
|
|
ns->recv += ((long long) 4294967295U - ns->last_read_recv) +
|
|
|
|
ifnet.if_ibytes;
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
ns->last_read_recv = ifnet.if_ibytes;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (ifnet.if_obytes < ns->last_read_trans) {
|
|
|
|
ns->trans += ((long long) 4294967295U - ns->last_read_trans) +
|
|
|
|
ifnet.if_obytes;
|
|
|
|
} else {
|
|
|
|
ns->trans += (ifnet.if_obytes - ns->last_read_trans);
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
ns->last_read_trans = ifnet.if_obytes;
|
|
|
|
|
|
|
|
ns->recv += (ifnet.if_ibytes - ns->last_read_recv);
|
|
|
|
ns->last_read_recv = ifnet.if_ibytes;
|
|
|
|
ns->trans += (ifnet.if_obytes - ns->last_read_trans);
|
|
|
|
ns->last_read_trans = ifnet.if_obytes;
|
|
|
|
|
|
|
|
ns->recv_speed = (ns->recv - last_recv) / delta;
|
|
|
|
ns->trans_speed = (ns->trans - last_trans) / delta;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_total_processes()
|
|
|
|
{
|
|
|
|
/* It's easier to use kvm here than sysctl */
|
|
|
|
|
|
|
|
int n_processes;
|
|
|
|
|
|
|
|
info.procs = 0;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (init_kvm() < 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
|
|
|
kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
|
|
|
|
&n_processes);
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
info.procs = n_processes;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_running_processes()
|
|
|
|
{
|
|
|
|
struct kinfo_proc2 *p;
|
|
|
|
int n_processes;
|
|
|
|
int i, cnt = 0;
|
|
|
|
|
|
|
|
info.run_procs = 0;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (init_kvm() < 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
} else {
|
|
|
|
p = kvm_getproc2(kd, KERN_PROC_ALL, 0, sizeof(struct kinfo_proc2),
|
|
|
|
&n_processes);
|
|
|
|
for (i = 0; i < n_processes; i++) {
|
|
|
|
if (p[i].p_stat == LSRUN || p[i].p_stat == LSIDL
|
|
|
|
|| p[i].p_stat == LSONPROC) {
|
2005-07-20 00:30:40 +00:00
|
|
|
cnt++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
info.run_procs = cnt;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct cpu_load_struct {
|
|
|
|
unsigned long load[5];
|
|
|
|
};
|
|
|
|
|
|
|
|
struct cpu_load_struct fresh = {
|
|
|
|
{0, 0, 0, 0, 0}
|
|
|
|
};
|
|
|
|
|
|
|
|
long cpu_used, oldtotal, oldused;
|
|
|
|
|
|
|
|
void update_cpu_usage()
|
|
|
|
{
|
|
|
|
long used, total;
|
|
|
|
static u_int64_t cp_time[CPUSTATES];
|
|
|
|
size_t len = sizeof(cp_time);
|
|
|
|
|
|
|
|
info.cpu_usage = 0;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (sysctlbyname("kern.cp_time", &cp_time, &len, NULL, 0) < 0) {
|
|
|
|
warn("cannot get kern.cp_time");
|
|
|
|
}
|
2005-07-20 00:30:40 +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];
|
|
|
|
|
|
|
|
if ((total - oldtotal) != 0) {
|
|
|
|
info.cpu_usage = ((double) (used - oldused)) /
|
|
|
|
(double) (total - oldtotal);
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
info.cpu_usage = 0;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
oldused = used;
|
|
|
|
oldtotal = total;
|
|
|
|
}
|
|
|
|
|
2007-08-13 23:14:01 +00:00
|
|
|
double get_sysfs_info(int *fd, int div, char *devtype)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update_load_average()
|
|
|
|
{
|
|
|
|
double v[3];
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
getloadavg(v, 3);
|
|
|
|
|
|
|
|
info.loadavg[0] = (float) v[0];
|
|
|
|
info.loadavg[1] = (float) v[1];
|
|
|
|
info.loadavg[2] = (float) v[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
double get_acpi_temperature(int fd)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2006-12-12 23:21:14 +00:00
|
|
|
void get_battery_stuff(char *buf, unsigned int n, const char *bat, int item)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
int open_sysfs_sensor(const char *dir, const char *dev, const char *type, int n,
|
|
|
|
int *div, char *devtype)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
int open_acpi_temperature(const char *name)
|
|
|
|
{
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void get_acpi_ac_adapter(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2005-11-12 21:42:00 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-11-12 21:42:00 +00:00
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
/* not implemented */
|
2008-02-20 20:30:45 +00:00
|
|
|
memset(p_client_buffer, 0, client_buffer_size);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
/* char *get_acpi_fan() */
|
|
|
|
void get_acpi_fan(char *p_client_buffer, size_t client_buffer_size)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!p_client_buffer || client_buffer_size <= 0) {
|
2005-11-12 21:42:00 +00:00
|
|
|
return;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-11-12 21:42:00 +00:00
|
|
|
|
2005-11-13 04:04:00 +00:00
|
|
|
/* not implemented */
|
2008-02-20 20:30:45 +00:00
|
|
|
memset(p_client_buffer, 0, client_buffer_size);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2006-11-30 20:46:34 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void update_entropy(void)
|
2006-11-30 20:46:34 +00:00
|
|
|
{
|
|
|
|
}
|