terraform { required_providers { grafana = { source = "grafana/grafana" version = "4.27.0" } } } provider "grafana" { url = var.grafana_url auth = var.grafana_auth } resource "grafana_data_source" "api" { name = "api" type = "yesoreyeram-infinity-datasource" json_data_encoded = jsonencode({ datasource_mode = "basic" }) } resource "grafana_dashboard" "uptime" { config_json = jsonencode({ title = "uptime" uid = "uptime_dashboard" panels = [{ id = 1 type = "timeseries" title = "redis" gridPos = { h = 10, w = 16 } datasource = { uid = grafana_data_source.api.uid } targets = [{ type = "json" source = "url" url = "http://datasource:6060/api/v1/metrics" url_options = { params = [ { key = "service", value = "redis" }, { key = "t0", value = "$${__from:date:seconds}" }, { key = "t1", value = "$${__to:date:seconds}" } ] } columns = [ { selector = "timestamp_sec", text = "time", type = "timestamp_epoch_s" }, { selector = "up", type = "number" } ] }] }] }) } resource "grafana_folder" "rules" { title = "rules" } resource "grafana_rule_group" "alert_rule" { name = "alert rule" folder_uid = grafana_folder.rules.uid interval_seconds = 10 rule { name = "down" condition = "B" for = "30s" annotations = { "__dashboardUid__" = grafana_dashboard.uptime.uid "__panelId__" = "1" "description" = "redis has been down for over 30 seconds" } data { ref_id = "A" datasource_uid = grafana_data_source.api.uid model = jsonencode({ type = "json" source = "url" parser = "backend" format = "timeseries" url = "http://datasource:6060/api/v1/metrics" url_options = { params = [ { key = "service", value = "redis" }, { key = "t0", value = "$${__from:date:seconds}" }, { key = "t1", value = "$${__to:date:seconds}" } ] } columns = [ { selector = "timestamp_sec", text = "time", type = "timestamp_epoch_s" }, { selector = "up", type = "number" } ] }) relative_time_range { from = 60 to = 0 } } data { ref_id = "B" datasource_uid = "__expr__" relative_time_range { from = 0 to = 0 } model = jsonencode({ type = "classic_conditions" conditions = [{ evaluator = { params = [1], type = "lt" }, reducer = { type = "last" }, query = { params = ["A"] } }] }) } } } resource "grafana_contact_point" "telegram" { name = "telegram" telegram { token = var.telegram_bot_token chat_id = var.telegram_chat_id } } resource "grafana_notification_policy" "main" { group_by = ["..."] contact_point = grafana_contact_point.telegram.name }