Cross-compiling a custom Net-SNMP agent

By | November 25, 2015

Building the default agents in Net-SNMP is quite easy if you’re using tools such as Buildroot, and even when building directly from the sources is not a problem. However, is not that easy if you want to cross-build a custom agent inside Net-SNMP.

This procedure worked for me, but keep in mind that it was not a clean procedure since it involves modifying the generated Makefiles, so every time that you ./configure you will loose your changes.

First, let’s export common environment variables that make our life easier:

export HOST=arm-linux
export BUILD=i386-linux
export PREFIX=/home/paguilar/rootfs

Download

Net-SNMP
Package: net-snmp-5.6.2.1

Configure

./configure \
--prefix=$PREFIX \
--host=$HOST \
--build=$BUILD \
--exec-prefix=$PREFIX/usr \
--sysconfdir=/etc \
--enable-static \
--enable-shared \
--with-persistent-directory=/var/lib/snmp \
--disable-static \
--with-defaults \
--enable-mini-agent \
--without-rpm \
--with-logfile=none \
--without-kmem-usage \
--enable-as-needed \
--disable-debugging \
--without-perl-modules \
--disable-embedded-perl \
--disable-perl-cc-checks \
--disable-scripts \
--with-default-snmp-version="1" \
--enable-silent-libtool \
--enable-mfd-rewrites \
--with-sys-contact="root@localhost" \
--with-sys-location="Unknown" \
--with-mib-modules="host ucd-snmp/dlmod examples/example my_mib/custom_mib" \
--with-out-transports="Unix" \
--with-endianness=little \
--without-openssl \
--disable-manuals \
--with-cflags="-I$PREFIX/usr/include -I$PREFIX/usr/include/glib-2.0 -I$PREFIX/usr/lib/glib-2.0/include" \
--with-ldflags="-L$PREFIX/lib" \
--with-mibdirs="$HOME/.snmp/mibs:/usr/share/snmp/mibs"

Instead of using the –with-cflags and –with-ldflags, you can define the CFLAGS and LDFLAGS:

CFLAGS="-I$PREFIX/usr/include -I$PREFIX/usr/include/glib-2.0 -I$PREFIX/usr/lib/glib-2.0/include" \
LDFLAGS="-L$PREFIX/usr/lib -pthread -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lxml2 -lintl -lsqlite3 -lntxipc -lntxmisc" \

The custom agent must be located inside the agent/mibgroup directory. In this case, create the directory agent/mibgroup/my_mib and write here the agent sources:

mkdir agent/mibgroup/my_mib
vi agent/mibgroup/my_mib/custom_mib.c
vi agent/mibgroup/my_mib/custom_mib.h

If these agent source use only libc it wouldn’t be an issue. But they use other libraries such as Glib2, SQLite3 and libxml2, thus we have to include these libraries by using the –with-cflags and –with-ldflags options or the CFLAGS and LDFLAGS environment variables.

However, when building, the linker didn’t find the GLib2, sqlite3 and libmlx2 shared libraries even though I specified the paths as above…
I got this kind of errors:

/home/paguilar/net-snmp-5.6.2.1/agent/.libs/libnetsnmpmibs.so: undefined reference to `sqlite3_close'
/home/paguilar/net-snmp-5.6.2.1/agent/.libs/libnetsnmpmibs.so: undefined reference to `sqlite3_exec'
/home/paguilar/net-snmp-5.6.2.1/agent/.libs/libnetsnmpmibs.so: undefined reference to `sqlite3_errmsg'
/home/paguilar/net-snmp-5.6.2.1/agent/.libs/libnetsnmpmibs.so: undefined reference to `sqlite3_open'
collect2: ld returned 1 exit status
Makefile:245: recipe for target 'snmptrapd' failed
make[1]: *** [snmptrapd] Error 1
make[1]: Leaving directory '/home/paguilar/net-snmp-5.6.2.1/apps'
Makefile:567: recipe for target 'subdirs' failed
make: *** [subdirs] Error 1

For solving this issue in a dirty but fast way, modify the files agent/Makefile and apps/Makefile. Set the LDFLAGS variable to the following value:

LDFLAGS = -L/home/paguilar/rootfs/usr/lib -pthread -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lxml2 -lintl -lsqlite3

In this way you can quickly build your custom SNMP agent inside Net-SNMP.

Leave a Reply

Your email address will not be published. Required fields are marked *