# k8s/monitoring/dashboards/service-availability.yaml
# Active uptime/availability from blackbox-exporter probes — the signal that
# covers low-traffic services (Vault, MinIO, Longhorn UI) where RED metrics
# alone can't distinguish "idle" from "down".
apiVersion: v1
kind: ConfigMap
metadata:
  name: service-availability-dashboard
  namespace: logging
  labels:
    grafana_dashboard: "1"
data:
  service-availability.json: |
    {
      "title": "Service Availability & Certificate Expiration",
      "uid": "svc-availability",
      "schemaVersion": 39,
      "timezone": "browser",
      "time": { "from": "now-24h", "to": "now" },
      "refresh": "30s",
      "panels": [
        {
          "id": 1,
          "title": "Up / Down — all probed services",
          "type": "stat",
          "gridPos": { "h": 6, "w": 24, "x": 0, "y": 0 },
          "datasource": { "type": "prometheus", "uid": "prometheus" },
          "fieldConfig": {
            "defaults": {
              "mappings": [
                { "type": "value", "options": { "0": { "text": "DOWN", "color": "red" } } },
                { "type": "value", "options": { "1": { "text": "UP", "color": "green" } } }
              ],
              "thresholds": {
                "mode": "absolute",
                "steps": [
                  { "value": 0, "color": "red" },
                  { "value": 1, "color": "green" }
                ]
              }
            }
          },
          "targets": [
            { "expr": "probe_success", "legendFormat": "{{instance}}" }
          ]
        },
        {
          "id": 2,
          "title": "Uptime % trend",
          "type": "timeseries",
          "gridPos": { "h": 8, "w": 12, "x": 0, "y": 6 },
          "datasource": { "type": "prometheus", "uid": "prometheus" },
          "fieldConfig": { "defaults": { "unit": "percent", "max": 100, "min": 0 } },
          "targets": [
            {
              "expr": "avg_over_time(probe_success[$__rate_interval]) * 100",
              "legendFormat": "{{instance}}"
            }
          ]
        },
        {
          "id": 3,
          "title": "Probe latency",
          "type": "timeseries",
          "gridPos": { "h": 8, "w": 12, "x": 12, "y": 6 },
          "datasource": { "type": "prometheus", "uid": "prometheus" },
          "fieldConfig": { "defaults": { "unit": "s" } },
          "targets": [
            { "expr": "probe_duration_seconds", "legendFormat": "{{instance}}" }
          ]
        },
        {
          "id": 4,
          "title": "7-day SLO (% successful probes)",
          "type": "table",
          "gridPos": { "h": 8, "w": 24, "x": 0, "y": 14 },
          "datasource": { "type": "prometheus", "uid": "prometheus" },
          "fieldConfig": {
            "defaults": {
              "unit": "percent",
              "thresholds": {
                "mode": "absolute",
                "steps": [
                  { "value": 0, "color": "red" },
                  { "value": 99, "color": "yellow" },
                  { "value": 99.9, "color": "green" }
                ]
              }
            }
          },
          "targets": [
            {
              "expr": "avg_over_time(probe_success[7d]) * 100",
              "format": "table",
              "instant": true
            }
          ]
        },
        {
          "id": 5,
          "title": "Services DOWN right now",
          "type": "stat",
          "gridPos": { "h": 4, "w": 12, "x": 0, "y": 22 },
          "datasource": { "type": "prometheus", "uid": "prometheus" },
          "fieldConfig": {
            "defaults": {
              "thresholds": {
                "mode": "absolute",
                "steps": [
                  { "value": 0, "color": "green" },
                  { "value": 1, "color": "red" }
                ]
              }
            }
          },
          "targets": [
            { "expr": "count(probe_success == 0) OR on() vector(0)", "legendFormat": "down" }
          ]
        },
        {
          "id": 6,
          "title": "Certs expiring in < 14 days",
          "type": "stat",
          "gridPos": { "h": 4, "w": 12, "x": 12, "y": 22 },
          "datasource": { "type": "prometheus", "uid": "prometheus" },
          "fieldConfig": {
            "defaults": {
              "thresholds": {
                "mode": "absolute",
                "steps": [
                  { "value": 0, "color": "green" },
                  { "value": 1, "color": "red" }
                ]
              }
            }
          },
          "targets": [
            {
              "expr": "count((certmanager_certificate_expiration_timestamp_seconds - time()) / 86400 < 14) OR on() vector(0)",
              "legendFormat": "expiring"
            }
          ]
        },
        {
          "id": 7,
          "title": "Certificate expiry — days remaining",
          "type": "table",
          "gridPos": { "h": 8, "w": 24, "x": 0, "y": 26 },
          "datasource": { "type": "prometheus", "uid": "prometheus" },
          "fieldConfig": {
            "defaults": {
              "thresholds": {
                "mode": "absolute",
                "steps": [
                  { "value": 0, "color": "red" },
                  { "value": 14, "color": "yellow" },
                  { "value": 30, "color": "green" }
                ]
              }
            }
          },
          "targets": [
            {
              "expr": "(certmanager_certificate_expiration_timestamp_seconds - time()) / 86400",
              "legendFormat": "{{name}}",
              "format": "table",
              "instant": true
            }
          ]
        }
      ]
    }
