-
Notifications
You must be signed in to change notification settings - Fork 174
/
timescaledb.go
134 lines (115 loc) · 3.42 KB
/
timescaledb.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// SPDX-License-Identifier: MIT OR Apache-2.0
package outputs
import (
"context"
"fmt"
"log"
"strings"
"github.com/DataDog/datadog-go/statsd"
"github.com/falcosecurity/falcosidekick/types"
"github.com/jackc/pgx/v5/pgxpool"
)
type timescaledbPayload struct {
SQL string `json:"sql"`
Values []any `json:"values"`
}
func NewTimescaleDBClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics,
statsdClient, dogstatsdClient *statsd.Client) (*Client, error) {
ctx := context.Background()
connStr := fmt.Sprintf(
"postgres://%s:%s@%s:%s/%s",
config.TimescaleDB.User,
config.TimescaleDB.Password,
config.TimescaleDB.Host,
config.TimescaleDB.Port,
config.TimescaleDB.Database,
)
connPool, err := pgxpool.New(ctx, connStr)
if err != nil {
log.Printf("[ERROR] : TimescaleDB - %v\n", err)
return nil, ErrClientCreation
}
return &Client{
OutputType: "TimescaleDB",
Config: config,
TimescaleDBClient: connPool,
Stats: stats,
PromStats: promStats,
StatsdClient: statsdClient,
DogstatsdClient: dogstatsdClient,
}, nil
}
func newTimescaleDBPayload(falcopayload types.FalcoPayload, config *types.Configuration) timescaledbPayload {
vals := make(map[string]any, 7+len(config.Customfields)+len(config.Templatedfields))
vals[Time] = falcopayload.Time
vals[Rule] = falcopayload.Rule
vals[Priority] = falcopayload.Priority.String()
vals[Source] = falcopayload.Source
vals["output"] = falcopayload.Output
if len(falcopayload.Tags) != 0 {
vals[Tags] = strings.Join(falcopayload.Tags, ",")
}
if falcopayload.Hostname != "" {
vals[Hostname] = falcopayload.Hostname
}
for i, j := range falcopayload.OutputFields {
switch v := j.(type) {
case string:
for k := range config.Customfields {
if i == k {
vals[i] = strings.ReplaceAll(v, "\"", "")
}
}
for k := range config.Templatedfields {
if i == k {
vals[i] = strings.ReplaceAll(v, "\"", "")
}
}
default:
continue
}
}
i := 0
retVals := make([]any, len(vals))
var cols strings.Builder
var args strings.Builder
for k, v := range vals {
cols.WriteString(k)
fmt.Fprintf(&args, "$%d", i+1)
if i < (len(vals) - 1) {
cols.WriteString(",")
args.WriteString(",")
}
str, isString := v.(string)
if isString && (strings.ToLower(str) == "null") {
retVals[i] = nil
} else {
retVals[i] = v
}
i++
}
sql := fmt.Sprintf("INSERT INTO %s (%s) VALUES (%s)",
config.TimescaleDB.HypertableName,
cols.String(),
args.String())
return timescaledbPayload{SQL: sql, Values: retVals}
}
func (c *Client) TimescaleDBPost(falcopayload types.FalcoPayload) {
c.Stats.TimescaleDB.Add(Total, 1)
var ctx = context.Background()
tsdbPayload := newTimescaleDBPayload(falcopayload, c.Config)
_, err := c.TimescaleDBClient.Exec(ctx, tsdbPayload.SQL, tsdbPayload.Values...)
if err != nil {
go c.CountMetric(Outputs, 1, []string{"output:timescaledb", "status:error"})
c.Stats.TimescaleDB.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "timescaledb", "status": Error}).Inc()
log.Printf("[ERROR] : TimescaleDB - %v\n", err)
return
}
go c.CountMetric(Outputs, 1, []string{"output:timescaledb", "status:ok"})
c.Stats.TimescaleDB.Add(OK, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "timescaledb", "status": OK}).Inc()
if c.Config.Debug {
log.Printf("[DEBUG] : TimescaleDB payload : %v\n", tsdbPayload)
}
}