B4R Question SNMP agent

Discussion in 'B4R Questions' started by takhmin77, Aug 27, 2018.

  1. takhmin77

    takhmin77 Member

    hi
    i want read temperature sensor data and send in SNMP format to SNMP servers like ZABBIX or SOLAR WINDS .

    i know that SNMP server GET data from IP and OID and device send parameter to server.
    Who helps me ?
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    I thought that SNMP was only used to manage networks.

    Do you have an example of the message format needed?
     
    Last edited: Aug 27, 2018
  3. takhmin77

    takhmin77 Member

  4. JordiCP

    JordiCP Well-Known Member Licensed User

    It's true that it was 'mainly' used to manage network-infrastructure devices (hubs, switches, gateways, DSL modems), but suitable and also used for all kind of network-connected devices. Previous to IOT-related explosion, it was (and still is, in many areas) one of the high-level standards for automated managing of network-deployed devices.
    Ages ago I programmed v2-compliant agents and a bunch of devices that served/responded to their parameters by protocol-bridging through the serial port (low-end micros to perform the basic task and a network-connected embedded PC to manage them all locally and respond to SNMP manager). Thousands of end-devices and hundredths of agents were deployed. Many of them are still active, being managed by the customer's SNMP management system.

    For only some objects, and if only GETs needed (no TRAPS nor SET nor WALK), I like very much the second link.

    I can try to solve doubts about SNMP itself if needed (not about its B4R implementation, since I haven't been into it yet)
     
    Erel likes this.
  5. Erel

    Erel Administrator Staff Member Licensed User

    JordiCP likes this.
  6. takhmin77

    takhmin77 Member

  7. takhmin77

    takhmin77 Member

    hi
    i try this sample and it work fine for me :
    http://41j.com/blog/2014/12/arduino-snmp-voltage-monitor/

    "Agentuino" library contain :

    SNMP Get
    SNMP Set
    SNMP Trap


    can i use the "Agentuino" library in b4a ?

    my arduino code is :
    Code:
    #include <Streaming.h>         // Include the Streaming library
    #include <Ethernet.h>          // Include the Ethernet library
    #include <MemoryFree.h>
    #include <Agentuino.h>
    #include <Flash.h>
    //
    #define DEBUG
    //
    static byte 
    mac[] = { 0xDE0xAD0xBE0xEF0xFE0xED };
    static byte ip[] = { 192, 168,0, 184 };
    static byte gateway[] = { 192, 168, 0, 2 };
    static byte subnet[] = { 255, 255, 255, 0 };
    static byte ServerIP[4] = {192, 168, 0, 64}; // The IP address of the server that will receive the trap

    const static char sysUpTime[] PROGMEM     = "1.3.6.1.2.1.1.3.0";  // read-only  (TimeTicks)
    const static char sysName[] PROGMEM       = "1.3.6.1.2.1.1.5.0";  // read-write (DisplayString)

    static uint32_t locUpTime           = 0;     // read-only (static)
    static char locName[20]             = "Amir Reza Amiri"; //read write
     
    uint32_t prevMillis = millis();
    char oid[SNMP_MAX_OID_LEN];
    SNMP_API_STAT_CODES api_status;
    SNMP_ERR_CODES status;
     
    void pduReceived()
    {
      SNMP_PDU pdu;
        Serial << F("UDP Packet Received Start..") << F(" RAM:") << freeMemory() << endl;
      api_status = Agentuino.requestPdu(&pdu);
      if ( pdu.type == SNMP_PDU_GET || pdu.type == SNMP_PDU_GET_NEXT || pdu.type == SNMP_PDU_SET
        && pdu.error == SNMP_ERR_NO_ERROR && api_status == SNMP_API_STAT_SUCCESS ) {
        pdu.OID.toString(oid);
     if ( strcmp_P(oid, sysUpTime ) == 0 ) {
          if ( pdu.type == SNMP_PDU_SET ) {
            pdu.type = SNMP_PDU_RESPONSE;
            pdu.error = SNMP_ERR_READ_ONLY;
          } else {
            status = pdu.VALUE.encode(SNMP_SYNTAX_TIME_TICKS, locUpTime);
            pdu.type = SNMP_PDU_RESPONSE;
            pdu.error = status;
          }
            Serial << F("sysUpTime...") << locUpTime << F(" ") << pdu.VALUE.size << endl;
        } else if ( strcmp_P(oid, sysName ) == 0 ) {
          if ( pdu.type == SNMP_PDU_SET ) {
            status = pdu.VALUE.decode(locName, strlen(locName));
            pdu.type = SNMP_PDU_RESPONSE;
            pdu.error = status;
          } else {
            status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locName);
            pdu.type = SNMP_PDU_RESPONSE;
            pdu.error = status;
          }
            Serial << F("sysName...") << locName << F(" ") << pdu.VALUE.size << endl;
        }
     
        else {
          pdu.type = SNMP_PDU_RESPONSE;
          pdu.error = SNMP_ERR_NO_SUCH_NAME;
        }
        Agentuino.responsePdu(&pdu);
     
      }
      Agentuino.freePdu(&pdu);
     Serial << "UDP Packet Received End.." << " RAM:" << freeMemory() << endl;
    }
     
    void setup()
    {
      Serial.begin(9600);
      Ethernet.begin(mac, ip);
      api_status = Agentuino.begin();
      if ( api_status == SNMP_API_STAT_SUCCESS ) {
        Agentuino.onPduReceive(pduReceived);
        delay(10);
        Serial << F("SNMP Agent Initalized...") << endl;
        return;
      }
      delay(10);
      Serial << F("SNMP Agent Initalization Problem...") << status << endl;
    }
     
    void loop()
    {
      Agentuino.listen();
      if ( millis() - prevMillis > 1000 ) {
        prevMillis += 1000;
        locUpTime += 100;
      }
    }

    and for trap :

    Code:
    /**
     * Agentuino SNMP Agent Library Prototyping...
     *
     * Copyright 
    2010 Eric C. Gionet <lavco_eg@hotmail.com>
     *
     * @Edit: M. Pivovarsky <miroslav.pivovarsky@gmail.com>
     *
     */

    #include "Streaming.h"         // Include the Streaming library
    #include <Ethernet.h>          // Include the Ethernet library
    #include <SPI.h>
    #include "Agentuino.h"



    IPAddress 
    address;

    static byte 
    mac[] = {0xDE0xAD0xBE0xEF0xFE0xEF};
    static byte ip[] = {192, 168, 0, 184};
    static byte gateway[] = {192, 168, 0, 2};
    static byte subnet[] = {255, 255, 255, 0};

    static byte RemoteIP[4] = {192, 168, 0, 184}; // The IP address of the host that will receive the trap

    void setup() {
        Serial.begin(9600);
        Serial.println("CPU Start");

        pinMode(6, INPUT);

        Ethernet.begin(mac,ip);
        //Ethernet.begin(mac,ip,gateway,subnet);

        IPAddress address = Ethernet.localIP();
        for (uint8_t i=0;i<=4;i++) {
            my_IP_address[i] = address[i];
            Serial.print(my_IP_address[i]);
            Serial.print(".");
        }
        Serial.println("");
     
        api_status = Agentuino.begin();
     
        if (api_status == SNMP_API_STAT_SUCCESS) {

            Agentuino.onPduReceive(pduReceived);

            delay(10);

            Serial << F("SNMP Agent Initalized...") << endl;

            return;
        }

        delay(10);

        Serial << F("SNMP Agent Initalization Problem...") << status << endl;
    }

    void loop() {
        // listen/handle for incoming SNMP requests
        Agentuino.listen();

        // Is pin 6 HIGH, send trap
        if (digitalRead(6) == 0) {
            Serial.println("Send TRAP");
       
         
            // send trap
            Agentuino.Trap("Arduino SNMP trap", RemoteIP, locUpTime);
            //Agentuino.Trap("Arduino SNMP trap", RemoteIP, locUpTime, "1.3.6.1.4.1.28032.1.1.1");
            //Agentuino.Trap("Arduino SNMP trap", RemoteIP, locUpTime, "1.3.6.1.4.1.36061.0", "1.3.6.1.4.1.36061.3.1.1.1");
            delay(1000);
            locUpTime = locUpTime + 100;
        }

        // sysUpTime - The time (in hundredths of a second) since
        // the network management portion of the system was last
        // re-initialized.
        if (millis() - prevMillis > 1000) {

            // increment previous milliseconds
            prevMillis += 1000;

            // increment up-time counter
            locUpTime += 100;
        }
    }
    source code of library is in this link :
    https://github.com/carlosdelfino/agentuino

    thanks
     
    Last edited: Sep 4, 2018
  8. Erel

    Erel Administrator Staff Member Licensed User

    You will need to wrap this library. It requires some C++ knowledge.
     
  9. takhmin77

    takhmin77 Member

    can i use inline c code ?
    or
    anybody can helps me to wrap this library ?
     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Everything is possible with inline c.

    Start with calling this setup code.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice