package notify import ( "context" "encoding/json" "errors" "net/http" "net/url" "strings" "time" ) // Incident management and operations tools. Where a service pairs alerts // with their resolution, the incident ID is the key. // kumaPayload is the body Uptime Kuma sends, which some services accept // as a format of their own ("uptimekuma" integrations). func kumaPayload(m Message) map[string]any { p := map[string]any{"msg": m.Title + "\n" + m.Body} if m.Event != Test { status := 0 if m.Event == Resolved { status = 1 } p["heartbeat"] = map[string]any{ "status": status, "msg": m.Body, "time": time.Now().UTC().Format(time.DateTime), "monitorID": m.MonitorID.String(), "incidentID": m.Key.String(), } p["monitor"] = map[string]any{"id": m.MonitorID.String(), "name": m.Monitor, "url": m.Target, "consoleUrl": m.URL} } return p } var severityField = func(options []string, def string) Field { return Field{Name: "severity", Kind: "select", Options: options, Default: def} } var incidentProviders = []Provider{ {Type: "alerta", Group: "incident", Fields: []Field{ urlField("api_url", true, ""), secret("api_key", true), text("environment", true), severityField([]string{"critical", "major", "minor", "warning"}, "critical"), }, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { severity := cfg["severity"] if m.Event == Resolved { severity = "normal" } return c.json(ctx, http.MethodPost, cfg["api_url"], http.Header{"Authorization": {"Key " + cfg["api_key"]}}, map[string]any{ "environment": cfg["environment"], "severity": or(severity, "critical"), "correlate": []string{"service_up", "service_down"}, "service": []string{"UptimeSebi"}, "resource": or(m.Monitor, "UptimeSebi"), "event": "service_" + m.status(), "group": "uptimesebi", "value": m.status(), "text": m.Title + "\n" + m.Body, "origin": "uptimesebi", "tags": []string{"uptimesebi"}, "type": "uptimesebiAlert", "attributes": map[string]any{"url": m.URL}, }) }}, {Type: "alertnow", Group: "incident", Fields: []Field{secretURL("webhook_url")}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { status, typ := "open", "ERROR" if m.Event == Resolved { status, typ = "close", "INFO" } return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, map[string]any{ "summary": m.Title + " - " + m.Body + " >> " + m.URL, "status": status, "event_type": typ, "event_id": m.Key.String(), }) }}, {Type: "flashduty", Group: "incident", Fields: []Field{secret("integration_key", true), severityField([]string{"Critical", "Warning", "Info"}, "Critical")}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { u := cfg["integration_key"] if !strings.HasPrefix(u, "http") { u = "https://api.flashcat.cloud/event/push/alert/standard?integration_key=" + url.QueryEscape(u) } status := "Info" switch m.Event { case Opened: status = or(cfg["severity"], "Critical") case Resolved: status = "Ok" } return c.json(ctx, http.MethodPost, u, nil, map[string]any{ "title": m.Title, "description": m.Body, "event_status": status, "alert_key": m.Key.String(), "labels": map[string]string{"resource": m.Target, "check": m.Monitor}, }) }}, {Type: "goalert", Group: "incident", Fields: []Field{urlField("server_url", true, ""), secret("token", true)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { v := url.Values{"summary": {truncate(m.Title, 250)}, "details": {m.Body + "\n" + m.URL}, "dedup": {m.Key.String()}} if m.Event == Resolved { v.Set("action", "close") } _, err := c.form(ctx, trimURL(cfg["server_url"])+"/api/v2/generic/incoming?token="+url.QueryEscape(cfg["token"]), nil, v) return err }}, {Type: "grafanaoncall", Group: "incident", Fields: []Field{secretURL("webhook_url")}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { state := "alerting" if m.Event == Resolved { state = "ok" } return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, map[string]any{ "title": m.Title, "message": m.Body, "state": state, "alert_uid": m.Key.String(), "link_to_upstream_details": m.URL, }) }}, {Type: "heiioncall", Group: "incident", Fields: []Field{secret("api_key", true), text("trigger_id", true)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { action := "alert" if m.Event == Resolved { action = "resolve" } return c.json(ctx, http.MethodPost, "https://heiioncall.com/triggers/"+url.PathEscape(cfg["trigger_id"])+"/"+action, bearer(cfg["api_key"]), map[string]any{"msg": m.Title + "\n" + m.Body, "url": m.URL, "name": m.Monitor}) }}, {Type: "jsm", Group: "incident", Fields: []Field{ text("cloud_id", true), text("email", true), secret("api_token", true), {Name: "priority", Kind: "select", Options: []string{"P1", "P2", "P3", "P4", "P5"}, Default: "P3"}, }, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { // Jira Service Management Operations (formerly Opsgenie). base := "https://api.atlassian.com/jsm/ops/api/" + url.PathEscape(cfg["cloud_id"]) + "/v1/alerts" h := basic(cfg["email"], cfg["api_token"]) alias := m.Key.String() if m.Event == Resolved { reply, err := c.get(ctx, base+"/alias", h, url.Values{"alias": {alias}}) if err != nil { return err } var alert struct { ID string `json:"id"` } if json.Unmarshal(reply, &alert) != nil || alert.ID == "" { return errors.New("Jira Service Management: alert to close not found") } return c.json(ctx, http.MethodPost, base+"/"+url.PathEscape(alert.ID)+"/close", h, map[string]any{"source": "UptimeSebi"}) } return c.json(ctx, http.MethodPost, base, h, map[string]any{ "message": truncate(m.Title, 130), "alias": alias, "description": m.Body + "\n" + m.URL, "source": "UptimeSebi", "priority": or(cfg["priority"], "P3"), "tags": []string{"UptimeSebi"}, }) }}, {Type: "pagertree", Group: "incident", Fields: []Field{ secretURL("integration_url"), {Name: "urgency", Kind: "select", Options: []string{"high", "critical", "medium", "low", "silent"}, Default: "high"}, }, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { event := "create" if m.Event == Resolved { event = "resolve" } return c.json(ctx, http.MethodPost, cfg["integration_url"], nil, map[string]any{ "event_type": event, "id": m.Key.String(), "title": m.Title, "description": m.Body + "\n" + m.URL, "urgency": or(cfg["urgency"], "high"), }) }}, {Type: "signl4", Group: "incident", Fields: []Field{secretURL("webhook_url")}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { p := kumaPayload(m) p["title"], p["message"], p["X-S4-SourceSystem"], p["monitorUrl"] = m.Title, m.Body, "UptimeSebi", m.URL if m.Event != Test { p["X-S4-ExternalID"] = "UptimeSebi-" + m.Key.String() p["X-S4-Status"] = map[string]string{"down": "new", "up": "resolved"}[m.status()] } return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, p) }}, {Type: "splunk", Group: "incident", Fields: []Field{secretURL("rest_url"), severityField([]string{"CRITICAL", "WARNING", "INFO"}, "CRITICAL")}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { // Splunk On-Call (VictorOps) REST endpoint, including the // routing key. typ := or(cfg["severity"], "CRITICAL") switch m.Event { case Resolved: typ = "RECOVERY" case Test: typ = "INFO" } return c.json(ctx, http.MethodPost, cfg["rest_url"], nil, map[string]any{ "message_type": typ, "entity_id": "UptimeSebi/" + m.Key.String(), "entity_display_name": m.Title, "state_message": m.Body + "\n" + m.URL, "monitoring_tool": "UptimeSebi", }) }}, {Type: "squadcast", Group: "incident", Fields: []Field{secretURL("webhook_url")}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { body := map[string]any{"message": m.Title, "description": m.Body + "\n" + m.URL, "source": "uptimesebi", "tags": map[string]any{"AlertAddress": m.Target}} if m.Event != Test { body["event_id"] = m.Key.String() body["status"] = map[string]string{"down": "trigger", "up": "resolve"}[m.status()] } return c.json(ctx, http.MethodPost, cfg["webhook_url"], nil, body) }}, {Type: "trigv", Group: "incident", Fields: []Field{secret("api_key", true), text("channel", false)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { level := map[string]string{"down": "error", "up": "success"}[m.status()] body := map[string]any{"channel": or(cfg["channel"], "general"), "title": truncate(m.Title, 255), "description": truncate(m.Body, 1000), "level": or(level, "info")} if m.URL != "" { body["url"] = m.URL } return c.json(ctx, http.MethodPost, "https://api.trigv.com/v1/events", bearer(cfg["api_key"]), body) }}, {Type: "halopsa", Group: "incident", Fields: []Field{secretURL("webhook_url"), text("username", false), secret("password", false)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { var h http.Header if cfg["username"] != "" { h = basic(cfg["username"], cfg["password"]) } status := map[string]string{"down": "DOWN", "up": "UP"}[m.status()] return c.json(ctx, http.MethodPost, cfg["webhook_url"], h, map[string]any{ "title": m.Title, "status": or(status, "NOTIFICATION"), "monitor": or(m.Monitor, "No Monitor"), "monitor_id": m.MonitorID.String(), "message": m.Body + "\n" + m.URL, "timestamp": time.Now().UTC().Format(time.RFC3339), }) }}, {Type: "flowtriq", Group: "incident", Fields: []Field{secretURL("webhook_url"), secret("api_key", false)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { var h http.Header if cfg["api_key"] != "" { h = http.Header{"X-API-Key": {cfg["api_key"]}} } status := map[string]string{"down": "down", "up": "up"}[m.status()] return c.json(ctx, http.MethodPost, cfg["webhook_url"], h, map[string]any{ "source": "uptimesebi", "status": or(status, "info"), "monitor": or(m.Monitor, "Unknown"), "msg": m.Title + "\n" + m.Body, }) }}, {Type: "keep", Group: "incident", Fields: []Field{urlField("server_url", true, ""), secret("api_key", true)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { return c.json(ctx, http.MethodPost, trimURL(cfg["server_url"])+"/alerts/event/uptimekuma", http.Header{"x-api-key": {cfg["api_key"]}}, kumaPayload(m)) }}, {Type: "opsimate", Group: "incident", Fields: []Field{urlField("server_url", true, ""), secret("api_token", true)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { return c.json(ctx, http.MethodPost, trimURL(cfg["server_url"])+"/api/v1/alerts/custom/uptimekuma", http.Header{"x-api-token": {cfg["api_token"]}}, kumaPayload(m)) }}, {Type: "pinglet", Group: "incident", Fields: []Field{urlField("publish_url", true, ""), secret("api_key", true)}, send: func(ctx context.Context, c *client, cfg map[string]string, m Message) error { return c.json(ctx, http.MethodPost, trimURL(cfg["publish_url"])+"?rewrite=uptimekuma", bearer(cfg["api_key"]), kumaPayload(m)) }}, }