/usr/share/pyshared/pysnmp/entity/rfc3413/ntforg.py is in python-pysnmp4 4.2.5-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
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 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 | import sys
from pyasn1.compat.octets import null
from pysnmp.entity.rfc3413 import config
from pysnmp.proto.proxy import rfc2576
from pysnmp.proto.api import v2c
from pysnmp.proto import error
from pysnmp import nextid
from pysnmp import debug
getNextHandle = nextid.Integer(0x7fffffff)
class NotificationOriginator:
acmID = 3 # default MIB access control method to use
def __init__(self, snmpContext):
self.__pendingReqs = {}
self.__pendingNotifications = {}
self.snmpContext = snmpContext
def processResponsePdu(
self,
snmpEngine,
messageProcessingModel,
securityModel,
securityName,
securityLevel,
contextEngineId,
contextName,
pduVersion,
PDU,
statusInformation,
sendPduHandle,
cbInfo
):
(cbFun, cbCtx) = cbInfo
# 3.3.6d
if sendPduHandle not in self.__pendingReqs:
raise error.ProtocolError('Missing sendPduHandle %s' % sendPduHandle)
( origTransportDomain,
origTransportAddress,
origMessageProcessingModel,
origSecurityModel,
origSecurityName,
origSecurityLevel,
origContextEngineId,
origContextName,
origPdu,
origTimeout,
origRetryCount,
origRetries,
metaSendPduHandle
) = self.__pendingReqs[sendPduHandle]
del self.__pendingReqs[sendPduHandle]
self.__pendingNotifications[metaSendPduHandle] -= 1
snmpEngine.transportDispatcher.jobFinished(id(self))
if statusInformation:
debug.logger & debug.flagApp and debug.logger('processResponsePdu: metaSendPduHandle %s, sendPduHandle %s statusInformation %s' % (metaSendPduHandle, sendPduHandle, statusInformation))
if origRetries == origRetryCount:
debug.logger & debug.flagApp and debug.logger('processResponsePdu: metaSendPduHandle %s, sendPduHandle %s retry count %d exceeded' % (metaSendPduHandle, sendPduHandle, origRetries))
if not self.__pendingNotifications[metaSendPduHandle]:
del self.__pendingNotifications[metaSendPduHandle]
self._handleResponse(
metaSendPduHandle,
statusInformation['errorIndication'],
0, 0, (),
cbFun,
cbCtx
)
return
# Convert timeout in seconds into timeout in timer ticks
timeoutInTicks = float(origTimeout)/100/snmpEngine.transportDispatcher.getTimerResolution()
# User-side API assumes SMIv2
if messageProcessingModel == 0:
reqPDU = rfc2576.v2ToV1(origPdu)
pduVersion = 0
else:
reqPDU = origPdu
pduVersion = 1
# 3.3.6a
try:
sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
snmpEngine,
origTransportDomain,
origTransportAddress,
origMessageProcessingModel,
origSecurityModel,
origSecurityName,
origSecurityLevel,
origContextEngineId,
origContextName,
pduVersion,
reqPDU,
1, # expectResponse
timeoutInTicks,
self.processResponsePdu,
(cbFun, cbCtx)
)
except error.StatusInformation:
statusInformation = sys.exc_info()[1]
debug.logger & debug.flagApp and debug.logger('processResponsePdu: metaSendPduHandle %s: sendPdu() failed with %r ' % (metaSendPduHandle, statusInformation))
if not self.__pendingNotifications[metaSendPduHandle]:
del self.__pendingNotifications[metaSendPduHandle]
self._handleResponse(
metaSendPduHandle,
statusInformation['errorIndication'],
0, 0, (),
cbFun,
cbCtx
)
return
self.__pendingNotifications[metaSendPduHandle] += 1
snmpEngine.transportDispatcher.jobStarted(id(self))
debug.logger & debug.flagApp and debug.logger('processResponsePdu: metaSendPduHandle %s, sendPduHandle %s, timeout %d, retry %d of %d' % (metaSendPduHandle, sendPduHandle, origTimeout, origRetries, origRetryCount))
# 3.3.6b
self.__pendingReqs[sendPduHandle] = (
origTransportDomain,
origTransportAddress,
origMessageProcessingModel,
origSecurityModel,
origSecurityName,
origSecurityLevel,
origContextEngineId,
origContextName,
origPdu,
origTimeout,
origRetryCount,
origRetries + 1,
metaSendPduHandle
)
return
# 3.3.6c
if not self.__pendingNotifications[metaSendPduHandle]:
del self.__pendingNotifications[metaSendPduHandle]
# User-side API assumes SMIv2
if messageProcessingModel == 0:
PDU = rfc2576.v1ToV2(PDU, origPdu)
self._handleResponse(metaSendPduHandle, None,
v2c.apiPDU.getErrorStatus(PDU),
v2c.apiPDU.getErrorIndex(PDU,muteErrors=True),
v2c.apiPDU.getVarBinds(PDU),
cbFun, cbCtx)
def _handleResponse(self,
sendRequestHandle,
errorIndication,
errorStatus, errorIndex,
varBinds,
cbFun, cbCtx):
try:
# we need to pass response PDU information to user for INFORMs
cbFun(sendRequestHandle, errorIndication,
errorStatus, errorIndex, varBinds, cbCtx)
except TypeError:
# a backward compatible way of calling user function
cbFun(sendRequestHandle, errorIndication, cbCtx)
def sendNotification(
self,
snmpEngine,
notificationTarget,
notificationName,
additionalVarBinds=(),
cbFun=None,
cbCtx=None,
contextName=null,
instanceIndex=None
):
debug.logger & debug.flagApp and debug.logger('sendNotification: notificationTarget %s, notificationName %s, additionalVarBinds %s, contextName "%s", instanceIndex %s' % (notificationTarget, notificationName, additionalVarBinds, contextName, instanceIndex))
if contextName:
__SnmpAdminString, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('SNMP-FRAMEWORK-MIB', 'SnmpAdminString')
contextName = __SnmpAdminString(contextName)
# 3.3
( notifyTag,
notifyType ) = config.getNotificationInfo(
snmpEngine, notificationTarget
)
metaSendPduHandle = getNextHandle()
debug.logger & debug.flagApp and debug.logger('sendNotification: metaSendPduHandle %s, notifyTag %s, notifyType %s' % (metaSendPduHandle, notifyTag, notifyType))
contextMibInstrumCtl = self.snmpContext.getMibInstrum(contextName)
additionalVarBinds = [ (v2c.ObjectIdentifier(x),y) for x,y in additionalVarBinds ]
for targetAddrName in config.getTargetNames(snmpEngine, notifyTag):
( transportDomain,
transportAddress,
timeout,
retryCount,
params ) = config.getTargetAddr(snmpEngine, targetAddrName)
( messageProcessingModel,
securityModel,
securityName,
securityLevel ) = config.getTargetParams(snmpEngine, params)
debug.logger & debug.flagApp and debug.logger('sendNotification: metaSendPduHandle %s, notifyTag %s yields: transportDomain %s, transportAddress %r, securityModel %s, securityName %s, securityLevel %s' % (metaSendPduHandle, notifyTag, transportDomain, transportAddress, securityModel, securityName, securityLevel))
# 3.3.1 XXX
# XXX filtering's yet to be implemented
# filterProfileName = config.getNotifyFilterProfile(params)
# ( filterSubtree,
# filterMask,
# filterType ) = config.getNotifyFilter(filterProfileName)
varBinds = []
# 3.3.2 & 3.3.3
sysUpTime, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMPv2-MIB', 'sysUpTime')
for varName, varVal in additionalVarBinds:
if varName == sysUpTime.name:
varBinds.append((varName, varVal))
break
if not varBinds:
varBinds.append((sysUpTime.name,
sysUpTime.syntax.clone())) # for actual value
snmpTrapOid, = snmpEngine.msgAndPduDsp.mibInstrumController.mibBuilder.importSymbols('__SNMPv2-MIB', 'snmpTrapOID')
if len(notificationName) == 2: # ('MIB', 'symbol')
notificationTypeObject, = contextMibInstrumCtl.mibBuilder.importSymbols(*notificationName)
varBinds.append((snmpTrapOid.name, v2c.ObjectIdentifier(notificationTypeObject.name)))
debug.logger & debug.flagApp and debug.logger('sendNotification: notification type object is %s' % notificationTypeObject)
for notificationObject in notificationTypeObject.getObjects():
mibNode, = contextMibInstrumCtl.mibBuilder.importSymbols(*notificationObject)
if instanceIndex:
mibNode = mibNode.getNode(mibNode.name + instanceIndex)
else:
mibNode = mibNode.getNextNode(mibNode.name)
varBinds.append((mibNode.name, mibNode.syntax))
debug.logger & debug.flagApp and debug.logger('sendNotification: processed notification object %s, instance index %s, var-bind %s' % (notificationObject, instanceIndex is None and "<first>" or instanceIndex, mibNode))
elif notificationName: # numeric OID
varBinds.append(
(snmpTrapOid.name,
snmpTrapOid.syntax.clone(notificationName))
)
else:
varBinds.append((snmpTrapOid.name, snmpTrapOid.syntax))
for varName, varVal in additionalVarBinds:
if varName in (sysUpTime.name, snmpTrapOid.name):
continue
try:
snmpEngine.accessControlModel[self.acmID].isAccessAllowed(
snmpEngine, securityModel, securityName,
securityLevel, 'notify', contextName, varName
)
except error.StatusInformation:
debug.logger & debug.flagApp and debug.logger('sendNotification: OID %s not allowed for %s, droppping notification' % (varName, securityName))
return
else:
varBinds.append((varName, varVal))
# 3.3.4
if notifyType == 1:
pdu = v2c.SNMPv2TrapPDU()
elif notifyType == 2:
pdu = v2c.InformRequestPDU()
else:
raise RuntimeError()
v2c.apiPDU.setDefaults(pdu)
v2c.apiPDU.setVarBinds(pdu, varBinds)
# User-side API assumes SMIv2
if messageProcessingModel == 0:
reqPDU = rfc2576.v2ToV1(pdu)
pduVersion = 0
else:
reqPDU = pdu
pduVersion = 1
# 3.3.5
if notifyType == 1:
try:
snmpEngine.msgAndPduDsp.sendPdu(
snmpEngine,
transportDomain,
transportAddress,
messageProcessingModel,
securityModel,
securityName,
securityLevel,
self.snmpContext.contextEngineId,
contextName,
pduVersion,
reqPDU,
None
)
except error.StatusInformation:
statusInformation = sys.exc_info()[1]
debug.logger & debug.flagApp and debug.logger('sendReq: metaSendPduHandle %s: sendPdu() failed with %r' % (metaSendPduHandle, statusInformation))
if not self.__pendingNotifications[metaSendPduHandle]:
del self.__pendingNotifications[metaSendPduHandle]
self._handleResponse(
metaSendPduHandle,
statusInformation['errorIndication'],
0, 0, (),
cbFun,
cbCtx
)
return metaSendPduHandle
else:
# Convert timeout in seconds into timeout in timer ticks
timeoutInTicks = float(timeout)/100/snmpEngine.transportDispatcher.getTimerResolution()
# 3.3.6a
try:
sendPduHandle = snmpEngine.msgAndPduDsp.sendPdu(
snmpEngine,
transportDomain,
transportAddress,
messageProcessingModel,
securityModel,
securityName,
securityLevel,
self.snmpContext.contextEngineId,
contextName,
pduVersion,
reqPDU,
1, # expectResponse
timeoutInTicks,
self.processResponsePdu,
(cbFun, cbCtx)
)
except error.StatusInformation:
statusInformation = sys.exc_info()[1]
debug.logger & debug.flagApp and debug.logger('sendReq: metaSendPduHandle %s: sendPdu() failed with %r' % (metaSendPduHandle, statusInformation))
if not self.__pendingNotifications[metaSendPduHandle]:
del self.__pendingNotifications[metaSendPduHandle]
self._handleResponse(
metaSendPduHandle,
statusInformation['errorIndication'],
0, 0, (),
cbFun,
cbCtx
)
return metaSendPduHandle
debug.logger & debug.flagApp and debug.logger('sendNotification: metaSendPduHandle %s, sendPduHandle %s, timeout %d' % (metaSendPduHandle, sendPduHandle, timeout))
# 3.3.6b
self.__pendingReqs[sendPduHandle] = (
transportDomain,
transportAddress,
messageProcessingModel,
securityModel,
securityName,
securityLevel,
self.snmpContext.contextEngineId,
contextName,
pdu,
timeout,
retryCount,
1,
metaSendPduHandle
)
if metaSendPduHandle not in self.__pendingNotifications:
self.__pendingNotifications[metaSendPduHandle] = 0
self.__pendingNotifications[metaSendPduHandle] += 1
snmpEngine.transportDispatcher.jobStarted(id(self))
debug.logger & debug.flagApp and debug.logger('sendNotification: metaSendPduHandle %s, notification(s) sent' % metaSendPduHandle)
return metaSendPduHandle
# XXX
# move/group/implement config setting/retrieval at a stand-alone module
|