apiVersion: 1

# ---------- Internet / ISP — uses contact point + policy from alerting.yaml ----------
groups:
  - orgId: 1
    name: internet
    folder: Homelab Alerts
    interval: 1m
    rules:

      - uid: isp-internet-down
        title: Internet down
        condition: C
        for: 2m
        noDataState: OK
        execErrState: Error
        labels: {severity: critical}
        annotations:
          summary: 'Internet was unreachable (no answer from 1.1.1.1, 8.8.8.8 and 9.9.9.9). This message may arrive only once the line is back.'
        data:
          - refId: A
            relativeTimeRange: {from: 300, to: 0}
            datasourceUid: prometheus
            model: {refId: A, instant: true, expr: 'max(probe_success{job="isp-ping", probe="internet"})'}
          - refId: C
            datasourceUid: __expr__
            model:
              refId: C
              type: threshold
              expression: A
              conditions: [{evaluator: {type: lt, params: [1]}}]

      - uid: isp-high-latency
        title: Internet slow (high ping)
        condition: C
        for: 15m
        noDataState: OK
        execErrState: Error
        labels: {severity: warning}
        annotations:
          summary: 'Average ping to the internet is {{ printf "%.0f" $values.A.Value }} ms for 15 minutes'
        data:
          - refId: A
            relativeTimeRange: {from: 600, to: 0}
            datasourceUid: prometheus
            model: {refId: A, instant: true, expr: 'avg(avg_over_time(probe_icmp_duration_seconds{job="isp-ping", probe="internet", phase="rtt"}[5m])) * 1000'}
          - refId: C
            datasourceUid: __expr__
            model:
              refId: C
              type: threshold
              expression: A
              conditions: [{evaluator: {type: gt, params: [100]}}]

      - uid: isp-speedtest-slow
        title: Internet slower than contract
        condition: C
        for: 0s
        noDataState: OK
        execErrState: Error
        labels: {severity: warning}
        annotations:
          summary: 'Speed test: {{ printf "%.0f" $values.A.Value }} Mbit/s download, below half of the 777 Mbit/s contract'
        data:
          - refId: A
            relativeTimeRange: {from: 600, to: 0}
            datasourceUid: prometheus
            model: {refId: A, instant: true, expr: '(speedtest_download_bits_per_second > 0) / 1e6'}
          - refId: C
            datasourceUid: __expr__
            model:
              refId: C
              type: threshold
              expression: A
              conditions: [{evaluator: {type: lt, params: [388.5]}}]
