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