コード例 #1
0
def test_device_versions(declared_hostinfo):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev)
    hostinfo.get_version()
    assert hostinfo.vendor == declared_hostinfo.vendor
    assert hostinfo.os == declared_hostinfo.os
    assert hostinfo.version == declared_hostinfo.version
コード例 #2
0
def test_device_versions(declared_hostinfo):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev)
    hostinfo.get_version()
    assert hostinfo.vendor == declared_hostinfo.vendor
    assert hostinfo.os == declared_hostinfo.os
    assert hostinfo.version == declared_hostinfo.version
コード例 #3
0
ファイル: test_hostinfo.py プロジェクト: scopier/nelsnmp
def test_hostinfo_pysnmp_netsnmp(patch_pysnmp_getnext):
    handler = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(
        handler,
        vendor='net-snmp',
        description='Cisco IOS Software, catalyst, Version 15.x, RELEASE')
    hostinfo.get_version()
    assert hostinfo.version == 'UNKNOWN'
コード例 #4
0
ファイル: test_hostinfo.py プロジェクト: scopier/nelsnmp
def test_hostinfo_cisco_ios_version():
    handler = SnmpHandler(host='1.1.1.1')
    hostinfo = HostInfo(
        handler,
        vendor='cisco',
        description='Cisco IOS Software, catalyst, Version 15.x, RELEASE')
    hostinfo.get_version()
    assert hostinfo.version == '15.x'
コード例 #5
0
def test_hostinfo_get_all(declared_hostinfo):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev)
    hostinfo.get_all()
    assert hostinfo.description == 'Cisco Adaptive Security Appliance Version 9.3(2)2'
    assert hostinfo.contact == 'Networklore'
    assert hostinfo.location == 'Westeros'
    assert hostinfo.vendor == 'cisco'
    assert hostinfo.version == '9.3(2)2'
コード例 #6
0
ファイル: snmp_deviceinfo.py プロジェクト: ktbyers/nelkit
def main():
    argparser = SnmpArgs(description)
    args = argparser.parser.parse_args()
    snmp = NelkitSnmp(args)
    hostinfo = HostInfo(snmp)
    hostinfo.get_all()
    print ('OS: %s' % hostinfo.os)
    print ('Version: %s' % hostinfo.version)
    print ('Vendor: %s' % hostinfo.vendor)
    print ('Description: %s' % hostinfo.description)
コード例 #7
0
def main():
    argparser = SnmpArgs(description)
    args = argparser.parser.parse_args()
    snmp = NelkitSnmp(args)
    hostinfo = HostInfo(snmp)
    hostinfo.get_all()
    print('OS: %s' % hostinfo.os)
    print('Version: %s' % hostinfo.version)
    print('Vendor: %s' % hostinfo.vendor)
    print('Description: %s' % hostinfo.description)
コード例 #8
0
ファイル: snmp_deviceinfo.py プロジェクト: smolz/nelkit
def main():
    """run nk-snmp-deviceinfo."""
    argparser = SnmpArgs(description)
    args = argparser.parser.parse_args()
    snmp = NelkitSnmp(args)
    hostinfo = HostInfo(snmp)
    try:
        hostinfo.get_all()
        print('OS: %s' % hostinfo.os)
        print('Version: %s' % hostinfo.version)
        print('Vendor: %s' % hostinfo.vendor)
        print('Description: %s' % hostinfo.description)
    except Exception as e:
        print('ERROR: {0}'.format(e))
コード例 #9
0
ファイル: scanner.py プロジェクト: rlaneyjr/nesbi
    def _get_device_os(self, ip):
        dev = SnmpHandler(host=ip,
                          version=self.snmp_version,
                          community=self.snmp_community)
        hostinfo = HostInfo(dev)

        try:
            hostinfo.get_version()
            self.logger.info(f'{ip}: detected os "{hostinfo.os}"')
        except SnmpError as e:
            self.logger.error(f'{ip}: snmp-timeout')
            return False

        return hostinfo.os
コード例 #10
0
    def _get_device_os(self, ip):
        dev = SnmpHandler(host=ip, version=self.snmp_version, community=self.snmp_community)
        hostinfo = HostInfo(dev)

        try:
            hostinfo.get_version()
            self.logger.info(f'{ip}: detected os "{hostinfo.os}"')
        except SnmpError:
            self.logger.error(f'{ip}: snmp-timeout')
            return False

        if self.network_driver != '':
            self.logger.info(f'{ip}: custom configs overwrites os to: "{self.network_driver}"')
            return self.network_driver

        return hostinfo.os
