Skip to content

Commit 12b9c98

Browse files
committed
SNMP support.
1 parent 4ef16b0 commit 12b9c98

File tree

8 files changed

+1851
-44
lines changed

8 files changed

+1851
-44
lines changed

IPT-NETFLOW-MIB.my

Lines changed: 795 additions & 0 deletions
Large diffs are not rendered by default.

Makefile.in

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,18 @@ IPTABLES_CFLAGS = @IPTABLES_CFLAGS@
88
IPTABLES_MODULES = @IPTABLES_MODULES@
99
DEPMOD = depmod -a
1010
CARGS = @CARGS@
11+
SNMPTGSO = /usr/lib/snmp/dlmod/@SNMPTARGET@
12+
SNMPCONF = /etc/snmp/snmpd.conf
13+
SNMPLINE = dlmod netflow $(SNMPTGSO)
14+
CC = gcc
1115

1216
# https://www.kernel.org/doc/Documentation/kbuild/modules.txt
1317
# https://www.kernel.org/doc/Documentation/kbuild/makefiles.txt
1418
obj-m = ipt_NETFLOW.o
1519
ccflags-y = @KOPTS@
1620

17-
all: ipt_NETFLOW.ko libipt_NETFLOW.so libip6t_NETFLOW.so
21+
all: ipt_NETFLOW.ko libipt_NETFLOW.so libip6t_NETFLOW.so @SNMPTARGET@
22+
1823
ipt_NETFLOW.ko: version.h ipt_NETFLOW.c ipt_NETFLOW.h Makefile
1924
@echo Compiling for kernel $(KVERSION)
2025
make -C $(KDIR) M=$(CURDIR) modules
@@ -25,6 +30,7 @@ sparse: | version.h ipt_NETFLOW.c ipt_NETFLOW.h Makefile
2530
make -C $(KDIR) M=$(CURDIR) modules C=1
2631
@touch ipt_NETFLOW.ko
2732
minstall: | ipt_NETFLOW.ko
33+
@echo " *"
2834
make -C $(KDIR) M=$(CURDIR) modules_install INSTALL_MOD_PATH=$(DESTDIR)
2935
$(DEPMOD)
3036
mclean:
@@ -34,6 +40,26 @@ lclean:
3440
clean: mclean lclean
3541
-rm -f *.so *.o modules.order version.h
3642

43+
snmp_NETFLOW.so: snmp_NETFLOW.c
44+
$(CC) `net-snmp-config --cflags` -fPIC -shared -g -O0 -o $@ $< `net-snmp-config --libs`
45+
46+
sinstall: | snmp_NETFLOW.so IPT-NETFLOW-MIB.my
47+
@echo " *"
48+
install -D IPT-NETFLOW-MIB.my $(DESTDIR)/usr/share/snmp/mibs/IPT-NETFLOW-MIB.my
49+
install -D snmp_NETFLOW.so $(DESTDIR)$(SNMPTGSO)
50+
@if ! egrep -qs "^ *$(SNMPLINE)" $(SNMPCONF); then \
51+
echo " *"; \
52+
echo " * Add this line to $(SNMPCONF) to enable IPT-NETFLOW-MIB:"; \
53+
echo " *"; \
54+
echo " * $(SNMPLINE)"; \
55+
echo " *"; \
56+
fi
57+
@if killall -0 snmpd >/dev/null 2>&1; then \
58+
echo " * (snmpd needs restart for changes to take effect.)"; \
59+
else \
60+
echo " * (snmpd is not started.)"; \
61+
fi
62+
3763
%_sh.o: libipt_NETFLOW.c
3864
gcc -O2 -Wall -Wunused $(IPTABLES_CFLAGS) -fPIC -o $@ -c libipt_NETFLOW.c
3965

@@ -48,10 +74,11 @@ version.h: ipt_NETFLOW.c ipt_NETFLOW.h Makefile
4874
fi > version.h
4975

5076
linstall: | libipt_NETFLOW.so libip6t_NETFLOW.so
77+
@echo " *"
5178
install -D libipt_NETFLOW.so $(DESTDIR)$(IPTABLES_MODULES)/libipt_NETFLOW.so
5279
install -D libip6t_NETFLOW.so $(DESTDIR)$(IPTABLES_MODULES)/libip6t_NETFLOW.so
5380

54-
install: minstall linstall
81+
install: minstall linstall @SNMPINSTALL@
5582

5683
uninstall:
5784
-rm -f $(DESTDIR)$(IPTABLES_MODULES)/libipt_NETFLOW.so

README

Lines changed: 109 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
3232
* Deterministic (systematic count-based), random and hash Flow Sampling.
3333
With appropriate differences in support of v5, v9, and IPFIX.
3434

