Ejemplo n.º 1
0
 def trap_v3(self, endpoint, message):
     # Send trap to one endpoint
     error_indication, error_status, error_index, var_binds = next(
         sendNotification(
             SnmpEngine(snmpEngineID=OctetString(
                 hexValue=endpoint.engineid)),
             endpoint.usm_user,
             UdpTransportTarget((endpoint.host, endpoint.port)),
             ContextData(),
             'trap',
             # sequence of custom OID-value pairs
             self.get_pdu(message)))
     if error_indication:
         log(
             "error",
             "notifier",
             {
                 "message": 'Unable to send snmp message to %s err:%s %s %s'
                 % (endpoint.host,
                    error_indication,
                    error_status,
                    error_index)
             }
         )
     else:
         log(
             "info",
             "notifier",
             {
                 "message": 'Sent snmp message to %s to alert about %s'
                 % (endpoint.host,
                    message)
             }
         )
Ejemplo n.º 2
0
def send_snmp_trap(event_context, **kwargs):
    notification_type = _create_notification_type(event_context)
    destination_address = kwargs['destination_address']
    destination_port = kwargs['destination_port']
    community_string = kwargs['community_string']

    error_indication, _, _, _ = next(
        sendNotification(
            SnmpEngine(), CommunityData(community_string, mpModel=1),
            UdpTransportTarget((destination_address, destination_port)),
            ContextData(), NOTIFY_TYPE, notification_type))

    logger = setup_logger('cloudify.snmp.snmp_trap')
    if error_indication:
        logger.error(error_indication)

    logger.info(
        'Sent SNMP trap of the event: {0} and the execution_id: {1}'.format(
            event_context['event_type'], event_context['execution_id']))
Ejemplo n.º 3
0
    def sendTrap(self, trap):
        trap_oid = trap['trap_oid']

        txAddress = self.args['--ipv6-host']
        txPort = self.args['--port']
        userData = UsmUserData('user-sha-aes128',
                               'authkey1',
                               'privkey1',
                               authProtocol=usmHMACSHAAuthProtocol,
                               privProtocol=usmAesCfb128Protocol)

        log.info("Sending SNMP Trap", address=txAddress, port=txPort)
        log.info("  Notification",
                 name=self.smd.getNameByNumOid(trap_oid),
                 oid=trap_oid)
        for var_bind in trap['var_binds']:
            var_oid = var_bind[0]
            var_val = var_bind[1]
            log.info("  with Object",
                     name=self.smd.getNameByNumOid(var_oid),
                     oid=var_oid,
                     type=type(var_val))

        trapOid = trap['trap_oid']
        txVarBinds = trap['var_binds']
        errorIndicationTx, errorStatusTx, errorIndexTx, varBindsTx = next(
            sendNotification(
                SnmpEngine(OctetString(hexValue='8000000001020304')), userData,
                Udp6TransportTarget((txAddress, txPort)), ContextData(),
                'trap',
                NotificationType(
                    ObjectIdentity(trapOid)).addVarBinds(*txVarBinds)))
        if errorIndicationTx:
            print(errorIndicationTx)
        else:
            for varBindTx in varBindsTx:
                print(' = '.join([x.prettyPrint() for x in varBindTx]))
Ejemplo n.º 4
0
def send_snmp_trap(config, trap_data):
    """
    Send a SNMP v2c trap.
    :param config: The configuration data.
    :type config: dict
    :param trap_data:
    """
    oids = {
        'alertname': '{}.1.1.1'.format(config['trap_oid_prefix']),
        'status': '{}.1.1.2'.format(config['trap_oid_prefix']),
        'severity': '{}.1.1.3'.format(config['trap_oid_prefix']),
        'instance': '{}.1.1.4'.format(config['trap_oid_prefix']),
        'job': '{}.1.1.5'.format(config['trap_oid_prefix']),
        'description': '{}.1.1.6'.format(config['trap_oid_prefix']),
        'labels': '{}.1.1.7'.format(config['trap_oid_prefix']),
        'timestamp': '{}.1.1.8'.format(config['trap_oid_prefix']),
        'rawdata': '{}.1.1.9'.format(config['trap_oid_prefix'])
    }

    transport_addr = (config['snmp_host'], config['snmp_port'])

    # Configure the transport target (IPv4 or IPv6).
    try:
        # Will raise an exception if ``snmp_host`` isn't an IPv6 address.
        ipaddress.IPv6Address(config['snmp_host'])
        transport_target = hlapi.Udp6TransportTarget(transport_addr)
    except ValueError:
        transport_target = hlapi.UdpTransportTarget(transport_addr)
    transport_target.retries = config['snmp_retries']
    transport_target.timeout = config['snmp_timeout']

    var_binds = hlapi.NotificationType(hlapi.ObjectIdentity(trap_data['oid']))
    var_binds.addVarBinds(
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['alertname']),
                         hlapi.OctetString(trap_data['alertname'] or '')),
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['status']),
                         hlapi.OctetString(trap_data['status'] or '')),
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['severity']),
                         hlapi.OctetString(trap_data['severity'] or '')),
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['instance']),
                         hlapi.OctetString(trap_data['instance'] or '')),
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['job']),
                         hlapi.OctetString(trap_data['job'] or '')),
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['description']),
                         hlapi.OctetString(trap_data['description'] or '')),
        hlapi.ObjectType(
            hlapi.ObjectIdentity(oids['labels']),
            hlapi.OctetString(json.dumps(trap_data['labels'] or ''))),
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['timestamp']),
                         hlapi.TimeTicks(trap_data['timestamp'])),
        hlapi.ObjectType(hlapi.ObjectIdentity(oids['rawdata']),
                         hlapi.OctetString(json.dumps(trap_data['rawdata']))))

    error_indication, error_status, error_index, _ = next(
        hlapi.sendNotification(
            hlapi.SnmpEngine(),
            hlapi.CommunityData(config['snmp_community'], mpModel=1),
            transport_target, hlapi.ContextData(), 'trap', var_binds))

    if error_indication:
        logger.error('SNMP trap not sent: %s', error_indication)
    elif error_status:
        logger.error('SNMP trap receiver returned error: %s @%s', error_status,
                     error_index)
    else:
        logger.debug('Sending SNMP trap: %s', trap_data)