Example #1
0
def check_snmp_version(ip, community, version):
    """Check if version of snmp is supported by device"""
    sysobjectid = '1.3.6.1.2.1.1.2.0'
    try:
        snmp = Snmp(ip, community, version)
        snmp.get(sysobjectid)
    except Exception:  # pylint: disable=W0703
        return False
    else:
        return True
Example #2
0
def check_snmp_version(ip, profile):
    """Check if version of snmp is supported by device"""
    sysobjectid = '1.3.6.1.2.1.1.2.0'
    try:
        snmp = Snmp(
            ip,
            profile.configuration.get("community"),
            profile.configuration.get("version"),
        )
        snmp.get(sysobjectid)
    except Exception:  # pylint: disable=W0703
        return False
    else:
        return True
Example #3
0
 def get_and_set_syslocation(ip, community):
     syslocation = '1.3.6.1.2.1.1.6.0'
     try:
         try:
             snmp = Snmp(ip, community, '2c')
             value = snmp.get(syslocation)
             snmp.set(syslocation, 's', value)
         except TimeOutException:
             snmp = Snmp(ip, community, '1')
             value = snmp.get(syslocation)
             snmp.set(syslocation, 's', value)
     except SnmpError:
         return None
     else:
         return True
Example #4
0
 def get_snmp_version(ip, community):
     sysobjectid = '1.3.6.1.2.1.1.2.0'
     try:
         try:
             snmp = Snmp(ip, community, '2c')
             snmp.get(sysobjectid)
             snmp_version = '2c'
         except Exception:
             snmp = Snmp(ip, community, '1')
             snmp.get(sysobjectid)
             snmp_version = '1'
     except SnmpError:
         return None
     else:
         return snmp_version
Example #5
0
 def get_and_set_syslocation(ip, community):
     syslocation = '1.3.6.1.2.1.1.6.0'
     try:
         try:
             snmp = Snmp(ip, community, '2c')
             value = snmp.get(syslocation)
             snmp.set(syslocation, 's', value)
         except TimeOutException:
             snmp = Snmp(ip, community, '1')
             value = snmp.get(syslocation)
             snmp.set(syslocation, 's', value)
     except SnmpError:
         return None
     else:
         return True
Example #6
0
 def get_snmp_version(ip, community):
     sysobjectid = '1.3.6.1.2.1.1.2.0'
     try:
         try:
             snmp = Snmp(ip, community, '2c')
             snmp.get(sysobjectid)
             snmp_version = '2c'
         except Exception:
             snmp = Snmp(ip, community, '1')
             snmp.get(sysobjectid)
             snmp_version = '1'
     except SnmpError:
         return None
     else:
         return snmp_version
Example #7
0
def snmp_write_test(ip, profile):
    """Test that snmp write works"""

    testresult = {
        'error_message': '',
        'custom_error': '',
        'status': False,
        'syslocation': ''
    }

    syslocation = '1.3.6.1.2.1.1.6.0'
    value = ''
    try:
        snmp = Snmp(
            ip,
            profile.configuration.get("community"),
            profile.configuration.get("version"),
        )
        value = safestring(snmp.get(syslocation))
        snmp.set(syslocation, 's', value.encode('utf-8'))
    except SnmpError as error:
        testresult['error_message'] = error.args
        testresult['status'] = False
    except UnicodeDecodeError as error:
        testresult['custom_error'] = 'UnicodeDecodeError'
        testresult['error_message'] = error.args
        testresult['status'] = False
    else:
        testresult['status'] = True

    testresult['syslocation'] = value
    return testresult
Example #8
0
def snmp_write_test(ip, community, snmp_version):
    """Test that snmp write works"""
    if not community:
        return False

    testresult = {
        'error_message': '',
        'custom_error': '',
        'status': False,
        'syslocation': ''
    }

    syslocation = '1.3.6.1.2.1.1.6.0'
    value = ''
    try:
        snmp = Snmp(ip, community, snmp_version)
        value = snmp.get(syslocation)
        snmp.set(syslocation, 's', value)
    except SnmpError as error:
        try:
            value.decode('ascii')
        except UnicodeDecodeError:
            testresult['custom_error'] = 'UnicodeDecodeError'

        testresult['error_message'] = error.message
        testresult['status'] = False
    else:
        testresult['status'] = True

    testresult['syslocation'] = value
    return testresult
Example #9
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
Example #10
0
def snmp_type(ip_addr, snmp_ro, snmp_version):
    """Query ip for sysobjectid using form data"""
    snmp = Snmp(ip_addr, snmp_ro, snmp_version)
    try:
        sysobjectid = snmp.get('.1.3.6.1.2.1.1.2.0')
    except SnmpError:
        return None
    sysobjectid = str(sysobjectid).lstrip('.')
    try:
        netbox_type = NetboxType.objects.get(sysobjectid=sysobjectid)
        return netbox_type
    except NetboxType.DoesNotExist:
        return None
Example #11
0
def snmp_type(ip_addr, snmp_ro, snmp_version):
    """Query ip for sysobjectid using form data"""
    snmp = Snmp(ip_addr, snmp_ro, snmp_version)
    try:
        sysobjectid = snmp.get('.1.3.6.1.2.1.1.2.0')
    except SnmpError:
        return None
    sysobjectid = sysobjectid.lstrip('.')
    try:
        netbox_type = NetboxType.objects.get(sysobjectid=sysobjectid)
        return netbox_type
    except NetboxType.DoesNotExist:
        return None
Example #12
0
def snmp_serials(ip_addr, snmp_ro, snmp_version):
    """Query ip for serial using form data"""
    snmp = Snmp(ip_addr, snmp_ro, snmp_version)
    oids = SnmpOid.objects.filter(oid_key__icontains='serial').values_list(
        'snmp_oid', 'get_next')
    serials = []
    for (oid, get_next) in oids:
        try:
            if get_next:
                result = snmp.walk(oid)
                serials.extend([r[1] for r in result if r[1]])
            else:
                result = snmp.get(oid)
                if result:
                    serials.append(result)
        except SnmpError:
            pass
    return serials
Example #13
0
def snmp_serials(ip_addr, snmp_ro, snmp_version):
    """Query ip for serial using form data"""
    snmp = Snmp(ip_addr, snmp_ro, snmp_version)
    oids = SnmpOid.objects.filter(
        oid_key__icontains='serial'
    ).values_list(
        'snmp_oid', 'get_next'
    )
    serials = []
    for (oid, get_next) in oids:
        try:
            if get_next:
                result = snmp.walk(oid)
                serials.extend([r[1] for r in result if r[1]])
            else:
                result = snmp.get(oid)
                if result:
                    serials.append(result)
        except SnmpError:
            pass
    return serials
Example #14
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
Example #15
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