]> arthur.barton.de Git - netdata.git/blob - src/proc_net_dev.c
allow any pattern of network interfaces and disk paths to be excluded; fixes #1556...
[netdata.git] / src / proc_net_dev.c
1 #include "common.h"
2
3 struct netdev {
4     char *name;
5     uint32_t hash;
6     size_t len;
7
8     // flags
9     int configured;
10     int enabled;
11
12     int do_bandwidth;
13     int do_packets;
14     int do_errors;
15     int do_drops;
16     int do_fifo;
17     int do_compressed;
18     int do_events;
19
20     // data collected
21     unsigned long long rbytes;
22     unsigned long long rpackets;
23     unsigned long long rerrors;
24     unsigned long long rdrops;
25     unsigned long long rfifo;
26     unsigned long long rframe;
27     unsigned long long rcompressed;
28     unsigned long long rmulticast;
29
30     unsigned long long tbytes;
31     unsigned long long tpackets;
32     unsigned long long terrors;
33     unsigned long long tdrops;
34     unsigned long long tfifo;
35     unsigned long long tcollisions;
36     unsigned long long tcarrier;
37     unsigned long long tcompressed;
38
39     // charts
40     RRDSET *st_bandwidth;
41     RRDSET *st_packets;
42     RRDSET *st_errors;
43     RRDSET *st_drops;
44     RRDSET *st_fifo;
45     RRDSET *st_compressed;
46     RRDSET *st_events;
47
48     // dimensions
49     RRDDIM *rd_rbytes;
50     RRDDIM *rd_rpackets;
51     RRDDIM *rd_rerrors;
52     RRDDIM *rd_rdrops;
53     RRDDIM *rd_rfifo;
54     RRDDIM *rd_rframe;
55     RRDDIM *rd_rcompressed;
56     RRDDIM *rd_rmulticast;
57
58     RRDDIM *rd_tbytes;
59     RRDDIM *rd_tpackets;
60     RRDDIM *rd_terrors;
61     RRDDIM *rd_tdrops;
62     RRDDIM *rd_tfifo;
63     RRDDIM *rd_tcollisions;
64     RRDDIM *rd_tcarrier;
65     RRDDIM *rd_tcompressed;
66
67     struct netdev *next;
68 };
69
70 static struct netdev *netdev_root = NULL;
71
72 static struct netdev *get_netdev(const char *name) {
73     static struct netdev *last = NULL;
74     struct netdev *d;
75
76     uint32_t hash = simple_hash(name);
77
78     // search it, from the last position to the end
79     for(d = last ; d ; d = d->next) {
80         if(unlikely(hash == d->hash && !strcmp(name, d->name))) {
81             last = d->next;
82             return d;
83         }
84     }
85
86     // search it from the beginning to the last position we used
87     for(d = netdev_root ; d != last ; d = d->next) {
88         if(unlikely(hash == d->hash && !strcmp(name, d->name))) {
89             last = d->next;
90             return d;
91         }
92     }
93
94     // create a new one
95     d = callocz(1, sizeof(struct netdev));
96     d->name = strdupz(name);
97     d->hash = simple_hash(d->name);
98     d->len = strlen(d->name);
99
100     // link it to the end
101     if(netdev_root) {
102         struct netdev *e;
103         for(e = netdev_root; e->next ; e = e->next) ;
104         e->next = d;
105     }
106     else
107         netdev_root = d;
108
109     return d;
110 }
111
112 int do_proc_net_dev(int update_every, usec_t dt) {
113     (void)dt;
114
115     static NETDATA_SIMPLE_PATTERN *disabled_list = NULL;
116     static procfile *ff = NULL;
117     static int enable_new_interfaces = -1;
118     static int do_bandwidth = -1, do_packets = -1, do_errors = -1, do_drops = -1, do_fifo = -1, do_compressed = -1, do_events = -1;
119
120     if(unlikely(enable_new_interfaces == -1)) {
121         enable_new_interfaces = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_ONDEMAND);
122
123         do_bandwidth    = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "bandwidth for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
124         do_packets      = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
125         do_errors       = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "errors for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
126         do_drops        = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "drops for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
127         do_fifo         = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "fifo for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
128         do_compressed   = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "compressed packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
129         do_events       = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "frames, collisions, carrier counters for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
130
131         disabled_list = netdata_simple_pattern_list_create(config_get("plugin:proc:/proc/net/dev", "disable by default interfaces matching", "lo fireqos* *-ifb"), NETDATA_SIMPLE_PATTERN_MODE_EXACT);
132     }
133
134     if(unlikely(!ff)) {
135         char filename[FILENAME_MAX + 1];
136         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/net/dev");
137         ff = procfile_open(config_get("plugin:proc:/proc/net/dev", "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT);
138         if(unlikely(!ff)) return 1;
139     }
140
141     ff = procfile_readall(ff);
142     if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
143
144     uint32_t lines = procfile_lines(ff), l;
145     for(l = 2; l < lines ;l++) {
146         // require 17 words on each line
147         if(unlikely(procfile_linewords(ff, l) < 17)) continue;
148
149         struct netdev *d = get_netdev(procfile_lineword(ff, l, 0));
150
151         if(unlikely(!d->configured)) {
152             // this is the first time we see this interface
153
154             // remember we configured it
155             d->configured = 1;
156
157             d->enabled = enable_new_interfaces;
158
159             if(d->enabled)
160                 d->enabled = !netdata_simple_pattern_list_matches(disabled_list, d->name);
161
162             char var_name[512 + 1];
163             snprintfz(var_name, 512, "plugin:proc:/proc/net/dev:%s", d->name);
164             d->enabled = config_get_boolean_ondemand(var_name, "enabled", d->enabled);
165
166             if(d->enabled == CONFIG_ONDEMAND_NO)
167                 continue;
168
169             d->do_bandwidth = config_get_boolean_ondemand(var_name, "bandwidth", do_bandwidth);
170             d->do_packets = config_get_boolean_ondemand(var_name, "packets", do_packets);
171             d->do_errors = config_get_boolean_ondemand(var_name, "errors", do_errors);
172             d->do_drops = config_get_boolean_ondemand(var_name, "drops", do_drops);
173             d->do_fifo = config_get_boolean_ondemand(var_name, "fifo", do_fifo);
174             d->do_compressed = config_get_boolean_ondemand(var_name, "compressed", do_compressed);
175             d->do_events = config_get_boolean_ondemand(var_name, "events", do_events);
176         }
177
178         if(unlikely(!d->enabled))
179             continue;
180
181         d->rbytes      = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
182         d->rpackets    = strtoull(procfile_lineword(ff, l, 2), NULL, 10);
183         d->rerrors     = strtoull(procfile_lineword(ff, l, 3), NULL, 10);
184         d->rdrops      = strtoull(procfile_lineword(ff, l, 4), NULL, 10);
185         d->rfifo       = strtoull(procfile_lineword(ff, l, 5), NULL, 10);
186         d->rframe      = strtoull(procfile_lineword(ff, l, 6), NULL, 10);
187         d->rcompressed = strtoull(procfile_lineword(ff, l, 7), NULL, 10);
188         d->rmulticast  = strtoull(procfile_lineword(ff, l, 8), NULL, 10);
189
190         d->tbytes      = strtoull(procfile_lineword(ff, l, 9), NULL, 10);
191         d->tpackets    = strtoull(procfile_lineword(ff, l, 10), NULL, 10);
192         d->terrors     = strtoull(procfile_lineword(ff, l, 11), NULL, 10);
193         d->tdrops      = strtoull(procfile_lineword(ff, l, 12), NULL, 10);
194         d->tfifo       = strtoull(procfile_lineword(ff, l, 13), NULL, 10);
195         d->tcollisions = strtoull(procfile_lineword(ff, l, 14), NULL, 10);
196         d->tcarrier    = strtoull(procfile_lineword(ff, l, 15), NULL, 10);
197         d->tcompressed = strtoull(procfile_lineword(ff, l, 16), NULL, 10);
198
199         // --------------------------------------------------------------------
200
201         if(unlikely((d->do_bandwidth == CONFIG_ONDEMAND_ONDEMAND && (d->rbytes || d->tbytes))))
202             d->do_bandwidth = CONFIG_ONDEMAND_YES;
203
204         if(d->do_bandwidth == CONFIG_ONDEMAND_YES) {
205             if(unlikely(!d->st_bandwidth)) {
206                 d->st_bandwidth = rrdset_find_bytype("net", d->name);
207
208                 if(!d->st_bandwidth) {
209                     d->st_bandwidth = rrdset_create("net", d->name, NULL, d->name, "net.net", "Bandwidth", "kilobits/s", 7000, update_every, RRDSET_TYPE_AREA);
210                     d->rd_rbytes = rrddim_add(d->st_bandwidth, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
211                     d->rd_tbytes = rrddim_add(d->st_bandwidth, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
212                 }
213             }
214             else rrdset_next(d->st_bandwidth);
215
216             rrddim_set_by_pointer(d->st_bandwidth, d->rd_rbytes, d->rbytes);
217             rrddim_set_by_pointer(d->st_bandwidth, d->rd_tbytes, d->tbytes);
218             rrdset_done(d->st_bandwidth);
219         }
220
221         // --------------------------------------------------------------------
222
223         if(unlikely((d->do_packets == CONFIG_ONDEMAND_ONDEMAND && (d->rpackets || d->tpackets || d->rmulticast))))
224             d->do_packets = CONFIG_ONDEMAND_YES;
225
226         if(d->do_packets == CONFIG_ONDEMAND_YES) {
227             if(unlikely(!d->st_packets)) {
228                 d->st_packets = rrdset_find_bytype("net_packets", d->name);
229
230                 if(!d->st_packets) {
231                     d->st_packets = rrdset_create("net_packets", d->name, NULL, d->name, "net.packets", "Packets", "packets/s", 7001, update_every, RRDSET_TYPE_LINE);
232                     d->st_packets->isdetail = 1;
233
234                     d->rd_rpackets = rrddim_add(d->st_packets, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
235                     d->rd_tpackets = rrddim_add(d->st_packets, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
236                     d->rd_rmulticast = rrddim_add(d->st_packets, "multicast", NULL, 1, 1, RRDDIM_INCREMENTAL);
237                 }
238             }
239             else rrdset_next(d->st_packets);
240
241             rrddim_set_by_pointer(d->st_packets, d->rd_rpackets, d->rpackets);
242             rrddim_set_by_pointer(d->st_packets, d->rd_tpackets, d->tpackets);
243             rrddim_set_by_pointer(d->st_packets, d->rd_rmulticast, d->rmulticast);
244             rrdset_done(d->st_packets);
245         }
246
247         // --------------------------------------------------------------------
248
249         if(unlikely((d->do_errors == CONFIG_ONDEMAND_ONDEMAND && (d->rerrors || d->terrors))))
250             d->do_errors = CONFIG_ONDEMAND_YES;
251
252         if(d->do_errors == CONFIG_ONDEMAND_YES) {
253             if(unlikely(!d->st_errors)) {
254                 d->st_errors = rrdset_find_bytype("net_errors", d->name);
255
256                 if(!d->st_errors) {
257                     d->st_errors = rrdset_create("net_errors", d->name, NULL, d->name, "net.errors", "Interface Errors", "errors/s", 7002, update_every, RRDSET_TYPE_LINE);
258                     d->st_errors->isdetail = 1;
259
260                     d->rd_rerrors = rrddim_add(d->st_errors, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
261                     d->rd_terrors = rrddim_add(d->st_errors, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
262                 }
263             }
264             else rrdset_next(d->st_errors);
265
266             rrddim_set_by_pointer(d->st_errors, d->rd_rerrors, d->rerrors);
267             rrddim_set_by_pointer(d->st_errors, d->rd_terrors, d->terrors);
268             rrdset_done(d->st_errors);
269         }
270
271         // --------------------------------------------------------------------
272
273         if(unlikely((d->do_drops == CONFIG_ONDEMAND_ONDEMAND && (d->rdrops || d->tdrops))))
274             d->do_drops = CONFIG_ONDEMAND_YES;
275
276         if(d->do_drops == CONFIG_ONDEMAND_YES) {
277             if(unlikely(!d->st_drops)) {
278                 d->st_drops = rrdset_find_bytype("net_drops", d->name);
279
280                 if(!d->st_drops) {
281                     d->st_drops = rrdset_create("net_drops", d->name, NULL, d->name, "net.drops", "Interface Drops", "drops/s", 7003, update_every, RRDSET_TYPE_LINE);
282                     d->st_drops->isdetail = 1;
283
284                     d->rd_rdrops = rrddim_add(d->st_drops, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
285                     d->rd_tdrops = rrddim_add(d->st_drops, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
286                 }
287             }
288             else rrdset_next(d->st_drops);
289
290             rrddim_set_by_pointer(d->st_drops, d->rd_rdrops, d->rdrops);
291             rrddim_set_by_pointer(d->st_drops, d->rd_tdrops, d->tdrops);
292             rrdset_done(d->st_drops);
293         }
294
295         // --------------------------------------------------------------------
296
297         if(unlikely((d->do_fifo == CONFIG_ONDEMAND_ONDEMAND && (d->rfifo || d->tfifo))))
298             d->do_fifo = CONFIG_ONDEMAND_YES;
299
300         if(d->do_fifo == CONFIG_ONDEMAND_YES) {
301             if(unlikely(!d->st_fifo)) {
302                 d->st_fifo = rrdset_find_bytype("net_fifo", d->name);
303
304                 if(!d->st_fifo) {
305                     d->st_fifo = rrdset_create("net_fifo", d->name, NULL, d->name, "net.fifo", "Interface FIFO Buffer Errors", "errors", 7004, update_every, RRDSET_TYPE_LINE);
306                     d->st_fifo->isdetail = 1;
307
308                     d->rd_rfifo = rrddim_add(d->st_fifo, "receive", NULL, 1, 1, RRDDIM_INCREMENTAL);
309                     d->rd_tfifo = rrddim_add(d->st_fifo, "transmit", NULL, -1, 1, RRDDIM_INCREMENTAL);
310                 }
311             }
312             else rrdset_next(d->st_fifo);
313
314             rrddim_set_by_pointer(d->st_fifo, d->rd_rfifo, d->rfifo);
315             rrddim_set_by_pointer(d->st_fifo, d->rd_tfifo, d->tfifo);
316             rrdset_done(d->st_fifo);
317         }
318
319         // --------------------------------------------------------------------
320
321         if(unlikely((d->do_compressed == CONFIG_ONDEMAND_ONDEMAND && (d->rcompressed || d->tcompressed))))
322             d->do_compressed = CONFIG_ONDEMAND_YES;
323
324         if(d->do_compressed == CONFIG_ONDEMAND_YES) {
325             if(unlikely(!d->st_compressed)) {
326                 d->st_compressed = rrdset_find_bytype("net_compressed", d->name);
327                 if(!d->st_compressed) {
328                     d->st_compressed = rrdset_create("net_compressed", d->name, NULL, d->name, "net.compressed", "Compressed Packets", "packets/s", 7005, update_every, RRDSET_TYPE_LINE);
329                     d->st_compressed->isdetail = 1;
330
331                     d->rd_rcompressed = rrddim_add(d->st_compressed, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
332                     d->rd_tcompressed = rrddim_add(d->st_compressed, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
333                 }
334             }
335             else rrdset_next(d->st_compressed);
336
337             rrddim_set_by_pointer(d->st_compressed, d->rd_rcompressed, d->rcompressed);
338             rrddim_set_by_pointer(d->st_compressed, d->rd_tcompressed, d->tcompressed);
339             rrdset_done(d->st_compressed);
340         }
341
342         // --------------------------------------------------------------------
343
344         if(unlikely((d->do_events == CONFIG_ONDEMAND_ONDEMAND && (d->rframe || d->tcollisions || d->tcarrier))))
345             d->do_events = CONFIG_ONDEMAND_YES;
346
347         if(d->do_events == CONFIG_ONDEMAND_YES) {
348             if(unlikely(!d->st_events)) {
349                 d->st_events = rrdset_find_bytype("net_events", d->name);
350                 if(!d->st_events) {
351                     d->st_events = rrdset_create("net_events", d->name, NULL, d->name, "net.events", "Network Interface Events", "events/s", 7006, update_every, RRDSET_TYPE_LINE);
352                     d->st_events->isdetail = 1;
353
354                     d->rd_rframe      = rrddim_add(d->st_events, "frames", NULL, 1, 1, RRDDIM_INCREMENTAL);
355                     d->rd_tcollisions = rrddim_add(d->st_events, "collisions", NULL, -1, 1, RRDDIM_INCREMENTAL);
356                     d->rd_tcarrier    = rrddim_add(d->st_events, "carrier", NULL, -1, 1, RRDDIM_INCREMENTAL);
357                 }
358             }
359             else rrdset_next(d->st_events);
360
361             rrddim_set_by_pointer(d->st_events, d->rd_rframe,      d->rframe);
362             rrddim_set_by_pointer(d->st_events, d->rd_tcollisions, d->tcollisions);
363             rrddim_set_by_pointer(d->st_events, d->rd_tcarrier,    d->tcarrier);
364             rrdset_done(d->st_events);
365         }
366     }
367
368     return 0;
369 }