specials: convert graph objects to new style
[monky] / src / net_stat.c
1 /* -*- mode: c; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: t -*-
2  * vim: ts=4 sw=4 noet ai cindent syntax=c
3  *
4  * Conky, a system monitor, based on torsmo
5  *
6  * Any original torsmo code is licensed under the BSD license
7  *
8  * All code written since the fork of torsmo is licensed under the GPL
9  *
10  * Please see COPYING for details
11  *
12  * Copyright (c) 2004, Hannu Saransaari and Lauri Hakkarainen
13  * Copyright (c) 2005-2009 Brenden Matthews, Philip Kovacs, et. al.
14  *      (see AUTHORS)
15  * All rights reserved.
16  *
17  * This program is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
25  * GNU General Public License for more details.
26  * You should have received a copy of the GNU General Public License
27  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
28  *
29  */
30
31 #include "config.h"
32 #include "conky.h"
33 #include "logging.h"
34 #include "specials.h"
35 #include "net/if.h"
36 #include "text_object.h"
37 #include "net_stat.h"
38 #include <errno.h>
39 #include <string.h>
40 #include <sys/ioctl.h>
41
42 /* network interface stuff */
43
44 struct net_stat netstats[16];
45
46 struct net_stat *get_net_stat(const char *dev, void *free_at_crash1, void *free_at_crash2)
47 {
48         unsigned int i;
49
50         if (!dev) {
51                 return 0;
52         }
53
54         /* find interface stat */
55         for (i = 0; i < 16; i++) {
56                 if (netstats[i].dev && strcmp(netstats[i].dev, dev) == 0) {
57                         return &netstats[i];
58                 }
59         }
60
61         /* wasn't found? add it */
62         for (i = 0; i < 16; i++) {
63                 if (netstats[i].dev == 0) {
64                         netstats[i].dev = strndup(dev, text_buffer_size);
65                         return &netstats[i];
66                 }
67         }
68
69         CRIT_ERR(free_at_crash1, free_at_crash2, "too many interfaces used (limit is 16)");
70         return 0;
71 }
72
73 void parse_net_stat_arg(struct text_object *obj, const char *arg, void *free_at_crash)
74 {
75         if (!arg)
76                 arg = DEFAULTNETDEV;
77
78         obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
79 }
80
81 void parse_net_stat_bar_arg(struct text_object *obj, const char *arg, void *free_at_crash)
82 {
83         if (arg) {
84                 arg = scan_bar(obj, arg);
85                 obj->data.opaque = get_net_stat(arg, obj, free_at_crash);
86         } else {
87                 // default to DEFAULTNETDEV
88                 char *buf = strndup(DEFAULTNETDEV, text_buffer_size);
89                 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
90                 free(buf);
91         }
92 }
93
94 void print_downspeed(struct text_object *obj, char *p, int p_max_size)
95 {
96         struct net_stat *ns = obj->data.opaque;
97
98         if (!ns)
99                 return;
100
101         human_readable(ns->recv_speed, p, p_max_size);
102 }
103
104 void print_downspeedf(struct text_object *obj, char *p, int p_max_size)
105 {
106         struct net_stat *ns = obj->data.opaque;
107
108         if (!ns)
109                 return;
110
111         spaced_print(p, p_max_size, "%.1f", 8, ns->recv_speed / 1024.0);
112 }
113
114 void print_upspeed(struct text_object *obj, char *p, int p_max_size)
115 {
116         struct net_stat *ns = obj->data.opaque;
117
118         if (!ns)
119                 return;
120
121         human_readable(ns->trans_speed, p, p_max_size);
122 }
123
124 void print_upspeedf(struct text_object *obj, char *p, int p_max_size)
125 {
126         struct net_stat *ns = obj->data.opaque;
127
128         if (!ns)
129                 return;
130
131         spaced_print(p, p_max_size, "%.1f", 8, ns->trans_speed / 1024.0);
132 }
133
134 void print_totaldown(struct text_object *obj, char *p, int p_max_size)
135 {
136         struct net_stat *ns = obj->data.opaque;
137
138         if (!ns)
139                 return;
140
141         human_readable(ns->recv, p, p_max_size);
142 }
143
144 void print_totalup(struct text_object *obj, char *p, int p_max_size)
145 {
146         struct net_stat *ns = obj->data.opaque;
147
148         if (!ns)
149                 return;
150
151         human_readable(ns->trans, p, p_max_size);
152 }
153
154 void print_addr(struct text_object *obj, char *p, int p_max_size)
155 {
156         struct net_stat *ns = obj->data.opaque;
157
158         if (!ns)
159                 return;
160
161         if ((ns->addr.sa_data[2] & 255) == 0 &&
162             (ns->addr.sa_data[3] & 255) == 0 &&
163             (ns->addr.sa_data[4] & 255) == 0 &&
164             (ns->addr.sa_data[5] & 255) == 0) {
165                 snprintf(p, p_max_size, "No Address");
166         } else {
167                 snprintf(p, p_max_size, "%u.%u.%u.%u",
168                          ns->addr.sa_data[2] & 255,
169                          ns->addr.sa_data[3] & 255,
170                          ns->addr.sa_data[4] & 255,
171                          ns->addr.sa_data[5] & 255);
172         }
173 }
174
175 #ifdef __linux__
176 void print_addrs(struct text_object *obj, char *p, int p_max_size)
177 {
178         struct net_stat *ns = obj->data.opaque;
179
180         if (!ns)
181                 return;
182
183         if (NULL != ns->addrs && strlen(ns->addrs) > 2) {
184                 ns->addrs[strlen(ns->addrs) - 2] = 0; /* remove ", " from end of string */
185                 strncpy(p, ns->addrs, p_max_size);
186         } else {
187                 strncpy(p, "0.0.0.0", p_max_size);
188         }
189 }
190 #endif /* __linux__ */
191
192 #ifdef X11
193 void parse_net_stat_graph_arg(struct text_object *obj, const char *arg, void *free_at_crash)
194 {
195         char *buf = 0;
196         buf = scan_graph(obj, arg);
197
198         // default to DEFAULTNETDEV
199         if (buf) {
200                 obj->data.opaque = get_net_stat(buf, obj, free_at_crash);
201                 free(buf);
202                 return;
203         }
204         obj->data.opaque = get_net_stat(DEFAULTNETDEV, obj, free_at_crash);
205 }
206
207 void print_downspeedgraph(struct text_object *obj, char *p)
208 {
209         struct net_stat *ns = obj->data.opaque;
210
211         if (!ns)
212                 return;
213
214         new_graph(obj, p, ns->recv_speed / 1024.0);
215 }
216
217 void print_upspeedgraph(struct text_object *obj, char *p)
218 {
219         struct net_stat *ns = obj->data.opaque;
220
221         if (!ns)
222                 return;
223
224         new_graph(obj, p, ns->trans_speed / 1024.0);
225 }
226 #endif /* X11 */
227
228 #ifdef __linux__
229 #ifdef HAVE_IWLIB
230 void print_wireless_essid(struct text_object *obj, char *p, int p_max_size)
231 {
232         struct net_stat *ns = obj->data.opaque;
233
234         if (!ns)
235                 return;
236
237         snprintf(p, p_max_size, "%s", ns->essid);
238 }
239 void print_wireless_mode(struct text_object *obj, char *p, int p_max_size)
240 {
241         struct net_stat *ns = obj->data.opaque;
242
243         if (!ns)
244                 return;
245
246         snprintf(p, p_max_size, "%s", ns->mode);
247 }
248 void print_wireless_bitrate(struct text_object *obj, char *p, int p_max_size)
249 {
250         struct net_stat *ns = obj->data.opaque;
251
252         if (!ns)
253                 return;
254
255         snprintf(p, p_max_size, "%s", ns->bitrate);
256 }
257 void print_wireless_ap(struct text_object *obj, char *p, int p_max_size)
258 {
259         struct net_stat *ns = obj->data.opaque;
260
261         if (!ns)
262                 return;
263
264         snprintf(p, p_max_size, "%s", ns->ap);
265 }
266 void print_wireless_link_qual(struct text_object *obj, char *p, int p_max_size)
267 {
268         struct net_stat *ns = obj->data.opaque;
269
270         if (!ns)
271                 return;
272
273         spaced_print(p, p_max_size, "%d", 4, ns->link_qual);
274 }
275 void print_wireless_link_qual_max(struct text_object *obj, char *p, int p_max_size)
276 {
277         struct net_stat *ns = obj->data.opaque;
278
279         if (!ns)
280                 return;
281
282         spaced_print(p, p_max_size, "%d", 4, ns->link_qual_max);
283 }
284 void print_wireless_link_qual_perc(struct text_object *obj, char *p, int p_max_size)
285 {
286         struct net_stat *ns = obj->data.opaque;
287
288         if (!ns)
289                 return;
290
291         if (ns->link_qual_max > 0) {
292                 spaced_print(p, p_max_size, "%.0f", 5,
293                                 (double) ns->link_qual /
294                                 ns->link_qual_max * 100);
295         } else {
296                 spaced_print(p, p_max_size, "unk", 5);
297         }
298 }
299 void print_wireless_link_bar(struct text_object *obj, char *p, int p_max_size)
300 {
301         struct net_stat *ns = obj->data.opaque;
302
303         if (!ns)
304                 return;
305
306 #ifdef X11
307         if(output_methods & TO_X) {
308                 new_bar(obj, p, ((double) ns->link_qual /
309                                         ns->link_qual_max) * 255.0);
310         } else
311 #endif /* X11 */
312                 new_bar_in_shell(obj, p, p_max_size, ((double) ns->link_qual /
313                                         ns->link_qual_max) * 100.0);
314 }
315 #endif /* HAVE_IWLIB */
316 #endif /* __linux__ */
317
318 void clear_net_stats(void)
319 {
320         int i;
321         for (i = 0; i < 16; i++) {
322                 if (netstats[i].dev) {
323                         free(netstats[i].dev);
324                 }
325         }
326         memset(netstats, 0, sizeof(netstats));
327 }
328
329 void parse_if_up_arg(struct text_object *obj, const char *arg)
330 {
331         obj->data.opaque = strndup(arg, text_buffer_size);
332 }
333
334 void free_if_up(struct text_object *obj)
335 {
336         if (obj->data.opaque) {
337                 free(obj->data.opaque);
338                 obj->data.opaque = NULL;
339         }
340 }
341
342 /* We should check if this is ok with OpenBSD and NetBSD as well. */
343 int interface_up(struct text_object *obj)
344 {
345         int fd;
346         struct ifreq ifr;
347         char *dev = obj->data.opaque;
348
349         if (!dev)
350                 return 0;
351
352         if ((fd = socket(PF_INET, SOCK_DGRAM, 0)) < 0) {
353                 CRIT_ERR(NULL, NULL, "could not create sockfd");
354                 return 0;
355         }
356         strncpy(ifr.ifr_name, dev, IFNAMSIZ);
357         if (ioctl(fd, SIOCGIFFLAGS, &ifr)) {
358                 /* if device does not exist, treat like not up */
359                 if (errno != ENODEV && errno != ENXIO)
360                         perror("SIOCGIFFLAGS");
361                 goto END_FALSE;
362         }
363
364         if (!(ifr.ifr_flags & IFF_UP)) /* iface is not up */
365                 goto END_FALSE;
366         if (ifup_strictness == IFUP_UP)
367                 goto END_TRUE;
368
369         if (!(ifr.ifr_flags & IFF_RUNNING))
370                 goto END_FALSE;
371         if (ifup_strictness == IFUP_LINK)
372                 goto END_TRUE;
373
374         if (ioctl(fd, SIOCGIFADDR, &ifr)) {
375                 perror("SIOCGIFADDR");
376                 goto END_FALSE;
377         }
378         if (((struct sockaddr_in *)&(ifr.ifr_ifru.ifru_addr))->sin_addr.s_addr)
379                 goto END_TRUE;
380
381 END_FALSE:
382         close(fd);
383         return 0;
384 END_TRUE:
385         close(fd);
386         return 1;
387 }
388
389 static struct {
390         int nscount;
391         char **ns_list;
392 } dns_data = {
393         .nscount = 0,
394         .ns_list = NULL,
395 };
396
397 void free_dns_data(void)
398 {
399         int i;
400         for (i = 0; i < dns_data.nscount; i++)
401                 free(dns_data.ns_list[i]);
402         if (dns_data.ns_list)
403                 free(dns_data.ns_list);
404         memset(&dns_data, 0, sizeof(dns_data));
405 }
406
407 void update_dns_data(void)
408 {
409         FILE *fp;
410         char line[256];
411         //static double last_dns_update = 0.0;
412
413         /* maybe updating too often causes higher load because of /etc lying on a real FS
414         if (current_update_time - last_dns_update < 10.0)
415                 return;
416
417         last_dns_update = current_update_time;
418         */
419
420         free_dns_data();
421
422         if ((fp = fopen("/etc/resolv.conf", "r")) == NULL)
423                 return;
424         while(!feof(fp)) {
425                 if (fgets(line, 255, fp) == NULL) {
426                         break;
427                 }
428                 if (!strncmp(line, "nameserver ", 11)) {
429                         line[strlen(line) - 1] = '\0';  // remove trailing newline
430                         dns_data.nscount++;
431                         dns_data.ns_list = realloc(dns_data.ns_list, dns_data.nscount * sizeof(char *));
432                         dns_data.ns_list[dns_data.nscount - 1] = strndup(line + 11, text_buffer_size);
433                 }
434         }
435         fclose(fp);
436 }
437
438 void parse_nameserver_arg(struct text_object *obj, const char *arg)
439 {
440         obj->data.l = arg ? atoi(arg) : 0;
441 }
442
443 void print_nameserver(struct text_object *obj, char *p, int p_max_size)
444 {
445         if (dns_data.nscount > obj->data.l)
446                 snprintf(p, p_max_size, "%s", dns_data.ns_list[obj->data.l]);
447 }