2 * Conky, a system monitor, based on torsmo
4 * Any original torsmo code is licensed under the BSD license
6 * All code written since the fork of torsmo is licensed under the GPL
8 * Please see COPYING for details
10 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
11 * Copyright (c) 2005-2007 Brenden Matthews, Philip Kovacs, et. al. (see AUTHORS)
12 * All rights reserved.
14 * This program is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
23 * You should have received a copy of the GNU General Public License
24 * along with this program. If not, see <http://www.gnu.org/licenses/>.
38 struct information info;
49 return tv.tv_sec + (tv.tv_usec / 1000000.0);
52 FILE *open_file(const char *file, int *reported)
54 FILE *fp = fopen(file, "r");
56 if (!reported || *reported == 0) {
57 ERR("can't open %s: %s", file, strerror(errno));
67 void variable_substitute(const char *s, char *dest, unsigned int n)
77 /* variable is either $foo or ${foo} */
81 while (*s && *s != '}')
85 while (*s && (isalnum((int) *s)
90 /* copy variable to buffer and look it up */
91 len = (s - a > 255) ? 255 : (s - a);
101 /* add var to dest */
105 strncpy(dest, var, len);
120 /* network interface stuff */
122 static struct net_stat netstats[16];
124 struct net_stat *get_net_stat(const char *dev)
131 /* find interface stat */
132 for (i = 0; i < 16; i++) {
133 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0)
137 /* wasn't found? add it */
139 for (i = 0; i < 16; i++) {
140 if (netstats[i].dev == 0) {
141 netstats[i].dev = strdup(dev);
147 CRIT_ERR("too many interfaces used (limit is 16)");
151 void clear_net_stats (void)
153 memset (netstats, 0, sizeof(netstats));
156 void format_seconds(char *buf, unsigned int n, long t)
158 if (t >= 24 * 60 * 60) /* hours necessary when there are days? */
159 snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24,
160 (t / 60 / 60) % 24, (t / 60) % 60);
161 else if (t >= 60 * 60)
162 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24,
165 snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
168 void format_seconds_short(char *buf, unsigned int n, long t)
170 if (t >= 24 * 60 * 60)
171 snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24,
173 else if (t >= 60 * 60)
174 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24,
177 snprintf(buf, n, "%ldm", t / 60);
180 static double last_meminfo_update;
181 static double last_fs_update;
183 unsigned long long need_mask;
184 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
192 need_mask |= 1 << INFO_BUFFERS;
194 /* clear speeds and up status in case device was removed and doesn't get
197 for (i = 0; i < 16; i++) {
198 if (netstats[i].dev) {
200 netstats[i].recv_speed = 0.0;
201 netstats[i].trans_speed = 0.0;
207 if (NEED(INFO_UPTIME))
210 if (NEED(INFO_PROCS))
211 update_total_processes();
213 if (NEED(INFO_RUN_PROCS))
214 update_running_processes();
222 if (NEED(INFO_DISKIO))
228 #if defined(__linux__)
231 #endif /* __linux__ */
234 if (NEED(INFO_MPD)) {
235 if (!mpd_timed_thread) {
236 mpd_timed_thread = timed_thread_create((void*)update_mpd, (void*) NULL, update_interval * 1000000);
242 if (NEED(INFO_XMMS2))
247 if (NEED(INFO_AUDACIOUS))
256 if (NEED(INFO_LOADAVG))
257 update_load_average();
260 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP)) &&
261 current_update_time - last_meminfo_update > 6.9) {
264 info.mem -= info.bufmem;
266 last_meminfo_update = current_update_time;
272 /* update_fs_stat() won't do anything if there aren't fs -things */
273 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
275 last_fs_update = current_update_time;
277 #ifdef TCP_PORT_MONITOR
278 if (NEED(INFO_TCP_PORT_MONITOR))
279 update_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
281 if (NEED(INFO_ENTROPY))
285 int round_to_int(float f)
288 double delta = f - intval;
289 if (!(delta < 0.5)) {