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)
39 char *ret = malloc(n + 1);
47 #endif /* HAVE_STRNDUP */
49 void update_uname(void)
59 return tv.tv_sec + (tv.tv_usec / 1000000.0);
62 FILE *open_file(const char *file, int *reported)
64 FILE *fp = fopen(file, "r");
67 if (!reported || *reported == 0) {
68 ERR("can't open %s: %s", file, strerror(errno));
79 void variable_substitute(const char *s, char *dest, unsigned int n)
89 /* variable is either $foo or ${foo} */
93 while (*s && *s != '}') {
98 while (*s && (isalnum((int) *s) || *s == '_')) {
103 /* copy variable to buffer and look it up */
104 len = (s - a > 255) ? 255 : (s - a);
105 strncpy(buf, a, len);
115 /* add var to dest */
120 strncpy(dest, var, len);
135 /* network interface stuff */
137 static struct net_stat netstats[16];
139 struct net_stat *get_net_stat(const char *dev)
147 /* find interface stat */
148 for (i = 0; i < 16; i++) {
149 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
154 /* wasn't found? add it */
156 for (i = 0; i < 16; i++) {
157 if (netstats[i].dev == 0) {
158 netstats[i].dev = strndup(dev, text_buffer_size);
164 CRIT_ERR("too many interfaces used (limit is 16)");
168 void clear_net_stats(void)
170 memset(netstats, 0, sizeof(netstats));
173 void free_dns_data(void)
176 struct dns_data *data = &info.nameserver_info;
177 for (i = 0; i < data->nscount; i++)
178 free(data->ns_list[i]);
181 memset(data, 0, sizeof(struct dns_data));
184 //static double last_dns_update;
186 void update_dns_data(void)
190 struct dns_data *data = &info.nameserver_info;
192 /* maybe updating too often causes higher load because of /etc lying on a real FS
193 if (current_update_time - last_dns_update < 10.0)
196 last_dns_update = current_update_time;
201 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
204 if (fgets(line, 255, fp) == NULL) {
207 if (!strncmp(line, "nameserver ", 11)) {
208 line[strlen(line) - 1] = '\0'; // remove trailing newline
210 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
211 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
217 void format_seconds(char *buf, unsigned int n, long seconds)
222 days = seconds / 86400;
224 hours = seconds / 3600;
226 minutes = seconds / 60;
230 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
232 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
236 void format_seconds_short(char *buf, unsigned int n, long seconds)
241 days = seconds / 86400;
243 hours = seconds / 3600;
245 minutes = seconds / 60;
249 snprintf(buf, n, "%ldd %dh", days, hours);
250 } else if (hours > 0) {
251 snprintf(buf, n, "%dh %dm", hours, minutes);
253 snprintf(buf, n, "%dm %lds", minutes, seconds);
257 static double last_meminfo_update;
258 static double last_fs_update;
260 unsigned long long need_mask;
262 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
264 void update_stuff(void)
271 need_mask |= 1 << INFO_BUFFERS;
274 /* clear speeds and up status in case device was removed and doesn't get
277 for (i = 0; i < 16; i++) {
278 if (netstats[i].dev) {
280 netstats[i].recv_speed = 0.0;
281 netstats[i].trans_speed = 0.0;
287 if (NEED(INFO_UPTIME)) {
291 if (NEED(INFO_PROCS)) {
292 update_total_processes();
295 if (NEED(INFO_RUN_PROCS)) {
296 update_running_processes();
299 if (NEED(INFO_CPU)) {
303 if (NEED(INFO_NET)) {
307 if (NEED(INFO_DISKIO)) {
311 #if defined(__linux__)
312 if (NEED(INFO_I8K)) {
315 #endif /* __linux__ */
318 if (NEED(INFO_MPD)) {
319 if (!info.mpd.timed_thread) {
320 init_mpd_stats(&info.mpd);
321 info.mpd.timed_thread = timed_thread_create(&update_mpd,
322 (void *) &info.mpd, info.music_player_interval * 1000000);
323 if (!info.mpd.timed_thread) {
324 ERR("Failed to create MPD timed thread");
326 timed_thread_register(info.mpd.timed_thread, &info.mpd.timed_thread);
327 if (timed_thread_run(info.mpd.timed_thread)) {
328 ERR("Failed to run MPD timed thread");
335 if (NEED(INFO_MOC)) {
336 if (!info.moc.timed_thread) {
338 info.moc.timed_thread = timed_thread_create(&update_moc,
339 (void *) &info.moc, info.music_player_interval * 100000);
340 if (!info.moc.timed_thread) {
341 ERR("Failed to create MOC timed thread");
343 timed_thread_register(info.moc.timed_thread, &info.moc.timed_thread);
344 if (timed_thread_run(info.moc.timed_thread)) {
345 ERR("Failed to run MOC timed thread");
352 if (NEED(INFO_XMMS2)) {
358 if (NEED(INFO_AUDACIOUS)) {
364 if (NEED(INFO_BMPX)) {
369 if (NEED(INFO_LOADAVG)) {
370 update_load_average();
373 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
374 && current_update_time - last_meminfo_update > 6.9) {
377 info.mem -= info.bufmem;
378 info.memeasyfree += info.bufmem;
380 last_meminfo_update = current_update_time;
384 if (NEED(INFO_X11)) {
389 if (NEED(INFO_TOP)) {
393 /* update_fs_stat() won't do anything if there aren't fs -things */
394 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
396 last_fs_update = current_update_time;
398 #ifdef TCP_PORT_MONITOR
399 if (NEED(INFO_TCP_PORT_MONITOR)) {
400 tcp_portmon_update();
403 if (NEED(INFO_ENTROPY)) {
406 #if defined(__linux__)
407 if (NEED(INFO_USERS)) {
411 update_gateway_info();
413 #endif /* __linux__ */
414 if (NEED(INFO_DNS)) {
419 int round_to_int(float f)
422 return (int) (f + 0.5);
424 return (int) (f - 0.5);