コード例 #11
0
def test_hostinfo_get_location(declared_hostinfo):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev)
    hostinfo.get_location()
    assert hostinfo.location == 'Westeros'
コード例 #12
0
ファイル: check_environment.py プロジェクト: ogenstad/nelmon
def main():

    argparser = SnmpArguments(description)

    args = argparser.parser.parse_nelmon_args()
    snmp = NelmonSnmp(args)

    hostinfo = HostInfo(snmp)
    hostinfo.get_vendor()

    if hostinfo.vendor != 'cisco':
        nelmon_exit(C.UNKNOWN, '%s v%s only works with Cisco devices' % (C.CURRENT_PLUGIN, NelmonGlobals.PLUGIN_VERSION))

    env_tables = snmp.getnext(O.ciscoEnvMonObjects)
    volt_sensors = {}
    temp_sensors = {}
    fan_sensors = {}
    pw_sensors = {}
    s = SensorCollection()
    for env_table in env_tables:
        for oid, value in env_table:
            if O.ciscoEnvMonVoltageStatusTable in oid:
                sensor_id = oid.rsplit('.', 1)[-1]
                if not sensor_id in volt_sensors.keys():
                    volt_sensors[sensor_id] = Sensor(sensor_id)
                if O.ciscoEnvMonVoltageStatusDescr in oid:
                    volt_sensors[sensor_id].set_description(value)
                if O.ciscoEnvMonVoltageStatusValue in oid:
                    volt_sensors[sensor_id].set_value(value)
                if O.ciscoEnvMonVoltageState in oid:
                    volt_sensors[sensor_id].set_state(value)
            if O.ciscoEnvMonTemperatureStatusTable in oid:
                sensor_id = oid.rsplit('.', 1)[-1]
                if not sensor_id in temp_sensors.keys():
                    temp_sensors[sensor_id] = Sensor(sensor_id)
                if O.ciscoEnvMonTemperatureStatusDescr in oid:
                    temp_sensors[sensor_id].set_description(value)
                if O.ciscoEnvMonTemperatureStatusValue in oid:
                    temp_sensors[sensor_id].set_value(value)
                if O.ciscoEnvMonTemperatureState in oid:
                    temp_sensors[sensor_id].set_state(value)
            if O.ciscoEnvMonFanStatusTable in oid:
                sensor_id = oid.rsplit('.', 1)[-1]
                if not sensor_id in fan_sensors.keys():
                    fan_sensors[sensor_id] = Sensor(sensor_id)
                if O.ciscoEnvMonFanStatusDescr in oid:
                    fan_sensors[sensor_id].set_description(value)
                if O.ciscoEnvMonFanState in oid:
                    fan_sensors[sensor_id].set_state(value)
            if O.ciscoEnvMonSupplyStatusTable in oid:
                sensor_id = oid.rsplit('.', 1)[-1]
                if not sensor_id in pw_sensors.keys():
                    pw_sensors[sensor_id] = Sensor(sensor_id)
                if O.ciscoEnvMonSupplyStatusDescr in oid:
                    pw_sensors[sensor_id].set_description(value)
                if O.ciscoEnvMonSupplyState in oid:
                    pw_sensors[sensor_id].set_state(value)

    for sensor in volt_sensors:
        s.add_sensor('volt', volt_sensors[sensor].description, volt_sensors[sensor].state)
    for sensor in temp_sensors:
        s.add_sensor('temp', temp_sensors[sensor].description, temp_sensors[sensor].state)
    for sensor in fan_sensors:
        s.add_sensor('fan', fan_sensors[sensor].description, fan_sensors[sensor].state)
    for sensor in pw_sensors:
        s.add_sensor('pw', pw_sensors[sensor].description, pw_sensors[sensor].state)

    s.set_message()
    nelmon_exit(s.status, s.output)
コード例 #13
0
def test_hostinfo_get_description(declared_hostinfo):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev)
    hostinfo.get_description()
    assert hostinfo.description == 'Cisco Adaptive Security Appliance Version 9.3(2)2'
コード例 #14
0
ファイル: test_hostinfo.py プロジェクト: scopier/nelsnmp
def test_hostinfo():
    handler = SnmpHandler(host='1.1.1.1')
    hostinfo = HostInfo(handler)
    assert hostinfo.os is None
コード例 #15
0
ファイル: test_hostinfo.py プロジェクト: scopier/nelsnmp
def test_hostinfo_get_vendor(sysobject):
    handler = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(handler)
    hostinfo.get_vendor()
    assert hostinfo.vendor == 'cisco'
