{{- if and .Values.exporter.enabled .Values.exporter.prometheusRule.enabled }}
apiVersion: monitoring.coreos.com/v1
kind: PrometheusRule
metadata:
  name: {{ template "redis-ha.fullname" . }}
  {{- if .Values.exporter.prometheusRule.namespace }}
  namespace: {{ .Values.exporter.prometheusRule.namespace }}
  {{- else }}
  namespace: {{ .Release.Namespace }}
  {{- end }}
  labels:
{{ include "labels.standard" . | indent 4 }}
spec:
  {{- if eq (len .Values.exporter.prometheusRule.rules) 0 }}
  groups:
    - name: {{ template "redis-ha.name" $ }}
      rules:
        - alert: Redis cpu使用率过高
          expr: (sum(rate(container_cpu_usage_seconds_total{image!="",pod!="",container="redis"}[2m])) by (namespace, pod)) / (sum(container_spec_cpu_quota{image!="", pod!="",container="redis"}) by(namespace, pod) / 100000) * 100 > 80
          for: 1m
          labels:
            severity: warning
          annotations:
            summary: "Instance {{ `{{ $labels.instance }}` }} CPU使用率过高"
            description: "{{ `{{ $labels.instance }}` }}CPU使用大于80% (当前使用率: {{ `{{ $value}}` }})"
        - alert: Redis 内存使用率过高
          expr: ((container_memory_usage_bytes{container="redis"} / container_spec_memory_limit_bytes{container="redis"}) != +Inf) * 100 > 80
          for: 5m
          labels:
            severity: warning
          annotations:
            summary: "Instance {{ `{{ $labels.namespace }}` }} {{ `{{ $labels.pod }}` }} 内存使用率过高"
            description: "{{ `{{ $labels.namespace }}` }} {{ `{{ $labels.pod }}` }}内存使用大于80% (当前使用率: {{ `{{ $value}}` }})"
        - alert: Redis 节点数异常
          expr: sum(redis_up) < 3
          for: 1m
          labels:
            severity: error
          annotations:
            summary: "Instance {{ `{{ $labels.pod }}` }} : Redis 节点数异常"
            description: "{{ `{{ $labels.pod }}` }}: Redis 节点数异常 (当前节点数: {{ `{{ $value}}`}})"
        - alert: RedisDown
          expr: redis_up{service="{{ template "redis-ha.fullname" . }}-exporter"} == 0
          for: 2m
          labels:
            severity: error
          annotations:
            summary: Redis instance {{ `{{  $labels.instance  }}` }} down
            description: Redis instance {{ `{{  $labels.instance }}` }} is down
        - alert: RedisKeyEviction
          expr: |
            increase(redis_evicted_keys_total{service="{{ template "redis-ha.fullname" . }}-.*exporter"}[5m]) > 0
          for: 1s
          labels:
            severity: error
          annotations:
            summary: Redis instance {{ `{{  $labels.instance  }}` }} has evicted keys
            description: |
              Redis instance {{ `{{  $labels.instance  }}` }} has evicted {{ `{{  $value }}` }} keys in the last 5 minutes.
  {{- else }}
  {{- with .Values.exporter.prometheusRule.rules }}
    groups:
      - name: {{ template "redis-ha.name" $ }}
        rules: {{- tpl (toYaml .) $ | nindent 8 }}
  {{- end }}
  {{- end }}
{{- end }}
