Beispiel #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)))
Beispiel #2
0
def test_zmon_view_urls(monkeypatch):
    zmon = Zmon(URL, token=TOKEN)

    # Checks
    check = {'id': 1}
    assert '{}#/check-definitions/view/1/'.format(
        URL) == zmon.check_definition_url(check)

    # Alerts
    alert = {'id': 1}
    assert '{}#/alert-details/1/'.format(URL) == zmon.alert_details_url(alert)

    # Dashboard
    dashboard_id = 1
    assert '{}#/dashboards/views/1/'.format(URL) == zmon.dashboard_url(
        dashboard_id)

    # Token
    token = '1234'
    assert '{}/tv/1234/'.format(URL) == zmon.token_login_url(token)

    # Grafana
    dashboard = {'id': 'grafana-dash'}
    assert '{}/visualization/dashboard/grafana-dash'.format(
        URL) == zmon.grafana_dashboard_url(dashboard)