コード例 #16
0
        if m_args['level'] == "authPriv" and m_args['privacy'] is None:
            module.fail_json(
                msg='Privacy algorithm not set when using authPriv')

    nelsnmp_args = {}
    for key in m_args:
        if key in NELSNMP_PARAMETERS and m_args[key] is not None:
            nelsnmp_args[key] = m_args[key]

    try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception, err:
        module.fail_json(msg=str(err))

    hostinfo = HostInfo(dev)
    try:
        hostinfo.get_version()
    except Exception, err:
        module.fail_json(msg=str(err))

    results = {}
    results['ansible_device_vendor'] = hostinfo.vendor
    results['ansible_device_os'] = hostinfo.os
    results['ansible_device_version'] = hostinfo.version

    module.exit_json(ansible_facts=results)


main()
コード例 #17
0
ファイル: check_version.py プロジェクト: networklore/nelmon
def main():
    """Plugin: check_version."""
    argparser = SnmpArguments(description)
    argparser.parser.add_argument(
        '-d',
        help="Directory containing yaml version files")
    args = argparser.parser.parse_nelmon_args()
    snmp = NelmonSnmp(args)
    hostinfo = HostInfo(snmp)
    hostinfo.get_version()

    if hostinfo.version == 'UNKNOWN':
        exit_str = ['Unable to determine device version']
        exit_str.append('Vendor: %s' % hostinfo.vendor)
        exit_str.append('OS: %s' % hostinfo.os)
        exit_str.append('Version: %s' % hostinfo.version)
        exit_str.append('*******************************')
        exit_str.append("Your version of Nelsnmp does't support this device")
        exit_str.append('please check:')
        exit_str.append('http://networklore.com/nelsnmp-hostinfo/')
        nelmon_exit(C.UNKNOWN, exit_str)

    if args.d:
        yaml_file = '%s/%s_%s.yml' % (args.d, hostinfo.vendor, hostinfo.os)
    else:
        nelmon_exit(C.OK, hostinfo.version)

    if not os.path.isfile(yaml_file):
        nelmon_exit(C.UNKNOWN, 'Unable to find file: %s' % yaml_file)

    data = None
    with open(yaml_file, 'r') as f:
        data = yaml.load(f.read())

    if data is None:
        nelmon_exit(C.UNKNOWN, 'Unable to parse %s' % yaml_file)

    declared_versions = []
    for policy in data:
        if data[policy] is not None:
            for version in data[policy]:
                declared_versions.append(version)
                if hostinfo.version == version:
                    match_policy = policy

    hits = declared_versions.count(hostinfo.version)
    if hits == 0:
        nelmon_exit(
            C.UNKNOWN, '%s not declared in %s' % (hostinfo.version, yaml_file))
    elif hits > 1:
        nelmon_exit(
            C.UNKNOWN,
            '%s is declared  %s times in %s' % (hostinfo.version,
                                                hits, yaml_file))
    else:
        if match_policy == 'approved':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.OK,
                    '%s - %s' % (hostinfo.version,
                                 data[match_policy][hostinfo.version])
                )
            else:
                nelmon_exit(C.OK, hostinfo.version)
        elif match_policy == 'critical':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.CRITICAL,
                    '%s - %s (critical)' % (
                        hostinfo.version,
                        data[match_policy][hostinfo.version])
                )
            else:
                nelmon_exit(C.CRITICAL, '%s - (critical)' % hostinfo.version)
        elif match_policy == 'vulnerable':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.WARNING,
                    '%s - %s (vulnerable)' % (
                        hostinfo.version,
                        data[match_policy][hostinfo.version])
                )
            else:
                nelmon_exit(C.WARNING, '%s - (warning)' % hostinfo.version)
        elif match_policy == 'obsolete':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.WARNING,
                    '%s - %s (obsolete)' % (
                        hostinfo.version,
                        data[match_policy][hostinfo.version])
                )
            else:
                nelmon_exit(C.WARNING, '%s - (obsolete)' % hostinfo.version)
        else:
            nelmon_exit(C.UNKNOWN,
                        '%s is under unknown policy: %s' % (
                            hostinfo.version, match_policy))

    nelmon_exit(C.OK, hostinfo.version)
コード例 #18
0
            module.fail_json(msg='Username not set when using snmp version 3')

        if m_args['level'] == "authPriv" and m_args['privacy'] is None:
            module.fail_json(msg='Privacy algorithm not set when using authPriv')

    nelsnmp_args = {}
    for key in m_args:
        if key in NELSNMP_PARAMETERS and m_args[key] is not None:
            nelsnmp_args[key] = m_args[key]

    try:
        dev = SnmpHandler(**nelsnmp_args)
    except Exception, err:
        module.fail_json(msg=str(err))

    hostinfo = HostInfo(dev)
    try:
        hostinfo.get_version()
    except Exception, err:
        module.fail_json(msg=str(err))

    results = {}
    results['ansible_device_vendor'] = hostinfo.vendor
    results['ansible_device_os'] = hostinfo.os
    results['ansible_device_version'] = hostinfo.version


    module.exit_json(ansible_facts=results)


