]> arthur.barton.de Git - netdata.git/blob - src/proc_net_netstat.c
all required system headers in common.h; some progress on health variables
[netdata.git] / src / proc_net_netstat.c
1 #include "common.h"
2
3 int do_proc_net_netstat(int update_every, unsigned long long dt) {
4         static int do_bandwidth = -1, do_inerrors = -1, do_mcast = -1, do_bcast = -1, do_mcast_p = -1, do_bcast_p = -1;
5         static procfile *ff = NULL;
6
7         if(do_bandwidth == -1)  do_bandwidth    = config_get_boolean_ondemand("plugin:proc:/proc/net/netstat", "bandwidth", CONFIG_ONDEMAND_ONDEMAND);
8         if(do_inerrors == -1)   do_inerrors             = config_get_boolean_ondemand("plugin:proc:/proc/net/netstat", "input errors", CONFIG_ONDEMAND_ONDEMAND);
9         if(do_mcast == -1)              do_mcast                = config_get_boolean_ondemand("plugin:proc:/proc/net/netstat", "multicast bandwidth", CONFIG_ONDEMAND_ONDEMAND);
10         if(do_bcast == -1)              do_bcast                = config_get_boolean_ondemand("plugin:proc:/proc/net/netstat", "broadcast bandwidth", CONFIG_ONDEMAND_ONDEMAND);
11         if(do_mcast_p == -1)    do_mcast_p              = config_get_boolean_ondemand("plugin:proc:/proc/net/netstat", "multicast packets", CONFIG_ONDEMAND_ONDEMAND);
12         if(do_bcast_p == -1)    do_bcast_p              = config_get_boolean_ondemand("plugin:proc:/proc/net/netstat", "broadcast packets", CONFIG_ONDEMAND_ONDEMAND);
13
14         if(dt) {};
15
16         if(!ff) {
17                 char filename[FILENAME_MAX + 1];
18                 snprintfz(filename, FILENAME_MAX, "%s%s", global_host_prefix, "/proc/net/netstat");
19                 ff = procfile_open(config_get("plugin:proc:/proc/net/netstat", "filename to monitor", filename), " \t:", PROCFILE_FLAG_DEFAULT);
20         }
21         if(!ff) return 1;
22
23         ff = procfile_readall(ff);
24         if(!ff) return 0; // we return 0, so that we will retry to open it next time
25
26         uint32_t lines = procfile_lines(ff), l;
27         uint32_t words;
28
29         for(l = 0; l < lines ;l++) {
30                 if(strcmp(procfile_lineword(ff, l, 0), "IpExt") == 0) {
31                         l++; // we need the next line
32
33                         if(strcmp(procfile_lineword(ff, l, 0), "IpExt") != 0) {
34                                 error("Cannot read IpExt line from /proc/net/netstat.");
35                                 break;
36                         }
37                         words = procfile_linewords(ff, l);
38                         if(words < 12) {
39                                 error("Cannot read /proc/net/netstat IpExt line. Expected 12 params, read %u.", words);
40                                 continue;
41                         }
42
43                         unsigned long long
44                                 InNoRoutes = 0, InTruncatedPkts = 0,
45                                 InOctets = 0,  InMcastPkts = 0,  InBcastPkts = 0,  InMcastOctets = 0,  InBcastOctets = 0,
46                                 OutOctets = 0, OutMcastPkts = 0, OutBcastPkts = 0, OutMcastOctets = 0, OutBcastOctets = 0;
47
48                         InNoRoutes                      = strtoull(procfile_lineword(ff, l, 1), NULL, 10);
49                         InTruncatedPkts         = strtoull(procfile_lineword(ff, l, 2), NULL, 10);
50                         InMcastPkts             = strtoull(procfile_lineword(ff, l, 3), NULL, 10);
51                         OutMcastPkts            = strtoull(procfile_lineword(ff, l, 4), NULL, 10);
52                         InBcastPkts             = strtoull(procfile_lineword(ff, l, 5), NULL, 10);
53                         OutBcastPkts            = strtoull(procfile_lineword(ff, l, 6), NULL, 10);
54                         InOctets                        = strtoull(procfile_lineword(ff, l, 7), NULL, 10);
55                         OutOctets                       = strtoull(procfile_lineword(ff, l, 8), NULL, 10);
56                         InMcastOctets           = strtoull(procfile_lineword(ff, l, 9), NULL, 10);
57                         OutMcastOctets          = strtoull(procfile_lineword(ff, l, 10), NULL, 10);
58                         InBcastOctets           = strtoull(procfile_lineword(ff, l, 11), NULL, 10);
59                         OutBcastOctets          = strtoull(procfile_lineword(ff, l, 12), NULL, 10);
60
61                         RRDSET *st;
62
63                         // --------------------------------------------------------------------
64
65                         if(do_bandwidth == CONFIG_ONDEMAND_YES || (do_bandwidth == CONFIG_ONDEMAND_ONDEMAND && (InOctets || OutOctets))) {
66                                 do_bandwidth = CONFIG_ONDEMAND_YES;
67                                 st = rrdset_find("system.ipv4");
68                                 if(!st) {
69                                         st = rrdset_create("system", "ipv4", NULL, "network", NULL, "IPv4 Bandwidth", "kilobits/s", 500, update_every, RRDSET_TYPE_AREA);
70
71                                         rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
72                                         rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
73                                 }
74                                 else rrdset_next(st);
75
76                                 rrddim_set(st, "sent", OutOctets);
77                                 rrddim_set(st, "received", InOctets);
78                                 rrdset_done(st);
79                         }
80
81                         // --------------------------------------------------------------------
82
83                         if(do_inerrors == CONFIG_ONDEMAND_YES || (do_inerrors == CONFIG_ONDEMAND_ONDEMAND && (InNoRoutes || InTruncatedPkts))) {
84                                 do_inerrors = CONFIG_ONDEMAND_YES;
85                                 st = rrdset_find("ipv4.inerrors");
86                                 if(!st) {
87                                         st = rrdset_create("ipv4", "inerrors", NULL, "errors", NULL, "IPv4 Input Errors", "packets/s", 4000, update_every, RRDSET_TYPE_LINE);
88                                         st->isdetail = 1;
89
90                                         rrddim_add(st, "noroutes", NULL, 1, 1, RRDDIM_INCREMENTAL);
91                                         rrddim_add(st, "truncated", NULL, 1, 1, RRDDIM_INCREMENTAL);
92                                 }
93                                 else rrdset_next(st);
94
95                                 rrddim_set(st, "noroutes", InNoRoutes);
96                                 rrddim_set(st, "truncated", InTruncatedPkts);
97                                 rrdset_done(st);
98                         }
99
100                         // --------------------------------------------------------------------
101
102                         if(do_mcast == CONFIG_ONDEMAND_YES || (do_mcast == CONFIG_ONDEMAND_ONDEMAND && (InMcastOctets || OutMcastOctets))) {
103                                 do_mcast = CONFIG_ONDEMAND_YES;
104                                 st = rrdset_find("ipv4.mcast");
105                                 if(!st) {
106                                         st = rrdset_create("ipv4", "mcast", NULL, "multicast", NULL, "IPv4 Multicast Bandwidth", "kilobits/s", 9000, update_every, RRDSET_TYPE_AREA);
107                                         st->isdetail = 1;
108
109                                         rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
110                                         rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
111                                 }
112                                 else rrdset_next(st);
113
114                                 rrddim_set(st, "sent", OutMcastOctets);
115                                 rrddim_set(st, "received", InMcastOctets);
116                                 rrdset_done(st);
117                         }
118
119                         // --------------------------------------------------------------------
120
121                         if(do_bcast == CONFIG_ONDEMAND_YES || (do_bcast == CONFIG_ONDEMAND_ONDEMAND && (InBcastOctets || OutBcastOctets))) {
122                                 do_bcast = CONFIG_ONDEMAND_YES;
123                                 st = rrdset_find("ipv4.bcast");
124                                 if(!st) {
125                                         st = rrdset_create("ipv4", "bcast", NULL, "broadcast", NULL, "IPv4 Broadcast Bandwidth", "kilobits/s", 8000, update_every, RRDSET_TYPE_AREA);
126                                         st->isdetail = 1;
127
128                                         rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
129                                         rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
130                                 }
131                                 else rrdset_next(st);
132
133                                 rrddim_set(st, "sent", OutBcastOctets);
134                                 rrddim_set(st, "received", InBcastOctets);
135                                 rrdset_done(st);
136                         }
137
138                         // --------------------------------------------------------------------
139
140                         if(do_mcast_p == CONFIG_ONDEMAND_YES || (do_mcast_p == CONFIG_ONDEMAND_ONDEMAND && (InMcastPkts || OutMcastPkts))) {
141                                 do_mcast_p = CONFIG_ONDEMAND_YES;
142                                 st = rrdset_find("ipv4.mcastpkts");
143                                 if(!st) {
144                                         st = rrdset_create("ipv4", "mcastpkts", NULL, "multicast", NULL, "IPv4 Multicast Packets", "packets/s", 9500, update_every, RRDSET_TYPE_LINE);
145                                         st->isdetail = 1;
146
147                                         rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
148                                         rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
149                                 }
150                                 else rrdset_next(st);
151
152                                 rrddim_set(st, "sent", OutMcastPkts);
153                                 rrddim_set(st, "received", InMcastPkts);
154                                 rrdset_done(st);
155                         }
156
157                         // --------------------------------------------------------------------
158
159                         if(do_bcast_p == CONFIG_ONDEMAND_YES || (do_bcast_p == CONFIG_ONDEMAND_ONDEMAND && (InBcastPkts || OutBcastPkts))) {
160                                 do_bcast_p = CONFIG_ONDEMAND_YES;
161                                 st = rrdset_find("ipv4.bcastpkts");
162                                 if(!st) {
163                                         st = rrdset_create("ipv4", "bcastpkts", NULL, "broadcast", NULL, "IPv4 Broadcast Packets", "packets/s", 8500, update_every, RRDSET_TYPE_LINE);
164                                         st->isdetail = 1;
165
166                                         rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
167                                         rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
168                                 }
169                                 else rrdset_next(st);
170
171                                 rrddim_set(st, "sent", OutBcastPkts);
172                                 rrddim_set(st, "received", InBcastPkts);
173                                 rrdset_done(st);
174                         }
175                 }
176         }
177
178         return 0;
179 }