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 t)
218 if (t >= 24 * 60 * 60) { /* hours necessary when there are days? */
219 snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24, (t / 60 / 60) % 24,
221 } else if (t >= 60 * 60) {
222 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
224 snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
228 void format_seconds_short(char *buf, unsigned int n, long t)
230 if (t >= 24 * 60 * 60) {
231 snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24, (t / 60 / 60) % 24);
232 } else if (t >= 60 * 60) {
233 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
235 snprintf(buf, n, "%ldm", t / 60);
239 static double last_meminfo_update;
240 static double last_fs_update;
242 unsigned long long need_mask;
244 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
246 void update_stuff(void)
253 need_mask |= 1 << INFO_BUFFERS;
256 /* clear speeds and up status in case device was removed and doesn't get
259 for (i = 0; i < 16; i++) {
260 if (netstats[i].dev) {
262 netstats[i].recv_speed = 0.0;
263 netstats[i].trans_speed = 0.0;
269 if (NEED(INFO_UPTIME)) {
273 if (NEED(INFO_PROCS)) {
274 update_total_processes();
277 if (NEED(INFO_RUN_PROCS)) {
278 update_running_processes();
281 if (NEED(INFO_CPU)) {
285 if (NEED(INFO_NET)) {
289 if (NEED(INFO_DISKIO)) {
293 #if defined(__linux__)
294 if (NEED(INFO_I8K)) {
297 #endif /* __linux__ */
300 if (NEED(INFO_MPD)) {
301 if (!info.mpd.timed_thread) {
302 init_mpd_stats(&info.mpd);
303 info.mpd.timed_thread = timed_thread_create(&update_mpd,
304 (void *) &info.mpd, info.music_player_interval * 1000000);
305 if (!info.mpd.timed_thread) {
306 ERR("Failed to create MPD timed thread");
308 timed_thread_register(info.mpd.timed_thread, &info.mpd.timed_thread);
309 if (timed_thread_run(info.mpd.timed_thread)) {
310 ERR("Failed to run MPD timed thread");
317 if (NEED(INFO_XMMS2)) {
323 if (NEED(INFO_AUDACIOUS)) {
329 if (NEED(INFO_BMPX)) {
334 if (NEED(INFO_LOADAVG)) {
335 update_load_average();
338 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
339 && current_update_time - last_meminfo_update > 6.9) {
342 info.mem -= info.bufmem;
344 last_meminfo_update = current_update_time;
347 if (NEED(INFO_TOP)) {
351 /* update_fs_stat() won't do anything if there aren't fs -things */
352 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
354 last_fs_update = current_update_time;
356 #ifdef TCP_PORT_MONITOR
357 if (NEED(INFO_TCP_PORT_MONITOR)) {
358 update_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
361 if (NEED(INFO_ENTROPY)) {
364 if (NEED(INFO_USERS)) {
368 update_gateway_info();
370 if (NEED(INFO_DNS)) {
375 int round_to_int(float f)
378 return (int) (f + 0.5);
380 return (int) (f - 0.5);