Example #1
0
def test_profile_sys_object_unknown(aggregator, caplog):
    """If the fetched sysObjectID is not referenced by any profiles, check fails."""
    caplog.set_level(logging.WARNING)

    unknown_sysobjectid = '1.2.3.4.5'
    init_config = {
        'profiles': {
            'profile1': {
                'definition': {
                    'metrics': common.SUPPORTED_METRIC_TYPES,
                    'sysobjectid': unknown_sysobjectid
                }
            }
        }
    }

    # Via config...

    instance = common.generate_instance_config([])
    check = SnmpCheck('snmp', init_config, [instance])
    check.check(instance)

    aggregator.assert_service_check("snmp.can_check",
                                    status=SnmpCheck.CRITICAL,
                                    tags=common.CHECK_TAGS,
                                    at_least=1)
    aggregator.all_metrics_asserted()

    # Via network discovery...

    host = socket.gethostbyname(common.HOST)
    network = ipaddress.ip_network(u'{}/29'.format(host),
                                   strict=False).with_prefixlen
    instance = {
        'name': 'snmp_conf',
        'network_address': network,
        'port': common.PORT,
        'community_string': 'public',
        'retries': 0,
        'discovery_interval': 0,
    }

    check = SnmpCheck('snmp', init_config, [instance])
    check._start_discovery()
    time.sleep(
        2)  # Give discovery a chance to fail finding a matching profile.
    check.check(instance)
    check._running = False
    check._thread.join()

    for record in caplog.records:
        if "Host {} didn't match a profile".format(host) in record.message:
            break
    else:
        pytest.fail()
def test_discovery(aggregator):
    host = socket.gethostbyname(common.HOST)
    network = ipaddress.ip_network(u'{}/29'.format(host),
                                   strict=False).with_prefixlen
    check_tags = [
        'snmp_device:{}'.format(host),
        'snmp_profile:profile1',
        'autodiscovery_subnet:{}'.format(to_native_string(network)),
    ]
    instance = {
        'name': 'snmp_conf',
        # Make sure the check handles bytes
        'network_address': to_native_string(network),
        'port': common.PORT,
        'community_string': 'public',
        'retries': 0,
        'discovery_interval': 0,
    }
    init_config = {
        'profiles': {
            'profile1': {
                'definition': {
                    'metrics': common.SUPPORTED_METRIC_TYPES,
                    'sysobjectid': '1.3.6.1.4.1.8072.*'
                }
            }
        }
    }
    check = SnmpCheck('snmp', init_config, [instance])
    try:
        for _ in range(30):
            check.check(instance)
            if len(aggregator.metric_names) > 1:
                break
            time.sleep(1)
            aggregator.reset()
    finally:
        check._running = False
        del check  # This is what the Agent would do when unscheduling the check.

    for metric in common.SUPPORTED_METRIC_TYPES:
        metric_name = "snmp." + metric['name']
        aggregator.assert_metric(metric_name, tags=check_tags, count=1)

    aggregator.assert_metric('snmp.sysUpTimeInstance')
    aggregator.assert_metric('snmp.discovered_devices_count',
                             tags=['network:{}'.format(network)])

    aggregator.assert_metric('snmp.devices_monitored',
                             metric_type=aggregator.GAUGE,
                             tags=check_tags)
    aggregator.assert_all_metrics_covered()
def test_discovery_devices_monitored_count(read_mock, aggregator):
    read_mock.return_value = '["192.168.0.1","192.168.0.2"]'

    host = socket.gethostbyname(common.HOST)
    network = ipaddress.ip_network(u'{}/29'.format(host),
                                   strict=False).with_prefixlen
    check_tags = [
        'autodiscovery_subnet:{}'.format(to_native_string(network)),
    ]
    instance = {
        'name': 'snmp_conf',
        # Make sure the check handles bytes
        'network_address': to_native_string(network),
        'port': common.PORT,
        'community_string': 'public',
        'retries': 0,
        'discovery_interval': 0,
    }
    init_config = {
        'profiles': {
            'profile1': {
                'definition': {
                    'metrics': common.SUPPORTED_METRIC_TYPES,
                    'sysobjectid': '1.3.6.1.4.1.8072.*'
                }
            }
        }
    }
    check = SnmpCheck('snmp', init_config, [instance])
    check.check(instance)
    check._running = False

    aggregator.assert_metric('snmp.discovered_devices_count',
                             tags=['network:{}'.format(network)])

    for device_ip in ['192.168.0.1', '192.168.0.2']:
        tags = check_tags + ['snmp_device:{}'.format(device_ip)]
        aggregator.assert_metric('snmp.devices_monitored',
                                 metric_type=aggregator.GAUGE,
                                 value=1,
                                 count=1,
                                 tags=tags)
    aggregator.assert_all_metrics_covered()
Example #4
0
def test_discovery(aggregator):
    host = socket.gethostbyname(common.HOST)
    network = ipaddress.ip_network(u'{}/29'.format(host),
                                   strict=False).with_prefixlen
    check_tags = ['snmp_device:{}'.format(host)]
    instance = {
        'name': 'snmp_conf',
        # Make sure the check handles bytes
        'network_address': network.encode('utf-8'),
        'port': common.PORT,
        'community_string': 'public',
    }
    init_config = {
        'profiles': {
            'profile1': {
                'definition': {
                    'metrics': common.SUPPORTED_METRIC_TYPES,
                    'sysobjectid': '1.3.6.1.4.1.8072.*'
                }
            }
        }
    }
    check = SnmpCheck('snmp', init_config, [instance])
    try:
        for _ in range(30):
            check.check(instance)
            if len(aggregator.metric_names) > 1:
                break
            time.sleep(1)
            aggregator.reset()
    finally:
        check._running = False

    for metric in common.SUPPORTED_METRIC_TYPES:
        metric_name = "snmp." + metric['name']
        aggregator.assert_metric(metric_name, tags=check_tags, count=1)

    aggregator.assert_metric('snmp.discovered_devices_count',
                             tags=['network:{}'.format(network)])
    aggregator.assert_all_metrics_covered()