wshaper: moved to github
[packages.git] / utils / collectd / patches / 900-add-iwinfo-plugin.patch
1 --- a/configure.in
2 +++ b/configure.in
3 @@ -490,6 +490,9 @@ AC_CHECK_HEADERS(netinet/if_ether.h, [],
4  have_termios_h="no"
5  AC_CHECK_HEADERS(termios.h, [have_termios_h="yes"])
6  
7 +# For the iwinfo plugin
8 +AC_CHECK_LIB(iwinfo, iwinfo_backend, [with_iwinfo="yes"], [with_iwinfo="no (libiwinfo not found)"], [])
9 +
10  #
11  # Checks for typedefs, structures, and compiler characteristics.
12  #
13 @@ -4081,6 +4084,7 @@ plugin_interface="no"
14  plugin_ipmi="no"
15  plugin_ipvs="no"
16  plugin_irq="no"
17 +plugin_iwinfo="no"
18  plugin_libvirt="no"
19  plugin_load="no"
20  plugin_memory="no"
21 @@ -4388,6 +4392,7 @@ AC_PLUGIN([ipmi],        [$plugin_ipmi],
22  AC_PLUGIN([iptables],    [$with_libiptc],      [IPTables rule counters])
23  AC_PLUGIN([ipvs],        [$plugin_ipvs],       [IPVS connection statistics])
24  AC_PLUGIN([irq],         [$plugin_irq],        [IRQ statistics])
25 +AC_PLUGIN([iwinfo],      [$with_iwinfo],       [Common iwinfo wireless statistics])
26  AC_PLUGIN([java],        [$with_java],         [Embed the Java Virtual Machine])
27  AC_PLUGIN([libvirt],     [$plugin_libvirt],    [Virtual machine statistics])
28  AC_PLUGIN([load],        [$plugin_load],       [System load])
29 @@ -4666,6 +4671,7 @@ Configuration:
30      protobuf-c  . . . . . $have_protoc_c
31      oracle  . . . . . . . $with_oracle
32      python  . . . . . . . $with_python
33 +    iwinfo  . . . . . . . $with_iwinfo
34  
35    Features:
36      daemon mode . . . . . $enable_daemon
37 @@ -4705,6 +4711,7 @@ Configuration:
38      iptables  . . . . . . $enable_iptables
39      ipvs  . . . . . . . . $enable_ipvs
40      irq . . . . . . . . . $enable_irq
41 +    iwinfo  . . . . . . . $enable_iwinfo
42      java  . . . . . . . . $enable_java
43      libvirt . . . . . . . $enable_libvirt
44      load  . . . . . . . . $enable_load
45 --- a/src/collectd.conf.in
46 +++ b/src/collectd.conf.in
47 @@ -82,6 +82,7 @@ FQDNLookup   true
48  #@BUILD_PLUGIN_IPMI_TRUE@LoadPlugin ipmi
49  #@BUILD_PLUGIN_IPVS_TRUE@LoadPlugin ipvs
50  #@BUILD_PLUGIN_IRQ_TRUE@LoadPlugin irq
51 +#@BUILD_PLUGIN_IWINFO_TRUE@LoadPlugin iwinfo
52  #@BUILD_PLUGIN_JAVA_TRUE@LoadPlugin java
53  #@BUILD_PLUGIN_LIBVIRT_TRUE@LoadPlugin libvirt
54  @BUILD_PLUGIN_LOAD_TRUE@@BUILD_PLUGIN_LOAD_TRUE@LoadPlugin load
55 @@ -376,6 +377,12 @@ FQDNLookup   true
56  #      IgnoreSelected true
57  #</Plugin>
58  
59 +#<Plugin iwinfo>
60 +#   Interface "ath0"
61 +#   Interface "ra0"
62 +#   Interface "wlan0"
63 +#</Plugin>
64 +
65  #<Plugin "java">
66  #      JVMArg "-verbose:jni"
67  #      JVMArg "-Djava.class.path=@prefix@/share/collectd/java/collectd-api.jar"
68 --- a/src/collectd.conf.pod
69 +++ b/src/collectd.conf.pod
70 @@ -1478,6 +1478,27 @@ and all other interrupts are collected.
71  
72  =back
73  
74 +=head2 Plugin C<iwinfo>
75 +
76 +=over 4
77 +
78 +=item B<Interface> I<Interface>
79 +
80 +Select this interface. By default all detected wireless interfaces will be
81 +collected. For a more detailed description see B<IgnoreSelected> below.
82 +
83 +=item B<IgnoreSelected> I<true>|I<false>
84 +
85 +If no configuration if given, the B<iwinfo>-plugin will collect data from all
86 +detected wireless interfaces. You can use the B<Interface>-option to pick the
87 +interfaces you're interested in. Sometimes, however, it's easier/preferred to
88 +collect all interfaces I<except> a few ones. This option enables you to do
89 +that: By setting B<IgnoreSelected> to I<true> the effect of B<Interface> is
90 +inverted: All selected interfaces are ignored and all other interfaces are
91 +collected.
92 +
93 +=back
94 +
95  =head2 Plugin C<java>
96  
97  The I<Java> plugin makes it possible to write extensions for collectd in Java.
98 --- /dev/null
99 +++ b/src/iwinfo.c
100 @@ -0,0 +1,150 @@
101 +/**
102 + * collectd - src/iwinfo.c
103 + * Copyright (C) 2011  Jo-Philipp Wich
104 + *
105 + * This program is free software; you can redistribute it and/or modify it
106 + * under the terms of the GNU General Public License as published by the
107 + * Free Software Foundation; only version 2 of the License is applicable.
108 + *
109 + * This program is distributed in the hope that it will be useful, but
110 + * WITHOUT ANY WARRANTY; without even the implied warranty of
111 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
112 + * General Public License for more details.
113 + *
114 + * You should have received a copy of the GNU General Public License along
115 + * with this program; if not, write to the Free Software Foundation, Inc.,
116 + * 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
117 + **/
118 +
119 +#include "collectd.h"
120 +#include "common.h"
121 +#include "plugin.h"
122 +#include "utils_ignorelist.h"
123 +
124 +#include <stdint.h>
125 +#include <iwinfo.h>
126 +
127 +#define PROCNETDEV "/proc/net/dev"
128 +
129 +static const char *config_keys[] = {
130 +       "Interface",
131 +       "IgnoreSelected"
132 +};
133 +static int config_keys_num = STATIC_ARRAY_SIZE (config_keys);
134 +
135 +static ignorelist_t *ignorelist = NULL;
136 +
137 +static int iwinfo_config(const char *key, const char *value)
138 +{
139 +       if (ignorelist == NULL)
140 +               ignorelist = ignorelist_create(1);
141 +
142 +       if (ignorelist == NULL)
143 +               return 1;
144 +
145 +       if (strcasecmp(key, "Interface") == 0)
146 +               ignorelist_add(ignorelist, value);
147 +       else if (strcasecmp(key, "IgnoreSelected") == 0)
148 +               ignorelist_set_invert(ignorelist, IS_TRUE(value) ? 0 : 1);
149 +       else
150 +               return -1;
151 +
152 +       return 0;
153 +}
154 +
155 +static void iwinfo_submit(const char *ifname, const char *type, int value)
156 +{
157 +       value_t values[1];
158 +       value_list_t vl = VALUE_LIST_INIT;
159 +
160 +       values[0].gauge = value;
161 +
162 +       vl.values = values;
163 +       vl.values_len = 1;
164 +
165 +       sstrncpy(vl.host, hostname_g, sizeof(vl.host));
166 +       sstrncpy(vl.plugin, "iwinfo", sizeof(vl.plugin));
167 +       sstrncpy(vl.plugin_instance, ifname, sizeof(vl.plugin_instance));
168 +       sstrncpy(vl.type, type, sizeof(vl.type));
169 +       /*sstrncpy(vl.type_instance, "", sizeof(vl.type_instance));*/
170 +
171 +       plugin_dispatch_values(&vl);
172 +}
173 +
174 +static void iwinfo_process(const char *ifname)
175 +{
176 +       int val;
177 +       char buf[IWINFO_BUFSIZE];
178 +       const struct iwinfo_ops *iw = iwinfo_backend(ifname);
179 +
180 +       /* does appear to be a wifi iface */
181 +       if (iw)
182 +       {
183 +               if (iw->bitrate(ifname, &val))
184 +                       val = 0;
185 +               iwinfo_submit(ifname, "bitrate", val * 1000);
186 +
187 +               if (iw->signal(ifname, &val))
188 +                       val = 0;
189 +               iwinfo_submit(ifname, "signal_power", val);
190 +
191 +               if (iw->noise(ifname, &val))
192 +                       val = 0;
193 +               iwinfo_submit(ifname, "signal_noise", val);
194 +
195 +               if (iw->quality(ifname, &val))
196 +                       val = 0;
197 +               iwinfo_submit(ifname, "signal_quality", val);
198 +
199 +               if (iw->assoclist(ifname, buf, &val))
200 +                       val = 0;
201 +               iwinfo_submit(ifname, "stations",
202 +                             val / sizeof(struct iwinfo_assoclist_entry));
203 +       }
204 +
205 +       iwinfo_finish();
206 +}
207 +
208 +static int iwinfo_read(void)
209 +{
210 +       char line[1024];
211 +       char ifname[128];
212 +       FILE *f;
213 +
214 +       f = fopen(PROCNETDEV, "r");
215 +       if (f == NULL)
216 +       {
217 +               char err[1024];
218 +               WARNING("iwinfo: Unable to open " PROCNETDEV ": %s",
219 +                       sstrerror(errno, err, sizeof(err)));
220 +               return -1;
221 +       }
222 +
223 +       while (fgets(line, sizeof(line), f))
224 +       {
225 +               if (!strchr(line, ':'))
226 +                       continue;
227 +
228 +               if (!sscanf(line, " %127[^:]", ifname))
229 +                       continue;
230 +
231 +               if (ignorelist_match(ignorelist, ifname))
232 +                       continue;
233 +
234 +               if (strstr(ifname, "mon.") || strstr(ifname, ".sta") ||
235 +                   strstr(ifname, "tmp.") || strstr(ifname, "wifi"))
236 +                       continue;
237 +
238 +               iwinfo_process(ifname);
239 +       }
240 +
241 +       fclose(f);
242 +
243 +       return 0;
244 +}
245 +
246 +void module_register(void)
247 +{
248 +       plugin_register_config("iwinfo", iwinfo_config, config_keys, config_keys_num);
249 +       plugin_register_read("iwinfo", iwinfo_read);
250 +}
251 --- a/src/Makefile.am
252 +++ b/src/Makefile.am
253 @@ -453,6 +453,15 @@ collectd_LDADD += "-dlopen" irq.la
254  collectd_DEPENDENCIES += irq.la
255  endif
256  
257 +if BUILD_PLUGIN_IWINFO
258 +pkglib_LTLIBRARIES += iwinfo.la
259 +iwinfo_la_SOURCES = iwinfo.c
260 +iwinfo_la_LDFLAGS = -module -avoid-version
261 +iwinfo_la_LIBADD = -liwinfo
262 +collectd_LDADD += "-dlopen" iwinfo.la
263 +collectd_DEPENDENCIES += iwinfo.la
264 +endif
265 +
266  if BUILD_PLUGIN_JAVA
267  pkglib_LTLIBRARIES += java.la
268  java_la_SOURCES = java.c
269 --- a/src/types.db
270 +++ b/src/types.db
271 @@ -171,3 +171,4 @@ voltage                     value:GAUGE:U:U
272  vs_memory              value:GAUGE:0:9223372036854775807
273  vs_processes           value:GAUGE:0:65535
274  vs_threads             value:GAUGE:0:65535
275 +stations               value:GAUGE:0:256