Exemplo n.º 1
0
def handleTrap(trap, config=None):
    """
    handleTrap is run by snmptrapd every time it receives a
    trap. Return False to signal trap was discarded, True if trap was
    accepted.
    """

    db = getConnection('default')

    # Event variables
    source = "snmptrapd"
    target = "eventEngine"
    eventtypeid = "upsPowerState"

    # Use the trap-object to access trap-variables and do stuff.
    for vendor in ONBATTERY.keys():
        if trap.snmpTrapOID in ONBATTERY[vendor]:
            logger.debug ("Got ups on battery trap (%s)" %vendor)

            # Get time to live
            try:
                batterytimeoid, format = BATTERYTIME[vendor]
                s = Snmp(trap.agent, trap.community)
                batterytime = s.get(batterytimeoid)
            except Exception, e:
                logger.info("Could not get battery time from %s: %s" \
                            %(trap.agent, e))
                batterytime = False
            else:
                batterytime = format_batterytime(batterytime, format)
                logger.debug("batterytime: %s" % batterytime)
                
            # Get netboxid from database
            c = db.cursor()
            c.execute("SELECT netboxid, sysname FROM netbox WHERE ip = %s",
                      (trap.agent,))
            if not c.rowcount > 0:
                logger.error("Could not find netbox in database, no event \
                will be posted")
                return False

            netboxid, sysname = c.fetchone()
            state = 's'

            # Create event-object, fill it and post event.
            e = Event(source=source, target=target, netboxid=netboxid,
                      eventtypeid=eventtypeid, state=state)
            e['alerttype'] = "upsOnBatteryPower"
            e['batterytime'] = batterytime
            e['sysname'] = sysname

            # Post event
            try:
                e.post()
            except Exception, e:
                logger.error(e)
                return False

            return True
Exemplo n.º 2
0
def handler(nblist, state):

    for deviceid, netboxid in nblist:

        e = Event('ipdevpoll', 'eventEngine', deviceid, netboxid,
                  eventtypeid='snmpAgentState', state=state, severity=100)
        e['alerttype'] = 'snmpAgentDown' if state == 's' else 'snmpAgentUp'
        e.post()
Exemplo n.º 3
0
def handleTrap(trap, config=None):

    # Two interesting traps:
    # bsnAPAssociated and bsnAPDisassociated

    if trap.snmpTrapOID not in [ "." + TRAPS['bsnAPAssociated']['oid'],
                                 "." + TRAPS['bsnAPDisassociated']['oid']
                                 ]:

        return False


    logger.debug("Got trap %s" %trap.snmpTrapOID)

    # Eventvariables:
    source = "snmptrapd"
    target = "eventEngine"
    eventtypeid = 'apState'
    alerttype = ""
    state = ""
    subid = ""
    mac = ""
    apname = ""

    # Name of AP: bsnAPName
    # MAC: bsnAPMacAddrTrapVariable
    for key, val in trap.varbinds.items():
        if key.find(NODES['bsnAPName']['oid']) >= 0:
            apname = val
            logger.debug("Set apname to %s" %apname)
        elif key.find(NODES['bsnAPMacAddrTrapVariable']['oid']) >= 0:
            mac = val
            subid = mac
            
    if trap.snmpTrapOID == "." + TRAPS['bsnAPAssociated']['oid']:
        state = 'e'
        alerttype = 'apUp'
    elif trap.snmpTrapOID == "." + TRAPS['bsnAPDisassociated']['oid']:
        state = 's'
        alerttype = 'apDown'


    e = Event(source=source, target=target, subid=subid,
              eventtypeid=eventtypeid, state=state)
    e['alerttype'] = alerttype
    e['mac'] = mac
    e['apname'] = apname

    logger.debug(e)
    
    try:
        e.post()
    except Exception, e:
        logger.error(e)
        return False
