]> arthur.barton.de Git - netdata.git/blob - src/proc_net_dev.c
53981182a4b22ad5158534e4f0db9cf9596fe276
[netdata.git] / src / proc_net_dev.c
1 #include "common.h"
2
3 int do_proc_net_dev(int update_every, unsigned long long dt) {
4     static procfile *ff = NULL;
5     static int enable_new_interfaces = -1, enable_ifb_interfaces = -1;
6     static int do_bandwidth = -1, do_packets = -1, do_errors = -1, do_drops = -1, do_fifo = -1, do_compressed = -1, do_events = -1;
7
8     if(dt) {};
9
10     if(!ff) {
11         char filename[FILENAME_MAX + 1];
12         snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/net/dev");
13         ff = procfile_open(config_get("plugin:proc:/proc/net/dev", "filename to monitor", filename), " \t,:|", PROCFILE_FLAG_DEFAULT);
14     }
15     if(!ff) return 1;
16
17     ff = procfile_readall(ff);
18     if(!ff) return 0; // we return 0, so that we will retry to open it next time
19
20     if(enable_new_interfaces == -1) enable_new_interfaces = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_ONDEMAND);
21     if(enable_ifb_interfaces == -1) enable_ifb_interfaces = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "enable ifb interfaces", CONFIG_ONDEMAND_NO);
22
23     if(do_bandwidth == -1)  do_bandwidth    = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "bandwidth for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
24     if(do_packets == -1)    do_packets      = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
25     if(do_errors == -1)     do_errors       = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "errors for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
26     if(do_drops == -1)      do_drops        = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "drops for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
27     if(do_fifo == -1)       do_fifo         = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "fifo for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
28     if(do_compressed == -1) do_compressed   = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "compressed packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
29     if(do_events == -1)     do_events       = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "frames, collisions, carrier counters for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
30
31     uint32_t lines = procfile_lines(ff), l;
32     uint32_t words;
33
34     char *iface;
35     unsigned long long rbytes, rpackets, rerrors, rdrops, rfifo, rframe, rcompressed, rmulticast;
36     unsigned long long tbytes, tpackets, terrors, tdrops, tfifo, tcollisions, tcarrier, tcompressed;
37
38     for(l = 2; l < lines ;l++) {
39         words = procfile_linewords(ff, l);
40         if(words < 17) continue;
41
42         iface       = procfile_lineword(ff, l, 0);
43
44         rbytes      = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
45         rpackets    = strtoull(procfile_lineword(ff, l, 2), NULL, 10);
46         rerrors     = strtoull(procfile_lineword(ff, l, 3), NULL, 10);
47         rdrops      = strtoull(procfile_lineword(ff, l, 4), NULL, 10);
48         rfifo       = strtoull(procfile_lineword(ff, l, 5), NULL, 10);
49         rframe      = strtoull(procfile_lineword(ff, l, 6), NULL, 10);
50         rcompressed = strtoull(procfile_lineword(ff, l, 7), NULL, 10);
51         rmulticast  = strtoull(procfile_lineword(ff, l, 8), NULL, 10);
52
53         tbytes      = strtoull(procfile_lineword(ff, l, 9), NULL, 10);
54         tpackets    = strtoull(procfile_lineword(ff, l, 10), NULL, 10);
55         terrors     = strtoull(procfile_lineword(ff, l, 11), NULL, 10);
56         tdrops      = strtoull(procfile_lineword(ff, l, 12), NULL, 10);
57         tfifo       = strtoull(procfile_lineword(ff, l, 13), NULL, 10);
58         tcollisions = strtoull(procfile_lineword(ff, l, 14), NULL, 10);
59         tcarrier    = strtoull(procfile_lineword(ff, l, 15), NULL, 10);
60         tcompressed = strtoull(procfile_lineword(ff, l, 16), NULL, 10);
61
62         int ddo_bandwidth = do_bandwidth, ddo_packets = do_packets, ddo_errors = do_errors, ddo_drops = do_drops, ddo_fifo = do_fifo, ddo_compressed = do_compressed, ddo_events = do_events;
63
64         int default_enable = enable_new_interfaces;
65
66         // prevent unused interfaces from creating charts
67         if(strcmp(iface, "lo") == 0)
68             default_enable = 0;
69         else {
70             int len = strlen(iface);
71             if(len >= 4 && strcmp(&iface[len-4], "-ifb") == 0)
72                 default_enable = enable_ifb_interfaces;
73         }
74
75         // check if the user wants it
76         {
77             char var_name[512 + 1];
78             snprintfz(var_name, 512, "plugin:proc:/proc/net/dev:%s", iface);
79             default_enable = config_get_boolean_ondemand(var_name, "enabled", default_enable);
80             if(default_enable == CONFIG_ONDEMAND_NO) continue;
81             if(default_enable == CONFIG_ONDEMAND_ONDEMAND && !rbytes && !tbytes) continue;
82
83             ddo_bandwidth = config_get_boolean_ondemand(var_name, "bandwidth", ddo_bandwidth);
84             ddo_packets = config_get_boolean_ondemand(var_name, "packets", ddo_packets);
85             ddo_errors = config_get_boolean_ondemand(var_name, "errors", ddo_errors);
86             ddo_drops = config_get_boolean_ondemand(var_name, "drops", ddo_drops);
87             ddo_fifo = config_get_boolean_ondemand(var_name, "fifo", ddo_fifo);
88             ddo_compressed = config_get_boolean_ondemand(var_name, "compressed", ddo_compressed);
89             ddo_events = config_get_boolean_ondemand(var_name, "events", ddo_events);
90
91             if(ddo_bandwidth == CONFIG_ONDEMAND_ONDEMAND && rbytes == 0 && tbytes == 0) ddo_bandwidth = 0;
92             if(ddo_errors == CONFIG_ONDEMAND_ONDEMAND && rerrors == 0 && terrors == 0) ddo_errors = 0;
93             if(ddo_drops == CONFIG_ONDEMAND_ONDEMAND && rdrops == 0 && tdrops == 0) ddo_drops = 0;
94             if(ddo_fifo == CONFIG_ONDEMAND_ONDEMAND && rfifo == 0 && tfifo == 0) ddo_fifo = 0;
95             if(ddo_compressed == CONFIG_ONDEMAND_ONDEMAND && rcompressed == 0 && tcompressed == 0) ddo_compressed = 0;
96             if(ddo_events == CONFIG_ONDEMAND_ONDEMAND && rframe == 0 && tcollisions == 0 && tcarrier == 0) ddo_events = 0;
97
98             // for absolute values, we need to switch the setting to 'yes'
99             // to allow it refresh from now on
100             // if(ddo_fifo == CONFIG_ONDEMAND_ONDEMAND) config_set(var_name, "fifo", "yes");
101         }
102
103         RRDSET *st;
104
105         // --------------------------------------------------------------------
106
107         if(ddo_bandwidth) {
108             st = rrdset_find_bytype("net", iface);
109             if(!st) {
110                 st = rrdset_create("net", iface, NULL, iface, "net.net", "Bandwidth", "kilobits/s", 7000, update_every, RRDSET_TYPE_AREA);
111
112                 rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
113                 rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
114             }
115             else rrdset_next(st);
116
117             rrddim_set(st, "received", rbytes);
118             rrddim_set(st, "sent", tbytes);
119             rrdset_done(st);
120         }
121
122         // --------------------------------------------------------------------
123
124         if(ddo_packets) {
125             st = rrdset_find_bytype("net_packets", iface);
126             if(!st) {
127                 st = rrdset_create("net_packets", iface, NULL, iface, "net.packets", "Packets", "packets/s", 7001, update_every, RRDSET_TYPE_LINE);
128                 st->isdetail = 1;
129
130                 rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
131                 rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
132                 rrddim_add(st, "multicast", NULL, 1, 1, RRDDIM_INCREMENTAL);
133             }
134             else rrdset_next(st);
135
136             rrddim_set(st, "received", rpackets);
137             rrddim_set(st, "sent", tpackets);
138             rrddim_set(st, "multicast", rmulticast);
139             rrdset_done(st);
140         }
141
142         // --------------------------------------------------------------------
143
144         if(ddo_errors) {
145             st = rrdset_find_bytype("net_errors", iface);
146             if(!st) {
147                 st = rrdset_create("net_errors", iface, NULL, iface, "net.errors", "Interface Errors", "errors/s", 7002, update_every, RRDSET_TYPE_LINE);
148                 st->isdetail = 1;
149
150                 rrddim_add(st, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
151                 rrddim_add(st, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
152             }
153             else rrdset_next(st);
154
155             rrddim_set(st, "inbound", rerrors);
156             rrddim_set(st, "outbound", terrors);
157             rrdset_done(st);
158         }
159
160         // --------------------------------------------------------------------
161
162         if(ddo_drops) {
163             st = rrdset_find_bytype("net_drops", iface);
164             if(!st) {
165                 st = rrdset_create("net_drops", iface, NULL, iface, "net.drops", "Interface Drops", "drops/s", 7003, update_every, RRDSET_TYPE_LINE);
166                 st->isdetail = 1;
167
168                 rrddim_add(st, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
169                 rrddim_add(st, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
170             }
171             else rrdset_next(st);
172
173             rrddim_set(st, "inbound", rdrops);
174             rrddim_set(st, "outbound", tdrops);
175             rrdset_done(st);
176         }
177
178         // --------------------------------------------------------------------
179
180         if(ddo_fifo) {
181             st = rrdset_find_bytype("net_fifo", iface);
182             if(!st) {
183                 st = rrdset_create("net_fifo", iface, NULL, iface, "net.fifo", "Interface FIFO Buffer Errors", "errors", 7004, update_every, RRDSET_TYPE_LINE);
184                 st->isdetail = 1;
185
186                 rrddim_add(st, "receive", NULL, 1, 1, RRDDIM_INCREMENTAL);
187                 rrddim_add(st, "transmit", NULL, -1, 1, RRDDIM_INCREMENTAL);
188             }
189             else rrdset_next(st);
190
191             rrddim_set(st, "receive", rfifo);
192             rrddim_set(st, "transmit", tfifo);
193             rrdset_done(st);
194         }
195
196         // --------------------------------------------------------------------
197
198         if(ddo_compressed) {
199             st = rrdset_find_bytype("net_compressed", iface);
200             if(!st) {
201                 st = rrdset_create("net_compressed", iface, NULL, iface, "net.compressed", "Compressed Packets", "packets/s", 7005, update_every, RRDSET_TYPE_LINE);
202                 st->isdetail = 1;
203
204                 rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
205                 rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
206             }
207             else rrdset_next(st);
208
209             rrddim_set(st, "received", rcompressed);
210             rrddim_set(st, "sent", tcompressed);
211             rrdset_done(st);
212         }
213
214         // --------------------------------------------------------------------
215
216         if(ddo_events) {
217             st = rrdset_find_bytype("net_events", iface);
218             if(!st) {
219                 st = rrdset_create("net_events", iface, NULL, iface, "net.events", "Network Interface Events", "events/s", 7006, update_every, RRDSET_TYPE_LINE);
220                 st->isdetail = 1;
221
222                 rrddim_add(st, "frames", NULL, 1, 1, RRDDIM_INCREMENTAL);
223                 rrddim_add(st, "collisions", NULL, -1, 1, RRDDIM_INCREMENTAL);
224                 rrddim_add(st, "carrier", NULL, -1, 1, RRDDIM_INCREMENTAL);
225             }
226             else rrdset_next(st);
227
228             rrddim_set(st, "frames", rframe);
229             rrddim_set(st, "collisions", tcollisions);
230             rrddim_set(st, "carrier", tcarrier);
231             rrdset_done(st);
232         }
233     }
234
235     return 0;
236 }