0

When PySNMP's nextCmd() iterates over the table I am working with, it outputs by column, rather than by row. This has proven difficult when trying to save the data into an Object. For example, I will see every value from the alarmID column before moving on to the next column.

So far, I've only created a print statement to output and see how the values are structured. I've been trying to think of a way to solve this, and my mind tends to take me to recursion, but it's something I haven't tried implementing yet.

Here is the function I am working with:

def query_alarm_table(device):
    manufacturer = Manufacturer(device.manufacturer)

    for (errorIndication, errorStatus, errorIndex, varBinds) in nextCmd(
        SnmpEngine(),
        CommunityData("public"),
        UdpTransportTarget((device.ip_address, device.snmp_port_number)),
        ContextData(),
        ObjectType(ObjectIdentity(manufacturer.alarm_table)),
        lexicographicMode=False,
    ):
        if errorIndication:
            print(errorIndication)
            break
        elif errorStatus:
            print("%s at %s" % (errorStatus.prettyPrint(), errorIndex and varBinds[int(errorIndex) - 1][0] or "?",))
            break
        else:
            for oid, raw_value in varBinds:
                print(f'oid: {oid} --- raw_value: {raw_value}')

This above function is properly iterating over the table. But I am unsure of how to work with the data as if it were a single row and put it into an Object within my database.

Bonteq
  • 777
  • 7
  • 24

1 Answers1

1

Perhaps the simplest solution would be to pass the columns you are interested in into the nextCmd all at once:

nextCmd(
    SnmpEngine(),
    CommunityData("public"),
    UdpTransportTarget((device.ip_address, device.snmp_port_number)),
    ContextData(),
    ObjectType(ObjectIdentity(manufacturer.alarm_table_column_1)),
    ObjectType(ObjectIdentity(manufacturer.alarm_table_column_2)),
    ObjectType(ObjectIdentity(manufacturer.alarm_table_column_3)),
    ...
)

On each iteration you should get one value per column in varBinds i.e. the row.

Ilya Etingof
  • 5,440
  • 1
  • 17
  • 21
  • Thank you for the reply @IlyaEtingof. The challenge here is I'm trying to make the function work for multiple different manufacturers which tend to have varying numbers of columns. – Bonteq Jan 15 '19 at 17:40