snmp implementation in Qt in Ubuntu

1.1k views Asked by At

Iam using Ubuntu 16.04 in which i have installed Qtcreator. I would like to work on SNMP. for this i have already installed net-snmp as well as mibs downloader.

I have taken example code from net-snmp (snmpdemoapp.c) and tried implementing same in qt.

#include "mainwindow.h"
#include <QApplication>

#include <string.h>
#include <cstring>

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>

#include <net-snmp/agent/net-snmp-agent-includes.h>


//using namespace std;

#define DEMO_USE_SNMP_VERSION_3

#ifdef DEMO_USE_SNMP_VERSION_3
const char *our_v3_passphase="The Net SNMP Pass Phrase";
#endif

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    MainWindow w;

    //*******************SNMP Code starts from here

    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("snmpdemoapp");

    //SnmpGet

    //initialize the session that defines who we are going to talk to

    snmp_sess_init(&session);
    session.peername="test.net-snmp.org";

    //set up the authentication parameters for talking to server

#ifdef DEMO_USE_SNMP_VERSION_3

    //if snmp version3

    session.version=SNMP_VERSION_3;

    //set the SNMP v3 user name

    session.securityName=strdup("MD5User");
    session.securityNameLen=strlen(session.securityName);

    //snmp security
    session.securityLevel=SNMP_SEC_LEVEL_AUTHNOPRIV;
    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 UCD Demo Password" (which must be at least 8
           characters long) */

    if(generate_Ku(session.securityAuthProto,session.securityAuthProtoLen,(uchar *)our_v3_passphase,
                   strlen(our_v3_passphase),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
    //if snmp version is snmp v1

    session.version=SNMP_VERSION_1;

    /* set the SNMPv1 community name used for authentication */

    session.community="demopublic";
    session.community_len=strlen(session.community);


#endif

        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;


    //*************************SNMP code ends here
    w.show();

    return a.exec();
}

when iam building the program, iam getting errors s below:

main.o: In function `main':
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:42: undefined reference to `init_snmp'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:48: undefined reference to `snmp_sess_init'
Makefile:216: recipe for target 'SNMP_Demo_Example' failed
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:66: undefined reference to `usmHMACMD5AuthProtocol'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:74: undefined reference to `generate_Ku'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:77: undefined reference to `snmp_perror'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:78: undefined reference to `snmp_log'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:97: undefined reference to `snmp_open'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:100: undefined reference to `snmp_sess_perror'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:109: undefined reference to `snmp_pdu_create'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:111: undefined reference to `snmp_parse_oid'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:112: undefined reference to `snmp_perror'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:127: undefined reference to `snmp_add_null_var'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:132: undefined reference to `snmp_synch_response'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:143: undefined reference to `print_variable'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:163: undefined reference to `snmp_errstring'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:169: undefined reference to `snmp_sess_perror'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:179: undefined reference to `snmp_free_pdu'
/home/srinivas/QT_Projects/build-SNMP_Demo_Example-Desktop_Qt_5_7_0_GCC_64bit-Debug/../SNMP_Demo_Example/main.cpp:180: undefined reference to `snmp_close'
collect2: error: ld returned 1 exit status
make: *** [SNMP_Demo_Example] Error 1
13:04:19: The process "/usr/bin/make" exited with code 2.
Error while building/deploying project SNMP_Demo_Example (kit: Desktop Qt 5.7.0 GCC 64bit)
When executing step "Make"

what could be the error????

this is my .pro file

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = SNMP_Demo_Example
TEMPLATE = app


SOURCES += main.cpp\
        mainwindow.cpp

HEADERS  += mainwindow.h

FORMS    += mainwindow.ui

INCLUDEPATH += /usr/local/include/net-snmp
INCLUDEPATH += /usr/local/include/net-snmp/agent
INCLUDEPATH += /usr/local/include/net-snmp/library
INCLUDEPATH += /usr/local/include/net-snmp/machine
INCLUDEPATH += /usr/local/include/net-snmp/system










unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lnetsnmp

INCLUDEPATH += $$PWD/../../../../usr/local/include
DEPENDPATH += $$PWD/../../../../usr/local/include


unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lnetsnmpagent

INCLUDEPATH += $$PWD/../../../../usr/local/include
DEPENDPATH += $$PWD/../../../../usr/local/include


unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lnetsnmphelpers

INCLUDEPATH += $$PWD/../../../../usr/local/include
DEPENDPATH += $$PWD/../../../../usr/local/include


unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lnetsnmpmibs

INCLUDEPATH += $$PWD/../../../../usr/local/include
DEPENDPATH += $$PWD/../../../../usr/local/include


unix:!macx: LIBS += -L$$PWD/../../../../usr/local/lib/ -lnetsnmptrapd

INCLUDEPATH += $$PWD/../../../../usr/local/include
DEPENDPATH += $$PWD/../../../../usr/local/include
0

There are 0 answers