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 void update_uname(void)
47 return tv.tv_sec + (tv.tv_usec / 1000000.0);
50 FILE *open_file(const char *file, int *reported)
52 FILE *fp = fopen(file, "r");
55 if (!reported || *reported == 0) {
56 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 != '}') {
86 while (*s && (isalnum((int) *s) || *s == '_')) {
91 /* copy variable to buffer and look it up */
92 len = (s - a > 255) ? 255 : (s - a);
103 /* add var to dest */
108 strncpy(dest, var, len);
123 /* network interface stuff */
125 static struct net_stat netstats[16];
127 struct net_stat *get_net_stat(const char *dev)
135 /* find interface stat */
136 for (i = 0; i < 16; i++) {
137 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
142 /* wasn't found? add it */
144 for (i = 0; i < 16; i++) {
145 if (netstats[i].dev == 0) {
146 netstats[i].dev = strdup(dev);
152 CRIT_ERR("too many interfaces used (limit is 16)");
156 void clear_net_stats(void)
158 memset(netstats, 0, sizeof(netstats));
161 void free_dns_data(void)
164 struct dns_data *data = &info.nameserver_info;
165 for (i = 0; i < data->nscount; i++)
166 free(data->ns_list[i]);
169 memset(data, 0, sizeof(struct dns_data));
172 //static double last_dns_update;
174 void update_dns_data(void)
178 struct dns_data *data = &info.nameserver_info;
180 /* maybe updating too often causes higher load because of /etc lying on a real FS
181 if (current_update_time - last_dns_update < 10.0)
184 last_dns_update = current_update_time;
189 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
192 if (fgets(line, 255, fp) == NULL)
194 if (!strncmp(line, "nameserver ", 11)) {
195 line[strlen(line) - 1] = '\0'; // remove trailing newline
197 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
198 data->ns_list[data->nscount - 1] = strdup(line + 11);
205 void format_seconds(char *buf, unsigned int n, long t)
207 if (t >= 24 * 60 * 60) { /* hours necessary when there are days? */
208 snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24, (t / 60 / 60) % 24,
210 } else if (t >= 60 * 60) {
211 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24, (t / 60) % 60);
213 snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
217 void format_seconds_short(char *buf, unsigned int n, long t)
219 if (t >= 24 * 60 * 60) {
220 snprintf(buf, n, "%ldd %ldh", 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", t / 60);
228 static double last_meminfo_update;
229 static double last_fs_update;
231 unsigned long long need_mask;
233 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
235 void update_stuff(void)
242 need_mask |= 1 << INFO_BUFFERS;
245 /* clear speeds and up status in case device was removed and doesn't get
248 for (i = 0; i < 16; i++) {
249 if (netstats[i].dev) {
251 netstats[i].recv_speed = 0.0;
252 netstats[i].trans_speed = 0.0;
258 if (NEED(INFO_UPTIME)) {
262 if (NEED(INFO_PROCS)) {
263 update_total_processes();
266 if (NEED(INFO_RUN_PROCS)) {
267 update_running_processes();
270 if (NEED(INFO_CPU)) {
274 if (NEED(INFO_NET)) {
278 if (NEED(INFO_DISKIO)) {
282 #if defined(__linux__)
283 if (NEED(INFO_I8K)) {
286 #endif /* __linux__ */
289 if (NEED(INFO_MPD)) {
290 if (!mpd_timed_thread) {
291 init_mpd_stats(&info);
292 mpd_timed_thread = timed_thread_create(&update_mpd,
293 (void *) NULL, info.music_player_interval * 1000000);
294 if (!mpd_timed_thread) {
295 ERR("Failed to create MPD timed thread");
297 timed_thread_register(mpd_timed_thread, &mpd_timed_thread);
298 if (timed_thread_run(mpd_timed_thread)) {
299 ERR("Failed to run MPD timed thread");
306 if (NEED(INFO_XMMS2)) {
312 if (NEED(INFO_AUDACIOUS)) {
318 if (NEED(INFO_BMPX)) {
323 if (NEED(INFO_LOADAVG)) {
324 update_load_average();
327 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
328 && current_update_time - last_meminfo_update > 6.9) {
331 info.mem -= info.bufmem;
333 last_meminfo_update = current_update_time;
336 if (NEED(INFO_TOP)) {
340 /* update_fs_stat() won't do anything if there aren't fs -things */
341 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
343 last_fs_update = current_update_time;
345 #ifdef TCP_PORT_MONITOR
346 if (NEED(INFO_TCP_PORT_MONITOR)) {
347 update_tcp_port_monitor_collection(info.p_tcp_port_monitor_collection);
350 if (NEED(INFO_ENTROPY)) {
353 if (NEED(INFO_USERS)) {
357 update_gateway_info();
359 if (NEED(INFO_DNS)) {
364 int round_to_int(float f)
367 return (int) (f + 0.5);
369 return (int) (f - 0.5);