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
|
#
# This file is part of pysnmp software.
#
# Copyright (c) 2005-2019, Ilya Etingof <etingof@gmail.com>
# License: http://snmplabs.com/pysnmp/license.html
#
from pysnmp.smi import builder
from pysnmp.smi import view
from pysnmp.smi.rfc1902 import *
__all__ = ['CommandGeneratorVarBinds', 'NotificationOriginatorVarBinds']
class MibViewControllerManager(object):
@staticmethod
def getMibViewController(userCache):
try:
mibViewController = userCache['mibViewController']
except KeyError:
mibViewController = view.MibViewController(builder.MibBuilder())
userCache['mibViewController'] = mibViewController
return mibViewController
class CommandGeneratorVarBinds(MibViewControllerManager):
def makeVarBinds(self, userCache, varBinds):
mibViewController = self.getMibViewController(userCache)
resolvedVarBinds = []
for varBind in varBinds:
if isinstance(varBind, ObjectType):
pass
elif isinstance(varBind[0], ObjectIdentity):
varBind = ObjectType(*varBind)
elif isinstance(varBind[0][0], tuple): # legacy
varBind = ObjectType(
ObjectIdentity(varBind[0][0][0], varBind[0][0][1],
*varBind[0][1:]), varBind[1])
else:
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
resolvedVarBinds.append(varBind.resolveWithMib(mibViewController))
return resolvedVarBinds
def unmakeVarBinds(self, userCache, varBinds, lookupMib=True):
if lookupMib:
mibViewController = self.getMibViewController(userCache)
varBinds = [
ObjectType(ObjectIdentity(x[0]),
x[1]).resolveWithMib(mibViewController)
for x in varBinds]
return varBinds
class NotificationOriginatorVarBinds(MibViewControllerManager):
def makeVarBinds(self, userCache, varBinds):
mibViewController = self.getMibViewController(userCache)
if isinstance(varBinds, NotificationType):
return varBinds.resolveWithMib(mibViewController)
resolvedVarBinds = []
for varBind in varBinds:
if isinstance(varBind, NotificationType):
resolvedVarBinds.extend(varBind.resolveWithMib(mibViewController))
continue
if isinstance(varBind, ObjectType):
pass
elif isinstance(varBind[0], ObjectIdentity):
varBind = ObjectType(*varBind)
else:
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
resolvedVarBinds.append(varBind.resolveWithMib(mibViewController))
return resolvedVarBinds
def unmakeVarBinds(self, userCache, varBinds, lookupMib=False):
if lookupMib:
mibViewController = self.getMibViewController(userCache)
varBinds = [
ObjectType(ObjectIdentity(x[0]),
x[1]).resolveWithMib(mibViewController)
for x in varBinds]
return varBinds
|