35+
* SNMP agent (for net-snmp) for remote management and monitoring.
36+
3537
* Options Templates (v9/IPFIX) let export useful statistical,
3638
configurational, and informational records to collector.
3739
Such as metering, exporting, sampling stat and reliability stat, sampling
@@ -65,7 +67,7 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
6567
= INSTALLATION =
6668
================
6769

68-
Four easy steps.
70+
Five easy steps.
6971

7072
** 1. Prepare Kernel source
7173

@@ -122,7 +124,23 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
122124

123125
c) Otherwise, for raw iptables source build it and make install.
124126

125-
** 3. Now, to actually build the module run:
127+
** 3. Prepare net-snmp (optional)
128+
129+
In case you want to manage or monitor module performance via SNMP you
130+
may install net-snmp. If you want to skip this step run configure
131+
with --disable-snmp-agent option.
132+
133+
a) For Centos:
134+
135+
# yum install net-snmp net-snmp-devel
136+
137+
b) For Debian:
138+
139+
# apt-get install snmpd libsnmp-dev
140+
141+
c) Otherwise install net-snmp from www.net-snmp.org
142+
143+
** 4. Now, to actually build the module run:
126144

127145
~/ipt-netflow# ./configure
128146
~/ipt-netflow# make all install
@@ -143,7 +161,13 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
143161
c) If you have sources in non-standard places or configure isn't able to
144162
find something run ./configure --help to see how to specify paths manually.
145163

146-
** 4. After this point you should be able to load module and
164+
d) On Debian:
165+
`gcc: error: unrecognized command line option `-fstack-protector-strong''
166+
`Makefile:43: recipe for target 'snmp_NETFLOW.so' failed'
167+
Solution is to install gcc-4.9: apt-get install gcc-4.9
168+
then compile with: make CC=gcc-4.9
169+
170+
** 5. After this point you should be able to load module and
147171
use -j NETFLOW target in your iptables. See next section.
148172

149173

@@ -161,6 +185,16 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
161185
install' you'll need to load nf_conntrack manually.
162186
Read below for explanation of natevents.
163187

188+
--enable-sampler
189+
enables flow sampler. Read below for explanation of its configuration
190+
option.
191+
192+
--enable-sampler=hash
193+
additionally enables 'hash' sampler.
194+
195+
--disable-snmp-agent
196+
disables building net-snmp agent module, which is enabled by default.
197+
164198
--enable-snmp-rules
165199
enables SNMP-index conversion rules. Read below for explanation
166200
of snmp-rules.
@@ -184,13 +218,6 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
184218
ingress flows(0), in OUTPUT and POSTROUTING as egress flows(1), and
185219
in FORWARD will have flowDirection set to undefined value 255.
186220

187-
--enable-sampler
188-
enables flow sampler. Read below for explanation of its configuration
189-
option.
190-
191-
--enable-sampler=hash
192-
additionally enables 'hash' sampler.
193-
194221
--disable-aggregation
195222
disables aggregation rules (they are enabled by default).
196223
Read below for explanation of aggregation.
@@ -220,6 +247,7 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
220247
options ipt_NETFLOW destination=127.0.0.1:2055 protocol=9 natevents=1
221248

222249
2. Statistics is in /proc/net/stat/ipt_netflow
250+
Machine readable statistics is in /proc/net/stat/ipt_netflow_snmp
223251
To view boring slab statistics: grep ipt_netflow /proc/slabinfo
224252

225253
3. You can view parameters and control them via sysctl, example:
@@ -267,13 +295,81 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
267295
For details on how they are exported for different protocol versions see
268296
below.
269297

298+
7. For SNMP support you will need to add this command into snmpd.conf to
299+
enable IPT-NETFLOW-MIB in SNMP agent:
300+
301+
dlmod netflow /usr/lib/snmp/dlmod/snmp_NETFLOW.so
302+
303+
Restart snmpd for changes to take effect. Don't forget to properly configure
304+
access control. Example simplest configuration may looks like (note that this
305+
is whole /etc/snmp/snmpd.conf):
306+
307+
rocommunity public 127.0.0.1
308+
dlmod netflow /usr/lib/snmp/dlmod/snmp_NETFLOW.so
309+
310+
Note, that this config will also allow _full_ read-only access to the whole
311+
linux MIB. To install IPT-NETFLOW-MIB locally, copy file IPT-NETFLOW-MIB.my
312+
into ~/.snmp/mibs/
313+
To check that MIB is installed well you may issue:
314+
315+
$ snmptranslate -m IPT-NETFLOW-MIB -IR -Tp iptNetflowMIB
316+
317+
This should output IPT-NETFLOW-MIB in tree form.
318+
319+
To check that snmp agent is working well issue:
320+
321+
$ snmpwalk -v 1 -c public 127.0.0.1 -m IPT-NETFLOW-MIB iptNetflowMIB
322+
323+
Should output full MIB. If MIB is not installed try:
324+
325+
$ snmpget -v 1 -c public 127.0.0.1 .1.3.6.1.4.1.37476.9000.10.1.1.1.1.0
326+
327+
Which should output STRING: "ipt_NETFLOW".
328+
329+
MIB profides access to very similar statistics that you have in
330+
/proc/net/stat/ipt_netflow, you can read description of objects in
331+
text file IPT-NETFLOW-MIB.my
332+
333+
If you want to access to SNMP stat in machine readable form for your
334+
scripts there is file /proc/net/stat/ipt_netflow_snmp
335+
336+
Note: Using of SNMP v2c or v3 is mandatory for most tables, because
337+
this MIB uses 64-bit counters (Counter64) which is not supported in old
338+
SNMP v1. You shoudl understand that 32-bit counter will wrap on 10Gbit
339+
traffic in just 3.4 seconds! So, always pass option `-v2c' or `-v3'
340+
to net-snmp utils. Or, for example, configure option `defVersion 2c'
341+
in ~/.snmp/snmp.conf You can also have `defCommunity public' ov v3
342+
auth parameters (defSecurityName, defSecurityLevel, defPassphrase)
343+
set there (man snmp.conf).
344+
345+
Examples for dumping typical IPT-NETFLOW-MIB objects:
346+
347+
- Module info (similar to modinfo, SNMPv1 is ok for following two objects):
348+
349+
$ snmpwalk -v 1 -c public 127.0.0.1 -m IPT-NETFLOW-MIB iptNetflowModule
350+
351+
- Read-write sysctl-like parameters (yes, they are writable via snmpset, you
352+
may need to configure write access to snmpd, though):
353+
354+
$ snmpwalk -v 1 -c public 127.0.0.1 -m IPT-NETFLOW-MIB iptNetflowSysctl
355+
356+
- Global performance stat of the module (note -v2c, because rest of the
357+
objects require SNMP v2c or SNMP v3):
358+
359+
$ snmpwalk -v2c -c public 127.0.0.1 -m IPT-NETFLOW-MIB iptNetflowTotals
360+
361+
- Per-CPU (metering) and per-socket (exporting) statistics in table format:
362+
363+
$ snmptable -v2c -c public 127.0.0.1 -m IPT-NETFLOW-MIB iptNetflowCpuTable
364+
$ snmptable -v2c -c public 127.0.0.1 -m IPT-NETFLOW-MIB iptNetflowSockTable
365+
270366

