def test_cache_building(write_mock, read_mock): instance = common.generate_instance_config(common.SUPPORTED_METRIC_TYPES) instance.pop('ip_address') read_mock.return_value = '[]' discovered_instance = instance.copy() discovered_instance['ip_address'] = '192.168.0.1' instance['network_address'] = '192.168.0.0/31' check = SnmpCheck('snmp', {}, [instance]) check._config.discovered_instances['192.168.0.1'] = InstanceConfig( discovered_instance) check._start_discovery() try: for _ in range(30): if write_mock.call_count: break time.sleep(0.5) finally: check._running = False write_mock.assert_called_once_with('', '["192.168.0.1"]')
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) common.assert_common_metrics(aggregator) 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_cache_loading_tags(thread_mock, read_mock): """When loading discovered instances from cache, tags don't leak from one to the others.""" read_mock.return_value = '["192.168.0.1", "192.168.0.2"]' instance = common.generate_instance_config(common.SUPPORTED_METRIC_TYPES) instance.pop('ip_address') instance['network_address'] = '192.168.0.0/29' instance['discovery_interval'] = 0 instance['tags'] = ['test:check'] check = SnmpCheck('snmp', {}, [instance]) check._start_discovery() config = check._config.discovered_instances['192.168.0.2'] assert set(config.tags) == {'snmp_device:192.168.0.2', 'test:check'}
def test_removing_host(): """If a discovered host is failing 3 times in a row, we stop querying it.""" instance = common.generate_instance_config(common.SUPPORTED_METRIC_TYPES) discovered_instance = instance.copy() discovered_instance['ip_address'] = '1.1.1.1' discovered_instance['retries'] = 0 instance.pop('ip_address') instance['network_address'] = '192.168.0.0/24' check = SnmpCheck('snmp', {}, [instance]) warnings = [] check.warning = warnings.append check._config.discovered_instances['1.1.1.1'] = InstanceConfig( discovered_instance) msg = 'No SNMP response received before timeout for instance 1.1.1.1' check._start_discovery = lambda: None check._executor = futures.ThreadPoolExecutor(max_workers=1) check.check(instance) assert warnings == [msg] check.check(instance) assert warnings == [msg, msg] check.check(instance) assert warnings == [msg, msg, msg] # Instance has been removed assert check._config.discovered_instances == {} check.check(instance) # No new warnings produced assert warnings == [msg, msg, msg]