모니터링(Grafana, Prometheus)/Grafana

Grafana Alert 템플릿 커스텀 및 사용하기

babbeolicoding 2023. 5. 10. 20:37

우선 Template을 등록하는 방법은 아래와 같다.

 

1. Alerting에서 Contact points 탭을 선택 후 "New template"을 선택

 

2. Template 이름 입력 및 Content를 작성

 

3. Template Data에 마우스를 가져가면 아래처럼 팝업이 뜨는데 참고해서 작성

 

이번에는 Template을 사용하는 방법에 대해서 알아보자

 

우선 telegram을 연동하고 test 알림을 보내면

기본적으로 아래 형태로 메시지가 온다.

**Firing**

Value: [no value]
Labels:
 - alertname = TestAlert
 - instance = Grafana
Annotations:
 - summary = Notification test
Silence: http://localhost:3000/alerting/silence/new?alertmanager=grafana&matcher=alertname%3DTestAlert&matcher=instance%3DGrafana

 

여기서 alertname과 summary부분만 가져오고 싶다면

아래 템플릿을 적용하면 된다.

{{ define "alerttemplate" }}                            => alerttemplate 이름
  {{ if gt (len .Alerts.Firing) 0 }}                    => Alert.Firing에 값이 있는 경우
     {{.CommonLabels.alertname}}                        => Lbaels.alertname 명청
    {{ range .Alerts.Firing }}                          => Alert.Firing 전체 검색(for문과 비슷하다고 생각하면 된다)
      {{ if gt (len .Annotations) 0 }}                  => Annotations에 값이 있는 경우
        {{ index .Annotations.Values 0 }}               => Annotations값이 여러개 인 경우 사용(index 0번인 값을 가져와서 보여줌)
      {{ end }}
    {{ end }}
  {{ end }}
{{ end }}

 

변환된 메시지

TestAlert  
Notification test

 

참고로 만든 템플릿은 아래 Optional Telegram settings에서 아래처럼 입력하면 된다.

{{ template "name" .}}
ex) {{ template "alerttemplate" .}}

 

 

 

Grafana와 telegram을 연동하는 방법은 아래 링크를 참고하시길 바란다.

https://babbeolicoding.tistory.com/26

 

Grafana Alert 설정하기-1(telegram 연동)

1. 종 모양 클릭 2. Alerting에서 Contact points 탭을 선택한 뒤 New contact point 선택 3. 설정 세팅 Name Contact point type => telegram BOT API Token CHAT ID 입력 저장 전 Test를 통해 telegram에 정상적으로 메시지 전송이

babbeolicoding.tistory.com