1 /* Conky, a system monitor, based on torsmo
3 * Any original torsmo code is licensed under the BSD license
5 * All code written since the fork of torsmo is licensed under the GPL
7 * Please see COPYING for details
9 * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
10 * Copyright (c) 2005-2008 Brenden Matthews, Philip Kovacs, et. al.
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/>.
37 /* check for OS and include appropriate headers */
38 #if defined(__linux__)
40 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
42 #elif defined(__OpenBSD__)
46 /* OS specific prototypes to be implemented by linux.c & Co. */
47 void update_entropy(void);
50 // use our own strndup() if it's not available
51 char *strndup(const char *s, size_t n)
54 char *ret = malloc(n + 1);
62 #endif /* HAVE_STRNDUP */
64 void update_uname(void)
74 return tv.tv_sec + (tv.tv_usec / 1000000.0);
77 FILE *open_file(const char *file, int *reported)
79 FILE *fp = fopen(file, "r");
82 if (!reported || *reported == 0) {
83 ERR("can't open %s: %s", file, strerror(errno));
94 void variable_substitute(const char *s, char *dest, unsigned int n)
104 /* variable is either $foo or ${foo} */
108 while (*s && *s != '}') {
113 while (*s && (isalnum((int) *s) || *s == '_')) {
118 /* copy variable to buffer and look it up */
119 len = (s - a > 255) ? 255 : (s - a);
120 strncpy(buf, a, len);
130 /* add var to dest */
135 strncpy(dest, var, len);
150 /* network interface stuff */
152 static struct net_stat netstats[16];
154 struct net_stat *get_net_stat(const char *dev)
162 /* find interface stat */
163 for (i = 0; i < 16; i++) {
164 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
169 /* wasn't found? add it */
171 for (i = 0; i < 16; i++) {
172 if (netstats[i].dev == 0) {
173 netstats[i].dev = strndup(dev, text_buffer_size);
179 CRIT_ERR("too many interfaces used (limit is 16)");
183 void clear_net_stats(void)
185 memset(netstats, 0, sizeof(netstats));
188 void free_dns_data(void)
191 struct dns_data *data = &info.nameserver_info;
192 for (i = 0; i < data->nscount; i++)
193 free(data->ns_list[i]);
196 memset(data, 0, sizeof(struct dns_data));
199 //static double last_dns_update;
201 static void update_dns_data(void)
205 struct dns_data *data = &info.nameserver_info;
207 /* maybe updating too often causes higher load because of /etc lying on a real FS
208 if (current_update_time - last_dns_update < 10.0)
211 last_dns_update = current_update_time;
216 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
219 if (fgets(line, 255, fp) == NULL) {
222 if (!strncmp(line, "nameserver ", 11)) {
223 line[strlen(line) - 1] = '\0'; // remove trailing newline
225 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
226 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
232 void format_seconds(char *buf, unsigned int n, long seconds)
237 days = seconds / 86400;
239 hours = seconds / 3600;
241 minutes = seconds / 60;
245 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
247 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
251 void format_seconds_short(char *buf, unsigned int n, long seconds)
256 days = seconds / 86400;
258 hours = seconds / 3600;
260 minutes = seconds / 60;
264 snprintf(buf, n, "%ldd %dh", days, hours);
265 } else if (hours > 0) {
266 snprintf(buf, n, "%dh %dm", hours, minutes);
268 snprintf(buf, n, "%dm %lds", minutes, seconds);
272 static double last_meminfo_update;
273 static double last_fs_update;
275 unsigned long long need_mask;
278 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
280 void update_stuff(void)
287 need_mask |= 1 << INFO_BUFFERS;
290 /* clear speeds and up status in case device was removed and doesn't get
293 for (i = 0; i < 16; i++) {
294 if (netstats[i].dev) {
296 netstats[i].recv_speed = 0.0;
297 netstats[i].trans_speed = 0.0;
303 if (NEED(INFO_UPTIME)) {
307 if (NEED(INFO_PROCS)) {
308 update_total_processes();
311 if (NEED(INFO_RUN_PROCS)) {
312 update_running_processes();
315 if (NEED(INFO_CPU)) {
319 if (NEED(INFO_NET)) {
323 if (NEED(INFO_DISKIO)) {
327 #if defined(__linux__)
328 if (NEED(INFO_I8K)) {
331 #endif /* __linux__ */
334 if (NEED(INFO_MPD)) {
340 if (NEED(INFO_MOC)) {
341 if (!info.moc.timed_thread) {
343 info.moc.timed_thread = timed_thread_create(&update_moc,
344 (void *) &info.moc, info.music_player_interval * 100000);
345 if (!info.moc.timed_thread) {
346 ERR("Failed to create MOC timed thread");
348 timed_thread_register(info.moc.timed_thread, &info.moc.timed_thread);
349 if (timed_thread_run(info.moc.timed_thread)) {
350 ERR("Failed to run MOC timed thread");
357 if (NEED(INFO_XMMS2)) {
363 if (NEED(INFO_AUDACIOUS)) {
369 if (NEED(INFO_BMPX)) {
374 if (NEED(INFO_LOADAVG)) {
375 update_load_average();
378 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
379 && current_update_time - last_meminfo_update > 6.9) {
382 info.mem -= info.bufmem;
383 info.memeasyfree += info.bufmem;
385 last_meminfo_update = current_update_time;
389 if (NEED(INFO_X11)) {
394 if (NEED(INFO_TOP)) {
398 /* update_fs_stat() won't do anything if there aren't fs -things */
399 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
401 last_fs_update = current_update_time;
403 #ifdef TCP_PORT_MONITOR
404 if (NEED(INFO_TCP_PORT_MONITOR)) {
405 tcp_portmon_update();
408 if (NEED(INFO_ENTROPY)) {
411 #if defined(__linux__)
412 if (NEED(INFO_USERS)) {
416 update_gateway_info();
418 #endif /* __linux__ */
419 if (NEED(INFO_DNS)) {
424 int round_to_int(float f)
427 return (int) (f + 0.5);
429 return (int) (f - 0.5);