]> arthur.barton.de Git - netdata.git/blob - src/proc_net_dev.c
layout: remove executable from unrelated files
[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_ondemand("plugin:proc:/proc/net/dev", "enable new interfaces detected at runtime", CONFIG_ONDEMAND_ONDEMAND);
32         if(enable_ifb_interfaces == -1) enable_ifb_interfaces = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "enable ifb interfaces", CONFIG_ONDEMAND_NO);
33
34         if(do_bandwidth == -1)  do_bandwidth    = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "bandwidth for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
35         if(do_packets == -1)    do_packets              = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
36         if(do_errors == -1)             do_errors               = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "errors for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
37         if(do_drops == -1)              do_drops                = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "drops for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
38         if(do_fifo == -1)               do_fifo                 = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "fifo for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
39         if(do_compressed == -1) do_compressed   = config_get_boolean_ondemand("plugin:proc:/proc/net/dev", "compressed packets for all interfaces", CONFIG_ONDEMAND_ONDEMAND);
40         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);
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                 int default_enable = enable_new_interfaces;
76
77                 // prevent unused interfaces from creating charts
78                 if(strcmp(iface, "lo") == 0)
79                         default_enable = 0;
80                 else {
81                         int len = strlen(iface);
82                         if(len >= 4 && strcmp(&iface[len-4], "-ifb") == 0)
83                                 default_enable = enable_ifb_interfaces;
84                 }
85
86                 // check if the user wants it
87                 {
88                         char var_name[512 + 1];
89                         snprintf(var_name, 512, "plugin:proc:/proc/net/dev:%s", iface);
90                         default_enable = config_get_boolean_ondemand(var_name, "enabled", default_enable);
91                         if(default_enable == CONFIG_ONDEMAND_NO) continue;
92                         if(default_enable == CONFIG_ONDEMAND_ONDEMAND && !rbytes && !tbytes) continue;
93
94                         ddo_bandwidth = config_get_boolean_ondemand(var_name, "bandwidth", ddo_bandwidth);
95                         ddo_packets = config_get_boolean_ondemand(var_name, "packets", ddo_packets);
96                         ddo_errors = config_get_boolean_ondemand(var_name, "errors", ddo_errors);
97                         ddo_drops = config_get_boolean_ondemand(var_name, "drops", ddo_drops);
98                         ddo_fifo = config_get_boolean_ondemand(var_name, "fifo", ddo_fifo);
99                         ddo_compressed = config_get_boolean_ondemand(var_name, "compressed", ddo_compressed);
100                         ddo_events = config_get_boolean_ondemand(var_name, "events", ddo_events);
101
102                         if(ddo_bandwidth == CONFIG_ONDEMAND_ONDEMAND && rbytes == 0 && tbytes == 0) ddo_bandwidth = 0;
103                         if(ddo_errors == CONFIG_ONDEMAND_ONDEMAND && rerrors == 0 && terrors == 0) ddo_errors = 0;
104                         if(ddo_drops == CONFIG_ONDEMAND_ONDEMAND && rdrops == 0 && tdrops == 0) ddo_drops = 0;
105                         if(ddo_fifo == CONFIG_ONDEMAND_ONDEMAND && rfifo == 0 && tfifo == 0) ddo_fifo = 0;
106                         if(ddo_compressed == CONFIG_ONDEMAND_ONDEMAND && rcompressed == 0 && tcompressed == 0) ddo_compressed = 0;
107                         if(ddo_events == CONFIG_ONDEMAND_ONDEMAND && rframe == 0 && tcollisions == 0 && tcarrier == 0) ddo_events = 0;
108
109                         // for absolute values, we need to switch the setting to 'yes'
110                         // to allow it refresh from now on
111                         // if(ddo_fifo == CONFIG_ONDEMAND_ONDEMAND) config_set(var_name, "fifo", "yes");
112                 }
113
114                 RRDSET *st;
115
116                 // --------------------------------------------------------------------
117
118                 if(ddo_bandwidth) {
119                         st = rrdset_find_bytype("net", iface);
120                         if(!st) {
121                                 st = rrdset_create("net", iface, NULL, iface, "net.net", "Bandwidth", "kilobits/s", 7000, update_every, RRDSET_TYPE_AREA);
122
123                                 rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
124                                 rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
125                         }
126                         else rrdset_next(st);
127
128                         rrddim_set(st, "received", rbytes);
129                         rrddim_set(st, "sent", tbytes);
130                         rrdset_done(st);
131                 }
132
133                 // --------------------------------------------------------------------
134
135                 if(ddo_packets) {
136                         st = rrdset_find_bytype("net_packets", iface);
137                         if(!st) {
138                                 st = rrdset_create("net_packets", iface, NULL, iface, "net.packets", "Packets", "packets/s", 7001, update_every, RRDSET_TYPE_LINE);
139                                 st->isdetail = 1;
140
141                                 rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
142                                 rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
143                                 rrddim_add(st, "multicast", NULL, 1, 1, RRDDIM_INCREMENTAL);
144                         }
145                         else rrdset_next(st);
146
147                         rrddim_set(st, "received", rpackets);
148                         rrddim_set(st, "sent", tpackets);
149                         rrddim_set(st, "multicast", rmulticast);
150                         rrdset_done(st);
151                 }
152
153                 // --------------------------------------------------------------------
154
155                 if(ddo_errors) {
156                         st = rrdset_find_bytype("net_errors", iface);
157                         if(!st) {
158                                 st = rrdset_create("net_errors", iface, NULL, iface, "net.errors", "Interface Errors", "errors/s", 7002, update_every, RRDSET_TYPE_LINE);
159                                 st->isdetail = 1;
160
161                                 rrddim_add(st, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
162                                 rrddim_add(st, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
163                         }
164                         else rrdset_next(st);
165
166                         rrddim_set(st, "inbound", rerrors);
167                         rrddim_set(st, "outbound", terrors);
168                         rrdset_done(st);
169                 }
170
171                 // --------------------------------------------------------------------
172
173                 if(ddo_drops) {
174                         st = rrdset_find_bytype("net_drops", iface);
175                         if(!st) {
176                                 st = rrdset_create("net_drops", iface, NULL, iface, "net.drops", "Interface Drops", "drops/s", 7003, update_every, RRDSET_TYPE_LINE);
177                                 st->isdetail = 1;
178
179                                 rrddim_add(st, "inbound", NULL, 1, 1, RRDDIM_INCREMENTAL);
180                                 rrddim_add(st, "outbound", NULL, -1, 1, RRDDIM_INCREMENTAL);
181                         }
182                         else rrdset_next(st);
183
184                         rrddim_set(st, "inbound", rdrops);
185                         rrddim_set(st, "outbound", tdrops);
186                         rrdset_done(st);
187                 }
188
189                 // --------------------------------------------------------------------
190
191                 if(ddo_fifo) {
192                         st = rrdset_find_bytype("net_fifo", iface);
193                         if(!st) {
194                                 st = rrdset_create("net_fifo", iface, NULL, iface, "net.fifo", "Interface FIFO Buffer Errors", "errors", 7004, update_every, RRDSET_TYPE_LINE);
195                                 st->isdetail = 1;
196
197                                 rrddim_add(st, "receive", NULL, 1, 1, RRDDIM_INCREMENTAL);
198                                 rrddim_add(st, "transmit", NULL, -1, 1, RRDDIM_INCREMENTAL);
199                         }
200                         else rrdset_next(st);
201
202                         rrddim_set(st, "receive", rfifo);
203                         rrddim_set(st, "transmit", tfifo);
204                         rrdset_done(st);
205                 }
206
207                 // --------------------------------------------------------------------
208
209                 if(ddo_compressed) {
210                         st = rrdset_find_bytype("net_compressed", iface);
211                         if(!st) {
212                                 st = rrdset_create("net_compressed", iface, NULL, iface, "net.compressed", "Compressed Packets", "packets/s", 7005, update_every, RRDSET_TYPE_LINE);
213                                 st->isdetail = 1;
214
215                                 rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
216                                 rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
217                         }
218                         else rrdset_next(st);
219
220                         rrddim_set(st, "received", rcompressed);
221                         rrddim_set(st, "sent", tcompressed);
222                         rrdset_done(st);
223                 }
224
225                 // --------------------------------------------------------------------
226
227                 if(ddo_events) {
228                         st = rrdset_find_bytype("net_events", iface);
229                         if(!st) {
230                                 st = rrdset_create("net_events", iface, NULL, iface, "net.events", "Network Interface Events", "events/s", 7006, update_every, RRDSET_TYPE_LINE);
231                                 st->isdetail = 1;
232
233                                 rrddim_add(st, "frames", NULL, 1, 1, RRDDIM_INCREMENTAL);
234                                 rrddim_add(st, "collisions", NULL, -1, 1, RRDDIM_INCREMENTAL);
235                                 rrddim_add(st, "carrier", NULL, -1, 1, RRDDIM_INCREMENTAL);
236                         }
237                         else rrdset_next(st);
238
239                         rrddim_set(st, "frames", rframe);
240                         rrddim_set(st, "collisions", tcollisions);
241                         rrddim_set(st, "carrier", tcarrier);
242                         rrdset_done(st);
243                 }
244         }
245
246         return 0;
247 }