Minimum C application for a snmp agent

I am beginning to know the net-snmp api for C applications and I have tried to find a simple example of an SNMP agent that is able to respond with GetResponse to GetRequest, GetNextRequest or SetRequest received from an NMS.
I have not found any such example, nor tutorials or books that explain it.
Is there a tutorial or an example (web, GitHub or GitLab) that serves as a starting point?

We cannot help for NET-SNMP or pure C here in this forum. But the n C++ is ok too, then you can download SNMP++ and AGENT++ from https://www.agentpp.com/download.html unpack both in the same (empty) directory on a Linux machine autoconf (see corresponding README.autoconf) and compile them with

autoreconf -i
./configure
sudo make install

In the folder

agent++/examples/atm-mib

you will find a fully functional SNMP agent examples including sources.
The tutorial that helps to understand the basics about AGENT++ can be found here - although it is about an outdated version 2, the fundamental concepts are still valid:

Hii @AGENTPP I am trying to compile the SNMP++ and AGENT++ in a 64bit Linux environment with the above steps.
Firstly for snmp+±3.4.6 followed below commands
autoreconf -i
./configure --host=powerpc-linux
make
make install
Secondly in agent+±4.5.1 ran below commands
autoreconf -i
./configure --host=powerpc-linux
make
But while running the make command for agent++ I am getting below errors

