10 #include "appconfig.h"
13 #include "plugin_proc.h"
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;
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);
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);
35 ff = procfile_readall(ff);
36 if(!ff) return 0; // we return 0, so that we will retry to open it next time
38 uint32_t lines = procfile_lines(ff), l;
41 for(l = 0; l < lines ;l++) {
42 if(strcmp(procfile_lineword(ff, l, 0), "IpExt") == 0) {
43 l++; // we need the next line
45 if(strcmp(procfile_lineword(ff, l, 0), "IpExt") != 0) {
46 error("Cannot read IpExt line from /proc/net/netstat.");
49 words = procfile_linewords(ff, l);
51 error("Cannot read /proc/net/netstat IpExt line. Expected 12 params, read %u.", words);
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;
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);
75 // --------------------------------------------------------------------
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");
81 st = rrdset_create("system", "ipv4", NULL, "network", NULL, "IPv4 Bandwidth", "kilobits/s", 500, update_every, RRDSET_TYPE_AREA);
83 rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
84 rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
88 rrddim_set(st, "sent", OutOctets);
89 rrddim_set(st, "received", InOctets);
93 // --------------------------------------------------------------------
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");
99 st = rrdset_create("ipv4", "inerrors", NULL, "errors", NULL, "IPv4 Input Errors", "packets/s", 4000, update_every, RRDSET_TYPE_LINE);
102 rrddim_add(st, "noroutes", NULL, 1, 1, RRDDIM_INCREMENTAL);
103 rrddim_add(st, "truncated", NULL, 1, 1, RRDDIM_INCREMENTAL);
105 else rrdset_next(st);
107 rrddim_set(st, "noroutes", InNoRoutes);
108 rrddim_set(st, "truncated", InTruncatedPkts);
112 // --------------------------------------------------------------------
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");
118 st = rrdset_create("ipv4", "mcast", NULL, "multicast", NULL, "IPv4 Multicast Bandwidth", "kilobits/s", 9000, update_every, RRDSET_TYPE_AREA);
121 rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
122 rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
124 else rrdset_next(st);
126 rrddim_set(st, "sent", OutMcastOctets);
127 rrddim_set(st, "received", InMcastOctets);
131 // --------------------------------------------------------------------
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");
137 st = rrdset_create("ipv4", "bcast", NULL, "broadcast", NULL, "IPv4 Broadcast Bandwidth", "kilobits/s", 8000, update_every, RRDSET_TYPE_AREA);
140 rrddim_add(st, "received", NULL, 8, 1024, RRDDIM_INCREMENTAL);
141 rrddim_add(st, "sent", NULL, -8, 1024, RRDDIM_INCREMENTAL);
143 else rrdset_next(st);
145 rrddim_set(st, "sent", OutBcastOctets);
146 rrddim_set(st, "received", InBcastOctets);
150 // --------------------------------------------------------------------
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");
156 st = rrdset_create("ipv4", "mcastpkts", NULL, "multicast", NULL, "IPv4 Multicast Packets", "packets/s", 9500, update_every, RRDSET_TYPE_LINE);
159 rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
160 rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
162 else rrdset_next(st);
164 rrddim_set(st, "sent", OutMcastPkts);
165 rrddim_set(st, "received", InMcastPkts);
169 // --------------------------------------------------------------------
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");
175 st = rrdset_create("ipv4", "bcastpkts", NULL, "broadcast", NULL, "IPv4 Broadcast Packets", "packets/s", 8500, update_every, RRDSET_TYPE_LINE);
178 rrddim_add(st, "received", NULL, 1, 1, RRDDIM_INCREMENTAL);
179 rrddim_add(st, "sent", NULL, -1, 1, RRDDIM_INCREMENTAL);
181 else rrdset_next(st);
183 rrddim_set(st, "sent", OutBcastPkts);
184 rrddim_set(st, "received", InBcastPkts);