main()
コード例 #19
0
def test_hostinfo_get_contact(declared_hostinfo):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev)
    hostinfo.get_contact()
    assert hostinfo.contact == 'Networklore'
コード例 #20
0
def test_hostinfo_get_vendor_unknown(declared_hostinfo_unknown_vendor):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev)
    hostinfo.get_vendor()
    assert hostinfo.vendor == 'UNKNOWN'
コード例 #21
0
def test_hostinfo_get_version_known_vendor(declared_hostinfo):
    dev = SnmpHandler(host='1.1.1.1', version='2c', community='public')
    hostinfo = HostInfo(dev, vendor='cisco')
    hostinfo.get_version()
    assert hostinfo.version == '9.3(2)2'
コード例 #22
0
def main():
    argparser = SnmpArguments(description)
    argparser.parser.add_argument(
        '-d', help="Directory containing yaml version files")
    args = argparser.parser.parse_nelmon_args()
    snmp = NelmonSnmp(args)
    hostinfo = HostInfo(snmp)
    hostinfo.get_version()

    if hostinfo.version == 'UNKNOWN':
        exit_str = ['Unable to determine device version']
        exit_str.append('Vendor: %s' % hostinfo.vendor)
        exit_str.append('OS: %s' % hostinfo.os)
        exit_str.append('Version: %s' % hostinfo.version)
        exit_str.append('*******************************')
        exit_str.append("Your version of Nelsnmp does't support this device")
        exit_str.append('please check:')
        exit_str.append('http://networklore.com/nelsnmp-hostinfo/')
        nelmon_exit(C.UNKNOWN, exit_str)

    if args.d:
        yaml_file = '%s/%s_%s.yml' % (args.d, hostinfo.vendor, hostinfo.os)
    else:
        nelmon_exit(C.OK, hostinfo.version)

    if not os.path.isfile(yaml_file):
        nelmon_exit(C.UNKNOWN, 'Unable to find file: %s' % yaml_file)

    data = None
    with open(yaml_file, 'r') as f:
        data = yaml.load(f.read())

    if data is None:
        nelmon_exit(C.UNKNOWN, 'Unable to parse %s' % yaml_file)

    declared_versions = []
    for policy in data:
        if data[policy] is not None:
            for version in data[policy]:
                declared_versions.append(version)
                if hostinfo.version == version:
                    match_policy = policy

    hits = declared_versions.count(hostinfo.version)
    if hits == 0:
        nelmon_exit(C.UNKNOWN,
                    '%s not declared in %s' % (hostinfo.version, yaml_file))
    elif hits > 1:
        nelmon_exit(
            C.UNKNOWN, '%s is declared  %s times in %s' %
            (hostinfo.version, hits, yaml_file))
    else:
        if match_policy == 'approved':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.OK, '%s - %s' %
                    (hostinfo.version, data[match_policy][hostinfo.version]))
            else:
                nelmon_exit(C.OK, hostinfo.version)
        elif match_policy == 'critical':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.CRITICAL, '%s - %s (critical)' %
                    (hostinfo.version, data[match_policy][hostinfo.version]))
            else:
                nelmon_exit(C.CRITICAL, '%s - (critical)' % hostinfo.version)
        elif match_policy == 'vulnerable':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.WARNING, '%s - %s (vulnerable)' %
                    (hostinfo.version, data[match_policy][hostinfo.version]))
            else:
                nelmon_exit(C.WARNING, '%s - (warning)' % hostinfo.version)
        elif match_policy == 'obsolete':
            if data[match_policy][hostinfo.version]:
                nelmon_exit(
                    C.WARNING, '%s - %s (obsolete)' %
                    (hostinfo.version, data[match_policy][hostinfo.version]))
            else:
                nelmon_exit(C.WARNING, '%s - (obsolete)' % hostinfo.version)
        else:
            nelmon_exit(
                C.UNKNOWN, '%s is under unknown policy: %s' %
                (hostinfo.version, match_policy))

    nelmon_exit(C.OK, hostinfo.version)
コード例 #23
0
def test_hostinfo_invalid_parameter():
    with pytest.raises(ValueError):
        dev = 'Not an SnmpHandler'
        hostinfo = HostInfo(dev)
        hostinfo.get_version()