Exemplo n.º 4
0
def post_event(psu_or_fan, status):
    """ Posts an event on the eventqueue."""
    source = "powersupplywatch"
    target = "eventEngine"
    eventtypeid = "psuState" if is_psu(psu_or_fan) else "fanState"
    value = 100
    severity = 50

    try:
        device_id = psu_or_fan.device.id
    except (Device.DoesNotExist, AttributeError):
        device_id = None

    event = Event(source=source, target=target,
                  deviceid=device_id,
                  netboxid=psu_or_fan.netbox.id,
                  subid=psu_or_fan.id,
                  eventtypeid=eventtypeid,
                  state='x',
                  value=value,
                  severity=severity)
    event['sysname'] = psu_or_fan.netbox.sysname
    if status in (PowerSupplyOrFan.STATE_DOWN,
                  PowerSupplyOrFan.STATE_WARNING):
        event['alerttype'] = 'psuNotOK' if is_psu(psu_or_fan) else 'fanNotOK'
        event.state = 's'
    elif status == PowerSupplyOrFan.STATE_UP:
        event['alerttype'] = 'psuOK' if is_psu(psu_or_fan) else 'fanOK'
        event.state = 'e'
    event['unitname'] = psu_or_fan.name
    event['state'] = status
    LOGGER.debug('Posting event: %s', event)
    try:
        event.post()
    except Exception, why:
        LOGGER.error('post_event: exception = %s', why)
        return False
Exemplo n.º 5
0
def post_event(psu_or_fan, status):
    """ Posts an event on the eventqueue."""
    source = "powersupplywatch"
    target = "eventEngine"
    eventtypeid = "psuState" if is_psu(psu_or_fan) else "fanState"
    value = 100
    severity = 50

    try:
        device_id = psu_or_fan.device.id
    except (Device.DoesNotExist, AttributeError):
        device_id = None

    event = Event(source=source,
                  target=target,
                  deviceid=device_id,
                  netboxid=psu_or_fan.netbox.id,
                  subid=psu_or_fan.id,
                  eventtypeid=eventtypeid,
                  state='x',
                  value=value,
                  severity=severity)
    event['sysname'] = psu_or_fan.netbox.sysname
    if status in (PowerSupplyOrFan.STATE_DOWN, PowerSupplyOrFan.STATE_WARNING):
        event['alerttype'] = 'psuNotOK' if is_psu(psu_or_fan) else 'fanNotOK'
        event.state = 's'
    elif status == PowerSupplyOrFan.STATE_UP:
        event['alerttype'] = 'psuOK' if is_psu(psu_or_fan) else 'fanOK'
        event.state = 'e'
    event['unitname'] = psu_or_fan.name
    event['state'] = status
    LOGGER.debug('Posting event: %s', event)
    try:
        event.post()
    except Exception as why:
        LOGGER.error('post_event: exception = %s', why)
        return False
    return True
Exemplo n.º 6
0
def handleTrap(trap, config=None):
    """
    handleTrap is run by snmptrapd every time it receives a
    trap. Return False to signal trap was discarded, True if trap was
    accepted.
    """

    # Event variables
    source = "snmptrapd"
    target = "eventEngine"
    eventtypeid = "upsPowerState"

    # Use the trap-object to access trap-variables and do stuff.
    for vendor, oids in ONBATTERY.items():
        if trap.snmpTrapOID in oids:
            _logger.debug("Got ups on battery trap (%s)", vendor)

            # Get time to live
            try:
                batterytimeoid, format = BATTERYTIME[vendor]
                s = Snmp(trap.agent, trap.community)
                batterytime = s.get(batterytimeoid)
            except Exception as err:
                _logger.info("Could not get battery time from %s: %s", trap.agent, err)
                batterytime = False
            else:
                batterytime = format_batterytime(batterytime, format)
                _logger.debug("batterytime: %s", batterytime)

            if not trap.netbox:
                _logger.error(
                    "Could not find netbox in database, no event will be posted",
                )
                return False

            # Create event-object, fill it and post event.
            e = Event(
                source=source,
                target=target,
                netboxid=trap.netbox.netboxid,
                eventtypeid=eventtypeid,
                state='s',
            )
            e['alerttype'] = "upsOnBatteryPower"
            e['batterytime'] = batterytime
            e['sysname'] = trap.netbox.sysname

            # Post event
            try:
                e.post()
            except Exception as e:
                _logger.error(e)
                return False

            return True

    for vendor, oids in OFFBATTERY.items():
        if trap.snmpTrapOID in oids:
            _logger.debug("Got ups on utility power trap (%s)", vendor)

            if not trap.netbox:
                _logger.error(
                    "Could not find netbox in database, no event will be posted",
                )
                return False

            # Create event-object, fill it and post event.
            e = Event(
                source=source,
                target=target,
                netboxid=trap.netbox.netboxid,
                eventtypeid=eventtypeid,
                state='e',
            )
            e['sysname'] = trap.netbox.sysname
            e['alerttype'] = "upsOnUtilityPower"

            # Post event
            try:
                e.post()
            except Exception as e:
                _logger.error(e)
                return False

            return True

    return False
