def test_fix_case(self): check = AgentCheck() metric_name = u'Klüft inför på fédéral' normalized_metric_name = b'kluft_infor_pa_federal' assert check.normalize(metric_name, fix_case=True) == normalized_metric_name
def test_duplicated_device_name(self): check = AgentCheck() tags = [] device_name = 'foo' check._normalize_tags_type(tags, device_name) normalized_tags = check._normalize_tags_type(tags, device_name) assert len(normalized_tags) == 1
def test_prefix_unicode_metric_bytes(self): check = AgentCheck() metric_name = b'metric' prefix = u'some' normalized_metric_name = b'some.metric' assert check.normalize(metric_name, prefix=prefix) == normalized_metric_name
def test__to_bytes(self): if PY3: pytest.skip('Method only exists on Python 2') check = AgentCheck() assert isinstance(check._to_bytes(b"tag:foo"), bytes) assert isinstance(check._to_bytes(u"tag:☣"), bytes) in_str = mock.MagicMock(side_effect=Exception) in_str.encode.side_effect = Exception assert check._to_bytes(in_str) is None
def test_unicode_device_name(self): check = AgentCheck() tags = [] device_name = u'unicode_string' normalized_tags = check._normalize_tags_type(tags, device_name) normalized_device_tag = normalized_tags[0] assert isinstance(normalized_device_tag, str if PY3 else bytes)
def test_default_string(self): check = AgentCheck() tag = 'default:string' tags = [tag] normalized_tags = check._normalize_tags_type(tags, None) normalized_tag = normalized_tags[0] assert normalized_tags is not tags # Ensure no new allocation occurs assert normalized_tag is tag
def test_valid_event(self, aggregator): check = AgentCheck() event = { "event_type": "new.event", "msg_title": "new test event", "aggregation_key": "test.event", "msg_text": "test event test event", "tags": None } check.event(event) aggregator.assert_event('test event test event')
def test_valid_event(self, aggregator): check = AgentCheck() event = { "timestamp": 123456789, "event_type": "new.event", "source_type_name": "new.source.type", "msg_title": "new test event", "aggregation_key": "test.event", "msg_text": "test event test event", "tags": None } check.event(event) aggregator.assert_event('test event test event')
def test_valid_sc(self, aggregator): check = AgentCheck() check.service_check("testservicecheck", AgentCheck.OK, tags=None, message="") aggregator.assert_service_check("testservicecheck", status=AgentCheck.OK) check.service_check("testservicecheckwithhostname", AgentCheck.OK, tags=["foo", "bar"], hostname="testhostname", message="a message") aggregator.assert_service_check("testservicecheckwithhostname", status=AgentCheck.OK, tags=["foo", "bar"], hostname="testhostname", message="a message") check.service_check("testservicecheckwithnonemessage", AgentCheck.OK, message=None) aggregator.assert_service_check( "testservicecheckwithnonemessage", status=AgentCheck.OK, )
def test_unicode_string(self): check = AgentCheck() tag = u'unicode:string' tags = [tag] normalized_tags = check._normalize_tags_type(tags, None) normalized_tag = normalized_tags[0] assert normalized_tags is not tags if PY3: # Ensure no new allocation occurs assert normalized_tag is tag else: assert normalized_tag == tag.encode('utf-8')
def test_bytes_string(self): check = AgentCheck() tag = b'bytes:string' tags = [tag] normalized_tags = check._normalize_tags_type(tags, None) normalized_tag = normalized_tags[0] assert normalized_tags is not tags if PY3: assert normalized_tag == tag.decode('utf-8') else: # Ensure no new allocation occurs assert normalized_tag is tag
def test_instance(): """ Simply assert the class can be instantiated """ AgentCheck() # rely on default check = AgentCheck() assert check.init_config == {} assert check.instances == [] # pass dict for 'init_config', a list for 'instances' init_config = {'foo': 'bar'} instances = [{'bar': 'baz'}] check = AgentCheck(init_config=init_config, instances=instances) assert check.init_config == {'foo': 'bar'} assert check.instances == [{'bar': 'baz'}]
def __init__(self, name, init_config, agentConfig, instances=None): self.NAMESPACE = 'kubernetes' if instances is not None and len(instances) > 1: raise Exception( 'Kubelet check only supports one configured instance.') inst = instances[0] if instances else None self.cluster_name = AgentCheck.get_cluster_name() cadvisor_instance = self._create_cadvisor_prometheus_instance(inst) kubelet_instance = self._create_kubelet_prometheus_instance(inst) generic_instances = [cadvisor_instance, kubelet_instance] super(KubeletCheck, self).__init__(name, init_config, agentConfig, generic_instances) self.cadvisor_legacy_port = inst.get('cadvisor_port', CADVISOR_DEFAULT_PORT) self.cadvisor_legacy_url = None self.cadvisor_scraper_config = self.get_scraper_config( cadvisor_instance) # Filter out system slices (empty pod name) to reduce memory footprint self.cadvisor_scraper_config['_text_filter_blacklist'] = [ 'pod_name=""' ] self.kubelet_scraper_config = self.get_scraper_config(kubelet_instance)
def test_topology_event(self, telemetry): check = AgentCheck() event = { "timestamp": 123456789, "event_type": "new.event", "source_type_name": "new.source.type", "msg_title": "new test event", "aggregation_key": "test.event", "msg_text": "test event test event", "tags": None, "context": { "element_identifiers": ["urn:test:/value"], "source": "test source", "category": "test category", } } check.event(event) telemetry.assert_topology_event(event)
def test_metric_limit_instance_config(self, aggregator): instances = [{ "max_returned_metrics": 42, }] check = AgentCheck("test", {}, instances) assert check.get_warnings() == [] for i in range(0, 42): check.gauge("metric", 0) assert len(check.get_warnings()) == 0 assert len(aggregator.metrics("metric")) == 42 check.gauge("metric", 0) assert len(check.get_warnings()) == 1 assert len(aggregator.metrics("metric")) == 42
def test_topology_event(self, telemetry): check = AgentCheck() event = { "timestamp": 123456789, "source_type_name": "new.source.type", "msg_title": "new test event", "aggregation_key": "test.event", "msg_text": "test event test event", "tags": [], "context": { "element_identifiers": ["urn:test:/value"], "source": "test source", "category": "test category", } } check.event(event) # event_type key and value should be generated by event method expected_event = event.copy() expected_event["event_type"] = "new.source.type" telemetry.assert_topology_event(expected_event)
def __init__(self, name, init_config, agentConfig, instances=None): AgentCheck.__init__(self, name, init_config, agentConfig, instances) self.log.setLevel(logging.DEBUG) self.account_id = None self.nagios_tails = {} check_freq = init_config.get("check_freq", 15) if instances is not None: for instance in instances: tailers = [] nagios_conf = {} instance_key = None if 'nagios_conf' in instance: # conf.d check conf_path = instance['nagios_conf'] nagios_conf = self.parse_nagios_config(conf_path) instance_key = conf_path # Retro compatibility Code elif 'nagios_perf_cfg' in instance: conf_path = instance['nagios_perf_cfg'] nagios_conf = self.parse_nagios_config(conf_path) instance["collect_host_performance_data"] = True instance["collect_service_performance_data"] = True instance_key = conf_path if 'nagios_log' in instance: nagios_conf["log_file"] = instance['nagios_log'] if instance_key is None: instance_key = instance['nagios_log'] # End of retro compatibility code if not nagios_conf: self.log.warning("Missing path to nagios_conf") continue if 'log_file' in nagios_conf and instance.get( 'collect_events', True): self.log.debug("Starting to tail the event log") tailers.append( NagiosEventLogTailer(log_path=nagios_conf['log_file'], file_template=None, logger=self.log, hostname=self.hostname, event_func=self.event, gauge_func=self.gauge, freq=check_freq, passive_checks=instance.get( 'passive_checks_events', False))) if 'host_perfdata_file' in nagios_conf and \ 'host_perfdata_file_template' in nagios_conf and \ instance.get('collect_host_performance_data', False): self.log.debug("Starting to tail the host_perfdata file") tailers.append( NagiosHostPerfDataTailer( log_path=nagios_conf['host_perfdata_file'], file_template=nagios_conf[ 'host_perfdata_file_template'], logger=self.log, hostname=self.hostname, event_func=self.event, gauge_func=self.gauge, freq=check_freq)) if 'service_perfdata_file' in nagios_conf and \ 'service_perfdata_file_template' in nagios_conf and \ instance.get('collect_service_performance_data', False): self.log.debug( "Starting to tail the service_perfdata file") tailers.append( NagiosServicePerfDataTailer( log_path=nagios_conf['service_perfdata_file'], file_template=nagios_conf[ 'service_perfdata_file_template'], logger=self.log, hostname=self.hostname, event_func=self.event, gauge_func=self.gauge, freq=check_freq)) self.nagios_tails[instance_key] = tailers self.account_id = self.hostname
def __init__(self, name, init_config, agentConfig, instances): AgentCheck.__init__(self, name, init_config, agentConfig, instances) self.wmi_samplers = {} self.wmi_props = {}
def test_underscores_at_ends(self): check = AgentCheck() metric_name = u'_some_underscores_' normalized_metric_name = b'some_underscores' assert check.normalize(metric_name) == normalized_metric_name
def test_underscores_redundant(self): check = AgentCheck() metric_name = u'a_few__redundant___underscores' normalized_metric_name = b'a_few_redundant_underscores' assert check.normalize(metric_name) == normalized_metric_name
def test_underscores_and_dots(self): check = AgentCheck() metric_name = u'some_.dots._and_._underscores' normalized_metric_name = b'some.dots.and.underscores' assert check.normalize(metric_name) == normalized_metric_name
def test_non_float_metric(self, aggregator): check = AgentCheck() metric_name = 'test_metric' with pytest.raises(ValueError): check.gauge(metric_name, '85k') aggregator.assert_metric(metric_name, count=0)
def test_default(self): check = AgentCheck() metric_name = u'Klüft inför på fédéral' normalized_metric_name = b'Kluft_infor_pa_federal' assert check.normalize(metric_name) == normalized_metric_name
def test_log_critical_error(): check = AgentCheck() with pytest.raises(NotImplementedError): check.log.critical('test')
def test_load_config(): assert AgentCheck.load_config("raw_foo: bar") == {'raw_foo': 'bar'}