Creating Custom MiB (With custom OID) to receive data From Python Script

181 Views Asked by At

I'm quite new on this type of field and would like to ask about suggestion and if there's any resources that might be fitting to dabble to finish this task (I've done research for a week and I'm currently stuck on this part)

My task is to scrape data and process it, and once it's ready is to save it to a database, seems easy. but the other task I'm having trouble with is to send the data I had to SNMP as well, since there will be another application waiting for that data via SNMP

I searched the internet, books and tutorials out there, and found out that I have to send it to an OID in SNMP service, I managed to send the data and view the data transmitted to an OID in SNMP

BUT, I only managed to send it to an already existing OID, which is the OID for SysContact (.1.3.6.1.2.1.1.4.0), so I already have the bare knowledge to send out and transmit data.

My problem and what made me stuck for 1 week is how to create my OWN Folder and Own variables inside the SNMP. (I also managed to understand that I need to create my own MiB to have my own custom MiB but the resources I've managed to attain were too far complicated and I couldn't understand a thing or two on how to create one, Also some of them were out of date, might be because I'm quite new on this system configuration)

Already managed to create SNMP server in Linux (But it was in .sh script and I only understand the part were pass protocol was used) and Windows

I'm more of a python programmer

1

There are 1 best solutions below

0
Lex Li On

If you just want some simple example, then now PySNMP unit test cases cover that,

  • First you need to register the custom objects on agent side, like this
        # --- create custom Managed Object Instance ---
        mibBuilder = snmpContext.getMibInstrum().getMibBuilder()

        MibScalar, MibScalarInstance = mibBuilder.importSymbols(
            "SNMPv2-SMI", "MibScalar", "MibScalarInstance"
        )

        class MyStaticMibScalarInstance(MibScalarInstance):
            # noinspection PyUnusedLocal,PyUnusedLocal
            def getValue(self, name, idx):
                time.sleep(2)  # Add a 2-second sleep
                return self.getSyntax().clone(f"Test agent")

            def setValue(self, name, idx, value):
                # Here you can handle the SET operation.
                # `value` is the new value for the scalar instance.
                # You should store this value somewhere, and return it in the `getValue` method.
                # For this example, let's just print it.
                print(f"SET operation received. New value: {value}")
                return self.getSyntax().clone(value)

        mibBuilder.exportSymbols(
            "__MY_MIB",
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 1), v2c.OctetString()),
            MyStaticMibScalarInstance(
                (1, 3, 6, 1, 4, 1, 60069, 9, 1), (0,), v2c.OctetString()
            ),
        )

        # --- end of Managed Object Instance initialization ----
  • Then you can consume the objects on manager side, like this
        snmpEngine = SnmpEngine()

        async def run_get():
            errorIndication, errorStatus, errorIndex, varBinds = await getCmd(
                snmpEngine,
                CommunityData("public", mpModel=0),
                UdpTransportTarget(("localhost", AGENT_PORT), timeout=1, retries=0),
                ContextData(),
                ObjectType(ObjectIdentity("1.3.6.1.4.1.60069.9.1.0")),
            )
            for varBind in varBinds:
                print([str(varBind[0]), varBind[1]])

        asyncio.run(run_get())
        snmpEngine.transportDispatcher.closeDispatcher()

A real world example will be more complex, and require a MIB document to be authored and compiled.