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/>.
38 /* check for OS and include appropriate headers */
39 #if defined(__linux__)
41 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
43 #elif defined(__OpenBSD__)
47 /* OS specific prototypes to be implemented by linux.c & Co. */
48 void update_entropy(void);
51 // use our own strndup() if it's not available
52 char *strndup(const char *s, size_t n)
55 char *ret = malloc(n + 1);
63 #endif /* HAVE_STRNDUP */
65 void update_uname(void)
75 return tv.tv_sec + (tv.tv_usec / 1000000.0);
78 FILE *open_file(const char *file, int *reported)
80 FILE *fp = fopen(file, "r");
83 if (!reported || *reported == 0) {
84 ERR("can't open %s: %s", file, strerror(errno));
95 void variable_substitute(const char *s, char *dest, unsigned int n)
105 /* variable is either $foo or ${foo} */
109 while (*s && *s != '}') {
114 while (*s && (isalnum((int) *s) || *s == '_')) {
119 /* copy variable to buffer and look it up */
120 len = (s - a > 255) ? 255 : (s - a);
121 strncpy(buf, a, len);
131 /* add var to dest */
136 strncpy(dest, var, len);
151 /* network interface stuff */
153 static struct net_stat netstats[16];
155 struct net_stat *get_net_stat(const char *dev)
163 /* find interface stat */
164 for (i = 0; i < 16; i++) {
165 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
170 /* wasn't found? add it */
172 for (i = 0; i < 16; i++) {
173 if (netstats[i].dev == 0) {
174 netstats[i].dev = strndup(dev, text_buffer_size);
180 CRIT_ERR("too many interfaces used (limit is 16)");
184 void clear_net_stats(void)
186 memset(netstats, 0, sizeof(netstats));
189 void free_dns_data(void)
192 struct dns_data *data = &info.nameserver_info;
193 for (i = 0; i < data->nscount; i++)
194 free(data->ns_list[i]);
197 memset(data, 0, sizeof(struct dns_data));
200 //static double last_dns_update;
202 static void update_dns_data(void)
206 struct dns_data *data = &info.nameserver_info;
208 /* maybe updating too often causes higher load because of /etc lying on a real FS
209 if (current_update_time - last_dns_update < 10.0)
212 last_dns_update = current_update_time;
217 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
220 if (fgets(line, 255, fp) == NULL) {
223 if (!strncmp(line, "nameserver ", 11)) {
224 line[strlen(line) - 1] = '\0'; // remove trailing newline
226 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
227 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
233 void format_seconds(char *buf, unsigned int n, long seconds)
238 days = seconds / 86400;
240 hours = seconds / 3600;
242 minutes = seconds / 60;
246 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
248 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
252 void format_seconds_short(char *buf, unsigned int n, long seconds)
257 days = seconds / 86400;
259 hours = seconds / 3600;
261 minutes = seconds / 60;
265 snprintf(buf, n, "%ldd %dh", days, hours);
266 } else if (hours > 0) {
267 snprintf(buf, n, "%dh %dm", hours, minutes);
269 snprintf(buf, n, "%dm %lds", minutes, seconds);
273 static double last_meminfo_update;
274 static double last_fs_update;
276 unsigned long long need_mask;
279 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
281 void update_stuff(void)
288 need_mask |= 1 << INFO_BUFFERS;
291 /* clear speeds and up status in case device was removed and doesn't get
294 for (i = 0; i < 16; i++) {
295 if (netstats[i].dev) {
297 netstats[i].recv_speed = 0.0;
298 netstats[i].trans_speed = 0.0;
304 if (NEED(INFO_UPTIME)) {
308 if (NEED(INFO_PROCS)) {
309 update_total_processes();
312 if (NEED(INFO_RUN_PROCS)) {
313 update_running_processes();
316 if (NEED(INFO_CPU)) {
320 if (NEED(INFO_NET)) {
324 if (NEED(INFO_DISKIO)) {
328 #if defined(__linux__)
329 if (NEED(INFO_I8K)) {
332 #endif /* __linux__ */
335 if (NEED(INFO_MPD)) {
341 if (NEED(INFO_MOC)) {
342 run_moc_thread(info.music_player_interval * 100000);
347 if (NEED(INFO_XMMS2)) {
353 if (NEED(INFO_AUDACIOUS)) {
359 if (NEED(INFO_BMPX)) {
364 if (NEED(INFO_LOADAVG)) {
365 update_load_average();
368 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
369 && current_update_time - last_meminfo_update > 6.9) {
372 info.mem -= info.bufmem;
373 info.memeasyfree += info.bufmem;
375 last_meminfo_update = current_update_time;
379 if (NEED(INFO_X11)) {
384 if (NEED(INFO_TOP)) {
388 /* update_fs_stat() won't do anything if there aren't fs -things */
389 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
391 last_fs_update = current_update_time;
393 #ifdef TCP_PORT_MONITOR
394 if (NEED(INFO_TCP_PORT_MONITOR)) {
395 tcp_portmon_update();
398 if (NEED(INFO_ENTROPY)) {
401 #if defined(__linux__)
402 if (NEED(INFO_USERS)) {
406 update_gateway_info();
408 #endif /* __linux__ */
409 if (NEED(INFO_DNS)) {
414 int round_to_int(float f)
417 return (int) (f + 0.5);
419 return (int) (f - 0.5);