/
syslog.go
147 lines (130 loc) · 3.99 KB
/
syslog.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
135
136
137
138
139
140
141
142
143
144
145
146
147
// SPDX-License-Identifier: Apache-2.0
/*
Copyright (C) 2023 The Falco Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package outputs
import (
"encoding/json"
"fmt"
"log"
"log/syslog"
"strings"
"time"
"github.com/DataDog/datadog-go/statsd"
"github.com/falcosecurity/falcosidekick/types"
)
func NewSyslogClient(config *types.Configuration, stats *types.Statistics, promStats *types.PromStatistics, statsdClient, dogstatsdClient *statsd.Client) (*Client, error) {
ok := isValidProtocolString(strings.ToLower(config.Syslog.Protocol))
if !ok {
return nil, fmt.Errorf("failed to configure Syslog client: invalid protocol %s", config.Syslog.Protocol)
}
return &Client{
OutputType: "Syslog",
Config: config,
Stats: stats,
PromStats: promStats,
StatsdClient: statsdClient,
DogstatsdClient: dogstatsdClient,
}, nil
}
func isValidProtocolString(protocol string) bool {
return protocol == TCP || protocol == UDP
}
func getCEFSeverity(priority types.PriorityType) string {
switch priority {
case types.Debug:
return "0"
case types.Informational:
return "3"
case types.Notice:
return "4"
case types.Warning:
return "6"
case types.Error:
return "7"
case types.Critical:
return "8"
case types.Alert:
return "9"
case types.Emergency:
return "10"
default:
return "Uknown"
}
}
func (c *Client) SyslogPost(falcopayload types.FalcoPayload) {
c.Stats.Syslog.Add(Total, 1)
endpoint := fmt.Sprintf("%s:%s", c.Config.Syslog.Host, c.Config.Syslog.Port)
var priority syslog.Priority
switch falcopayload.Priority {
case types.Emergency:
priority = syslog.LOG_EMERG
case types.Alert:
priority = syslog.LOG_ALERT
case types.Critical:
priority = syslog.LOG_CRIT
case types.Error:
priority = syslog.LOG_ERR
case types.Warning:
priority = syslog.LOG_WARNING
case types.Notice:
priority = syslog.LOG_NOTICE
case types.Informational:
priority = syslog.LOG_INFO
case types.Debug:
priority = syslog.LOG_DEBUG
}
sysLog, err := syslog.Dial(c.Config.Syslog.Protocol, endpoint, priority, Falco)
if err != nil {
go c.CountMetric(Outputs, 1, []string{"output:syslog", "status:error"})
c.Stats.Syslog.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "syslog", "status": Error}).Inc()
log.Printf("[ERROR] : Syslog - %v\n", err)
return
}
var payload []byte
if c.Config.Syslog.Format == "cef" {
s := fmt.Sprintf(
"CEF:0|Falcosecurity|Falco|1.0|Falco Event|%v|%v|uuid=%v start=%v msg=%v source=%v",
falcopayload.Rule,
getCEFSeverity(falcopayload.Priority),
falcopayload.UUID,
falcopayload.Time.Format(time.RFC3339),
falcopayload.Output,
falcopayload.Source,
)
if falcopayload.Hostname != "" {
s += " hostname=" + falcopayload.Hostname
}
s += " outputfields="
for i, j := range falcopayload.OutputFields {
s += fmt.Sprintf("%v:%v ", i, j)
}
if len(falcopayload.Tags) != 0 {
s += "tags=" + strings.Join(falcopayload.Tags, ",")
}
payload = []byte(strings.TrimSuffix(s, " "))
} else {
payload, _ = json.Marshal(falcopayload)
}
_, err = sysLog.Write(payload)
if err != nil {
go c.CountMetric(Outputs, 1, []string{"output:syslog", "status:error"})
c.Stats.Syslog.Add(Error, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "syslog", "status": Error}).Inc()
log.Printf("[ERROR] : Syslog - %v\n", err)
return
}
go c.CountMetric(Outputs, 1, []string{"output:syslog", "status:ok"})
c.Stats.Syslog.Add(OK, 1)
c.PromStats.Outputs.With(map[string]string{"destination": "syslog", "status": OK}).Inc()
}