File: agent_context.py

package info (click to toggle)
python-pysnmp4 7.1.21-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 3,564 kB
  • sloc: python: 33,654; makefile: 166; javascript: 4
file content (299 lines) | stat: -rw-r--r-- 10,654 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
from pysnmp.carrier.asyncio.dgram import udp, udp6
from pysnmp.entity import config, engine
from pysnmp.entity.rfc3413 import cmdrsp, context
from pysnmp.hlapi.v3arch.asyncio import SnmpEngine
from pysnmp.proto.api import v2c


import asyncio
import time


# Set the port to 1611 instead of 161, because 161 is a
# privileged port and requires root access
AGENT_PORT = 1611


async def start_agent(
    enable_ipv6: bool = False,
    enable_custom_objects: bool = False,
    enable_table_creation: bool = False,
) -> SnmpEngine:
    # Create SNMP engine
    snmpEngine = engine.SnmpEngine()

    # Set up transport endpoint
    config.add_transport(
        snmpEngine,
        udp.DOMAIN_NAME,
        udp.UdpTransport().open_server_mode(("localhost", AGENT_PORT)),
    )

    if enable_ipv6:
        config.add_transport(
            snmpEngine,
            udp6.DOMAIN_NAME,
            udp6.Udp6Transport().open_server_mode(("localhost", AGENT_PORT)),
        )

    # Set up community data
    config.add_v1_system(snmpEngine, "public", "public")
    # Add SNMP v3 user
    config.add_v3_user(
        snmpEngine, "usr-none-none", config.USM_AUTH_NONE, config.USM_PRIV_NONE
    )

    config.add_v3_user(
        snmpEngine, "usr-sha-none", config.USM_AUTH_HMAC96_SHA, "authkey1"
    )

    config.add_v3_user(
        snmpEngine,
        "usr-sha-aes",
        config.USM_AUTH_HMAC96_SHA,
        "authkey1",
        config.USM_PRIV_CFB128_AES,
        "privkey1",
    )

    config.add_v3_user(
        snmpEngine,
        "usr-sha-aes256",
        config.USM_AUTH_HMAC96_SHA,
        "authkey1",
        config.USM_PRIV_CFB256_AES,
        "privkey1",
    )

    # Allow read MIB access for this user / securityModels at VACM
    config.add_vacm_user(snmpEngine, 1, "public", "noAuthNoPriv", (1, 3, 6), (1, 3, 6))
    config.add_vacm_user(snmpEngine, 2, "public", "noAuthNoPriv", (1, 3, 6), (1, 3, 6))
    config.add_vacm_user(snmpEngine, 3, "usr-none-none", "noAuthNoPriv", (1, 3, 6))
    config.add_vacm_user(snmpEngine, 3, "usr-sha-none", "authNoPriv", (1, 3, 6))
    config.add_vacm_user(snmpEngine, 3, "usr-sha-aes", "authPriv", (1, 3, 6))
    config.add_vacm_user(snmpEngine, 3, "usr-sha-aes256", "authPriv", (1, 3, 6))

    # Configure SNMP context
    snmpContext = context.SnmpContext(snmpEngine)

    if enable_custom_objects:
        # --- create custom Managed Object Instances ---
        mibBuilder = snmpContext.get_mib_instrum().get_mib_builder()

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

        (
            DisplayString,
            PhysAddress,
            MacAddress,
            DateAndTime,
            TextualConvention,
        ) = mibBuilder.importSymbols(
            "SNMPv2-TC",
            "DisplayString",
            "PhysAddress",
            "MacAddress",
            "DateAndTime",
            "TextualConvention",
        )

        class SlowMibScalarInstance(MibScalarInstance):
            def getValue(self, name, **context):
                time.sleep(2)  # Add a 2-second sleep
                print("sleep ended")
                return self.getSyntax().clone(f"Test agent")

            def setValue(self, value, name, **context):
                print(f"SET operation received. New value: {value}")
                return self.getSyntax().clone(value)

        # Define a fixed date and time in the DateAndTime format
        def get_fixed_date_and_time():
            # Fixed date: 2024-11-03, 19:52:40.0, UTC offset +0:0
            return [
                2024 // 256,
                2024 % 256,  # Year (2024)
                11,  # Month (November)
                3,  # Day
                19,  # Hour (7 PM UTC)
                52,  # Minute
                40,  # Second
                0,  # Deciseconds (0 for simplicity)
                ord("+"),  # UTC offset direction ('+')
                0,  # Hours offset from UTC
                0,  # Minutes offset from UTC
            ]

        # Initialize the PhysAddress object with a sample MAC address (e.g., 00:11:22:33:44:55)
        initial_phys_address = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]

        # Initialize the MacAddress object with a sample MAC address (e.g., 00:11:22:33:44:55)
        initial_mac_address = [0x00, 0x11, 0x22, 0x33, 0x44, 0x55]

        mibBuilder.export_symbols(
            "__MY_MIB",
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 1), v2c.OctetString()),
            SlowMibScalarInstance(
                (1, 3, 6, 1, 4, 1, 60069, 9, 1), (0,), v2c.OctetString()
            ),
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 3), v2c.OctetString()).setMaxAccess(
                "not-accessible"
            ),
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 4), v2c.OctetString()).setMaxAccess(
                "readonly"
            ),  # PySMI <1.3.0 generates such objects
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 5), DateAndTime()).setMaxAccess(
                "read-write"
            ),
            MibScalarInstance(
                (1, 3, 6, 1, 4, 1, 60069, 9, 5),
                (0,),
                DateAndTime(get_fixed_date_and_time()),
            ),
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 6), PhysAddress()).setMaxAccess(
                "read-write"
            ),
            MibScalarInstance(
                (1, 3, 6, 1, 4, 1, 60069, 9, 6), (0,), PhysAddress(initial_phys_address)
            ),
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 7), v2c.Integer32()).setMaxAccess(
                "read-write"
            ),
            MibScalarInstance((1, 3, 6, 1, 4, 1, 60069, 9, 7), (0,), v2c.Integer32(50)),
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 8), v2c.Unsigned32()).setMaxAccess(
                "read-write"
            ),
            MibScalarInstance((1, 3, 6, 1, 4, 1, 60069, 9, 8), (0,), v2c.Unsigned32(5)),
            MibScalar((1, 3, 6, 1, 4, 1, 60069, 9, 9), MacAddress()).setMaxAccess(
                "read-write"
            ),
            MibScalarInstance(
                (1, 3, 6, 1, 4, 1, 60069, 9, 9), (0,), MacAddress(initial_mac_address)
            ),
        )

        # --- end of Managed Object Instance initialization ----

    if enable_table_creation:
        # --- define custom SNMP Table within a newly defined EXAMPLE-MIB ---

        mibBuilder = snmpContext.get_mib_instrum().get_mib_builder()

        (
            MibTable,
            MibTableRow,
            MibTableColumn,
            MibScalarInstance,
        ) = mibBuilder.import_symbols(
            "SNMPv2-SMI",
            "MibTable",
            "MibTableRow",
            "MibTableColumn",
            "MibScalarInstance",
        )

        (RowStatus,) = mibBuilder.import_symbols("SNMPv2-TC", "RowStatus")

        mibBuilder.export_symbols(
            "__EXAMPLE-MIB",
            # table object
            exampleTable=MibTable((1, 3, 6, 6, 1)).setMaxAccess("read-create"),
            # table row object, also carries references to table indices
            exampleTableEntry=MibTableRow((1, 3, 6, 6, 1, 5))
            .setMaxAccess("read-create")
            .setIndexNames((0, "__EXAMPLE-MIB", "exampleTableColumn1")),
            # table column: string index
            exampleTableColumn1=MibTableColumn(
                (1, 3, 6, 6, 1, 5, 1), v2c.OctetString()
            ).setMaxAccess("read-create"),
            # table column: string value
            exampleTableColumn2=MibTableColumn(
                (1, 3, 6, 6, 1, 5, 2), v2c.OctetString()
            ).setMaxAccess("read-create"),
            # table column: integer value with default
            exampleTableColumn3=MibTableColumn(
                (1, 3, 6, 6, 1, 5, 3), v2c.Integer32(123)
            ).setMaxAccess("read-create"),
            # table column: row status
            exampleTableStatus=MibTableColumn(
                (1, 3, 6, 6, 1, 5, 4), RowStatus("notExists")
            ).setMaxAccess("read-create"),
        )

        # --- end of custom SNMP table definition, empty table now exists ---

        # --- populate custom SNMP table with one row ---

        (
            exampleTableEntry,
            exampleTableColumn2,
            exampleTableColumn3,
            exampleTableStatus,
        ) = mibBuilder.import_symbols(
            "__EXAMPLE-MIB",
            "exampleTableEntry",
            "exampleTableColumn2",
            "exampleTableColumn3",
            "exampleTableStatus",
        )
        rowInstanceId = exampleTableEntry.getInstIdFromIndices("example record one")
        mibInstrumentation = snmpContext.get_mib_instrum()
        mibInstrumentation.write_variables(
            (exampleTableColumn2.name + rowInstanceId, "my string value"),
            (exampleTableColumn3.name + rowInstanceId, 123456),
            (exampleTableStatus.name + rowInstanceId, "createAndGo"),
        )

    # Register SNMP Applications at the SNMP engine for particular SNMP context
    cmdrsp.GetCommandResponder(snmpEngine, snmpContext)
    cmdrsp.NextCommandResponder(snmpEngine, snmpContext)
    cmdrsp.BulkCommandResponder(snmpEngine, snmpContext)
    cmdrsp.SetCommandResponder(snmpEngine, snmpContext)

    # Start the event loop
    snmpEngine.transport_dispatcher.job_started(1)

    snmpEngine.open_dispatcher()

    # Wait for the agent to start
    await asyncio.sleep(1)

    # return the engine
    return snmpEngine


class AgentContextManager:
    """
    A context manager for managing the lifecycle of an SNMP test agent.

    Usage:
    async with AgentContextManager() as agent:
        # Perform operations with the agent

    When the context is entered, the agent is started using the `start_agent()` function.
    When the context is exited, the agent's transport dispatcher is stopped and closed.

    Note: The `start_agent()` function and the `transportDispatcher` attribute are not defined in this code snippet.
    """

    def __init__(
        self,
        enable_ipv6: bool = False,
        enable_custom_objects: bool = False,
        enable_table_creation: bool = False,
    ):
        self.enable_ipv6 = enable_ipv6
        self.enable_custom_objects = enable_custom_objects
        self.enable_table_creation = enable_table_creation

    async def __aenter__(self):
        self.agent = await start_agent(
            self.enable_ipv6, self.enable_custom_objects, self.enable_table_creation
        )
        return self.agent

    async def __aexit__(self, exc_type, exc_val, exc_tb):
        self.agent.transport_dispatcher.job_finished(1)
        self.agent.close_dispatcher()