]> arthur.barton.de Git - netdata.git/blob - src/proc_net_dev.c
9b942326240baf710e9848d705b1635c3c8bcf92
[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     kernel_uint_t rbytes;
22     kernel_uint_t rpackets;
23     kernel_uint_t rerrors;
24     kernel_uint_t rdrops;
25     kernel_uint_t rfifo;
26     kernel_uint_t rframe;
27     kernel_uint_t rcompressed;
28     kernel_uint_t rmulticast;
29
30     kernel_uint_t tbytes;
31     kernel_uint_t tpackets;
32     kernel_uint_t terrors;
33     kernel_uint_t tdrops;
34     kernel_uint_t tfifo;
35     kernel_uint_t tcollisions;
36     kernel_uint_t tcarrier;
37     kernel_uint_t 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     static SIMPLE_PATTERN *disabled_list = NULL;
115     static procfile *ff = NULL;
116     static int enable_new_interfaces = -1;
117     static int do_bandwidth = -1, do_packets = -1, do_errors = -1, do_drops = -1, do_fifo = -1, do_compressed = -1, do_events = -1;
118
119     if(unlikely(enable_new_interfaces == -1)) {
120         enable_new_interfaces = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "enable new interfaces detected at runtime", CONFIG_BOOLEAN_AUTO);
121
122         do_bandwidth    = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "bandwidth for all interfaces", CONFIG_BOOLEAN_AUTO);
123         do_packets      = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "packets for all interfaces", CONFIG_BOOLEAN_AUTO);
124         do_errors       = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "errors for all interfaces", CONFIG_BOOLEAN_AUTO);
125         do_drops        = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "drops for all interfaces", CONFIG_BOOLEAN_AUTO);
126         do_fifo         = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "fifo for all interfaces", CONFIG_BOOLEAN_AUTO);
127         do_compressed   = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "compressed packets for all interfaces", CONFIG_BOOLEAN_AUTO);
128         do_events       = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "frames, collisions, carrier counters for all interfaces", CONFIG_BOOLEAN_AUTO);
129
130         disabled_list = simple_pattern_create(
131                 config_get("plugin:proc:/proc/net/dev", "disable by default interfaces matching", "lo fireqos* *-ifb")
132                 , SIMPLE_PATTERN_EXACT);
133     }
134
135     if(unlikely(!ff)) {
136         char filename[FILENAME_MAX + 1];
137         snprintfz(filename, FILENAME_MAX, "%s%s", netdata_configured_host_prefix, "/proc/net/dev");
138         ff = procfile_open(config_get("plugin:proc:/proc/net/dev", "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT);
139         if(unlikely(!ff)) return 1;
140     }
141
142     ff = procfile_readall(ff);
143     if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
144
145     size_t lines = procfile_lines(ff), l;
146     for(l = 2; l < lines ;l++) {
147         // require 17 words on each line
148         if(unlikely(procfile_linewords(ff, l) < 17)) continue;
149
150         struct netdev *d = get_netdev(procfile_lineword(ff, l, 0));
151
152         if(unlikely(!d->configured)) {
153             // this is the first time we see this interface
154
155             // remember we configured it
156             d->configured = 1;
157
158             d->enabled = enable_new_interfaces;
159
160             if(d->enabled)
161                 d->enabled = !simple_pattern_matches(disabled_list, d->name);
162
163             char var_name[512 + 1];
164             snprintfz(var_name, 512, "plugin:proc:/proc/net/dev:%s", d->name);
165             d->enabled = config_get_boolean_ondemand(var_name, "enabled", d->enabled);
166
167             if(d->enabled == CONFIG_BOOLEAN_NO)
168                 continue;
169
170             d->do_bandwidth  = config_get_boolean_ondemand(var_name, "bandwidth", do_bandwidth);
171             d->do_packets    = config_get_boolean_ondemand(var_name, "packets", do_packets);
172             d->do_errors     = config_get_boolean_ondemand(var_name, "errors", do_errors);
173             d->do_drops      = config_get_boolean_ondemand(var_name, "drops", do_drops);
174             d->do_fifo       = config_get_boolean_ondemand(var_name, "fifo", do_fifo);
175             d->do_compressed = config_get_boolean_ondemand(var_name, "compressed", do_compressed);
176             d->do_events     = config_get_boolean_ondemand(var_name, "events", do_events);
177         }
178
179         if(unlikely(!d->enabled))
180             continue;
181
182         if(likely(d->do_bandwidth != CONFIG_BOOLEAN_NO)) {
183             d->rbytes      = str2kernel_uint_t(procfile_lineword(ff, l, 1));
184             d->tbytes      = str2kernel_uint_t(procfile_lineword(ff, l, 9));
185         }
186
187         if(likely(d->do_packets != CONFIG_BOOLEAN_NO)) {
188             d->rpackets    = str2kernel_uint_t(procfile_lineword(ff, l, 2));
189             d->rmulticast  = str2kernel_uint_t(procfile_lineword(ff, l, 8));
190             d->tpackets    = str2kernel_uint_t(procfile_lineword(ff, l, 10));
191         }
192
193         if(likely(d->do_errors != CONFIG_BOOLEAN_NO)) {
194             d->rerrors     = str2kernel_uint_t(procfile_lineword(ff, l, 3));
195             d->terrors     = str2kernel_uint_t(procfile_lineword(ff, l, 11));
196         }
197
198         if(likely(d->do_drops != CONFIG_BOOLEAN_NO)) {
199             d->rdrops      = str2kernel_uint_t(procfile_lineword(ff, l, 4));
200             d->tdrops      = str2kernel_uint_t(procfile_lineword(ff, l, 12));
201         }
202
203         if(likely(d->do_fifo != CONFIG_BOOLEAN_NO)) {
204             d->rfifo       = str2kernel_uint_t(procfile_lineword(ff, l, 5));
205             d->tfifo       = str2kernel_uint_t(procfile_lineword(ff, l, 13));
206         }
207
208         if(likely(d->do_compressed != CONFIG_BOOLEAN_NO)) {
209             d->rcompressed = str2kernel_uint_t(procfile_lineword(ff, l, 7));
210             d->tcompressed = str2kernel_uint_t(procfile_lineword(ff, l, 16));
211         }
212
213         if(likely(d->do_events != CONFIG_BOOLEAN_NO)) {
214             d->rframe      = str2kernel_uint_t(procfile_lineword(ff, l, 6));
215             d->tcollisions = str2kernel_uint_t(procfile_lineword(ff, l, 14));
216             d->tcarrier    = str2kernel_uint_t(procfile_lineword(ff, l, 15));
217         }
218
219         // --------------------------------------------------------------------
220
221         if(unlikely((d->do_bandwidth == CONFIG_BOOLEAN_AUTO && (d->rbytes || d->tbytes))))
222             d->do_bandwidth = CONFIG_BOOLEAN_YES;
223
224         if(d->do_bandwidth == CONFIG_BOOLEAN_YES) {
225             if(unlikely(!d->st_bandwidth)) {
226                 d->st_bandwidth = rrdset_find_bytype_localhost("net", d->name);
227
228                 if(!d->st_bandwidth)
229                     d->st_bandwidth = rrdset_create_localhost("net", d->name, NULL, d->name, "net.net", "Bandwidth"
230                                                               , "kilobits/s", 7000, update_every, RRDSET_TYPE_AREA);
231
232                 d->rd_rbytes = rrddim_add(d->st_bandwidth, "received", NULL, 8, 1024, RRD_ALGORITHM_INCREMENTAL);
233                 d->rd_tbytes = rrddim_add(d->st_bandwidth, "sent", NULL, -8, 1024, RRD_ALGORITHM_INCREMENTAL);
234             }
235             else rrdset_next(d->st_bandwidth);
236
237             rrddim_set_by_pointer(d->st_bandwidth, d->rd_rbytes, d->rbytes);
238             rrddim_set_by_pointer(d->st_bandwidth, d->rd_tbytes, d->tbytes);
239             rrdset_done(d->st_bandwidth);
240         }
241
242         // --------------------------------------------------------------------
243
244         if(unlikely((d->do_packets == CONFIG_BOOLEAN_AUTO && (d->rpackets || d->tpackets || d->rmulticast))))
245             d->do_packets = CONFIG_BOOLEAN_YES;
246
247         if(d->do_packets == CONFIG_BOOLEAN_YES) {
248             if(unlikely(!d->st_packets)) {
249                 d->st_packets = rrdset_find_bytype_localhost("net_packets", d->name);
250
251                 if(!d->st_packets)
252                     d->st_packets = rrdset_create_localhost("net_packets", d->name, NULL, d->name, "net.packets"
253                                                             , "Packets", "packets/s", 7001, update_every
254                                                             , RRDSET_TYPE_LINE);
255                 rrdset_flag_set(d->st_packets, RRDSET_FLAG_DETAIL);
256
257                 d->rd_rpackets = rrddim_add(d->st_packets, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
258                 d->rd_tpackets = rrddim_add(d->st_packets, "sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
259                 d->rd_rmulticast = rrddim_add(d->st_packets, "multicast", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
260             }
261             else rrdset_next(d->st_packets);
262
263             rrddim_set_by_pointer(d->st_packets, d->rd_rpackets, d->rpackets);
264             rrddim_set_by_pointer(d->st_packets, d->rd_tpackets, d->tpackets);
265             rrddim_set_by_pointer(d->st_packets, d->rd_rmulticast, d->rmulticast);
266             rrdset_done(d->st_packets);
267         }
268
269         // --------------------------------------------------------------------
270
271         if(unlikely((d->do_errors == CONFIG_BOOLEAN_AUTO && (d->rerrors || d->terrors))))
272             d->do_errors = CONFIG_BOOLEAN_YES;
273
274         if(d->do_errors == CONFIG_BOOLEAN_YES) {
275             if(unlikely(!d->st_errors)) {
276                 d->st_errors = rrdset_find_bytype_localhost("net_errors", d->name);
277
278                 if(!d->st_errors)
279                     d->st_errors = rrdset_create_localhost("net_errors", d->name, NULL, d->name, "net.errors"
280                                                            , "Interface Errors", "errors/s", 7002, update_every
281                                                            , RRDSET_TYPE_LINE);
282
283                 rrdset_flag_set(d->st_errors, RRDSET_FLAG_DETAIL);
284
285                 d->rd_rerrors = rrddim_add(d->st_errors, "inbound", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
286                 d->rd_terrors = rrddim_add(d->st_errors, "outbound", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
287             }
288             else rrdset_next(d->st_errors);
289
290             rrddim_set_by_pointer(d->st_errors, d->rd_rerrors, d->rerrors);
291             rrddim_set_by_pointer(d->st_errors, d->rd_terrors, d->terrors);
292             rrdset_done(d->st_errors);
293         }
294
295         // --------------------------------------------------------------------
296
297         if(unlikely((d->do_drops == CONFIG_BOOLEAN_AUTO && (d->rdrops || d->tdrops))))
298             d->do_drops = CONFIG_BOOLEAN_YES;
299
300         if(d->do_drops == CONFIG_BOOLEAN_YES) {
301             if(unlikely(!d->st_drops)) {
302                 d->st_drops = rrdset_find_bytype_localhost("net_drops", d->name);
303
304                 if(!d->st_drops)
305                     d->st_drops = rrdset_create_localhost("net_drops", d->name, NULL, d->name, "net.drops"
306                                                           , "Interface Drops", "drops/s", 7003, update_every
307                                                           , RRDSET_TYPE_LINE);
308
309                 rrdset_flag_set(d->st_drops, RRDSET_FLAG_DETAIL);
310
311                 d->rd_rdrops = rrddim_add(d->st_drops, "inbound", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
312                 d->rd_tdrops = rrddim_add(d->st_drops, "outbound", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
313             }
314             else rrdset_next(d->st_drops);
315
316             rrddim_set_by_pointer(d->st_drops, d->rd_rdrops, d->rdrops);
317             rrddim_set_by_pointer(d->st_drops, d->rd_tdrops, d->tdrops);
318             rrdset_done(d->st_drops);
319         }
320
321         // --------------------------------------------------------------------
322
323         if(unlikely((d->do_fifo == CONFIG_BOOLEAN_AUTO && (d->rfifo || d->tfifo))))
324             d->do_fifo = CONFIG_BOOLEAN_YES;
325
326         if(d->do_fifo == CONFIG_BOOLEAN_YES) {
327             if(unlikely(!d->st_fifo)) {
328                 d->st_fifo = rrdset_find_bytype_localhost("net_fifo", d->name);
329
330                 if(!d->st_fifo)
331                     d->st_fifo = rrdset_create_localhost("net_fifo", d->name, NULL, d->name, "net.fifo"
332                                                          , "Interface FIFO Buffer Errors", "errors", 7004, update_every
333                                                          , RRDSET_TYPE_LINE);
334
335                 rrdset_flag_set(d->st_fifo, RRDSET_FLAG_DETAIL);
336
337                 d->rd_rfifo = rrddim_add(d->st_fifo, "receive", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
338                 d->rd_tfifo = rrddim_add(d->st_fifo, "transmit", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
339             }
340             else rrdset_next(d->st_fifo);
341
342             rrddim_set_by_pointer(d->st_fifo, d->rd_rfifo, d->rfifo);
343             rrddim_set_by_pointer(d->st_fifo, d->rd_tfifo, d->tfifo);
344             rrdset_done(d->st_fifo);
345         }
346
347         // --------------------------------------------------------------------
348
349         if(unlikely((d->do_compressed == CONFIG_BOOLEAN_AUTO && (d->rcompressed || d->tcompressed))))
350             d->do_compressed = CONFIG_BOOLEAN_YES;
351
352         if(d->do_compressed == CONFIG_BOOLEAN_YES) {
353             if(unlikely(!d->st_compressed)) {
354                 d->st_compressed = rrdset_find_bytype_localhost("net_compressed", d->name);
355                 if(!d->st_compressed)
356                     d->st_compressed = rrdset_create_localhost("net_compressed", d->name, NULL, d->name
357                                                                , "net.compressed", "Compressed Packets", "packets/s"
358                                                                , 7005, update_every, RRDSET_TYPE_LINE);
359
360                 rrdset_flag_set(d->st_compressed, RRDSET_FLAG_DETAIL);
361
362                 d->rd_rcompressed = rrddim_add(d->st_compressed, "received", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
363                 d->rd_tcompressed = rrddim_add(d->st_compressed, "sent", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
364             }
365             else rrdset_next(d->st_compressed);
366
367             rrddim_set_by_pointer(d->st_compressed, d->rd_rcompressed, d->rcompressed);
368             rrddim_set_by_pointer(d->st_compressed, d->rd_tcompressed, d->tcompressed);
369             rrdset_done(d->st_compressed);
370         }
371
372         // --------------------------------------------------------------------
373
374         if(unlikely((d->do_events == CONFIG_BOOLEAN_AUTO && (d->rframe || d->tcollisions || d->tcarrier))))
375             d->do_events = CONFIG_BOOLEAN_YES;
376
377         if(d->do_events == CONFIG_BOOLEAN_YES) {
378             if(unlikely(!d->st_events)) {
379                 d->st_events = rrdset_find_bytype_localhost("net_events", d->name);
380                 if(!d->st_events)
381                     d->st_events = rrdset_create_localhost("net_events", d->name, NULL, d->name, "net.events"
382                                                            , "Network Interface Events", "events/s", 7006, update_every
383                                                            , RRDSET_TYPE_LINE);
384
385                 rrdset_flag_set(d->st_events, RRDSET_FLAG_DETAIL);
386
387                 d->rd_rframe      = rrddim_add(d->st_events, "frames", NULL, 1, 1, RRD_ALGORITHM_INCREMENTAL);
388                 d->rd_tcollisions = rrddim_add(d->st_events, "collisions", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
389                 d->rd_tcarrier    = rrddim_add(d->st_events, "carrier", NULL, -1, 1, RRD_ALGORITHM_INCREMENTAL);
390             }
391             else rrdset_next(d->st_events);
392
393             rrddim_set_by_pointer(d->st_events, d->rd_rframe,      d->rframe);
394             rrddim_set_by_pointer(d->st_events, d->rd_tcollisions, d->tcollisions);
395             rrddim_set_by_pointer(d->st_events, d->rd_tcarrier,    d->tcarrier);
396             rrdset_done(d->st_events);
397         }
398     }
399
400     return 0;
401 }