collectd-mod-ipstatistics: fix handling of long `/proc` lines
[feed/packages.git] / utils / collectd / patches / 932-add-ipstatistics.patch
1 --- /dev/null
2 +++ b/src/ipstatistics.c
3 @@ -0,0 +1,104 @@
4 +/*
5 + This Plugin is based opn the interface.c Plugin.
6 +*/
7 +#include <errno.h>
8 +#include <stdbool.h>
9 +#include <stdint.h>
10 +#include <stdio.h>
11 +#include <stdlib.h>
12 +#include <string.h>
13 +
14 +#include <ifaddrs.h>
15 +#include <net/if.h>
16 +#include <sys/types.h>
17 +
18 +#include "plugin.h"
19 +#include "utils/cmds/putval.h"
20 +#include "utils/common/common.h"
21 +
22 +/* Copied from interface.c */
23 +static void ipstatistics_submit(const char *type, derive_t ip4rx,
24 + derive_t ip4tx, derive_t ip6rx, derive_t ip6tx) {
25 + value_list_t vl = VALUE_LIST_INIT;
26 + value_t values[] = {
27 + {.derive = ip4rx},
28 + {.derive = ip4tx},
29 + {.derive = ip6rx},
30 + {.derive = ip6tx}
31 + };
32 +
33 + vl.values = values;
34 + vl.values_len = STATIC_ARRAY_SIZE(values);
35 + sstrncpy(vl.plugin, "ipstatistics", sizeof(vl.plugin));
36 + sstrncpy(vl.plugin_instance, "all", sizeof(vl.plugin_instance));
37 + sstrncpy(vl.type, type, sizeof(vl.type));
38 +
39 + plugin_dispatch_values(&vl);
40 +} /* void if_submit */
41 +
42 +int ipstatistics_read() {
43 + FILE *fh;
44 + char buffer[4096];
45 + char *fields[19];
46 + int numfields;
47 +
48 + derive_t ip4_in = 0;
49 + derive_t ip4_out = 0;
50 + derive_t ip6_in = 0;
51 + derive_t ip6_out = 0;
52 +
53 + if ((fh = fopen("/proc/net/snmp6", "r")) == NULL) {
54 + WARNING("ipstatistics plugin: try opening %s : fopen: %s", "/proc/net/snmp6",
55 + STRERRNO);
56 + return -1;
57 + }
58 +
59 + while (fgets(buffer, 4096, fh) != NULL) {
60 + numfields = strsplit(buffer, fields, 2);
61 +
62 + if (numfields < 2)
63 + return -1;
64 +
65 + if (strcasecmp(fields[0], "Ip6OutOctets") == 0) {
66 + ip6_out = atoll(fields[1]);
67 + }
68 +
69 + if (strcasecmp(fields[0], "Ip6InOctets") == 0) {
70 + ip6_in = atoll(fields[1]);
71 + }
72 + }
73 +
74 + fclose(fh);
75 +
76 + if ((fh = fopen("/proc/net/netstat", "r")) == NULL) {
77 + WARNING("ipstatistics plugin: try opening %s : fopen: %s", "/proc/net/netstat",
78 + STRERRNO);
79 + return -1;
80 + }
81 +
82 + int count_ipext = 0;
83 + while (fgets(buffer, 4096, fh) != NULL) {
84 + numfields = strsplit(buffer, fields, 19);
85 +
86 + if (numfields < 8)
87 + return -1;
88 +
89 + if (strcasecmp(fields[0], "IpExt:") == 0) {
90 + count_ipext++;
91 + if (count_ipext == 2) {
92 + ip4_in = atoll(fields[7]);
93 + ip4_out = atoll(fields[8]);
94 + }
95 + }
96 + }
97 +
98 + fclose(fh);
99 +
100 + ipstatistics_submit("ip_stats_octets", ip4_in, ip4_out, ip6_in, ip6_out);
101 + return 0;
102 +}
103 +
104 +void module_register(void) {
105 + plugin_register_read("ipstatistics", ipstatistics_read);
106 +} /* void module_register */
107 +
108 --- a/src/types.db
109 +++ b/src/types.db
110 @@ -148,6 +148,7 @@ invocations value:DERIVE:0:U
111 io_octets rx:DERIVE:0:U, tx:DERIVE:0:U
112 io_ops read:DERIVE:0:U, write:DERIVE:0:U
113 io_packets rx:DERIVE:0:U, tx:DERIVE:0:U
114 +ip_stats_octets ip4rx:DERIVE:0:U, ip4tx:DERIVE:0:U, ip6rx:DERIVE:0:U, ip6tx:DERIVE:0:U
115 ipc value:GAUGE:0:U
116 ipt_bytes value:DERIVE:0:U
117 ipt_packets value:DERIVE:0:U
118 --- a/Makefile.am
119 +++ b/Makefile.am
120 @@ -1239,6 +1239,12 @@ ipstats_la_SOURCES = src/ipstats.c
121 ipstats_la_LDFLAGS = $(PLUGIN_LDFLAGS)
122 endif
123
124 +if BUILD_PLUGIN_IPSTATISTICS
125 +pkglib_LTLIBRARIES += ipstatistics.la
126 +ipstatistics_la_SOURCES = src/ipstatistics.c
127 +ipstatistics_la_LDFLAGS = $(PLUGIN_LDFLAGS)
128 +endif
129 +
130 if BUILD_PLUGIN_IPVS
131 pkglib_LTLIBRARIES += ipvs.la
132 ipvs_la_SOURCES = src/ipvs.c
133 --- a/configure.ac
134 +++ b/configure.ac
135 @@ -7091,6 +7091,7 @@ AC_PLUGIN([ipc], [$plugi
136 AC_PLUGIN([ipmi], [$plugin_ipmi], [IPMI sensor statistics])
137 AC_PLUGIN([iptables], [$with_libiptc], [IPTables rule counters])
138 AC_PLUGIN([ipstats], [$plugin_ipstats], [IP packet statistics])
139 +AC_PLUGIN([ipstatistics], [yes], [IP4 and IP6 statistics])
140 AC_PLUGIN([ipvs], [$plugin_ipvs], [IPVS connection statistics])
141 AC_PLUGIN([irq], [$plugin_irq], [IRQ statistics])
142 AC_PLUGIN([iwinfo], [$with_iwinfo], [Common iwinfo wireless statistics])
143 @@ -7542,6 +7543,7 @@ AC_MSG_RESULT([ ipc . . . . . . . . .
144 AC_MSG_RESULT([ ipmi . . . . . . . . $enable_ipmi])
145 AC_MSG_RESULT([ iptables . . . . . . $enable_iptables])
146 AC_MSG_RESULT([ ipstats . . . . . . . $enable_ipstats])
147 +AC_MSG_RESULT([ ipstatistics . . . . $enable_ipstatistics])
148 AC_MSG_RESULT([ ipvs . . . . . . . . $enable_ipvs])
149 AC_MSG_RESULT([ irq . . . . . . . . . $enable_irq])
150 AC_MSG_RESULT([ iwinfo . . . . . . . $enable_iwinfo])
151 --- a/src/collectd.conf.in
152 +++ b/src/collectd.conf.in
153 @@ -145,6 +145,7 @@
154 #@BUILD_PLUGIN_IPC_TRUE@LoadPlugin ipc
155 #@BUILD_PLUGIN_IPMI_TRUE@LoadPlugin ipmi
156 #@BUILD_PLUGIN_IPSTATS_TRUE@LoadPlugin ipstats
157 +#@BUILD_PLUGIN_IPSTATISTICS_TRUE@LoadPlugin ipstatistics
158 #@BUILD_PLUGIN_IPTABLES_TRUE@LoadPlugin iptables
159 #@BUILD_PLUGIN_IPVS_TRUE@LoadPlugin ipvs
160 #@BUILD_PLUGIN_IRQ_TRUE@LoadPlugin irq