# Grafana Notification Channels Configuration for Adtlas
# This file defines notification channels for alert delivery

apiVersion: 1

notifiers:
  # Email notification channel
  - name: email-alerts
    type: email
    uid: email_alerts
    org_id: 1
    is_default: true
    send_reminder: true
    frequency: "10m"
    disable_resolve_message: false
    settings:
      addresses: "admin@adtlas.com;ops@adtlas.com"
      subject: "[Adtlas Alert] {{ .GroupLabels.alertname }}"
      body: |
        {{ range .Alerts }}
        Alert: {{ .Annotations.summary }}
        Description: {{ .Annotations.description }}
        Severity: {{ .Labels.severity }}
        Team: {{ .Labels.team }}
        Started: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
        {{ if .EndsAt }}Ended: {{ .EndsAt.Format "2006-01-02 15:04:05" }}{{ end }}
        
        Labels:
        {{ range .Labels.SortedPairs }}
        - {{ .Name }}: {{ .Value }}
        {{ end }}
        
        {{ end }}
        
        Dashboard: {{ .ExternalURL }}

  # Slack notification channel (configure webhook URL)
  - name: slack-alerts
    type: slack
    uid: slack_alerts
    org_id: 1
    is_default: false
    send_reminder: true
    frequency: "5m"
    disable_resolve_message: false
    settings:
      url: "${SLACK_WEBHOOK_URL}"  # Set this environment variable
      username: "Adtlas Monitoring"
      channel: "#alerts"
      title: "Adtlas Alert: {{ .GroupLabels.alertname }}"
      text: |
        {{ range .Alerts }}
        *Alert:* {{ .Annotations.summary }}
        *Description:* {{ .Annotations.description }}
        *Severity:* {{ .Labels.severity }}
        *Team:* {{ .Labels.team }}
        *Started:* {{ .StartsAt.Format "2006-01-02 15:04:05" }}
        {{ if .EndsAt }}*Ended:* {{ .EndsAt.Format "2006-01-02 15:04:05" }}{{ end }}
        {{ end }}
      icon_emoji: ":warning:"
      color: |
        {{ if eq .Status "firing" }}
          {{ if eq .GroupLabels.severity "critical" }}danger{{ else }}warning{{ end }}
        {{ else }}good{{ end }}

  # Webhook notification channel for custom integrations
  - name: webhook-alerts
    type: webhook
    uid: webhook_alerts
    org_id: 1
    is_default: false
    send_reminder: false
    frequency: "1m"
    disable_resolve_message: false
    settings:
      url: "${WEBHOOK_URL}"  # Set this environment variable
      http_method: "POST"
      username: "${WEBHOOK_USERNAME}"
      password: "${WEBHOOK_PASSWORD}"
      title: "Adtlas Alert"
      body: |
        {
          "status": "{{ .Status }}",
          "alerts": [
            {{ range $index, $alert := .Alerts }}
            {{ if $index }},{{ end }}
            {
              "summary": "{{ $alert.Annotations.summary }}",
              "description": "{{ $alert.Annotations.description }}",
              "severity": "{{ $alert.Labels.severity }}",
              "team": "{{ $alert.Labels.team }}",
              "started_at": "{{ $alert.StartsAt.Format "2006-01-02T15:04:05Z07:00" }}",
              "ended_at": "{{ if $alert.EndsAt }}{{ $alert.EndsAt.Format "2006-01-02T15:04:05Z07:00" }}{{ end }}",
              "labels": {
                {{ range $key, $value := $alert.Labels }}
                "{{ $key }}": "{{ $value }}",
                {{ end }}
                "dummy": "dummy"
              }
            }
            {{ end }}
          ],
          "external_url": "{{ .ExternalURL }}",
          "group_labels": {
            {{ range $key, $value := .GroupLabels }}
            "{{ $key }}": "{{ $value }}",
            {{ end }}
            "dummy": "dummy"
          }
        }

  # PagerDuty notification channel for critical alerts
  - name: pagerduty-critical
    type: pagerduty
    uid: pagerduty_critical
    org_id: 1
    is_default: false
    send_reminder: true
    frequency: "2m"
    disable_resolve_message: false
    settings:
      integration_key: "${PAGERDUTY_INTEGRATION_KEY}"  # Set this environment variable
      severity: "critical"
      component: "Adtlas Application"
      group: "Infrastructure"
      summary: "{{ .GroupLabels.alertname }}: {{ .Annotations.summary }}"
      description: |
        {{ range .Alerts }}
        Alert: {{ .Annotations.summary }}
        Description: {{ .Annotations.description }}
        Severity: {{ .Labels.severity }}
        Team: {{ .Labels.team }}
        Started: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
        {{ end }}

  # Microsoft Teams notification channel
  - name: teams-alerts
    type: teams
    uid: teams_alerts
    org_id: 1
    is_default: false
    send_reminder: true
    frequency: "5m"
    disable_resolve_message: false
    settings:
      url: "${TEAMS_WEBHOOK_URL}"  # Set this environment variable
      title: "Adtlas Monitoring Alert"
      message: |
        {{ range .Alerts }}
        **Alert:** {{ .Annotations.summary }}
        
        **Description:** {{ .Annotations.description }}
        
        **Severity:** {{ .Labels.severity }}
        
        **Team:** {{ .Labels.team }}
        
        **Started:** {{ .StartsAt.Format "2006-01-02 15:04:05" }}
        
        {{ if .EndsAt }}**Ended:** {{ .EndsAt.Format "2006-01-02 15:04:05" }}{{ end }}
        {{ end }}

# Contact points for unified alerting (Grafana 8+)
contactPoints:
  - orgId: 1
    name: email-contact
    receivers:
      - uid: email_receiver
        type: email
        settings:
          addresses:
            - admin@adtlas.com
            - ops@adtlas.com
          subject: "[Adtlas] {{ .GroupLabels.alertname }}"
          body: |
            {{ range .Alerts }}
            Alert: {{ .Annotations.summary }}
            Description: {{ .Annotations.description }}
            Severity: {{ .Labels.severity }}
            Started: {{ .StartsAt.Format "2006-01-02 15:04:05" }}
            {{ end }}
        disableResolveMessage: false

  - orgId: 1
    name: slack-contact
    receivers:
      - uid: slack_receiver
        type: slack
        settings:
          url: "${SLACK_WEBHOOK_URL}"
          username: "Adtlas Monitoring"
          channel: "#alerts"
          title: "{{ .GroupLabels.alertname }}"
          text: |
            {{ range .Alerts }}
            *{{ .Annotations.summary }}*
            {{ .Annotations.description }}
            Severity: {{ .Labels.severity }}
            {{ end }}
        disableResolveMessage: false

# Notification policies
policies:
  - orgId: 1
    receiver: email-contact
    group_by:
      - alertname
      - severity
    group_wait: 10s
    group_interval: 5m
    repeat_interval: 12h
    routes:
      - receiver: slack-contact
        matchers:
          - severity = critical
        group_wait: 5s
        group_interval: 2m
        repeat_interval: 30m
        continue: true
      
      - receiver: email-contact
        matchers:
          - severity = warning
        group_wait: 30s
        group_interval: 10m
        repeat_interval: 6h
        continue: false