#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# Copyright (C) 2019 tribe29 GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# NOTE: Careful when replacing the *-import below with a more specific import. This can cause
# problems because it might remove variables from the check-context which are necessary for
# resolving legacy discovery results such as [("SUMMARY", "diskstat_default_levels")]. Furthermore,
# it might also remove variables needed for accessing discovery rulesets.
from cmk.base.check_legacy_includes.azure import *  # pylint: disable=wildcard-import,unused-wildcard-import

factory_settings["levels_azure_sites"] = {
    # https://www.nngroup.com/articles/response-times-3-important-limits/
    "avg_response_time_levels": (1.0, 10.0),
    # https://www.unigma.com/2016/07/11/best-practices-for-monitoring-microsoft-azure/
    "error_rate_levels": (0.01, 0.04),
    "cpu_time_percent_levels": (85.0, 95.0),
}

_AZURE_SITES_METRICS = (  # metric_key, cmk_key, display_name, use_rate_flag
    ("total_CpuTime", "cpu_time_percent", "CPU time", True),
    ("total_AverageResponseTime", "avg_response_time", "Average response time", False),
    ("total_Http5xx", "error_rate", "Rate of server errors", True),
)


@get_data_or_go_stale
def check_azure_sites(_item, params, resource):

    for key, cmk_key, displ, use_rate in _AZURE_SITES_METRICS:
        levels = params.get("%s_levels" % cmk_key, (None, None))
        mcheck = check_azure_metric(resource, key, cmk_key, displ, levels=levels, use_rate=use_rate)
        if mcheck:
            yield mcheck

    for kv_pair in azure_iter_informative_attrs(resource):
        yield 0, "%s: %s" % kv_pair


check_info["azure_sites"] = {
    "parse_function": parse_azure,
    "inventory_function": discover(),
    "check_function": check_azure_sites,
    "has_perfdata": True,
    "service_description": "Site %s",
    "default_levels_variable": "levels_azure_sites",
    "group": "webserver",
}
