{"id":435,"date":"2015-11-25T12:59:15","date_gmt":"2015-11-25T11:59:15","guid":{"rendered":"http:\/\/paguilar.org\/?p=435"},"modified":"2015-11-25T12:59:15","modified_gmt":"2015-11-25T11:59:15","slug":"cross-compiling-a-custom-net-snmp-agent","status":"publish","type":"post","link":"https:\/\/paguilar.org\/?p=435","title":{"rendered":"Cross-compiling a custom Net-SNMP agent"},"content":{"rendered":"<p>Building the default agents in Net-SNMP is quite easy if you&#8217;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.<\/p>\n<p>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 <em>.\/configure<\/em> you will loose your changes.<\/p>\n<p>First, let&#8217;s export common environment variables that make our life easier:<\/p>\n<pre lang=\"bash\">\r\nexport HOST=arm-linux\r\nexport BUILD=i386-linux\r\nexport PREFIX=\/home\/paguilar\/rootfs<\/pre>\n<p><strong>Download<\/strong><\/p>\n<p><span style=\"color: #5077d7;\">Net-SNMP<\/span><br \/>\nPackage: <a href=\"http:\/\/sourceforge.net\/projects\/net-snmp\/files\/net-snmp\/5.6.2.1\/net-snmp-5.6.2.1.tar.gz\/download\">net-snmp-5.6.2.1<\/a><\/p>\n<p><strong>Configure<\/strong><\/p>\n<pre lang=\"bash\">\r\n.\/configure \\\r\n--prefix=$PREFIX \\\r\n--host=$HOST \\\r\n--build=$BUILD \\\r\n--exec-prefix=$PREFIX\/usr \\\r\n--sysconfdir=\/etc \\\r\n--enable-static \\\r\n--enable-shared \\\r\n--with-persistent-directory=\/var\/lib\/snmp \\\r\n--disable-static \\\r\n--with-defaults \\\r\n--enable-mini-agent \\\r\n--without-rpm \\\r\n--with-logfile=none \\\r\n--without-kmem-usage \\\r\n--enable-as-needed \\\r\n--disable-debugging \\\r\n--without-perl-modules \\\r\n--disable-embedded-perl \\\r\n--disable-perl-cc-checks \\\r\n--disable-scripts \\\r\n--with-default-snmp-version=\"1\" \\\r\n--enable-silent-libtool \\\r\n--enable-mfd-rewrites \\\r\n--with-sys-contact=\"root@localhost\" \\\r\n--with-sys-location=\"Unknown\" \\\r\n--with-mib-modules=\"host ucd-snmp\/dlmod examples\/example my_mib\/custom_mib\" \\\r\n--with-out-transports=\"Unix\" \\\r\n--with-endianness=little \\\r\n--without-openssl \\\r\n--disable-manuals \\\r\n--with-cflags=\"-I$PREFIX\/usr\/include -I$PREFIX\/usr\/include\/glib-2.0 -I$PREFIX\/usr\/lib\/glib-2.0\/include\" \\\r\n--with-ldflags=\"-L$PREFIX\/lib\" \\\r\n--with-mibdirs=\"$HOME\/.snmp\/mibs:\/usr\/share\/snmp\/mibs\"\r\n<\/pre>\n<p>Instead of using the <em>&#8211;with-cflags<\/em> and <em>&#8211;with-ldflags<\/em>, you can define the CFLAGS and LDFLAGS:<\/p>\n<pre lang=\"bash\">\r\nCFLAGS=\"-I$PREFIX\/usr\/include -I$PREFIX\/usr\/include\/glib-2.0 -I$PREFIX\/usr\/lib\/glib-2.0\/include\" \\\r\nLDFLAGS=\"-L$PREFIX\/usr\/lib -pthread -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lxml2 -lintl -lsqlite3 -lntxipc -lntxmisc\" \\\r\n<\/pre>\n<p>The custom agent must be located inside the <em>agent\/mibgroup<\/em> directory. In this case, create the directory <em>agent\/mibgroup\/my_mib<\/em> and write here the agent sources:<\/p>\n<pre lang=\"bash\">\r\nmkdir agent\/mibgroup\/my_mib\r\nvi agent\/mibgroup\/my_mib\/custom_mib.c\r\nvi agent\/mibgroup\/my_mib\/custom_mib.h\r\n<\/pre>\n<p>If these agent source use only <em>libc<\/em> it wouldn&#8217;t be an issue. But they use other libraries such as <a href=\"https:\/\/developer.gnome.org\/glib\/\" target=\"_blank\">Glib2<\/a>, <a href=\"http:\/\/sqlite.org\/\">SQLite3<\/a> and <a href=\"http:\/\/www.xmlsoft.org\/\">libxml2<\/a>, thus we have to include these libraries by using the <em>&#8211;with-cflags<\/em> and <em>&#8211;with-ldflags<\/em> options or the <em>CFLAGS<\/em> and <em>LDFLAGS<\/em> environment variables.<\/p>\n<p>However, when building, the linker didn&#8217;t find the GLib2, sqlite3 and libmlx2 shared libraries even though I specified the paths as above&#8230;<br \/>\nI got this kind of errors:<\/p>\n<pre lang=\"bash\">\r\n\/home\/paguilar\/net-snmp-5.6.2.1\/agent\/.libs\/libnetsnmpmibs.so: undefined reference to `sqlite3_close'\r\n\/home\/paguilar\/net-snmp-5.6.2.1\/agent\/.libs\/libnetsnmpmibs.so: undefined reference to `sqlite3_exec'\r\n\/home\/paguilar\/net-snmp-5.6.2.1\/agent\/.libs\/libnetsnmpmibs.so: undefined reference to `sqlite3_errmsg'\r\n\/home\/paguilar\/net-snmp-5.6.2.1\/agent\/.libs\/libnetsnmpmibs.so: undefined reference to `sqlite3_open'\r\ncollect2: ld returned 1 exit status\r\nMakefile:245: recipe for target 'snmptrapd' failed\r\nmake[1]: *** [snmptrapd] Error 1\r\nmake[1]: Leaving directory '\/home\/paguilar\/net-snmp-5.6.2.1\/apps'\r\nMakefile:567: recipe for target 'subdirs' failed\r\nmake: *** [subdirs] Error 1\r\n<\/pre>\n<p>For solving this issue in a dirty but fast way, modify the files <em>agent\/Makefile<\/em> and <em>apps\/Makefile<\/em>. Set the <em>LDFLAGS<\/em> variable to the following value:<\/p>\n<pre lang=\"bash\">\r\nLDFLAGS = -L\/home\/paguilar\/rootfs\/usr\/lib -pthread -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lxml2 -lintl -lsqlite3\r\n<\/pre>\n<p>In this way you can quickly build your custom SNMP agent inside Net-SNMP.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Building the default agents in Net-SNMP is quite easy if you&#8217;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\u2026 <span class=\"read-more\"><a href=\"https:\/\/paguilar.org\/?p=435\">Read More &raquo;<\/a><\/span><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[61,6,45,17],"tags":[],"class_list":["post-435","post","type-post","status-publish","format-standard","hentry","category-buildroot","category-compiling","category-linux","category-xml"],"_links":{"self":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=435"}],"version-history":[{"count":8,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions"}],"predecessor-version":[{"id":443,"href":"https:\/\/paguilar.org\/index.php?rest_route=\/wp\/v2\/posts\/435\/revisions\/443"}],"wp:attachment":[{"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/paguilar.org\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}