summaryrefslogtreecommitdiff
path: root/docs/source/faq/how-to-implement-agent-mib.rst
blob: 6d2bbb9812eb5e3b4f8a4e76ab1bd2d934900160 (plain)
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

How to implement MIB at the Agent
---------------------------------

Q. How to instantiate static MIB table at my SNMP Agent?

A. You need to create MibScalarInstance class instances and register 
   them with your Agent's SNMP engine (mibBuilder, more specifically). 
   Here's an example code for a IP-MIB table:

.. code-block:: python

    # SNMP Agent (AKA CommandResponder) is built around SNMP engine object
    snmpEngine = engine.SnmpEngine()

    # Import table columns
    ( ipAddressAddrType,
      ipAddressAddr,
      ipAddressIfIndex,
      ipAddressType,
      ipAddressPrefix,
      ipAddressOrigin, 
      ipAddressStatus, 
      ipAddressCreated, 
      ipAddressLastChanged, 
      ipAddressRowStatus, 
      ipAddressStorageType ) = snmpEngine.msgAndPduDsp.mibInstrumController
    .mibBuilder.importSymbols(
      'IP-MIB',
      'ipAddressAddrType',
      'ipAddressAddr', 
      'ipAddressIfIndex', 
      'ipAddressType', 
      'ipAddressPrefix',
      'ipAddressOrigin',
      'ipAddressStatus',
      'ipAddressCreated',
      'ipAddressLastChanged', 
      'ipAddressRowStatus', 
      'ipAddressStorageType'
    )

    # Import MibScalarInstance

    MibScalarInstance, = snmpEngine.msgAndPduDsp.mibInstrumController.
    mibBuilder.importSymbols('SNMPv2-SMI', 'MibScalarInstance')

    # Create table columns instances

    _ipAddressAddrType = MibScalarInstance(
        ipAddressAddrType.name, (1, 4, 1, 2, 3, 4),
        ipAddressAddrType.syntax.clone(1)
    )
    _ipAddressAddr = MibScalarInstance(
        ipAddressAddr.name, (1, 4, 1, 2, 3, 4), 
        ipAddressAddr.syntax.clone('1.2.3.4')
    )
    _ipAddressIfIndex = MibScalarInstance(
        ipAddressIfIndex.name, (1, 4, 1, 2, 3, 4), 
        ipAddressIfIndex.syntax.clone(1)
    )
    _ipAddressType = MibScalarInstance(
        ipAddressType.name, (1, 4, 1, 2, 3, 4),
        ipAddressType.syntax.clone(1)
    )
    _ipAddressPrefix = MibScalarInstance(
        ipAddressPrefix.name, (1, 4, 1, 2, 3, 4), 
        ipAddressPrefix.syntax.clone((0,0))
    )
    _ipAddressOrigin = MibScalarInstance(
        ipAddressOrigin.name, (1, 4, 1, 2, 3, 4),
        ipAddressOrigin.syntax.clone(1)
    )
    _ipAddressStatus = MibScalarInstance(
        ipAddressStatus.name, (1, 4, 1, 2, 3, 4),
        ipAddressStatus.syntax.clone(1)
    )
    _ipAddressCreated = MibScalarInstance(
        ipAddressCreated.name, (1, 4, 1, 2, 3, 4), 
        ipAddressCreated.syntax.clone(800)
    )
    _ipAddressLastChanged = MibScalarInstance(
        ipAddressLastChanged.name, (1, 4, 1, 2, 3, 4), 
        ipAddressLastChanged.syntax.clone(600)
    )
    _ipAddressRowStatus = MibScalarInstance(
        ipAddressRowStatus.name, (1, 4, 1, 2, 3, 4), 
        ipAddressRowStatus.syntax.clone(1)
    )
    _ipAddressStorageType = MibScalarInstance(
        ipAddressStorageType.name, (1, 4, 1, 2, 3, 4),
        ipAddressStorageType.syntax
    )

    # add anonymous column instances
    snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.exportSymbols(
        '_IP-MIB',
        _ipAddressAddrType,
        _ipAddressAddr,
        _ipAddressIfIndex,
        _ipAddressType,
        _ipAddressPrefix,
        _ipAddressOrigin,
        _ipAddressStatus,
        _ipAddressCreated,
        _ipAddressLastChanged,
        _ipAddressRowStatus,
        _ipAddressStorageType
        )

    # Command responder code would follow...

   Keep in mind that the values of this table row will not change by 
   themselves. They basically hold a snapshot of a data set so your 
   application may have to update them somehow. For example, an app could 
   periodically lookup particular MibScalarInstance by OID at mibBuilder and 
   update its "syntax" attribute with a new value.

   There are other ways for building MIB tables that represent dynamic 
   Managed Objects.