271367
===========
272368
= OPTIONS =
273369
===========
274370

275371
Options can be passed as parameters to module or changed dynamically
276-
via sysctl net.netflow
372+
via sysctl net.netflow or IPT-NETFLOW-MIB::iptNetflowSysctl
277373

278374
protocol=5
279375
- what version of NetFlow protocol to use. Default is 5.
@@ -435,8 +531,8 @@ ipt_NETFLOW linux 2.6.x-3.x kernel module by <abc@telekom.ru> -- 2008-2014.
435531
Statistics is your friend to fine tune and understand netflow module
436532
performance.
437533

438-
To see stat:
439-
# cat /proc/net/stat/ipt_netflow
534+
To see stat in human readable form:
535+
# cat /proc/net/stat/ipt_netflow
440536

441537
How to interpret the data:
442538

configure

Lines changed: 71 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -260,6 +260,7 @@ show_help() {
260260
echo " --enable-sampler=hash enables Hash sampler"
261261
echo " --disable-aggregation disables aggregation rules"
262262
echo " --enable-promisc enables promisc hack"
263+
echo " --disable-snmp-agent disables net-snmp agent"
263264
exit 0
264265
}
265266

@@ -281,15 +282,17 @@ do
281282
--kdir=*) KDIR="$ac_optarg" ;;
282283
--enable-nat*) KOPTS="$KOPTS -DENABLE_NAT" ;;
283284
--enable-debug*) KOPTS="$KOPTS -DENABLE_DEBUGFS" ;;
284-
--enable-snmp*) KOPTS="$KOPTS -DSNMP_RULES" ;;
285285
--enable-mac*) KOPTS="$KOPTS -DENABLE_MAC" ;;
286286
--enable-vlan*) KOPTS="$KOPTS -DENABLE_VLAN" ;;
287287
--enable-direc*) KOPTS="$KOPTS -DENABLE_DIRECTION" ;;
288288
--enable-sampl*hash) KOPTS="$KOPTS -DENABLE_SAMPLER -DSAMPLING_HASH" ;;
289289
--enable-sampl*) KOPTS="$KOPTS -DENABLE_SAMPLER" ;;
290290
--disable-aggr*) KOPTS="$KOPTS -DDISABLE_AGGR" ;;
291-
--enable-promisc*) KOPTS="$KOPTS -DENABLE_PROMISC" ;;
292-
--make) echo called from make ;;
291+
--enable-promi*) KOPTS="$KOPTS -DENABLE_PROMISC" ;;
292+
--enable-snmp-r*) KOPTS="$KOPTS -DENABLE_SNMP" ;;
293+
--disable-snmp-a*) SKIPSNMP=1 ;;
294+
--disable-net-snmp*) SKIPSNMP=1 ;;
295+
--make) echo called from make; CARGS=`echo $CARGS | sed s/--make//g` ;;
293296
-Werror) KOPTS="$KOPTS -Werror" ;;
294297
--help|-h) show_help ;;
295298
-*) echo Invalid option: $ac_option; exit 1 ;;
@@ -410,6 +413,67 @@ kernel_check_features() {
410413
kernel_check_include include/linux/llist.h -DHAVE_LLIST
411414
}
412415

