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