<% macro make_alert(extensions, tags, mgroup, warn_threshold, crit_threshold) -%>
    <% set alert_base = {
        "juggler_host": "ntp_{mgroups[0]}_{geo}",
        "juggler_tags": [
            "auto_downtime",
            "ntp"
        ],
        "juggler_base": {
            "namespace": "RTC",
            "aggregator": "logic_or",
            "aggregator_kwargs": {
                "nodata_mode": "force_crit"
            },
            "flaps": {
                "boost": 0,
                "critical": 0,
                "stable": 300
            }
        },
        "mgroups": [mgroup],
        "warn": [warn_threshold, crit_threshold],
        "crit": [crit_threshold, None],
        "value_modify": {
            "type": "min",
            "window": 300
        }
    } %>
    <<Alert(base=alert_base|combine(extensions), tags=tags)>>
<%- endmacro %>

<% set ntp_offset = {
    "signal": "hperc(ntpd-clock_offset_thhh, 20, inf)",
    "abc": "srertc",
    "name_tmpl": "{mgroups[0]}_{geo}_ntp_offset",
    "juggler_service": "ntp_offset",
    "description_tmpl": "% of hosts with ntp offset more than 20ms at {mgroups[0]} {geo}"
} %>

<% set max_ntp_offset = {
    "signal": "quant(ntpd-clock_offset_thhh, 999)",
    "abc": "srertc",
    "name_tmpl": "{mgroups[0]}_{geo}_max_ntp_offset",
    "juggler_service": "max_ntp_offset",
    "description_tmpl": "99.9% of ntp offset at {mgroups[0]} {geo}"
} %>

[
<% for tags in list_tags_combinations('runtimecloud', fill_tags=['geo']) %>
<% for mgroup in ['ASEARCH', 'QLOUD'] %>
    <% if tags['geo'] == 'msk' %>
    << make_alert(ntp_offset, tags, mgroup, 0.5, 1) >>,
    << make_alert(max_ntp_offset, tags, mgroup, 50, 100) >>,
    <% elif tags['geo'] == 'sas' %>
    << make_alert(ntp_offset, tags, mgroup, 0.5, 1) >>,
    << make_alert(max_ntp_offset, tags, mgroup, 50, 100) >>,
    <% elif tags['geo'] == 'man' %>
    << make_alert(ntp_offset, tags, mgroup, 1, 2) >>,
    << make_alert(max_ntp_offset, tags, mgroup, 50, 100) >>,
    <% elif tags['geo'] == 'vla' %>
    << make_alert(ntp_offset, tags, mgroup, 0.25, 0.5) >>,
    << make_alert(max_ntp_offset, tags, mgroup, 50, 100) >>,
    <% endif %>
<% endfor %>
<% endfor %>
]
