告警通知

简单的 webhook 测试,可以看看告警的消息长啥样

测试 Webhook

package main

import (
	"encoding/json"
	"fmt"
	"log"
	"net/http"
	"time"
)

type AlertMsg struct {
	Receiver          string
	Status            string
	GroupLabels       map[string]string
	CommonLabels      map[string]string
	CommonAnnotations map[string]string
	ExternalURL       string
	Alerts            []Alert
}

type Alert struct {
	Status       string
	Labels       map[string]string
	Annotations  map[string]string
	StartsAt     time.Time
	EndsAt       time.Time
	GeneratorURL string
	Fingerprint  string
}

func main() {
	http.HandleFunc("/alerts", alertHandler)
	log.Fatal(http.ListenAndServe("0.0.0.0:5001", nil))
}

func alertHandler(w http.ResponseWriter, r *http.Request) {
	dec := json.NewDecoder(r.Body)
	defer r.Body.Close()

	var m AlertMsg
	if err := dec.Decode(&m); err != nil {
		log.Printf("decoding alert message error: %v", err)
		return
	}
	content, _ := json.MarshalIndent(m, "", "    ")
	fmt.Println(string(content))
}
上次更新:
贡献者: kongzZ