libtool: compile: g++ -DHAVE_CONFIG_H -I. -I… -I…/include -pthread -I/usr/local/include -D_GNU_SOURCE -D_XOPEN_SOURCE=XPG6 -g -O2 -pthread -MT mib.lo -MD -MP -MF .deps/mib.Tpo -c mib.cpp -o mib.o
In file included from mib.cpp:31:0:
…/include/agent_pp/notification_originator.h:232:39: error: ‘snmpCommunityEntry’ has not been declared

  •     void set_snmp_community_entry(snmpCommunityEntry* communityEntryRef) {*
    
  •                                   ^*
    

…/include/agent_pp/notification_originator.h:244:17: error: ‘snmpCommunityEntry’ does not name a type

  •     virtual snmpCommunityEntry* get_snmp_community_entry();*
    
  •             ^*
    

…/include/agent_pp/notification_originator.h:296:32: error: ‘nlmLogEntry’ has not been declared

  •     void set_nlm_log_entry(nlmLogEntry* nlmLogEntryRef) {*
    
  •                            ^*
    

…/include/agent_pp/notification_originator.h:308:17: error: ‘nlmLogEntry’ does not name a type

  •     virtual nlmLogEntry* get_nlm_log_entry();*
    
  •             ^*
    

…/include/agent_pp/notification_originator.h:318:9: error: ‘snmpCommunityEntry’ does not name a type

  •     snmpCommunityEntry*     communityEntry;*
    
  •     ^*
    

…/include/agent_pp/notification_originator.h:321:9: error: ‘nlmLogEntry’ does not name a type

  •     nlmLogEntry*            _nlmLogEntry;*
       ^
    

…/include/agent_pp/notification_originator.h: In constructor ‘Agentpp::NotificationOriginator::NotificationOriginator(Agentpp::Mib*)’:
…/include/agent_pp/notification_originator.h:71:62: warning: delegating constructors only available with -std=c++11 or -std=gnu++11
NotificationOriginator(Mib* mibRef): NotificationOriginator() {
^
…/include/agent_pp/notification_originator.h: In member function ‘void Agentpp::NotificationOriginator::set_snmp_community_entry(int*)’:
…/include/agent_pp/notification_originator.h:233:13: error: ‘communityEntry’ was not declared in this scope
communityEntry = communityEntryRef;
^
…/include/agent_pp/notification_originator.h: In member function ‘void Agentpp::NotificationOriginator::set_nlm_log_entry(int*)’:
…/include/agent_pp/notification_originator.h:297:13: error: ‘_nlmLogEntry’ was not declared in this scope
_nlmLogEntry = nlmLogEntryRef;
^
Makefile:564: recipe for target ‘mib.lo’ failed
make[2]: *** [mib.lo] Error 1
make[2]: Leaving directory ‘/home/administrator/SNMP++_test/agent+±4.5.1/src’
Makefile:573: recipe for target ‘all-recursive’ failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory ‘/home/administrator/SNMP++_test/agent+±4.5.1’
Makefile:459: recipe for target ‘all’ failed
make: *** [all] Error 2

I have searched in the forum and got some topics like [Agent++ compile fails if snmp++ has –disable-snmpv3] but none of them answers correctly how to fix this issue, and I have not disabled the snmpv3 option and also using the latest code.
Any suggestions how can this be fixed?

Hi,

can you try again with at least snmp++ version 3.4.7. and the following changes in include/agent_pp/notification_originator.h? Change the four includes at the top of the file from #include "xxx.h" to #include <agent_pp/xxx.h>.

Regards,
Jochen

Hi,
Still getting the same errors after making the change in the #includes in include/agent_pp/notification_originator.h, I was using snmp++ version 3.4.6.
Compiling version 3.4.7 is giving some errors like

auth_priv.cpp:2289:3: error: ‘unique_ptr’ is not a member of ‘std’
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2289:25: error: expected primary-expression before ‘>’ token
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2289:41: error: ‘h’ was not declared in this scope
std::unique_ptr h(get_hasher());
^
auth_priv.cpp: In member function ‘virtual int Snmp_pp::AuthSHABase::hash(const unsigned char*, unsigned int, unsigned char*) const’:
auth_priv.cpp:2338:3: error: ‘unique_ptr’ is not a member of ‘std’
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2338:25: error: expected primary-expression before ‘>’ token
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2338:41: error: ‘h’ was not declared in this scope
std::unique_ptr h(get_hasher());
^
auth_priv.cpp: In member function ‘virtual int Snmp_pp::AuthSHABase::auth_out_msg(const unsigned char*, unsigned char*, int, unsigned char*)’:
auth_priv.cpp:2352:3: error: ‘unique_ptr’ is not a member of ‘std’
std::unique_ptr h(get_hasher());

Hi,

the file auth_priv.cpp did not change from 3.4.6 to 3.4.7, so something seems to go wrong on your system.

Do you have some older header files of snmp++/agent++ installed on the system? Can you post the complete (not the shortened version with “...”) compiler command line?

Regards,
Jochen

Hi,

I have checked I do not have any older header files installed on the system.
Attached is the complete error logs.

make all-recursive
make[1]: Entering directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6’
Making all in include
make[2]: Entering directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/include’
Making all in snmp_pp
make[3]: Entering directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/include/snmp_pp’
make[3]: Nothing to be done for ‘all’.
make[3]: Leaving directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/include/snmp_pp’
make[3]: Entering directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/include’
make[3]: Nothing to be done for ‘all-am’.
make[3]: Leaving directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/include’
make[2]: Leaving directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/include’
Making all in src
make[2]: Entering directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/src’
/bin/bash …/libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -I… -I/home/administrator/SNMP++_test/snmp+±3.4.6/include -pthread -D_GNU_SOURCE -g -O2 -pthread -MT address.lo -MD -MP -MF .deps/address.Tpo -c -o address.lo address.cpp
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I… -I/home/administrator/SNMP++_test/snmp+±3.4.6/include -pthread -D_GNU_SOURCE -g -O2 -pthread -MT address.lo -MD -MP -MF .deps/address.Tpo -c address.cpp -o address.o
mv -f .deps/address.Tpo .deps/address.Plo
/bin/bash …/libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -I… -I/home/administrator/SNMP++_test/snmp+±3.4.6/include -pthread -D_GNU_SOURCE -g -O2 -pthread -MT asn1.lo -MD -MP -MF .deps/asn1.Tpo -c -o asn1.lo asn1.cpp
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I… -I/home/administrator/SNMP++_test/snmp+±3.4.6/include -pthread -D_GNU_SOURCE -g -O2 -pthread -MT asn1.lo -MD -MP -MF .deps/asn1.Tpo -c asn1.cpp -o asn1.o
mv -f .deps/asn1.Tpo .deps/asn1.Plo
/bin/bash …/libtool --tag=CXX --mode=compile g++ -DHAVE_CONFIG_H -I. -I… -I/home/administrator/SNMP++_test/snmp+±3.4.6/include -pthread -D_GNU_SOURCE -g -O2 -pthread -MT auth_priv.lo -MD -MP -MF .deps/auth_priv.Tpo -c -o auth_priv.lo auth_priv.cpp
libtool: compile: g++ -DHAVE_CONFIG_H -I. -I… -I/home/administrator/SNMP++_test/snmp+±3.4.6/include -pthread -D_GNU_SOURCE -g -O2 -pthread -MT auth_priv.lo -MD -MP -MF .deps/auth_priv.Tpo -c auth_priv.cpp -o auth_priv.o
auth_priv.cpp: In member function ‘virtual int Snmp_pp::AuthSHABase::password_to_key(const unsigned char*, unsigned int, const unsigned char*, unsigned int, unsigned char*, unsigned int*)’:
auth_priv.cpp:2289:3: error: ‘unique_ptr’ is not a member of ‘std’
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2289:25: error: expected primary-expression before ‘>’ token
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2289:41: error: ‘h’ was not declared in this scope
std::unique_ptr h(get_hasher());
^
auth_priv.cpp: In member function ‘virtual int Snmp_pp::AuthSHABase::hash(const unsigned char*, unsigned int, unsigned char*) const’:
auth_priv.cpp:2338:3: error: ‘unique_ptr’ is not a member of ‘std’
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2338:25: error: expected primary-expression before ‘>’ token
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2338:41: error: ‘h’ was not declared in this scope
std::unique_ptr h(get_hasher());
^
auth_priv.cpp: In member function ‘virtual int Snmp_pp::AuthSHABase::auth_out_msg(const unsigned char*, unsigned char*, int, unsigned char*)’:
auth_priv.cpp:2352:3: error: ‘unique_ptr’ is not a member of ‘std’
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2352:25: error: expected primary-expression before ‘>’ token
std::unique_ptr h(get_hasher());
^
auth_priv.cpp:2352:41: error: ‘h’ was not declared in this scope
std::unique_ptr h(get_hasher());
^
Makefile:559: recipe for target ‘auth_priv.lo’ failed
make[2]: *** [auth_priv.lo] Error 1
make[2]: Leaving directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6/src’
Makefile:587: recipe for target ‘all-recursive’ failed
make[1]: *** [all-recursive] Error 1
make[1]: Leaving directory ‘/home/administrator/SNMP++_test/snmp+±3.4.6’
Makefile:473: recipe for target ‘all’ failed
make: *** [all] Error 2

Hi,

when I replaced auto_ptr with unique_ptr I forgot to change the check in the configure script from c++0x to c++11. As my compiler seems to automatically enable c++11, this was not detected.

Until this is fixed, run configure with the environment variable CXXFLAGS="-std=c++11" (or whatever the compiler needs to enable c++11.

Kind regards,
Jochen

Hello,

Thanks for your inputs, changing the CXXFLAGS worked. It got compiled with some warnings.

Hello,
While building the agent+±4.5.2 I am getting the following errors while running make command.
Here is the config that I have used
./configure CXX=C:/cygwin/cdk/ppc/60x/glibc-2.3.4/bin/ppc_60x-g++ CC=C:/cygwin/cdk/ppc/60x/glibc-2.3.4/bin/ppc_60x-gcc --host=powerpc-linux --with-examples --with-agenpro --enable-static=NO --enable-shared

Error:-
In file included from mib.cpp:31:
…/include/agent_pp/notification_originator.h: In constructor Agentpp::NotificationOriginator::NotificationOriginator(Agentpp::Mib*)': ../include/agent_pp/notification_originator.h:70: error: type class Agentpp::NotificationOriginator’ is not a direct base of `Agentpp::NotificationOriginator’
make[2]: *** [Makefile:588: mib.lo] Error 1
make[2]: Leaving directory ‘/cygdrive/e/Agent++POC/agent+±4.5.2/src’
make[1]: *** [Makefile:573: all-recursive] Error 1
make[1]: Leaving directory ‘/cygdrive/e/Agent++POC/agent+±4.5.2’
make: *** [Makefile:459: all] Error 2

I have checked everything seems to build correctly with the version 4.5.1 but I am facing this error with version 4.5.2. What could be the posssible reason for this?
Thanks in advance.

You can use v4.5.3 to fix it (wrong include paths).

Still getting the same errors.

I would assume that the error message is not pointing to the right direction.
Please check what error message is produced if you change the notification_originator.h and replace the call to the constructor with the implementation:

	NotificationOriginator(Mib* mibRef) { 
#ifdef _SNMPv3
        localEngineID = 0;
#endif
        targetAddrEntry = 0;
        targetParamsEntry = 0;
#ifdef _SNMPv3
        communityEntry = 0;
#endif
        notifyEntry = 0;
        notifyFilterEntry = 0;
#ifdef _SNMPv3
        _nlmLogEntry = 0;
        v3mp = 0;
#endif
            mib = mibRef; 
        }

After making the suggested change I am getting the below errors :-

request.cpp: In constructor Agentpp::RequestList::RequestList(Agentpp::Mib*)': request.cpp:726: error: type class Agentpp::RequestList’ is not a direct base of `Agentpp::RequestList’
make[2]: *** [Makefile:588: request.lo] Error 1
make[2]: Leaving directory ‘/cygdrive/e/Agent++POC/agent+±4.5.3/src’
make[1]: *** [Makefile:573: all-recursive] Error 1
make[1]: Leaving directory ‘/cygdrive/e/Agent++POC/agent+±4.5.3’
make: *** [Makefile:459: all] Error 2

Can you please try again with enabled C++11 support of the compiler, using something like -std=c++11 or -std=gnu++11?

Hello,
yes I guess the issue was related to the tool chain as it was not supporting the c++11 standard. After building the agent with newer toolchain I got it to compile and libraries were created.
After copying the generated code through AgenPro when I tried building the agent executable. I am getting an error : “error: ‘engineid’ was not declared in this scope” What could this issue be related to?

Thanks

Please show us the code parts that trigger the error. Does the error occur in the generated code? Is SNMPv3 enabled?

The error is triggered in agenpro/agent/agent.cpp, I have done no modifications in this file after extracting the zip file downloaded from Download snmp.software though I have added other files in agenpro/agent/src and /include directories which I generated using AgenPro tool.
agent.cpp: In function ‘int main(int, char)’:**
agent.cpp:287:13: error: ‘engineId’ was not declared in this scope
Makefile:444: recipe for target ‘agent.o’ failed
make[4]: *** [agent.o] Error 1

And I have not explicitly disabled the SNMPv3 during configuration. Below is the configuration used
./configure CXX=/home/administrator/toolchain/usr/bin/powerpc-linux-g++ CXXFLAGS="-std=c++11" CC=/home/administrator/toolchain/usr/bin/powerpc-linux-gcc --host=powerpc-linux --enable-static=NO --enable-shared --with-examples --with-agenpro

This is the part of code where error is triggered
init(*mib, engineId); Line 287 of agent.cpp file

Agent++ does not have a separate option to enable/disable SNMPv3, it just uses the setting from snmp++. In the current release some of the agent.cpp files are missing the engineId declaration if SNMPv3 is disabled (will be fixed in the next release). See the needed patch below.

Kind regards,
Jochen

@@ -272,6 +272,8 @@ int main (int argc, char* argv[])
 	int stat;
         v3MP *v3mp = new v3MP(engineId, snmpEngineBoots, stat);
         snmp.set_mpv3(v3mp);
+#else
+        OctetStr engineId; // not used without SNMPv3
 #endif
 	reqList = new RequestList(mib);
 #ifdef _SNMPv3