2008-02-20 20:30:45 +00:00
|
|
|
/* Conky, a system monitor, based on torsmo
|
2005-08-05 01:06:17 +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) 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/>.
|
2005-08-05 01:06:17 +00:00
|
|
|
*
|
2008-02-20 20:30:45 +00:00
|
|
|
* $Id$ */
|
2005-08-05 01:06:17 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#include "conky.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <ctype.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <sys/time.h>
|
2007-08-30 17:21:30 +00:00
|
|
|
#include <pthread.h>
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
void update_uname(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
uname(&info.uname_s);
|
|
|
|
}
|
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
double get_time(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
struct timeval tv;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
gettimeofday(&tv, 0);
|
2005-11-27 06:56:35 +00:00
|
|
|
return tv.tv_sec + (tv.tv_usec / 1000000.0);
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *open_file(const char *file, int *reported)
|
|
|
|
{
|
|
|
|
FILE *fp = fopen(file, "r");
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
if (!fp) {
|
|
|
|
if (!reported || *reported == 0) {
|
2005-09-16 01:43:58 +00:00
|
|
|
ERR("can't open %s: %s", file, strerror(errno));
|
2008-02-20 20:30:45 +00:00
|
|
|
if (reported) {
|
2005-07-20 00:30:40 +00:00
|
|
|
*reported = 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return fp;
|
|
|
|
}
|
|
|
|
|
|
|
|
void variable_substitute(const char *s, char *dest, unsigned int n)
|
|
|
|
{
|
|
|
|
while (*s && n > 1) {
|
|
|
|
if (*s == '$') {
|
|
|
|
s++;
|
|
|
|
if (*s != '$') {
|
|
|
|
char buf[256];
|
|
|
|
const char *a, *var;
|
|
|
|
unsigned int len;
|
|
|
|
|
|
|
|
/* variable is either $foo or ${foo} */
|
|
|
|
if (*s == '{') {
|
|
|
|
s++;
|
|
|
|
a = s;
|
2008-02-20 20:30:45 +00:00
|
|
|
while (*s && *s != '}') {
|
2005-07-20 00:30:40 +00:00
|
|
|
s++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
} else {
|
|
|
|
a = s;
|
2008-02-20 20:30:45 +00:00
|
|
|
while (*s && (isalnum((int) *s) || *s == '_')) {
|
2005-07-20 00:30:40 +00:00
|
|
|
s++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* copy variable to buffer and look it up */
|
|
|
|
len = (s - a > 255) ? 255 : (s - a);
|
|
|
|
strncpy(buf, a, len);
|
|
|
|
buf[len] = '\0';
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (*s == '}') {
|
2005-07-20 00:30:40 +00:00
|
|
|
s++;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
var = getenv(buf);
|
|
|
|
|
|
|
|
if (var) {
|
|
|
|
/* add var to dest */
|
|
|
|
len = strlen(var);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (len >= n) {
|
2005-07-20 00:30:40 +00:00
|
|
|
len = n - 1;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
strncpy(dest, var, len);
|
|
|
|
dest += len;
|
|
|
|
n -= len;
|
|
|
|
}
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest++ = *s++;
|
|
|
|
n--;
|
|
|
|
}
|
|
|
|
|
|
|
|
*dest = '\0';
|
|
|
|
}
|
|
|
|
|
|
|
|
/* network interface stuff */
|
|
|
|
|
|
|
|
static struct net_stat netstats[16];
|
|
|
|
|
|
|
|
struct net_stat *get_net_stat(const char *dev)
|
|
|
|
{
|
|
|
|
unsigned int i;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (!dev) {
|
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
|
|
|
|
|
|
|
/* find interface stat */
|
|
|
|
for (i = 0; i < 16; i++) {
|
2008-02-20 20:30:45 +00:00
|
|
|
if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
|
2005-07-20 00:30:40 +00:00
|
|
|
return &netstats[i];
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* wasn't found? add it */
|
|
|
|
if (i == 16) {
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (netstats[i].dev == 0) {
|
|
|
|
netstats[i].dev = strdup(dev);
|
|
|
|
return &netstats[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CRIT_ERR("too many interfaces used (limit is 16)");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
void clear_net_stats(void)
|
2006-12-23 06:01:16 +00:00
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
memset(netstats, 0, sizeof(netstats));
|
2006-12-23 06:01:16 +00:00
|
|
|
}
|
|
|
|
|
2008-03-22 22:58:26 +00:00
|
|
|
void free_dns_data(void)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
struct dns_data *data = &info.nameserver_info;
|
|
|
|
for (i = 0; i < data->nscount; i++)
|
|
|
|
free(data->ns_list[i]);
|
|
|
|
if (data->ns_list)
|
|
|
|
free(data->ns_list);
|
|
|
|
memset(data, 0, sizeof(struct dns_data));
|
|
|
|
}
|
|
|
|
|
|
|
|
//static double last_dns_update;
|
|
|
|
|
|
|
|
void update_dns_data(void)
|
|
|
|
{
|
|
|
|
FILE *fp;
|
|
|
|
char line[256];
|
|
|
|
struct dns_data *data = &info.nameserver_info;
|
|
|
|
|
|
|
|
/* maybe updating too often causes higher load because of /etc lying on a real FS
|
|
|
|
if (current_update_time - last_dns_update < 10.0)
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
last_dns_update = current_update_time;
|
|
|
|
*/
|
|
|
|
|
|
|
|
free_dns_data();
|
|
|
|
|
|
|
|
if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
|
|
|
|
return;
|
|
|
|
while(!feof(fp)) {
|
|
|
|
if (fgets(line, 255, fp) == NULL)
|
|
|
|
goto OUT;
|
|
|
|
if (!strncmp(line, "nameserver ", 11)) {
|
|
|
|
line[strlen(line) - 1] = '\0'; // remove trailing newline
|
|
|
|
data->nscount++;
|
|
|
|
data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
|
|
|
|
data->ns_list[data->nscount - 1] = strdup(line + 11);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
OUT:
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
void format_seconds(char *buf, unsigned int n, long t)
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (t >= 24 * 60 * 60) { /* hours necessary when there are days? */
|
|
|
|
snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24, (t / 60 / 60) % 24,
|
|
|
|
(t / 60) % 60);
|
|
|
|
} else if (t >= 60 * 60) {
|
|
|
|
snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void format_seconds_short(char *buf, unsigned int n, long t)
|
|
|
|
{
|
2008-02-20 20:30:45 +00:00
|
|
|
if (t >= 24 * 60 * 60) {
|
|
|
|
snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24, (t / 60 / 60) % 24);
|
|
|
|
} else if (t >= 60 * 60) {
|
|
|
|
snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
|
|
|
|
} else {
|
2005-07-20 00:30:40 +00:00
|
|
|
snprintf(buf, n, "%ldm", t / 60);
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static double last_meminfo_update;
|
|
|
|
static double last_fs_update;
|
|
|
|
|
2005-07-30 11:23:26 +00:00
|
|
|
unsigned long long need_mask;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2006-12-23 06:01:16 +00:00
|
|
|
#define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-03-29 02:01:03 +00:00
|
|
|
void update_stuff(void)
|
2005-07-20 00:30:40 +00:00
|
|
|
{
|
|
|
|
unsigned int i;
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
info.mask = 0;
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (no_buffers) {
|
2005-07-20 00:30:40 +00:00
|
|
|
need_mask |= 1 << INFO_BUFFERS;
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
/* clear speeds and up status in case device was removed and doesn't get
|
2008-02-20 20:30:45 +00:00
|
|
|
* updated */
|
2005-07-20 00:30:40 +00:00
|
|
|
|
|
|
|
for (i = 0; i < 16; i++) {
|
|
|
|
if (netstats[i].dev) {
|
|
|
|
netstats[i].up = 0;
|
|
|
|
netstats[i].recv_speed = 0.0;
|
|
|
|
netstats[i].trans_speed = 0.0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
prepare_update();
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_UPTIME)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
update_uptime();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_PROCS)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
update_total_processes();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_RUN_PROCS)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
update_running_processes();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_CPU)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
update_cpu_usage();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_NET)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
update_net_stats();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_DISKIO)) {
|
2005-08-26 02:52:54 +00:00
|
|
|
update_diskio();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-08-26 02:52:54 +00:00
|
|
|
|
2005-08-27 12:59:46 +00:00
|
|
|
#if defined(__linux__)
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_I8K)) {
|
2005-08-27 07:15:44 +00:00
|
|
|
update_i8k();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-08-27 12:59:46 +00:00
|
|
|
#endif /* __linux__ */
|
2008-02-20 20:30:45 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
#ifdef MPD
|
2007-08-30 17:21:30 +00:00
|
|
|
if (NEED(INFO_MPD)) {
|
|
|
|
if (!mpd_timed_thread) {
|
2007-08-31 23:45:41 +00:00
|
|
|
init_mpd_stats(&info);
|
2008-03-29 05:14:35 +00:00
|
|
|
mpd_timed_thread = timed_thread_create(&update_mpd,
|
2008-02-20 20:30:45 +00:00
|
|
|
(void *) NULL, info.music_player_interval * 1000000);
|
2007-08-31 02:05:02 +00:00
|
|
|
if (!mpd_timed_thread) {
|
2007-08-31 21:35:30 +00:00
|
|
|
ERR("Failed to create MPD timed thread");
|
2007-08-31 02:05:02 +00:00
|
|
|
}
|
|
|
|
timed_thread_register(mpd_timed_thread, &mpd_timed_thread);
|
2008-02-20 20:30:45 +00:00
|
|
|
if (timed_thread_run(mpd_timed_thread)) {
|
|
|
|
ERR("Failed to run MPD timed thread");
|
|
|
|
}
|
2007-08-30 17:21:30 +00:00
|
|
|
}
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
#endif
|
2006-03-25 21:21:07 +00:00
|
|
|
|
|
|
|
#ifdef XMMS2
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_XMMS2)) {
|
2006-03-25 21:21:07 +00:00
|
|
|
update_xmms2();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-03-25 21:21:07 +00:00
|
|
|
#endif
|
|
|
|
|
2006-11-03 20:54:52 +00:00
|
|
|
#ifdef AUDACIOUS
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_AUDACIOUS)) {
|
2006-11-03 20:54:52 +00:00
|
|
|
update_audacious();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2006-01-07 07:01:22 +00:00
|
|
|
#endif
|
2006-11-03 20:54:52 +00:00
|
|
|
|
2005-12-30 09:44:40 +00:00
|
|
|
#ifdef BMPX
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_BMPX)) {
|
|
|
|
update_bmpx();
|
|
|
|
}
|
2006-01-05 07:06:42 +00:00
|
|
|
#endif
|
2006-01-08 08:02:37 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_LOADAVG)) {
|
2005-07-20 00:30:40 +00:00
|
|
|
update_load_average();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
|
|
|
|
&& current_update_time - last_meminfo_update > 6.9) {
|
2005-07-20 00:30:40 +00:00
|
|
|
update_meminfo();
|
2005-11-11 05:28:20 +00:00
|
|
|
if (no_buffers) {
|
2005-07-20 00:30:40 +00:00
|
|
|
info.mem -= info.bufmem;
|
2005-11-11 05:28:20 +00:00
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
last_meminfo_update = current_update_time;
|
|
|
|
}
|
|
|
|
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_TOP)) {
|
2005-11-10 01:20:19 +00:00
|
|
|
update_top();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2005-11-10 01:20:19 +00:00
|
|
|
|
2005-07-20 00:30:40 +00:00
|
|
|
/* update_fs_stat() won't do anything if there aren't fs -things */
|
|
|
|
if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
|
|
|
|
update_fs_stats();
|
|
|
|
last_fs_update = current_update_time;
|
|
|
|
}
|
2005-10-31 05:17:06 +00:00
|
|
|
#ifdef TCP_PORT_MONITOR
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_TCP_PORT_MONITOR)) {
|
|
|
|
update_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
|
|
|
|
}
|
2005-10-31 05:17:06 +00:00
|
|
|
#endif
|
2008-02-20 20:30:45 +00:00
|
|
|
if (NEED(INFO_ENTROPY)) {
|
2006-11-30 20:46:34 +00:00
|
|
|
update_entropy();
|
2008-02-20 20:30:45 +00:00
|
|
|
}
|
2008-03-18 00:23:16 +00:00
|
|
|
if (NEED(INFO_USERS)) {
|
|
|
|
update_users();
|
|
|
|
}
|
2008-03-22 19:06:09 +00:00
|
|
|
if (NEED(INFO_GW)) {
|
|
|
|
update_gateway_info();
|
|
|
|
}
|
2008-03-22 22:58:26 +00:00
|
|
|
if (NEED(INFO_DNS)) {
|
|
|
|
update_dns_data();
|
|
|
|
}
|
2005-07-20 00:30:40 +00:00
|
|
|
}
|
2005-08-30 14:59:37 +00:00
|
|
|
|
|
|
|
int round_to_int(float f)
|
|
|
|
{
|
2008-02-19 03:33:12 +00:00
|
|
|
if (f >= 0.0) {
|
|
|
|
return (int) (f + 0.5);
|
|
|
|
} else {
|
|
|
|
return (int) (f - 0.5);
|
|
|
|
}
|
2005-08-30 14:59:37 +00:00
|
|
|
}
|