示例#1
0
def _sendNotification(self,
                      snmpEngine,
                      notificationTarget,
                      notificationName,
                      additionalVarBinds=(),
                      cbFun=None,
                      cbCtx=None,
                      contextName=null,
                      instanceIndex=None):
    if self.snmpContext is None:
        raise error.ProtocolError('SNMP context not specified')
        
    #
    # Here we first expand trap OID into associated OBJECTS
    # and then look them up at context-specific MIB
    #

    mibViewController = snmpEngine.getUserContext('mibViewController')
    if not mibViewController:
        mibViewController = view.MibViewController(snmpEngine.getMibBuilder())
        snmpEngine.setUserContext(mibViewController=mibViewController)

    # Support the following syntax:
    #   '1.2.3.4'
    #   (1,2,3,4)
    #   ('MIB', 'symbol')
    if isinstance(notificationName, (tuple, list)) and \
            notificationName and isinstance(notificationName[0], str):
        notificationName = rfc1902.ObjectIdentity(*notificationName)
    else:
        notificationName = rfc1902.ObjectIdentity(notificationName)

    varBinds = rfc1902.NotificationType(
        notificationName, instanceIndex=instanceIndex
    ).resolveWithMib(mibViewController)

    mibInstrumController = self.snmpContext.getMibInstrum(contextName)

    varBinds = varBinds[:1] + mibInstrumController.readVars(varBinds[1:])

    return self.sendVarBinds(snmpEngine,
                             notificationTarget,
                             self.snmpContext.contextEngineId,
                             contextName,
                             varBinds + list(additionalVarBinds),
                             _sendNotificationCbFun,
                             (cbFun, cbCtx))
# to what targets (chosen by tag) and what filter should apply to
# the set of targets (selected by tag)
config.addNotificationTarget(snmpEngine, 'my-notification', 'my-filter',
                             'all-my-managers', 'trap')

# Allow NOTIFY access to Agent's MIB by this SNMP model (2), securityLevel
# and SecurityName
config.addContext(snmpEngine, '')
config.addVacmUser(snmpEngine, 2, 'my-area', 'noAuthNoPriv', (), (), (1, 3, 6))

# *** SNMP engine configuration is complete by this line ***

# Create Notification Originator App instance.
ntfOrg = ntforg.NotificationOriginator()

# Build and submit notification message to dispatcher
ntfOrg.sendVarBinds(
    snmpEngine,
    'my-notification',  # notification targets
    None,
    '',  # contextEngineId, contextName
    rfc1902.NotificationType(
        rfc1902.ObjectIdentity('IF-MIB', 'linkUp'),
        instanceIndex=instanceIndex,
        objects=objects).resolveWithMib(mibViewController))

print('Notification is scheduled to be sent')

# Run I/O dispatcher which would send pending message and process response
snmpEngine.transportDispatcher.runDispatcher()
示例#3
0
    rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID', 0),
    '1.3.6.1').resolveWithMib(mibView)

print(varBind[0].prettyPrint(), varBind[1].__class__.__name__,
      varBind[1].prettyPrint())

# Create just OID
varBind = rfc1902.ObjectType(
    rfc1902.ObjectIdentity('SNMPv2-MIB', 'sysObjectID',
                           0)).resolveWithMib(mibView)

print(varBind[0].prettyPrint(), varBind[1].__class__.__name__,
      varBind[1].prettyPrint())

# Create var-binds from MIB notification object (without OBJECTS clause)
varBinds = rfc1902.NotificationType(
    rfc1902.ObjectIdentity('SNMPv2-MIB', 'coldStart')).resolveWithMib(mibView)

print([
    '%s = %s(%s)' %
    (x[0].prettyPrint(), x[1].__class__.__name__, x[1].prettyPrint())
    for x in varBinds
])

# Create var-binds from MIB notification object (with OBJECTS clause)
varBinds = rfc1902.NotificationType(rfc1902.ObjectIdentity('IF-MIB', 'linkUp'),
                                    instanceIndex=(1, ),
                                    objects={
                                        ('IF-MIB', 'ifOperStatus'): 'down'
                                    }).resolveWithMib(mibView)

print(varBinds.prettyPrint())