type ( // HookMessage is the message we receive from Alertmanager HookMessage struct { Version string`json:"version"` GroupKey string`json:"groupKey"` Status string`json:"status"` Receiver string`json:"receiver"` GroupLabels map[string]string`json:"groupLabels"` CommonLabels map[string]string`json:"commonLabels"` CommonAnnotations map[string]string`json:"commonAnnotations"` ExternalURL string`json:"externalURL"` Alerts []Alert `json:"alerts"` }
// Alert is a single alert. Alert struct { Labels map[string]string`json:"labels"` Annotations map[string]string`json:"annotations"` StartsAt string`json:"startsAt,omitempty"` EndsAt string`json:"EndsAt,omitempty"` } )
funchealthzHandler(w http.ResponseWriter, r *http.Request) { io.WriteString(w, `# HELP Information about the health. # TYPE go_info gauge healthz 1`) }
funcalertsHandler(w http.ResponseWriter, r *http.Request) { log.Printf("recv request.") switch r.Method { case http.MethodGet: getHandler(w, r) case http.MethodPost: postHandler(w, r) default: http.Error(w, "unsupported HTTP method", 400) } } // 从配置文件中读取邮件列表 funcgetEmailList(companyID string) []string { list := viper.GetStringSlice("accounts." + companyID + ".emails") return list } // 从配置中读取短信通知列表 funcgetTelList(companyID string) []string { list := viper.GetStringSlice("accounts." + companyID + ".tels") return list }
funcgetHandler(w http.ResponseWriter, r *http.Request) { // do something here }
funcpostHandler(w http.ResponseWriter, r *http.Request) {
dec := json.NewDecoder(r.Body) defer r.Body.Close()
var m HookMessage if err := dec.Decode(&m); err != nil { log.Printf("error decoding message: %v", err) http.Error(w, "invalid request body", 400) return } // send message to ums sendMSG(m) io.WriteString(w, "OK\n") } // 读取信息并发送 funcsendMSG(msg HookMessage)error { var msgStr string companyID := msg.CommonLabels["company"] log.Printf("try send ums: %v", msg)