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-2009 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 #include <sys/ioctl.h>
37 #include <netinet/in.h>
42 /* check for OS and include appropriate headers */
43 #if defined(__linux__)
45 #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
47 #elif defined(__OpenBSD__)
51 /* OS specific prototypes to be implemented by linux.c & Co. */
52 void update_entropy(void);
55 // use our own strndup() if it's not available
56 char *strndup(const char *s, size_t n)
59 char *ret = malloc(n + 1);
67 #endif /* HAVE_STRNDUP */
69 void update_uname(void)
79 return tv.tv_sec + (tv.tv_usec / 1000000.0);
82 FILE *open_file(const char *file, int *reported)
84 FILE *fp = fopen(file, "r");
87 if (!reported || *reported == 0) {
88 ERR("can't open %s: %s", file, strerror(errno));
99 void variable_substitute(const char *s, char *dest, unsigned int n)
101 while (*s && n > 1) {
109 /* variable is either $foo or ${foo} */
113 while (*s && *s != '}') {
118 while (*s && (isalnum((int) *s) || *s == '_')) {
123 /* copy variable to buffer and look it up */
124 len = (s - a > 255) ? 255 : (s - a);
125 strncpy(buf, a, len);
135 /* add var to dest */
140 strncpy(dest, var, len);
155 /* network interface stuff */
157 static struct net_stat netstats[16];
159 struct net_stat *get_net_stat(const char *dev)
167 /* find interface stat */
168 for (i = 0; i < 16; i++) {
169 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
174 /* wasn't found? add it */
176 for (i = 0; i < 16; i++) {
177 if (netstats[i].dev == 0) {
178 netstats[i].dev = strndup(dev, text_buffer_size);
184 CRIT_ERR("too many interfaces used (limit is 16)");
188 void clear_net_stats(void)
190 memset(netstats, 0, sizeof(netstats));
193 /* We should check if this is ok with OpenBSD and NetBSD as well. */
194 int interface_up(const char *dev)
199 if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
200 CRIT_ERR("could not create sockfd");
203 strncpy(ifr.ifr_name, dev, IFNAMSIZ);
204 if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
205 /* if device does not exist, treat like not up */
206 if (errno != ENODEV && errno != ENXIO)
207 perror("SIOCGIFFLAGS");
211 if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
213 if (ifup_strictness == IFUP_UP)
216 if (!(ifr.ifr_flags & IFF_RUNNING))
218 if (ifup_strictness == IFUP_LINK)
221 if (ioctl(fd, SIOCGIFADDR, &ifr)) {
222 perror("SIOCGIFADDR");
225 if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
236 void free_dns_data(void)
239 struct dns_data *data = &info.nameserver_info;
240 for (i = 0; i < data->nscount; i++)
241 free(data->ns_list[i]);
244 memset(data, 0, sizeof(struct dns_data));
247 //static double last_dns_update;
249 static void update_dns_data(void)
253 struct dns_data *data = &info.nameserver_info;
255 /* maybe updating too often causes higher load because of /etc lying on a real FS
256 if (current_update_time - last_dns_update < 10.0)
259 last_dns_update = current_update_time;
264 if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
267 if (fgets(line, 255, fp) == NULL) {
270 if (!strncmp(line, "nameserver ", 11)) {
271 line[strlen(line) - 1] = '\0'; // remove trailing newline
273 data->ns_list = realloc(data->ns_list, data->nscount * sizeof(char *));
274 data->ns_list[data->nscount - 1] = strndup(line + 11, text_buffer_size);
280 void format_seconds(char *buf, unsigned int n, long seconds)
285 days = seconds / 86400;
287 hours = seconds / 3600;
289 minutes = seconds / 60;
293 snprintf(buf, n, "%ldd %dh %dm", days, hours, minutes);
295 snprintf(buf, n, "%dh %dm %lds", hours, minutes, seconds);
299 void format_seconds_short(char *buf, unsigned int n, long seconds)
304 days = seconds / 86400;
306 hours = seconds / 3600;
308 minutes = seconds / 60;
312 snprintf(buf, n, "%ldd %dh", days, hours);
313 } else if (hours > 0) {
314 snprintf(buf, n, "%dh %dm", hours, minutes);
316 snprintf(buf, n, "%dm %lds", minutes, seconds);
320 static double last_meminfo_update;
321 static double last_fs_update;
323 unsigned long long need_mask;
326 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
328 void update_stuff(void)
335 need_mask |= 1 << INFO_BUFFERS;
338 /* clear speeds and up status in case device was removed and doesn't get
341 for (i = 0; i < 16; i++) {
342 if (netstats[i].dev) {
344 netstats[i].recv_speed = 0.0;
345 netstats[i].trans_speed = 0.0;
351 if (NEED(INFO_UPTIME)) {
355 if (NEED(INFO_PROCS)) {
356 update_total_processes();
359 if (NEED(INFO_RUN_PROCS)) {
360 update_running_processes();
363 if (NEED(INFO_CPU)) {
367 if (NEED(INFO_NET)) {
371 if (NEED(INFO_DISKIO)) {
375 #if defined(__linux__)
376 if (NEED(INFO_I8K)) {
379 #endif /* __linux__ */
382 if (NEED(INFO_MPD)) {
388 if (NEED(INFO_MOC)) {
389 run_moc_thread(info.music_player_interval * 100000);
394 if (NEED(INFO_XMMS2)) {
400 if (NEED(INFO_AUDACIOUS)) {
406 if (NEED(INFO_BMPX)) {
411 if (NEED(INFO_LOADAVG)) {
412 update_load_average();
415 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP))
416 && current_update_time - last_meminfo_update > 6.9) {
419 info.mem -= info.bufmem;
420 info.memeasyfree += info.bufmem;
422 last_meminfo_update = current_update_time;
426 if (NEED(INFO_X11)) {
431 if (NEED(INFO_TOP)) {
435 /* update_fs_stat() won't do anything if there aren't fs -things */
436 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
438 last_fs_update = current_update_time;
440 #ifdef TCP_PORT_MONITOR
441 if (NEED(INFO_TCP_PORT_MONITOR)) {
442 tcp_portmon_update();
445 if (NEED(INFO_ENTROPY)) {
448 #if defined(__linux__)
449 if (NEED(INFO_USERS)) {
453 update_gateway_info();
455 #endif /* __linux__ */
456 if (NEED(INFO_DNS)) {
461 int round_to_int(float f)
464 return (int) (f + 0.5);
466 return (int) (f - 0.5);