Dennis Keipp
Dennis Keipp

Reputation: 1

sending snmp subtree via mqtt and node-red

I'm playing with nodered.. so far i can collect some data with modbus and send this via mqtt to a broker. But I also want to do this for some snmp subtrees from my switches for monitoring reasons. I can't get this to work. I can collect the data from the switch with the "snmp-subtree" node. In the debug view I can see the payload of that what I want. Now I need have some kind of function that takes each object from the array and translate it into "my" mqtt topic and payload.

msg : Object
    object
    _msgid: "xyz"
    topic: ""
    payload: array[33]
        [0 … 9]
            0: object
                oid: "1.3.6.1.2.1.2.2.1.10.1"
                value: 1231397597
            1: object
                oid: "1.3.6.1.2.1.2.2.1.10.2"
                value: 0

each object is a interface. I need for each interface a message with the topic "room/switch/interfaceIn/#" (# shold be the last number of the OID) and the value as payload. I really have no idea how I can get this to work. I want to have a for loop (for every object do some magic and shoot a message). Does somebody have a example that I can use?

New comment:

I tried coding with examples I found with Google. For now I have some working code in my function. I will run this for a few days to test.

function shootMsg(element, index, array) {
    node.send ({payload: element.value, topic:'test/mult/1/' + index});
}

msg.payload.forEach(shootMsg);

Feel free to leave a comment if I can improve this in any way... I'm new to Java.. I don't know what I'm doing :-)

I have a new question to reach my goal here. I think it is better to start a new topic? (I need to know if it is possible to read the IP adress assigned in the node before, it is not in the message body.)

regards Dennis

Upvotes: 0

Views: 777

Answers (1)

SteveR
SteveR

Reputation: 1131

I see that you already have a workable javascript solution, using a function node to iterate over the array of objects and send each one as a separate msg. There are at least 2 other good ways to accomplish this same task:

  1. Use a split node to break up the payload array into 33 separate msg objects -- then wire that into a change node which moves the oid to msg.topic, and value to msg.payload

  2. Pass the entire array to a change node, configured to use this Jsonata expression to build a new array of the msg objects you need:

    payload.{
      "topic": $reverse($split(oid, "."))[0],
      "payload": value
    }
    

I tend to use this second technique for most situations where I need to reformat one JSON structure into another.

Note: to use a Jsonata expression in a change node, you must select the J: option on the "type" pulldown, not the {} option for entering a JSON string.

Upvotes: 0

Related Questions