<% set notifications = [
    {
        "template_name": "on_status_change",
        "template_kwargs": {
            "status": [
                "CRIT"
            ],
            "login": [
                "rtc-networking",
            ],
            "method": [
                "telegram"
            ]
        }
    }
] %>

<% macro make_alert(mgroup, host_prfx, signal_name, signal, dc, warn_threshold, crit_threshold) -%>
    <% set alert_base = {
      "abc": "infracloudnetwork",
			"juggler_base": {
					"namespace": "RTC",
					"notifications": notifications,
					"aggregator": "logic_or",
					"aggregator_kwargs": {
							"nodata_mode": "force_crit"
					},
					"flaps": {
						"stable": 180,
						"critical": 900,
						"boost": 0,
					}
			},
      "juggler_host": "{}_{}_{}".format(host_prfx, mgroup, dc),
      "juggler_tags": ["{}".format(host_prfx)],
			"tags": {
				"itype": "runtimecloud",
				"geo": dc,
      },
			"value_modify": {
					"window": 300,
					"type": "aver",
			},
      "name_tmpl": "{}_{}_{}_{}_count".format(host_prfx, mgroup, dc, signal_name),
      "signal": signal,
			"mgroups": [mgroup],
			"warn": warn_threshold,
			"crit": crit_threshold,
      "juggler_service": "{}_count".format(signal_name),
      "description_tmpl": "{} count".format(signal_name) }%>
    << Alert(base=alert_base) >>
<%- endmacro %>

<% set req_count = "sum(push-dnsmanager-coredns-view_all_in_AAAA_tmmm,push-dnsmanager-coredns-view_all_in_A_tmmm,push-dnsmanager-coredns-view_all_in_PTR_tmmm,push-dnsmanager-coredns-view_all_in_SOA_tmmm,push-dnsmanager-coredns-view_all_in_MX_tmmm,push-dnsmanager-coredns-view_all_in_NS_tmmm)" %>

<% set resp_err_count = "sum(push-dnsmanager-coredns-view_all_SERVFAIL_tmmm,push-dnsmanager-coredns-view_syn_SERVFAIL_tmmm,push-dnsmanager-coredns-view_ycyn_SERVFAIL_tmmm,push-dnsmanager-coredns-view_dns64_SERVFAIL_tmmm)" %>

<% set status_fail_count = "sum(push-dnsmanager-named-exit_by_error_tmmm, push-dnsmanager-named-exit_by_self_tmmm, push-dnsmanager-named-exit_by_signal_tmmm, push-dnsmanager-named-stopping_tmmm, push-dnsmanager-named-starting_tmmm)" %>

<% set upstream_hc_error = "sum(push-dnsmanager-coredns-fwd_dns_cache_healthcheck_failure_tmmm,push-dnsmanager-coredns-fwd_ns_cache_healthcheck_failure_tmmm,push-dnsmanager-coredns-fwd_ns_cache_sas_healthcheck_failure_tmmm,push-dnsmanager-coredns-fwd_ns_cache_man_healthcheck_failure_tmmm,push-dnsmanager-coredns-fwd_ns_cache_vla_healthcheck_failure_tmmm,push-dnsmanager-coredns-fwd_yp_dns1_healthcheck_failure_tmmm,push-dnsmanager-coredns-fwd_yp_dns2_healthcheck_failure_tmmm,push-dnsmanager-coredns-fwd_dns64_healthcheck_failure_tmmm)" %>

<% set resp_time_1000ms = "hperc(hmerge(push-dnsmanager-coredns-view_ycyn_duration_in_AAAA_hgram, push-dnsmanager-coredns-view_syn_duration_in_AAAA_hgram, push-dnsmanager-coredns-view_all_duration_in_AAAA_hgram), 1.024, inf)" %>

<% set signals = [
    {
        "host_prfx": "localcache",
        "name": "resp_fail",
        "signal": "perc({}, {})".format(resp_err_count, req_count),
        "thresholds": {
            "msk":  [[1, 5], [5, None]],
            "man":  [[1, 3], [3, None]],
            "sas":  [[1, 2], [2, None]],
            "vla":  [[1, 2], [2, None]]},
    },
    {
        "host_prfx": "localcache",
        "name": "status_fail",
        "signal": status_fail_count,
        "thresholds": {
            "msk":  [[1, 100], [100, None]],
            "man":  [[1, 100], [100, None]],
            "sas":  [[1, 100], [100, None]],
            "vla":  [[1, 100], [100, None]]},
    },
    {
        "host_prfx": "localcache",
        "name": "upstream_hc_error",
        "signal": upstream_hc_error,
        "thresholds": {
            "msk":  [[1000, 2000], [2000, None]],
            "man":  [[1000, 2000], [2000, None]],
            "sas":  [[1000, 2000], [2000, None]],
            "vla":  [[1000, 2000], [2000, None]]},
    },
    {
        "host_prfx": "localcache",
        "name": "resp_time_1000ms",
        "signal": resp_time_1000ms,
        "thresholds": {
            "msk":  [[0.1, 0.2], [0.2, None]],
            "man":  [[0.1, 0.2], [0.2, None]],
            "sas":  [[0.1, 0.2], [0.2, None]],
            "vla":  [[0.1, 0.2], [0.2, None]]},
    },
] %>

[
<% for data in signals %>
<% for dc in data["thresholds"] %>
<< make_alert('ASEARCH', data['host_prfx'], data['name'], data['signal'], dc, data["thresholds"][dc][0], data["thresholds"][dc][1]) >>,
<% endfor %>
<% endfor %>
]
