File size: 2,636 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package site24x7

import (
	"encoding/json"
	"net/http"
	"strings"
	"time"

	"github.com/pkg/errors"
	"github.com/target/goalert/alert"
	"github.com/target/goalert/integrationkey"
	"github.com/target/goalert/permission"
	"github.com/target/goalert/retry"
	"github.com/target/goalert/util/errutil"
	"github.com/target/goalert/util/log"
	"github.com/target/goalert/validation/validate"
)

type post struct {
	MonitorDashboardURL string `json:"MONITOR_DASHBOARD_LINK"` // using URL instead of Link to match fields used in GoAlert, we can just map it to the JSON name
	Status              string `json:"STATUS"`
	MonitorName         string `json:"MONITORNAME"`
}

func clientError(w http.ResponseWriter, code int, err error) bool {
	if err == nil {
		return false
	}

	http.Error(w, http.StatusText(code), code)
	return true
}

func Site24x7ToEventsAPI(aDB *alert.Store, intDB *integrationkey.Store) http.HandlerFunc {

	return func(w http.ResponseWriter, r *http.Request) {

		ctx := r.Context()

		err := permission.LimitCheckAny(ctx, permission.Service)
		if errutil.HTTPError(ctx, w, err) {
			return
		}
		serviceID := permission.ServiceID(ctx)

		var g post
		err = json.NewDecoder(r.Body).Decode(&g)
		if clientError(w, http.StatusBadRequest, err) {
			log.Logf(ctx, "bad request from site24x7: %v", err)
			return
		}

		ctx = log.WithFields(ctx, log.Fields{
			"RuleURL": g.MonitorDashboardURL,
			"State":   g.Status,
		})

		var site24x7State alert.Status
		switch g.Status {
		case "DOWN", "CRITICAL", "TROUBLE":
			site24x7State = alert.StatusTriggered
		case "UP":
			site24x7State = alert.StatusClosed
		default:
			log.Logf(ctx, "bad request from site24x7: missing or invalid state")
			http.Error(w, "invalid state", http.StatusBadRequest)
			return
		}

		var urlStr string
		if validate.AbsoluteURL("MONITOR_DASHBOARD_LINK", g.MonitorDashboardURL) == nil {
			urlStr = g.MonitorDashboardURL
		}
		body := strings.TrimSpace(urlStr + "\n\n" + g.MonitorName)

		//dedupe is description, source, and serviceID
		msg := &alert.Alert{
			Summary:   validate.SanitizeText(g.MonitorName, alert.MaxSummaryLength),
			Details:   validate.SanitizeText(body, alert.MaxDetailsLength),
			Status:    site24x7State,
			Source:    alert.SourceSite24x7,
			ServiceID: serviceID,
			Dedup:     alert.NewUserDedup(r.FormValue("dedup")),
		}

		err = retry.DoTemporaryError(func(int) error {
			_, _, err = aDB.CreateOrUpdate(ctx, msg)
			return err
		},
			retry.Log(ctx),
			retry.Limit(10),
			retry.FibBackoff(time.Second),
		)
		if errutil.HTTPError(ctx, w, errors.Wrap(err, "create or update alert for site24x7")) {
			return
		}
	}
}