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/>.
35 // use our own strndup() if it's not available
36 char *strndup(const char *s, size_t n)
38 if (strlen(s) + 1 > n) {
39 char *ret = malloc(n);
46 #endif /* HAVE_STRNDUP */
48 void update_uname(void)
58 return tv.tv_sec + (tv.tv_usec / 1000000.0);
61 FILE *open_file(const char *file, int *reported)
63 FILE *fp = fopen(file, "r");
66 if (!reported || *reported == 0) {
67 ERR("can't open %s: %s", file, strerror(errno));
78 void variable_substitute(const char *s, char *dest, unsigned int n)
88 /* variable is either $foo or ${foo} */
92 while (*s && *s != '}') {
97 while (*s && (isalnum((int) *s) || *s == '_')) {
102 /* copy variable to buffer and look it up */
103 len = (s - a > 255) ? 255 : (s - a);
104 strncpy(buf, a, len);
114 /* add var to dest */
119 strncpy(dest, var, len);
134 /* network interface stuff */
136 static struct net_stat netstats[16];
138 struct net_stat *get_net_stat(const char *dev)
146 /* find interface stat */
147 for (i = 0; i < 16; i++) {
148 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
153 /* wasn't found? add it */
155 for (i = 0; i < 16; i++) {
156 if (netstats[i].dev == 0) {
157 netstats[i].dev = strndup(dev, text_buffer_size);
163 CRIT_ERR("too many interfaces used (limit is 16)");
167 void clear_net_stats(void)
169 memset(netstats, 0, sizeof(netstats));
172 void free_dns_data(void)
175 struct dns_data *data = &info.nameserver_info;
176 for (i = 0; i < data->nscount; i++)
177 free(data->ns_list[i]);
180 memset(data, 0, sizeof(struct dns_data));
183 //static double last_dns_update;
185 void update_dns_data(void)
189 struct dns_data *data = &info.nameserver_info;
191 /* maybe updating too often causes higher load because of /etc lying on a real FS
192 if (current_update_time - last_dns_update < 10.0)
195 last_dns_update = current_update_time;
200 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
203 if (fgets(line, 255, fp) == NULL) {
206 if (!strncmp(line, "nameserver ", 11)) {
207 line[strlen(line) - 1] = '\0'; // remove trailing newline
209 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
210 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
216 void format_seconds(char *buf, unsigned int n, long seconds)
221 days = seconds / 86400;
223 hours = seconds / 3600;
225 minutes = seconds / 60;
229 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
231 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
235 void format_seconds_short(char *buf, unsigned int n, long seconds)
240 days = seconds / 86400;
242 hours = seconds / 3600;
244 minutes = seconds / 60;
248 snprintf(buf, n, "%ldd %dh", days, hours);
249 } else if (hours > 0) {
250 snprintf(buf, n, "%dh %dm", hours, minutes);
252 snprintf(buf, n, "%dm %lds", minutes, seconds);
256 static double last_meminfo_update;
257 static double last_fs_update;
259 unsigned long long need_mask;
261 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
263 void update_stuff(void)
270 need_mask |= 1 << INFO_BUFFERS;
273 /* clear speeds and up status in case device was removed and doesn't get
276 for (i = 0; i < 16; i++) {
277 if (netstats[i].dev) {
279 netstats[i].recv_speed = 0.0;
280 netstats[i].trans_speed = 0.0;
286 if (NEED(INFO_UPTIME)) {
290 if (NEED(INFO_PROCS)) {
291 update_total_processes();
294 if (NEED(INFO_RUN_PROCS)) {
295 update_running_processes();
298 if (NEED(INFO_CPU)) {
302 if (NEED(INFO_NET)) {
306 if (NEED(INFO_DISKIO)) {
310 #if defined(__linux__)
311 if (NEED(INFO_I8K)) {
314 #endif /* __linux__ */
317 if (NEED(INFO_MPD)) {
318 if (!info.mpd.timed_thread) {
319 init_mpd_stats(&info.mpd);
320 info.mpd.timed_thread = timed_thread_create(&update_mpd,
321 (void *) &info.mpd, info.music_player_interval * 1000000);
322 if (!info.mpd.timed_thread) {
323 ERR("Failed to create MPD timed thread");
325 timed_thread_register(info.mpd.timed_thread, &info.mpd.timed_thread);
326 if (timed_thread_run(info.mpd.timed_thread)) {
327 ERR("Failed to run MPD timed thread");
334 if (NEED(INFO_MOC)) {
335 if (!info.moc.timed_thread) {
337 info.moc.timed_thread = timed_thread_create(&update_moc,
338 (void *) &info.moc, info.music_player_interval * 100000);
339 if (!info.moc.timed_thread) {
340 ERR("Failed to create MOC timed thread");
342 timed_thread_register(info.moc.timed_thread, &info.moc.timed_thread);
343 if (timed_thread_run(info.moc.timed_thread)) {
344 ERR("Failed to run MOC timed thread");
351 if (NEED(INFO_XMMS2)) {
357 if (NEED(INFO_AUDACIOUS)) {
363 if (NEED(INFO_BMPX)) {
368 if (NEED(INFO_LOADAVG)) {
369 update_load_average();
372 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
373 && current_update_time - last_meminfo_update > 6.9) {
376 info.mem -= info.bufmem;
377 info.memeasyfree += info.bufmem;
379 last_meminfo_update = current_update_time;
383 if (NEED(INFO_X11)) {
388 if (NEED(INFO_TOP)) {
392 /* update_fs_stat() won't do anything if there aren't fs -things */
393 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
395 last_fs_update = current_update_time;
397 #ifdef TCP_PORT_MONITOR
398 if (NEED(INFO_TCP_PORT_MONITOR)) {
399 update_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
402 if (NEED(INFO_ENTROPY)) {
405 #if defined(__linux__)
406 if (NEED(INFO_USERS)) {
410 update_gateway_info();
412 #endif /* __linux__ */
413 if (NEED(INFO_DNS)) {
418 int round_to_int(float f)
421 return (int) (f + 0.5);
423 return (int) (f - 0.5);