22 kernel_uint_t rpackets;
23 kernel_uint_t rerrors;
27 kernel_uint_t rcompressed;
28 kernel_uint_t rmulticast;
31 kernel_uint_t tpackets;
32 kernel_uint_t terrors;
35 kernel_uint_t tcollisions;
36 kernel_uint_t tcarrier;
37 kernel_uint_t tcompressed;
45 RRDSET *st_compressed;
55 RRDDIM *rd_rcompressed;
56 RRDDIM *rd_rmulticast;
63 RRDDIM *rd_tcollisions;
65 RRDDIM *rd_tcompressed;
70 static struct netdev *netdev_root = NULL;
72 static struct netdev *get_netdev(const char *name) {
73 static struct netdev *last = NULL;
76 uint32_t hash = simple_hash(name);
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))) {
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))) {
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);
100 // link it to the end
103 for(e = netdev_root; e->next ; e = e->next) ;
112 int do_proc_net_dev(int update_every, usec_t 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;
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_ONDEMAND_ONDEMAND);
122 do_bandwidth = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "bandwidth for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
123 do_packets = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
124 do_errors = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "errors for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
125 do_drops = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "drops for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
126 do_fifo = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "fifo for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
127 do_compressed = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "compressed packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
128 do_events = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "frames, collisions, carrier counters for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
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);
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;
142 ff = procfile_readall(ff);
143 if(unlikely(!ff)) return 0; // we return 0, so that we will retry to open it next time
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;
150 struct netdev *d = get_netdev(procfile_lineword(ff, l, 0));
152 if(unlikely(!d->configured)) {
153 // this is the first time we see this interface
155 // remember we configured it
158 d->enabled = enable_new_interfaces;
161 d->enabled = !simple_pattern_matches(disabled_list, d->name);
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);
167 if(d->enabled == CONFIG_ONDEMAND_NO)
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);
179 if(unlikely(!d->enabled))
182 if(likely(d->do_bandwidth != CONFIG_ONDEMAND_NO)) {
183 d->rbytes = str2kernel_uint_t(procfile_lineword(ff, l, 1));
184 d->tbytes = str2kernel_uint_t(procfile_lineword(ff, l, 9));
187 if(likely(d->do_packets != CONFIG_ONDEMAND_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));
193 if(likely(d->do_errors != CONFIG_ONDEMAND_NO)) {
194 d->rerrors = str2kernel_uint_t(procfile_lineword(ff, l, 3));
195 d->terrors = str2kernel_uint_t(procfile_lineword(ff, l, 11));
198 if(likely(d->do_drops != CONFIG_ONDEMAND_NO)) {
199 d->rdrops = str2kernel_uint_t(procfile_lineword(ff, l, 4));
200 d->tdrops = str2kernel_uint_t(procfile_lineword(ff, l, 12));
203 if(likely(d->do_fifo != CONFIG_ONDEMAND_NO)) {
204 d->rfifo = str2kernel_uint_t(procfile_lineword(ff, l, 5));
205 d->tfifo = str2kernel_uint_t(procfile_lineword(ff, l, 13));
208 if(likely(d->do_compressed != CONFIG_ONDEMAND_NO)) {
209 d->rcompressed = str2kernel_uint_t(procfile_lineword(ff, l, 7));
210 d->tcompressed = str2kernel_uint_t(procfile_lineword(ff, l, 16));
213 if(likely(d->do_events != CONFIG_ONDEMAND_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));
219 // --------------------------------------------------------------------
221 if(unlikely((d->do_bandwidth == CONFIG_ONDEMAND_ONDEMAND && (d->rbytes || d->tbytes))))
222 d->do_bandwidth = CONFIG_ONDEMAND_YES;
224 if(d->do_bandwidth == CONFIG_ONDEMAND_YES) {
225 if(unlikely(!d->st_bandwidth)) {
226 d->st_bandwidth = rrdset_find_bytype("net", d->name);
229 d->st_bandwidth = rrdset_create("net", d->name, NULL, d->name, "net.net", "Bandwidth", "kilobits/s", 7000, update_every, RRDSET_TYPE_AREA);
231 d->rd_rbytes = rrddim_add(d->st_bandwidth, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
232 d->rd_tbytes = rrddim_add(d->st_bandwidth, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
234 else rrdset_next(d->st_bandwidth);
236 rrddim_set_by_pointer(d->st_bandwidth, d->rd_rbytes, d->rbytes);
237 rrddim_set_by_pointer(d->st_bandwidth, d->rd_tbytes, d->tbytes);
238 rrdset_done(d->st_bandwidth);
241 // --------------------------------------------------------------------
243 if(unlikely((d->do_packets == CONFIG_ONDEMAND_ONDEMAND && (d->rpackets || d->tpackets || d->rmulticast))))
244 d->do_packets = CONFIG_ONDEMAND_YES;
246 if(d->do_packets == CONFIG_ONDEMAND_YES) {
247 if(unlikely(!d->st_packets)) {
248 d->st_packets = rrdset_find_bytype("net_packets", d->name);
251 d->st_packets = rrdset_create("net_packets", d->name, NULL, d->name, "net.packets", "Packets", "packets/s", 7001, update_every, RRDSET_TYPE_LINE);
253 d->st_packets->isdetail = 1;
255 d->rd_rpackets = rrddim_add(d->st_packets, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
256 d->rd_tpackets = rrddim_add(d->st_packets, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
257 d->rd_rmulticast = rrddim_add(d->st_packets, "multicast", NULL, 1, 1, RRDDIM_INCREMENTAL);
259 else rrdset_next(d->st_packets);
261 rrddim_set_by_pointer(d->st_packets, d->rd_rpackets, d->rpackets);
262 rrddim_set_by_pointer(d->st_packets, d->rd_tpackets, d->tpackets);
263 rrddim_set_by_pointer(d->st_packets, d->rd_rmulticast, d->rmulticast);
264 rrdset_done(d->st_packets);
267 // --------------------------------------------------------------------
269 if(unlikely((d->do_errors == CONFIG_ONDEMAND_ONDEMAND && (d->rerrors || d->terrors))))
270 d->do_errors = CONFIG_ONDEMAND_YES;
272 if(d->do_errors == CONFIG_ONDEMAND_YES) {
273 if(unlikely(!d->st_errors)) {
274 d->st_errors = rrdset_find_bytype("net_errors", d->name);
277 d->st_errors = rrdset_create("net_errors", d->name, NULL, d->name, "net.errors", "Interface Errors", "errors/s", 7002, update_every, RRDSET_TYPE_LINE);
279 d->st_errors->isdetail = 1;
281 d->rd_rerrors = rrddim_add(d->st_errors, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
282 d->rd_terrors = rrddim_add(d->st_errors, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
284 else rrdset_next(d->st_errors);
286 rrddim_set_by_pointer(d->st_errors, d->rd_rerrors, d->rerrors);
287 rrddim_set_by_pointer(d->st_errors, d->rd_terrors, d->terrors);
288 rrdset_done(d->st_errors);
291 // --------------------------------------------------------------------
293 if(unlikely((d->do_drops == CONFIG_ONDEMAND_ONDEMAND && (d->rdrops || d->tdrops))))
294 d->do_drops = CONFIG_ONDEMAND_YES;
296 if(d->do_drops == CONFIG_ONDEMAND_YES) {
297 if(unlikely(!d->st_drops)) {
298 d->st_drops = rrdset_find_bytype("net_drops", d->name);
301 d->st_drops = rrdset_create("net_drops", d->name, NULL, d->name, "net.drops", "Interface Drops", "drops/s", 7003, update_every, RRDSET_TYPE_LINE);
303 d->st_drops->isdetail = 1;
305 d->rd_rdrops = rrddim_add(d->st_drops, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
306 d->rd_tdrops = rrddim_add(d->st_drops, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
308 else rrdset_next(d->st_drops);
310 rrddim_set_by_pointer(d->st_drops, d->rd_rdrops, d->rdrops);
311 rrddim_set_by_pointer(d->st_drops, d->rd_tdrops, d->tdrops);
312 rrdset_done(d->st_drops);
315 // --------------------------------------------------------------------
317 if(unlikely((d->do_fifo == CONFIG_ONDEMAND_ONDEMAND && (d->rfifo || d->tfifo))))
318 d->do_fifo = CONFIG_ONDEMAND_YES;
320 if(d->do_fifo == CONFIG_ONDEMAND_YES) {
321 if(unlikely(!d->st_fifo)) {
322 d->st_fifo = rrdset_find_bytype("net_fifo", d->name);
325 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);
327 d->st_fifo->isdetail = 1;
329 d->rd_rfifo = rrddim_add(d->st_fifo, "receive", NULL, 1, 1, RRDDIM_INCREMENTAL);
330 d->rd_tfifo = rrddim_add(d->st_fifo, "transmit", NULL, -1, 1, RRDDIM_INCREMENTAL);
332 else rrdset_next(d->st_fifo);
334 rrddim_set_by_pointer(d->st_fifo, d->rd_rfifo, d->rfifo);
335 rrddim_set_by_pointer(d->st_fifo, d->rd_tfifo, d->tfifo);
336 rrdset_done(d->st_fifo);
339 // --------------------------------------------------------------------
341 if(unlikely((d->do_compressed == CONFIG_ONDEMAND_ONDEMAND && (d->rcompressed || d->tcompressed))))
342 d->do_compressed = CONFIG_ONDEMAND_YES;
344 if(d->do_compressed == CONFIG_ONDEMAND_YES) {
345 if(unlikely(!d->st_compressed)) {
346 d->st_compressed = rrdset_find_bytype("net_compressed", d->name);
347 if(!d->st_compressed)
348 d->st_compressed = rrdset_create("net_compressed", d->name, NULL, d->name, "net.compressed", "Compressed Packets", "packets/s", 7005, update_every, RRDSET_TYPE_LINE);
350 d->st_compressed->isdetail = 1;
352 d->rd_rcompressed = rrddim_add(d->st_compressed, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
353 d->rd_tcompressed = rrddim_add(d->st_compressed, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
355 else rrdset_next(d->st_compressed);
357 rrddim_set_by_pointer(d->st_compressed, d->rd_rcompressed, d->rcompressed);
358 rrddim_set_by_pointer(d->st_compressed, d->rd_tcompressed, d->tcompressed);
359 rrdset_done(d->st_compressed);
362 // --------------------------------------------------------------------
364 if(unlikely((d->do_events == CONFIG_ONDEMAND_ONDEMAND && (d->rframe || d->tcollisions || d->tcarrier))))
365 d->do_events = CONFIG_ONDEMAND_YES;
367 if(d->do_events == CONFIG_ONDEMAND_YES) {
368 if(unlikely(!d->st_events)) {
369 d->st_events = rrdset_find_bytype("net_events", d->name);
371 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);
373 d->st_events->isdetail = 1;
375 d->rd_rframe = rrddim_add(d->st_events, "frames", NULL, 1, 1, RRDDIM_INCREMENTAL);
376 d->rd_tcollisions = rrddim_add(d->st_events, "collisions", NULL, -1, 1, RRDDIM_INCREMENTAL);
377 d->rd_tcarrier = rrddim_add(d->st_events, "carrier", NULL, -1, 1, RRDDIM_INCREMENTAL);
379 else rrdset_next(d->st_events);
381 rrddim_set_by_pointer(d->st_events, d->rd_rframe, d->rframe);
382 rrddim_set_by_pointer(d->st_events, d->rd_tcollisions, d->tcollisions);
383 rrddim_set_by_pointer(d->st_events, d->rd_tcarrier, d->tcarrier);
384 rrdset_done(d->st_events);