-
Notifications
You must be signed in to change notification settings - Fork 0
/
nftables_log.go
151 lines (134 loc) · 3.46 KB
/
nftables_log.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
148
149
150
151
package main
import (
"bytes"
"strconv"
"strings"
"unicode"
"unicode/utf8"
"github.com/pkg/errors"
"github.com/elastic/beats/libbeat/beat"
"github.com/elastic/beats/libbeat/common"
"github.com/elastic/beats/libbeat/processors"
)
// NftablesLogParser contains the data of a log parser processor
type NftablesLogParser struct {
config NftablesLogConfig
}
var integerFields map[string]bool = map[string]bool{"LEN": true, "TOS": true, "PREC": true, "TTL": true, "ID": true, "SPT": true, "DPT": true, "WINDOW": true, "RES": true, "URGP": true, "UID": true, "GID": true}
func (f NftablesLogParser) String() string {
b := new(bytes.Buffer)
b.WriteString("nftables_log_parser=[")
b.WriteString("field=")
b.WriteString(f.config.Field)
b.WriteString(", target=")
b.WriteString(f.config.Target)
b.WriteRune(']')
return b.String()
}
// New creates a new nftables log parser processor
func New(c *common.Config) (processors.Processor, error) {
fc := defaultNftablesLogConfig
err := c.Unpack(&fc)
if err != nil {
return nil, errors.Wrap(err, "failed to unpack nftables log parser config")
}
return &NftablesLogParser{
config: fc,
}, nil
}
// Run runs an event through a log parser processor
func (f *NftablesLogParser) Run(event *beat.Event) (*beat.Event, error) {
msgField, _ := event.GetValue(f.config.Field)
msgFieldStr, ok := msgField.(string)
if !ok {
return nil, errors.Errorf("unexpected type %T for nftables message field", msgField)
}
if f.config.Target != "" {
newMap := common.MapStr{}
f.parseNftablesLog(msgFieldStr, newMap)
if !f.config.OverwriteKeys && f.config.Target != f.config.Field {
if _, err := event.GetValue(f.config.Target); err == nil {
return nil, errors.Errorf("target field %s already has a value. Set the overwrite_keys flag or drop/rename the field first", f.config.Target)
}
}
event.PutValue(f.config.Target, newMap)
} else {
f.parseNftablesLog(msgFieldStr, event.Fields)
}
return event, nil
}
func (f *NftablesLogParser) parseNftablesLog(msg string, event common.MapStr) {
pos := uint(0)
if len(f.config.Marker) > 0 {
posNew := strings.Index(msg, f.config.Marker)
if posNew > 0 {
pos = uint(posNew + len(f.config.Marker))
} else {
return
}
}
outer:
for pos < uint(len(msg)) {
// skip whitespace
for {
var width int
c, width := utf8.DecodeRuneInString(msg[pos:])
if width < 0 {
break outer
}
if unicode.IsSpace(c) {
pos += uint(width)
} else {
break
}
if pos >= uint(len(msg)) {
break outer
}
}
// look for next whitespace
nextPos := pos
for {
var width int
c, width := utf8.DecodeRuneInString(msg[nextPos:])
if width < 0 {
break outer
}
if !unicode.IsSpace(c) {
nextPos += uint(width)
} else {
break
}
if nextPos >= uint(len(msg)) {
break
}
}
word := msg[pos:nextPos]
equalPos := strings.IndexByte(word, '=')
if equalPos < 0 {
flags, existFlags := event.GetValue("flags")
if existFlags != nil {
flags = []string{word}
} else {
if flagsArr, ok := flags.([]string); ok {
flags = append(flagsArr, word)
}
}
event.Put("flags", flags)
} else {
if equalPos+1 < len(word) {
key := word[0:equalPos]
value := word[equalPos+1:]
if _, ok := integerFields[key]; ok {
valueInt64, err := strconv.ParseInt(value, 0, 32)
valueInt := int(valueInt64)
if err == nil {
event.Put(key, valueInt)
}
} else {
event.Put(key, value)
}
}
}
pos = nextPos
}
}