2023-02-12 03:04:17 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
|
|
|
"encoding/json"
|
2023-02-12 14:15:02 +01:00
|
|
|
"fmt"
|
2023-02-12 03:04:17 +01:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
const dateLayout = "2006-01-02 15:04:05"
|
|
|
|
|
|
|
|
type silence struct {
|
|
|
|
Matchers []matcher `json:"matchers"`
|
|
|
|
StartsAt string `json:"startsAt"`
|
|
|
|
EndsAt string `json:"endsAt"`
|
|
|
|
CreatedBy string `json:"createdBy"`
|
|
|
|
Comment string `json:"comment"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type matcher struct {
|
|
|
|
Name string `json:"name"`
|
|
|
|
Value string `json:"value"`
|
|
|
|
IsRegex bool `json:"isRegex"`
|
|
|
|
IsEqual bool `json:"isEqual"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type silenceBody struct {
|
2023-02-12 14:40:32 +01:00
|
|
|
AlertManagerURL string `json:"alertmanagerURL"`
|
|
|
|
Labels map[string]string `json:"labels"`
|
2023-02-12 03:04:17 +01:00
|
|
|
}
|
|
|
|
|
2023-02-12 16:07:49 +01:00
|
|
|
type silenceResponse struct {
|
|
|
|
ID string `json:"silenceID"`
|
|
|
|
}
|
|
|
|
|
2023-02-21 13:57:21 +01:00
|
|
|
func (br *bridge) handleSilences(w http.ResponseWriter, r *http.Request) {
|
2023-02-12 03:04:17 +01:00
|
|
|
defer r.Body.Close()
|
|
|
|
|
|
|
|
if r.Method != http.MethodPost {
|
|
|
|
http.Error(w, "Only POST allowed", http.StatusMethodNotAllowed)
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: illegal HTTP method: expected %q, got %q", "POST", r.Method)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = base64.StdEncoding.DecodeString(string(b))
|
|
|
|
if err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var sb silenceBody
|
|
|
|
err = json.Unmarshal(b, &sb)
|
|
|
|
if err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var matchers []matcher
|
|
|
|
for key, value := range sb.Labels {
|
|
|
|
m := matcher{
|
|
|
|
Name: key,
|
2023-02-12 14:40:32 +01:00
|
|
|
Value: value,
|
2023-02-12 03:04:17 +01:00
|
|
|
IsRegex: false,
|
|
|
|
IsEqual: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
matchers = append(matchers, m)
|
|
|
|
}
|
|
|
|
|
|
|
|
silence := &silence{
|
|
|
|
StartsAt: time.Now().UTC().Format(dateLayout),
|
2023-02-21 13:57:21 +01:00
|
|
|
EndsAt: time.Now().Add(br.cfg.am.SilenceDuration).UTC().Format(dateLayout),
|
2023-02-12 03:04:17 +01:00
|
|
|
CreatedBy: "ntfy-alertmanager",
|
|
|
|
Comment: "",
|
|
|
|
Matchers: matchers,
|
|
|
|
}
|
|
|
|
|
|
|
|
b, err = json.Marshal(silence)
|
|
|
|
if err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-12 14:58:37 +01:00
|
|
|
url := sb.AlertManagerURL
|
2023-02-21 13:57:21 +01:00
|
|
|
if br.cfg.am.URL != "" {
|
|
|
|
url = br.cfg.am.URL
|
2023-02-12 14:58:37 +01:00
|
|
|
}
|
|
|
|
url += "/api/v2/silences"
|
|
|
|
|
2023-02-12 03:04:17 +01:00
|
|
|
req, err := http.NewRequest(http.MethodPost, url, bytes.NewBuffer(b))
|
|
|
|
if err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-12 14:15:02 +01:00
|
|
|
// Basic auth
|
2023-02-21 13:57:21 +01:00
|
|
|
if br.cfg.am.User != "" && br.cfg.am.Password != "" {
|
|
|
|
auth := base64.StdEncoding.EncodeToString([]byte(fmt.Sprintf("%s:%s", br.cfg.am.User, br.cfg.am.Password)))
|
2023-02-12 14:15:02 +01:00
|
|
|
req.Header.Set("Authorization", fmt.Sprintf("Basic %s", auth))
|
|
|
|
}
|
|
|
|
|
2023-02-12 03:04:17 +01:00
|
|
|
req.Header.Add("Content-Type", "application/json")
|
2023-02-21 13:57:21 +01:00
|
|
|
resp, err := br.client.Do(req)
|
2023-02-12 03:04:17 +01:00
|
|
|
if err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
defer resp.Body.Close()
|
|
|
|
|
|
|
|
b, err = io.ReadAll(resp.Body)
|
|
|
|
if err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: received status code %d", resp.StatusCode)
|
2023-02-12 03:04:17 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-12 16:07:49 +01:00
|
|
|
var id silenceResponse
|
|
|
|
if err := json.Unmarshal(b, &id); err != nil {
|
2023-02-21 13:57:21 +01:00
|
|
|
br.logger.Debugf("silences: %v", err)
|
2023-02-12 16:07:49 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-02-21 14:00:21 +01:00
|
|
|
br.logger.Infof("Created new silence %s", id.ID)
|
2023-02-12 03:04:17 +01:00
|
|
|
}
|