416+
snmp_check() {
417+
test "$SKIPSNMP" && return
418+
419+
echo -n "Searching for net-snmp-config... "
420+
if type -p net-snmp-config > /dev/null; then
421+
echo Yes `type -p net-snmp-config`
422+
else
423+
echo No.
424+
SNMPCONFIG=no
425+
fi
426+
427+
echo -n "Searching for net-snmp agent... "
428+
if [ -s /etc/redhat-release ]; then
429+
if ! rpm --quiet -q net-snmp; then
430+
echo No.
431+
SNMPADD="do: yum install net-snmp"
432+
if [ "$SNMPCONFIG" ]; then
433+
SNMPADD="$SNMPADD net-snmp-devel"
434+
fi
435+
else
436+
echo Yes.
437+
fi
438+
if [ "$SNMPCONFIG" ]; then
439+
SNMPCONFIG="run: yum install net-snmp-devel"
440+
fi
441+
elif [ -s /etc/debian_version ]; then
442+
if ! dpkg -s snmpd >/dev/null 2>&1; then
443+
echo No.
444+
SNMPADD="do: apt-get install snmpd"
445+
if [ "$SNMPCONFIG" ]; then
446+
SNMPADD="$SNMPADD libsnmp-dev"
447+
fi
448+
else
449+
echo Yes.
450+
fi
451+
if [ "$SNMPCONFIG" ]; then
452+
SNMPCONFIG="run: apt-get install libsnmp-dev"
453+
fi
454+
elif [ -s /etc/snmp/snmpd.conf ]; then
455+
echo Yes.
456+
else
457+
echo No.
458+
SNMPADD="install net-snmp (www.net-snmp.org)"
459+
SNMPCONFIG="reinstall net-snmp with agent support."
460+
fi
461+
462+
if [ "$SNMPADD" ]; then
463+
echo " Assuming you don't want net-snmp agent support".
464+
echo " Otherwise $SNMPADD"
465+
return
466+
elif [ "$SNMPCONFIG" ]; then
467+
echo "! You have net-snmp agent but not development package."
468+
echo "! net-snmp agent will not be built, to fix:"
469+
echo "! $SNMPCONFIG"
470+
return
471+
fi
472+
473+
SNMPTARGET=snmp_NETFLOW.so
474+
SNMPINSTALL=sinstall
475+
}
476+
413477
kernel_find_version #KVERSION
414478
test "$KLIBMOD" || KLIBMOD=$KVERSION
415479
echo "Kernel version: $KVERSION ($KHOW)"
@@ -428,11 +492,15 @@ iptables_src_version #check that IPTSRC match to IPTVER
428492
iptables_inc #IPTINC
429493
iptables_modules #IPTLIB
430494

495+
snmp_check
496+
431497
REPLACE="\
432498
s!@CARGS@!$CARGS!;\
433499
s!@KVERSION@!$KVERSION!;\
434500
s!@KDIR@!$KDIR!;\
435501
s!@KOPTS@!$KOPTS!;\
502+
s!@SNMPTARGET@!$SNMPTARGET!;\
503+
s!@SNMPINSTALL@!$SNMPINSTALL!;\
436504
s!@IPTABLES_VERSION@!$IPTVER!;\
437505
s!@IPTABLES_CFLAGS@!$IPTCFLAGS $IPTINC!;\
438506
s!@IPTABLES_MODULES@!$IPTLIB!"

0 commit comments

Comments
 (0)