Developing SNMP Agent

Hello,
We are developing a custom board based in YOCTO. We add several services that run perfect. We also added SNMP as you can see from the local.conf file.


# DO NOT SET THE MACHINE AFTER THE ABOVE INCLUDE
CORE_IMAGE_EXTRA_INSTALL += "plymouth"


PACKAGECONFIG:append_pn-php = " mysql sqlite3 imap opcache openssl ${@bb.utils.filter('DISTRO_FEATURES', 'ipv6 pam', d)} apache2"

IMAGE_INSTALL:append = " apache2" 
#IMAGE_INSTALL:append = " phpmyadmin"
IMAGE_INSTALL:append = " php php-cli" 
IMAGE_INSTALL:append = " php-modphp" 
#IMAGE_INSTALL:append = " proftpd"
#IMAGE_INSTALL:append = " mysql5"	

IMAGE_INSTALL:append = " net-snmp-server net-snmp-server-snmpd net-snmp-libs net-snmp-mibs" 
IMAGE_INSTALL:append = " libgpiod libgpiod-tools" 
IMAGE_INSTALL:append = " gdbserver" 

# Remove connman
IMAGE_INSTALL:remove = " connman"
IMAGE_INSTALL:remove = " connman-client"
IMAGE_INSTALL:remove = " connman-gnome"
IMAGE_INSTALL:remove = " connman-plugin-wifi"
IMAGE_INSTALL:remove = " connman-plugin-ethernet"
IMAGE_INSTALL:remove = " connman-plugin-loopback"


SDKIMAGE_FEATURES  = "dev-pkgs dbg-pkgs staticdev-pkgs"



ACCEPT_FSL_EULA = "1"

The daemon for SNMP is working properly, but we are in trouble to develop the agent from our application. We have generated the sdk with all the headers files.We source previous to run Eclipse the script provided by sdk “environment-setup-armv7at2hf-neon-tdx-linux-gnueabi” to configure enviromental settings, with this we can include those libraries.

We included in the libraries section (-l) of the linker configuration these libraries to link:
netsnmp
netsnmpmibs
netsnmpagent
netsnmphelpers

The issue is that when we call snmp_sess_init the program blocks and we have not any message about what is going on. Is there any module missed to include in the local.conf?. Is there any other module needed to add to local.conf?

Thank you for the ideas,

/*
 * start be including the appropriate header files
 */
#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>

/* change the word "define" to "undef" to try the (insecure) SNMPv1 version */
#undef DEMO_USE_SNMP_VERSION_3

#ifdef DEMO_USE_SNMP_VERSION_3
#include "net-snmp/transform_oids.h"
const char *our_v3_passphrase = "The Net-SNMP Demo Password";
#endif

bool test_snmp()
{
          netsnmp_session session, *ss;
            netsnmp_pdu *pdu;
            netsnmp_pdu *response;

            oid anOID[MAX_OID_LEN];
            size_t anOID_len;

            netsnmp_variable_list *vars;
            int status;
            int count=1;

            /*
             * Initialize the SNMP library
             */
            init_snmp("snmpapp");

            /*
             * Initialize a "session" that defines who we're going to talk to
             */
            snmp_sess_init( &session );                   /* set up defaults */
            session.peername = strdup("test.net-snmp.org");

            /* set up the authentication parameters for talking to the server */

        #ifdef DEMO_USE_SNMP_VERSION_3

            /* Use SNMPv3 to talk to the experimental server */

            /* set the SNMP version number */
            session.version=SNMP_VERSION_3;

            /* set the SNMPv3 user name */
            session.securityName = strdup("MD5User");
            session.securityNameLen = strlen(session.securityName);

            /* set the security level to authenticated, but not encrypted */
            session.securityLevel = SNMP_SEC_LEVEL_AUTHNOPRIV;

            /* set the authentication method to MD5 */
            session.securityAuthProto = usmHMACMD5AuthProtocol;
            session.securityAuthProtoLen = sizeof(usmHMACMD5AuthProtocol)/sizeof(oid);
            session.securityAuthKeyLen = USM_AUTH_KU_LEN;

            /* set the authentication key to a MD5 hashed version of our
               passphrase "The Net-SNMP Demo Password" (which must be at least 8
               characters long) */
            if (generate_Ku(session.securityAuthProto,
                            session.securityAuthProtoLen,
                            (u_char *) our_v3_passphrase, strlen(our_v3_passphrase),
                            session.securityAuthKey,
                            &session.securityAuthKeyLen) != SNMPERR_SUCCESS) {
                snmp_perror(argv[0]);
                snmp_log(LOG_ERR,
                         "Error generating Ku from authentication pass phrase. \n");
                exit(1);
            }

        #else /* we'll use the insecure (but simplier) SNMPv1 */

            /* set the SNMP version number */
            session.version = SNMP_VERSION_1;

            /* set the SNMPv1 community name used for authentication */
            session.community = "demopublic";
            session.community_len = strlen(session.community);

        #endif /* SNMPv1 */

            /*
             * Open the session
             */
            SOCK_STARTUP;
            ss = snmp_open(&session);                     /* establish the session */

            if (!ss) {
              snmp_sess_perror("ack", &session);
              SOCK_CLEANUP;
              exit(1);
            }

            /*
             * Create the PDU for the data for our request.
             *   1) We're going to GET the system.sysDescr.0 node.
             */
            pdu = snmp_pdu_create(SNMP_MSG_GET);
            anOID_len = MAX_OID_LEN;
            if (!snmp_parse_oid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len)) {
              snmp_perror(".1.3.6.1.2.1.1.1.0");
              SOCK_CLEANUP;
              exit(1);
            }
        #if OTHER_METHODS
            /*
             *  These are alternatives to the 'snmp_parse_oid' call above,
             *    e.g. specifying the OID by name rather than numerically.
             */
            read_objid(".1.3.6.1.2.1.1.1.0", anOID, &anOID_len);
            get_node("sysDescr.0", anOID, &anOID_len);
            read_objid("system.sysDescr.0", anOID, &anOID_len);
        #endif

            snmp_add_null_var(pdu, anOID, anOID_len);

            /*
             * Send the Request out.
             */
            status = snmp_synch_response(ss, pdu, &response);

            /*
             * Process the response.
             */
            if (status == STAT_SUCCESS && response->errstat == SNMP_ERR_NOERROR) {
              /*
               * SUCCESS: Print the result variables
               */

              for(vars = response->variables; vars; vars = vars->next_variable)
                print_variable(vars->name, vars->name_length, vars);

              /* manipuate the information ourselves */
              for(vars = response->variables; vars; vars = vars->next_variable) {
                if (vars->type == ASN_OCTET_STR) {
                  char *sp = (char *)malloc(1 + vars->val_len);
                  memcpy(sp, vars->val.string, vars->val_len);
                  sp[vars->val_len] = '\0';
                  printf("value #%d is a string: %s\n", count++, sp);
                  free(sp);
                }
                else
                  printf("value #%d is NOT a string! Ack!\n", count++);
              }
            } else {
              /*
               * FAILURE: print what went wrong!
               */

              if (status == STAT_SUCCESS)
                fprintf(stderr, "Error in packet\nReason: %s\n",
                        snmp_errstring(response->errstat));
              else if (status == STAT_TIMEOUT)
                fprintf(stderr, "Timeout: No response from %s.\n",
                        session.peername);
              else
                snmp_sess_perror("snmpdemoapp", ss);

            }

            /*
             * Clean up:
             *  1) free the response.
             *  2) close the session.
             */
            if (response)
              snmp_free_pdu(response);
            snmp_close(ss);

            SOCK_CLEANUP;
            return (0);
}

