summaryrefslogtreecommitdiff
path: root/examples/smi/agent/operations-on-managed-objects.py
blob: 75b9a765d2d6a04246d95a93cd45294c5554c9a0 (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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
"""
Agent operations on MIB
+++++++++++++++++++++++

This script explains how SNMP Agent application manipulates
its MIB possibly triggered by SNMP Manager's commands.

"""#
from pysnmp.smi import builder
from pysnmp.smi import instrum
from pysnmp.smi import exval
from pysnmp.smi import error
from pysnmp import debug

#debug.setLogger(debug.Debug('all'))


def walkMib():
    def cbFun(varBinds, **context):
        err = context.get('error')
        if err:
            print(err)

        for oid, val in varBinds:
            if exval.endOfMib.isSameTypeWith(val):
                context['app']['stop'] = True

            elif not (exval.noSuchInstance.isSameTypeWith(val) or
                      exval.noSuchObject.isSameTypeWith(val)):
                print('%s = %s' % ('.'.join([str(x) for x in oid]),
                                   not val.isValue and 'N/A' or val.prettyPrint()))

            context['app']['varBinds'] = varBinds

    app_context = {
        'varBinds': [((1, 3, 6), None)],
        'stop': False
    }

    print('Read whole MIB (table walk)')
    while not app_context['stop']:
        mibInstrum.readNextMibObjects(*app_context['varBinds'], cbFun=cbFun, app=app_context)


print('Loading MIB modules...')
mibBuilder = builder.MibBuilder().loadModules(
    'SNMPv2-MIB', 'SNMP-FRAMEWORK-MIB', 'SNMP-COMMUNITY-MIB'
)

print('Building MIB tree...')
mibInstrum = instrum.MibInstrumController(mibBuilder)

walkMib()

print('Building table entry index from human-friendly representation...')
snmpCommunityEntry, = mibBuilder.importSymbols(
    'SNMP-COMMUNITY-MIB', 'snmpCommunityEntry'
)
instanceId = snmpCommunityEntry.getInstIdFromIndices('my-router')

print('Create/update some of SNMP-COMMUNITY-MIB::snmpCommunityEntry table columns: ')


def cbFun(varBinds, **context):
    err = context.get('error')
    if err:
        print(err)

    for oid, val in varBinds:
        print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))


mibInstrum.writeMibObjects(
    (snmpCommunityEntry.name + (2,) + instanceId, 'mycomm'),
    (snmpCommunityEntry.name + (3,) + instanceId, 'mynmsname'),
    (snmpCommunityEntry.name + (7,) + instanceId, 'volatile'),
    cbFun=cbFun
)

walkMib()


def cbFun(varBinds, **context):
    err = context.get('error')
    if err:
        print(err)

    for oid, val in varBinds:
        print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))


print('Destroy SNMP-COMMUNITY-MIB::snmpCommunityEntry table row via RowStatus column: ')

mibInstrum.writeMibObjects(
    (snmpCommunityEntry.name + (8,) + instanceId, 'destroy'),
    cbFun=cbFun
)

walkMib()


def cbFun(varBinds, **context):
    err = context.get('errors', None)
    if err:
        print(err)
    for oid, val in varBinds:
        print('%s = %s' % ('.'.join([str(x) for x in oid]), not val.isValue and 'N/A' or val.prettyPrint()))


print('Create SNMP-COMMUNITY-MIB::snmpCommunityEntry table row: ')

mibInstrum.writeMibObjects(
    (snmpCommunityEntry.name + (1,) + instanceId, 'mycomm'),
    (snmpCommunityEntry.name + (2,) + instanceId, 'mycomm'),
    (snmpCommunityEntry.name + (3,) + instanceId, 'mysecname'),
    (snmpCommunityEntry.name + (4,) + instanceId, 'abcdef'),
    (snmpCommunityEntry.name + (5,) + instanceId, ''),
    (snmpCommunityEntry.name + (6,) + instanceId, 'mytag'),
    (snmpCommunityEntry.name + (7,) + instanceId, 'nonVolatile'),
    (snmpCommunityEntry.name + (8,) + instanceId, 'createAndGo'),
    cbFun=cbFun
)

walkMib()

print('Destroy SNMP-COMMUNITY-MIB::snmpCommunityEntry table row via RowStatus column: ')

mibInstrum.writeMibObjects(
    (snmpCommunityEntry.name + (8,) + instanceId, 'destroy'),
    cbFun=cbFun
)

walkMib()

print('Unloading MIB modules...'),
mibBuilder.unloadModules()