Exemplo n.º 7
0
def handleTrap(trap, config=None):
    """
    handleTrap is run by snmptrapd every time it receives a
    trap. Return False to signal trap was discarded, True if trap was
    accepted.
    """

    db = getConnection('default')

    # Event variables
    source = "snmptrapd"
    target = "eventEngine"
    eventtypeid = "upsPowerState"

    # Use the trap-object to access trap-variables and do stuff.
    for vendor in ONBATTERY.keys():
        if trap.snmpTrapOID in ONBATTERY[vendor]:
            logger.debug("Got ups on battery trap (%s)", vendor)

            # Get time to live
            try:
                batterytimeoid, format = BATTERYTIME[vendor]
                s = Snmp(trap.agent, trap.community)
                batterytime = s.get(batterytimeoid)
            except Exception as err:
                logger.info("Could not get battery time from %s: %s",
                            trap.agent, err)
                batterytime = False
            else:
                batterytime = format_batterytime(batterytime, format)
                logger.debug("batterytime: %s", batterytime)

            # Get netboxid from database
            c = db.cursor()
            c.execute("SELECT netboxid, sysname FROM netbox WHERE ip = %s",
                      (trap.agent, ))
            if not c.rowcount > 0:
                logger.error("Could not find netbox in database, no event \
                will be posted")
                return False

            netboxid, sysname = c.fetchone()
            state = 's'

            # Create event-object, fill it and post event.
            e = Event(source=source,
                      target=target,
                      netboxid=netboxid,
                      eventtypeid=eventtypeid,
                      state=state)
            e['alerttype'] = "upsOnBatteryPower"
            e['batterytime'] = batterytime
            e['sysname'] = sysname

            # Post event
            try:
                e.post()
            except Exception as e:
                logger.error(e)
                return False

            return True

    for vendor in OFFBATTERY.keys():
        if trap.snmpTrapOID in OFFBATTERY[vendor]:
            logger.debug("Got ups on utility power trap (%s)", vendor)

            # Get netboxid from database
            c = db.cursor()
            c.execute("SELECT netboxid, sysname FROM netbox WHERE ip = %s",
                      (trap.agent, ))
            if not c.rowcount > 0:
                logger.error("Could not find netbox in database, no event \
                will be posted")
                return False

            netboxid, sysname = c.fetchone()
            state = 'e'

            # Create event-object, fill it and post event.
            e = Event(source=source,
                      target=target,
                      netboxid=netboxid,
                      eventtypeid=eventtypeid,
                      state=state)
            e['sysname'] = sysname
            e['alerttype'] = "upsOnUtilityPower"

            # Post event
            try:
                e.post()
            except Exception as e:
                logger.error(e)
                return False

            return True

    return False
Exemplo n.º 8
0
            # Get netboxid from database
            c = db.cursor()
            c.execute("SELECT netboxid, sysname FROM netbox WHERE ip = %s",
                      (trap.agent, ))
            if not c.rowcount > 0:
                logger.error("Could not find netbox in database, no event \
                will be posted")
                return False

            netboxid, sysname = c.fetchone()
            state = 'e'

            # Create event-object, fill it and post event.
            e = Event(source=source,
                      target=target,
                      netboxid=netboxid,
                      eventtypeid=eventtypeid,
                      state=state)
            e['sysname'] = sysname
            e['alerttype'] = "upsOnUtilityPower"

            # Post event
            try:
                e.post()
            except Exception, e:
                logger.error(e)
                return False

            return True

    return False
Exemplo n.º 9
0
            logger.debug("Got ups on utility power trap (%s)" %vendor)

            # Get netboxid from database
            c = db.cursor()
            c.execute("SELECT netboxid, sysname FROM netbox WHERE ip = %s",
                      (trap.agent,))
            if not c.rowcount > 0:
                logger.error("Could not find netbox in database, no event \
                will be posted")
                return False

            netboxid, sysname = c.fetchone()
            state = 'e'

            # Create event-object, fill it and post event.
            e = Event(source=source, target=target, netboxid=netboxid,
                      eventtypeid=eventtypeid, state=state)
            e['sysname'] = sysname
            e['alerttype'] = "upsOnUtilityPower"
            
            # Post event
            try:
                e.post()
            except Exception, e:
                logger.error(e)
                return False

            return True

    return False

def format_batterytime(timeunit, format):