Esempio n. 1
0
    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
Esempio n. 2
0
 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
Esempio n. 3
0
    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
Esempio n. 4
0
 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
Esempio n. 5
0
    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)
Esempio n. 6
0
    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')
Esempio n. 8
0
 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')
Esempio n. 9
0
    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,
        )
Esempio n. 10
0
    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')
Esempio n. 11
0
    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
Esempio n. 12
0
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)
Esempio n. 14
0
 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)
Esempio n. 15
0
    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)
Esempio n. 17
0
    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
Esempio n. 18
0
 def __init__(self, name, init_config, agentConfig, instances):
     AgentCheck.__init__(self, name, init_config, agentConfig, instances)
     self.wmi_samplers = {}
     self.wmi_props = {}
Esempio n. 19
0
    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
Esempio n. 20
0
    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
Esempio n. 21
0
    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
Esempio n. 22
0
 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)
Esempio n. 23
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
Esempio n. 24
0
def test_log_critical_error():
    check = AgentCheck()

    with pytest.raises(NotImplementedError):
        check.log.critical('test')
Esempio n. 25
0
def test_load_config():
    assert AgentCheck.load_config("raw_foo: bar") == {'raw_foo': 'bar'}