Our hardware is:
Apalis iMX6D 1GB IT
Custom board base in Apalis Eval Board v1.1
Linux apalis-imx6-10806407 6.1.22-6.2.0 (Reference image Apalis-iMX6 tdx-reference-minimal-image)

Hi, I’m the Toradex AI Assistant. I’m in early beta, and I would appreciate your feedback.
The Toradex Support team will follow up with you shortly!

Did you like my answer?

:+1: :-1:


Handshake,

Developing an SNMP agent on a Yocto-based system for the Apalis iMX6D involves integrating various components, including the net-snmp library and its associated modules. From the details you’ve provided, it appears you’ve correctly added the necessary packages (net-snmp-server, net-snmp-server-snmpd, net-snmp-libs, net-snmp-mibs) to your image. If snmp_sess_init is causing your application to block without any output, there are a few areas to investigate:

  1. Library Dependencies: Ensure all runtime dependencies of the net-snmp libraries are satisfied. Although you’ve included the necessary libraries at compile time, there might be runtime dependencies not explicitly mentioned or included in your image. Use tools like ldd on your target device to check if all shared libraries are correctly linked.

  2. Debugging the SNMP Configuration: The net-snmp library offers extensive debugging capabilities that can be enabled to provide more insight into what might be causing the blockage. You can enable debugging in your application with snmp_set_do_debugging(1); or by setting the SNMP_DEBUG environment variable before running your application. This might provide more clues as to where the blockage is occurring.

  3. SELinux/AppArmor: If your system uses SELinux or AppArmor, ensure that there are no policies preventing your application from executing as expected. These security modules can sometimes block network communication or file access, leading to unexpected behavior.

  4. Network Configuration: Since SNMP relies on network communication, ensure that your network configuration allows for SNMP traffic. Firewalls or network policies might be blocking SNMP packets, causing the session initialization to hang.

  5. SNMP Configuration Files: Check the SNMP configuration files (e.g., snmpd.conf) for any misconfigurations that might affect the operation of your SNMP agent. Incorrect settings can lead to issues during session initialization.

  6. Thread Safety: If your application is multi-threaded, ensure that your usage of the net-snmp library is thread-safe. The net-snmp library has certain thread safety considerations that need to be addressed, especially when initializing sessions.

  7. Hardware and Firmware Compatibility: Although unlikely to be the direct cause of this issue, ensure your Apalis iMX6D firmware is up to date. Firmware issues can sometimes manifest as unrelated software problems.

If after investigating these areas the problem persists, consider isolating the SNMP initialization in a minimal, standalone

Please, also check if these links can help you solve your problem:

Running FreeRTOS on the Cortex-M4 of a Colibri iMX8X | Toradex Developer Center.

Hello @aarribas,

Thanks for providing the detailed information regarding your problem. Could you please post the output of systemctl status snmpd.service

Hello @rudhi.tx:
Thanks for the support.
This is the screenshot of this command.

Best Regards

Hi, Andres.

I apologize for the delay.

We are not very familiar with SNMP as it is very specific to the application itself, but we have partners who are capable of helping you with it.
Here, you can find our partner network. If you have some difficulty finding a partner for your issue, we can help you connect with them.

Best regards.
Lucas Azeituno.