Esempio n. 1
0
def validate_sli_source(config, source, ignore_keys=False):
    if 'zmon_url' not in config:
        config = set_config_file()

    zmon = Zmon(config['zmon_url'], token=zign.api.get_token('uid', ['uid']))

    check_id = int(source['check_id'])

    try:
        check = zmon.get_check_definition(check_id)
    except Exception:
        raise SLRClientError(
            'Check definition {} does not seem to exist!'.format(check_id))

    alerts = zmon.get_alert_definitions()
    filtered = [
        alert for alert in alerts if alert['check_definition_id'] == check_id
    ]
    if not filtered:
        raise SLRClientError(
            'Check definition has no alerts. Please create an Alert for this check on ZMON {}'
            .format(zmon.check_definition_url(check)))

    if ignore_keys:
        return

    keys = [k for k in source['keys'] if '.*' not in k]
    if not keys:
        # Do not validate keys if we have wildcards
        return

    sli_exists = False
    sample_data = set()
    for alert in filtered:
        if sli_exists:
            break

        alert_data = zmon.get_alert_data(alert['id'])

        values = {
            v['entity']: v['results'][0]['value']
            for v in alert_data if len(v['results'])
        }

        for entity, data in values.items():
            if type(data) is dict:
                flattened = flatten(data)

                data_keys = flattened.keys()
                sample_data.update(list(data_keys))

                if not (set(keys) - set(data_keys)):
                    sli_exists = True
                    break

    if not sli_exists:
        raise SLRClientError(
            'Some SLI keys do not exist. Please check the data returned from the check and the corresponding keys '
            'in the SLI source. Found the following keys returned from check {}: {}'
            .format(check_id, set(sample_data)))
Esempio n. 2
0
def test_zmon_alert_data(monkeypatch):
    get = MagicMock()
    result = {'entity-1': []}
    get.return_value.json.return_value = result

    monkeypatch.setattr('requests.Session.get', get)

    zmon = Zmon(URL, token=TOKEN)

    check = zmon.get_alert_data(1)

    assert check == result

    get.assert_called_with(zmon.endpoint(client.ALERT_DATA, 1, 'all-entities'))
Esempio n. 3
0
def validate_sli(config, data_source):
    if 'zmon_url' not in config:
        config = set_config_file()

    zmon = Zmon(config['zmon_url'], token=zign.api.get_token('uid', ['uid']))

    check_id = int(data_source['definition']['check_id'])

    try:
        zmon.get_check_definition(check_id)
    except:
        raise SLRClientError(
            'Check definition {} does not seem to exist!'.format(check_id))

    alerts = zmon.get_alert_definitions()
    filtered = [
        alert for alert in alerts if alert['check_definition_id'] == check_id
    ]
    if not filtered:
        raise SLRClientError(
            'Check definition has no alerts. Please create an Alert for this check on ZMON.'
        )

    keys = data_source['definition']['keys']
    sli_exists = False
    sample_data = {}
    for alert in filtered:
        if sli_exists:
            break
        alert_data = zmon.get_alert_data(alert['id'])

        values = {
            v['entity']: v['results'][0]['value']
            for v in alert_data if len(v['results'])
        }

        for entity, data in values.items():
            if type(data) is dict:
                data_keys = data.keys()
                if data_keys:
                    sample_data = data_keys
                if not (set(keys) - set(data_keys)):
                    sli_exists = True
                    break

    if not sli_exists:
        raise SLRClientError(
            'Some SLI keys do not exist. Please check the data returned from the check and the corresponding keys '
            'in the data-source. Found the following keys returned from check {}: {}'
            .format(check_id, set(sample_data)))