Home > C/C++, Networking > SNMP C++ client implementation

SNMP C++ client implementation

March 22nd, 2013 Leave a comment Go to comments

Working with SNMP under QT

Four years ago I worked on a multi-platform desktop application, which main purpose was to observe and control various devices on the local network using SNMP. The main questions that had to be solved were :

  1. How to write an application that can run on multiple platforms (like Windows, Linux, Mac OS)
  2. How to communicate with the SNMP devices

The client didn't want a web app, so I had to think of other variants. And because I'm a big C++ fan, I decided to utilize a C++ based solution.

 

What is QT ?

For those of you you have never heard of it, QT is an intuitive, easy to use cross-platform library written entirely in C++, developed by Trolltech but later bought by Nokia with the idea of using QT in platforms like Maemo (which later morphed into MeeGo). It's available on Window, Linux, Mac OS, along with some mobile platforms. You can use it for game development and even create browser plugins with it. But that was few years ago, for a complete list of the currently supported platforms check this link. 

 

What is SNMP ? 

The Simple Network Management Protocol (SNMP) is used for observation and management of devices over an IP network. It's basically a tool to monitor availability and performance, among other things. 

 

SNMP libraries

At the time I was working on the project, there weren't so many SNMP libraries available for C++ (and I think this is still the case). So I was left with more or less two options :

  • SNMP++ (An object-oriented C++ library, which sounded like a good option)
  • NetSNMP (Library written entirely in C, which I really didn't want to wrap at that time)

Unfortunately, it turned out that SNMP++ was not designed to work under MinGW (which is the GCC port that QT uses on Windows).

So I decided to create my own class to operate the SNMP packets. It uses the QT network module, creating a UDP packet and manually including all the bytes necessary from the Application layer according to the correct SNMP format. 

 

SNMPv1 QT/C++ implementation

I know there are not so many choices when it comes to SNMP management in C++, so I decided to share this code.

The following is a simple class used to communicate using SNMPv1 devices :

/**
 * @version 1.0
 *
 * @section DESCRIPTION
 *
 * The class represents a SNMP session, which provides get and set
 * operations on SNMP agents.
 *
 */
 
 
#ifndef SNMPSESSION_H
#define SNMPSESSION_H
 
#include <QObject>
#include <QHostAddress>
#include <QByteArray>
#include <QString>
#include <QUdpSocket>
 
class SNMPSession : public QObject {
 
    Q_OBJECT
 
public:
    SNMPSession();
    SNMPSession(const QString &agentAddress, qint16 agentPort, qint16 socketPort);
    ~SNMPSession();
 
// get/set methods
    QHostAddress *getAgentAddress() const;
    qint16 getAgentPort() const;
    qint16 getSocketPort() const;
    QString getAgentMACAddress() const;
    void setAgentAddress(const QString &agentAddress);
    void setAgentPort(qint16 agentPort);
    void setSocketPort(qint16 socketPort);
 
// SNMP message methods
    int sendSetRequest(const QString &communityStringParameter, 
                       const QString oidParameter, int value);
    int sendSetRequest(const QString &communityStringParameter,
                       const QString oidParameter, const QString &valueParameter);
    int sendGetRequest(QString &receivedValue,
                       const QString &communityStringParameter, const QString &oidParameter);
 
// additional public methods
 
private:
    int getValueFromGetResponse(QString &receivedValue, QByteArray &receivedDatagram,
                                    const int &errorIndex, const int &valueTypeIndex,
                                    const int &valueIndex, const int &valueLenghtIndex);
    QByteArray convertIntAccordingToBER(int valueToConvert);
    void convertOIDAccordingToBER(QByteArray &oid);
    void convertOIDAccordingToBER(QByteArray &oid);
 
    QUdpSocket udpSocket;
    QHostAddress *agentAddress;
    qint16 agentPort;
    qint16 socketPort;
};
 
#endif // SNMPSESSION_H


You can find the .cpp implementation here.

I should warn you that the code should not be used as an example of best coding practices, but it has a clear and understandable interface. And, honestly, sometimes you just need something that works, even if it's not the most clever implementation.

And since I need to set a license here (because if I don't, you'll have practically no rights over the code), I decided to share it under LGPL.

Hope this will help someone somewhere. ;)



Like the article ? Share it ! ;)


  1. November 13th, 2013 at 18:59 | #1

    Hi Kosta,
    nice job, why don’t you create a GitHub folder or a new project on http://qt-apps.org/ ?

    Thanks.

  2. November 14th, 2013 at 00:29 | #2

    Hey Danilo,

    Thanks ! I don’t know if it’s worth it only for that piece of code, but I can definitely do it in the future. ;)

    Cheers

  3. Michael Olberg
    September 4th, 2014 at 18:43 | #3

    This could actually turn out to be useful for me. I notice, however, that the .cpp file makes reference to a managerthread.h file. Is that available, too? Thanks!

  4. gilberto
    October 30th, 2015 at 00:50 | #4

    Hi Kosta, can you help me with the managerthread.h and managerthread.cpp, i am trying compiling your code but no have this code.

  5. Eddy
    November 2nd, 2016 at 17:01 | #5

    What is in managerthread.h and managerthread.cpp?

  6. Daz
    November 28th, 2017 at 12:08 | #6

    Try to replace #include “managerthread.h” by #include
    and every ManagerThread::sendSleep(1); by
    QThread::msleep(1);
    P.S. there is no toAskii() (use toLatin1() instead)

  7. Ryan Sharpe
    August 6th, 2018 at 17:50 | #7

    This worked for me as SNMP Get in the 2015 edition of Qt with the help of the above comment. I inserted the Ip address, OID, and Community String as Qstring. I also put in the device port (agentPort) and the host port (socketPort) as int.

    I had to tweak a few things on mine as well, but Idk if that was just me. A “.3” kept being added to my OID, so I deleted the first period and it started working. Because I deleted the period, the length changed, so I also had to change the value index math to 0, +1, +2 instead of +1, +2, +3.

    I hope this helps.

  8. November 23rd, 2018 at 10:10 | #8

    Do you have a similar code for snmp agent also where agent receives get set request and responds to the snmp manager. Your help is highly appreciated.

  1. No trackbacks yet.


Copyright © Developing the future 2013. Licensed under the CC> BY-NC-ND 3.0 Creative Commons license.       
Audi R8 wallpapers Sony Xperia Z4 Tablet WiFi