-
Notifications
You must be signed in to change notification settings - Fork 166
/
opsgenie.go
84 lines (73 loc) · 2.3 KB
/
opsgenie.go
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
// SPDX-License-Identifier: MIT OR Apache-2.0
package outputs
import (
"log"
"strings"
"github.com/falcosecurity/falcosidekick/types"
)
type opsgeniePayload struct {
Message string `json:"message"`
Entity string `json:"entity,omitempty"`
Description string `json:"description,omitempty"`
Details map[string]string `json:"details,omitempty"`
Priority string `json:"priority,omitempty"`
}
func newOpsgeniePayload(falcopayload types.FalcoPayload, config *types.Configuration) opsgeniePayload {
details := make(map[string]string, len(falcopayload.OutputFields))
for i, j := range falcopayload.OutputFields {
switch v := j.(type) {
case string:
details[strings.ReplaceAll(i, ".", "_")] = v
default:
continue
}
}
details["source"] = falcopayload.Source
details["rule"] = falcopayload.Rule
details["priority"] = falcopayload.Priority.String()
if falcopayload.Hostname != "" {
details[Hostname] = falcopayload.Hostname
}
if len(falcopayload.Tags) != 0 {
details["tags"] = strings.Join(falcopayload.Tags, ", ")
}
var prio string
switch falcopayload.Priority {
case types.Emergency, types.Alert:
prio = "P1"
case types.Critical:
prio = "P2"
case types.Error:
prio = "P3"
case types.Warning:
prio = "P4"
default:
prio = "P5"
}
return opsgeniePayload{
Message: falcopayload.Output,
Entity: "Falcosidekick",
Description: falcopayload.Rule,
Details: details,
Priority: prio,
}
}
// OpsgeniePost posts event to OpsGenie
func (c *Client) OpsgeniePost(falcopayload types.FalcoPayload) {
c.Stats.Opsgenie.Add(Total, 1)
c.httpClientLock.Lock()
defer c.httpClientLock.Unlock()
c.AddHeader(AuthorizationHeaderKey, "GenieKey "+c.Config.Opsgenie.APIKey)
err := c.Post(newOpsgeniePayload(falcopayload, c.Config))
if err != nil {
go c.CountMetric(Outputs, 1, []string{"output:opsgenie", "status:error"})
c.Stats.Opsgenie.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "opsgenie", "status": Error}).Inc()
log.Printf("[ERROR] : OpsGenie - %v\n", err)
return
}
// Setting the success status
go c.CountMetric(Outputs, 1, []string{"output:opsgenie", "status:ok"})
c.Stats.Opsgenie.Add("ok", 1)
c.PromStats.Outputs.With(map[string]string{"destination": "opsgenie", "status": OK}).Inc()
}