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
|
from pyasn1.compat.octets import null
from pysnmp import nextid, error
from pysnmp.entity import engine, config
from pysnmp.smi.rfc1902 import *
from pysnmp.entity.rfc3413 import ntforg, context
from pysnmp.entity.rfc3413.oneliner.auth import *
from pysnmp.entity.rfc3413.oneliner.target import *
from pysnmp.entity.rfc3413.oneliner.ctx import *
from pysnmp.entity.rfc3413.oneliner import cmdgen
# obsolete, compatibility symbols
from pysnmp.entity.rfc3413.oneliner.mibvar import MibVariable
SnmpEngine = engine.SnmpEngine
nextID = nextid.Integer(0xffffffff)
class AsyncNotificationOriginator:
def __init__(self):
self.__asyncCmdGen = cmdgen.AsyncCommandGenerator()
def _getCache(self, snmpEngine):
cache = snmpEngine.getUserContext('ntforg')
if cache is None:
cache = { 'auth': {}, 'name': {} }
snmpEngine.setUserContext(ntforg=cache)
return cache
def getMibViewController(self, snmpEngine):
return self.__asyncCmdGen.getMibViewController(snmpEngine)
def cfgNtfOrg(self, snmpEngine, authData, transportTarget, notifyType):
cache = self._getCache(snmpEngine)
addrName, paramsName = self.__asyncCmdGen.cfgCmdGen( snmpEngine, authData, transportTarget )
tagList = transportTarget.tagList.split()
if not tagList:
tagList = ['']
for tag in tagList:
notifyNameKey = paramsName, tag, notifyType
if notifyNameKey in cache['name']:
notifyName, paramsName, useCount = cache['name'][notifyNameKey]
cache['name'][notifyNameKey] = notifyName, paramsName, useCount + 1
else:
notifyName = 'n%s' % nextID()
config.addNotificationTarget(
snmpEngine,
notifyName,
paramsName,
tag,
notifyType
)
cache['name'][notifyNameKey] = notifyName, paramsName, 1
authDataKey = authData.securityName, authData.securityModel
if authDataKey in cache['auth']:
authDataX, subTree, useCount = cache['auth'][authDataKey]
cache['auth'][authDataKey] = authDataX, subTree, useCount + 1
else:
subTree = (1,3,6)
config.addTrapUser(
snmpEngine,
authData.securityModel,
authData.securityName,
authData.securityLevel,
subTree
)
cache['auth'][authDataKey] = authData, subTree, 1
return notifyName
def uncfgNtfOrg(self, snmpEngine, authData=None):
cache = self._getCache(snmpEngine)
if authData:
authDataKey = authData.securityName, authData.securityModel
if authDataKey in cache['auth']:
authDataKeys = ( authDataKey, )
else:
raise error.PySnmpError('Unknown authData %s' % (authData,))
else:
authDataKeys = tuple(cache['auth'].keys())
addrNames, paramsNames = self.__asyncCmdGen.uncfgCmdGen(snmpEngine, authData)
notifyAndParamsNames = [ (cache['name'][x], x) for x in cache['name'].keys() if x[0] in paramsNames ]
for (notifyName, paramsName, useCount), notifyNameKey in notifyAndParamsNames:
useCount -= 1
if useCount:
cache['name'][notifyNameKey] = notifyName, paramsName, useCount
else:
config.delNotificationTarget(
snmpEngine, notifyName, paramsName
)
del cache['name'][notifyNameKey]
for authDataKey in authDataKeys:
authDataX, subTree, useCount = cache['auth'][authDataKey]
useCount -= 1
if useCount:
cache['auth'][authDataKey] = authDataX, subTree, useCount
else:
config.delTrapUser(
snmpEngine,
authDataX.securityModel,
authDataX.securityName,
authDataX.securityLevel,
subTree
)
del cache['auth'][authDataKey]
def makeVarBinds(self, snmpEngine, varBinds):
mibViewController = self.getMibViewController(snmpEngine)
if isinstance(varBinds, NotificationType):
varBinds.resolveWithMib(mibViewController)
__varBinds = []
for varBind in varBinds:
if isinstance(varBind, ObjectType):
pass
elif isinstance(varBind[0], ObjectIdentity):
varBind = ObjectType(*varBind)
else:
varBind = ObjectType(ObjectIdentity(varBind[0]), varBind[1])
__varBinds.append(varBind.resolveWithMib(mibViewController))
return __varBinds
def unmakeVarBinds(self, snmpEngine, varBinds, lookupMib=False):
if lookupMib:
mibViewController = self.getMibViewController(snmpEngine)
varBinds = [ ObjectType(ObjectIdentity(x[0]), x[1]).resolveWithMib(mibViewController) for x in varBinds ]
return varBinds
def sendNotification(self, snmpEngine,
authData, transportTarget, contextData,
notifyType,
varBinds,
cbInfo=(None, None),
lookupMib=False):
def __cbFun(snmpEngine, sendRequestHandle, errorIndication,
errorStatus, errorIndex, varBinds, cbCtx):
lookupMib, cbFun, cbCtx = cbCtx
return cbFun and cbFun(
snmpEngine,
sendRequestHandle,
errorIndication,
errorStatus, errorIndex,
self.unmakeVarBinds(
snmpEngine, varBinds, lookupMib
),
cbCtx
)
cbFun, cbCtx = cbInfo
# Create matching transport tags if not given by user
if not transportTarget.tagList:
transportTarget.tagList = str(
hash((authData.securityName, transportTarget.transportAddr))
)
if isinstance(authData, CommunityData) and not authData.tag:
authData.tag = transportTarget.tagList.split()[0]
notifyName = self.cfgNtfOrg(
snmpEngine, authData, transportTarget, notifyType
)
return ntforg.NotificationOriginator().sendVarBinds(snmpEngine, notifyName, contextData.contextEngineId, contextData.contextName, self.makeVarBinds(snmpEngine, varBinds), __cbFun, (lookupMib, cbFun, cbCtx))
#
# The rest of code in this file belongs to obsolete, compatibility wrappers.
# Never use interfaces below for new applications!
#
# substitute sendNotification return object for backward compatibility
class ErrorIndicationReturn:
def __init__(self, *vars): self.__vars = vars
def __getitem__(self, i): return self.__vars[i]
def __nonzero__(self): return self.__vars[0] and 1 or 0
def __bool__(self): return bool(self.__vars[0])
def __str__(self): return str(self.__vars[0])
class AsynNotificationOriginator:
def __init__(self, snmpEngine=None, snmpContext=None):
if snmpEngine is None:
self.snmpEngine = snmpEngine = engine.SnmpEngine()
else:
self.snmpEngine = snmpEngine
if snmpContext is None:
self.snmpContext = context.SnmpContext(self.snmpEngine)
config.addContext(
self.snmpEngine, '' # this is leaky
)
else:
self.snmpContext = snmpContext
self.__asyncNtfOrg = AsyncNotificationOriginator()
self.mibViewController = self.__asyncNtfOrg.getMibViewController(self.snmpEngine)
def __del__(self): self.uncfgNtfOrg()
def cfgNtfOrg(self, authData, transportTarget, notifyType):
return self.__asyncNtfOrg.cfgNtfOrg(
self.snmpEngine, authData, transportTarget, notifyType
)
def uncfgNtfOrg(self, authData=None):
return self.__asyncNtfOrg.uncfgNtfOrg(self.snmpEngine, authData)
def makeVarBinds(self, varBinds):
return self.__asyncNtfOrg.makeVarBinds(
self.snmpEngine, varBinds
)
def unmakeVarBinds(self, varBinds, lookupNames, lookupValues):
return self.__asyncNtfOrg.unmakeVarBinds(
self.snmpEngine, varBinds, lookupNames or lookupValues
)
def sendNotification(self, authData, transportTarget,
notifyType, notificationType,
varBinds=(), # legacy, use NotificationType instead
cbInfo=(None, None),
lookupNames=False, lookupValues=False,
contextEngineId=None, # XXX ordering incompatibility
contextName=null):
def __cbFun(snmpEngine, sendRequestHandle, errorIndication,
errorStatus, errorIndex, varBinds, cbCtx):
cbFun, cbCtx = cbCtx
try:
# we need to pass response PDU information to user for INFORMs
return cbFun and cbFun(
sendRequestHandle,
errorIndication,
errorStatus, errorIndex,
varBinds,
cbCtx
)
except TypeError:
# a backward compatible way of calling user function
return cbFun(
sendRequestHandle,
errorIndication,
cbCtx
)
# for backward compatibility
if contextName is null and authData.contextName:
contextName = authData.contextName
if not isinstance(notificationType,
(ObjectIdentity, ObjectType, NotificationType)):
if isinstance(notificationType[0], tuple):
# legacy
notificationType = ObjectIdentity(notificationType[0][0], notificationType[0][1], *notificationType[1:])
else:
notificationType = ObjectIdentity(notificationType)
if not isinstance(notificationType, NotificationType):
notificationType = NotificationType(notificationType)
return self.__asyncNtfOrg.sendNotification(
self.snmpEngine,
authData, transportTarget,
ContextData(contextEngineId or self.snmpContext.contextEngineId,
contextName),
notifyType, notificationType.addVarBinds(*varBinds),
(__cbFun, cbInfo),
lookupNames or lookupValues
)
asyncSendNotification = sendNotification
class NotificationOriginator:
def __init__(self, snmpEngine=None, snmpContext=None, asynNtfOrg=None):
# compatibility attributes
self.snmpEngine = snmpEngine or SnmpEngine()
self.mibViewController = AsyncNotificationOriginator().getMibViewController(self.snmpEngine)
# the varBinds parameter is legacy, use NotificationType instead
def sendNotification(self, authData, transportTarget, notifyType,
notificationType, *varBinds, **kwargs):
for x in sendNotification(self.snmpEngine, authData, transportTarget,
ContextData(kwargs.get('contextEngineId'),
kwargs.get('contextName', null)),
notifyType,
notificationType.addVarBinds(*varBinds),
**kwargs):
return x
# circular module import dependency
from sys import version_info
if version_info[:2] < (2, 6):
from pysnmp.entity.rfc3413.oneliner.sync.compat.ntforg import *
else:
from pysnmp.entity.rfc3413.oneliner.sync.ntforg import *
|