2 * Conky, a system monitor, based on torsmo
4 * This program is licensed under BSD license, read COPYING
19 struct information info;
30 return tv.tv_sec + (tv.tv_usec / 1000000.0);
33 FILE *open_file(const char *file, int *reported)
35 FILE *fp = fopen(file, "r");
37 if (!reported || *reported == 0) {
38 ERR("can't open %s: %s", file, strerror(errno));
48 void variable_substitute(const char *s, char *dest, unsigned int n)
58 /* variable is either $foo or ${foo} */
62 while (*s && *s != '}')
66 while (*s && (isalnum((int) *s)
71 /* copy variable to buffer and look it up */
72 len = (s - a > 255) ? 255 : (s - a);
86 strncpy(dest, var, len);
101 /* network interface stuff */
103 static struct net_stat netstats[16];
105 struct net_stat *get_net_stat(const char *dev)
112 /* find interface stat */
113 for (i = 0; i < 16; i++) {
114 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0)
118 /* wasn't found? add it */
120 for (i = 0; i < 16; i++) {
121 if (netstats[i].dev == 0) {
122 netstats[i].dev = strdup(dev);
128 CRIT_ERR("too many interfaces used (limit is 16)");
132 void format_seconds(char *buf, unsigned int n, long t)
134 if (t >= 24 * 60 * 60) /* hours necessary when there are days? */
135 snprintf(buf, n, "%ldd %ldh %ldm", t / 60 / 60 / 24,
136 (t / 60 / 60) % 24, (t / 60) % 60);
137 else if (t >= 60 * 60)
138 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24,
141 snprintf(buf, n, "%ldm %lds", t / 60, t % 60);
144 void format_seconds_short(char *buf, unsigned int n, long t)
146 if (t >= 24 * 60 * 60)
147 snprintf(buf, n, "%ldd %ldh", t / 60 / 60 / 24,
149 else if (t >= 60 * 60)
150 snprintf(buf, n, "%ldh %ldm", (t / 60 / 60) % 24,
153 snprintf(buf, n, "%ldm", t / 60);
156 static double last_meminfo_update;
157 static double last_fs_update;
159 unsigned long long need_mask;
167 need_mask |= 1 << INFO_BUFFERS;
169 /* clear speeds and up status in case device was removed and doesn't get
172 for (i = 0; i < 16; i++) {
173 if (netstats[i].dev) {
175 netstats[i].recv_speed = 0.0;
176 netstats[i].trans_speed = 0.0;
181 /* client(); this is approximately where the client should be called */
182 #define NEED(a) ((need_mask & (1 << a)) && ((info.mask & (1 << a)) == 0))
184 if (NEED(INFO_UPTIME))
187 if (NEED(INFO_PROCS))
188 update_total_processes();
190 if (NEED(INFO_RUN_PROCS))
191 update_running_processes();
199 if (NEED(INFO_DISKIO))
210 #if defined(__linux__)
213 #endif /* __linux__ */
221 if (NEED(INFO_XMMS2))
226 if (NEED(INFO_AUDACIOUS))
235 if (NEED(INFO_LOADAVG))
236 update_load_average();
239 if ((NEED(INFO_MEM) || NEED(INFO_BUFFERS) || NEED(INFO_TOP)) &&
240 current_update_time - last_meminfo_update > 6.9) {
243 info.mem -= info.bufmem;
245 last_meminfo_update = current_update_time;
251 /* update_fs_stat() won't do anything if there aren't fs -things */
252 if (NEED(INFO_FS) && current_update_time - last_fs_update > 12.9) {
254 last_fs_update = current_update_time;
256 #ifdef TCP_PORT_MONITOR
257 if (NEED(INFO_TCP_PORT_MONITOR))
258 update_tcp_port_monitor_collection( info.p_tcp_port_monitor_collection );
260 if (NEED(INFO_ENTROPY))
264 int round_to_int(float f)
267 double delta = f - intval;
268 if (!(delta < 0.5)) {