-
Notifications
You must be signed in to change notification settings - Fork 0
/
event.go
274 lines (249 loc) · 5.64 KB
/
event.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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
// Copyright 2019 Luis Guillén Civera <luisguillenc@gmail.com>. View LICENSE.
package event
import (
"encoding/json"
"errors"
"fmt"
"regexp"
"sort"
"time"
)
// Event stores event info.
type Event struct {
// info completed on construction
Code Code `json:"code"`
Level Level `json:"level"`
Created time.Time `json:"created"`
Source Source `json:"source"`
Duplicates int `json:"duplicates"`
Data map[string]interface{} `json:"data,omitempty" bson:",omitempty"`
// info completed by Notifier
ID string `json:"id" bson:"_id"`
Type Type `json:"type"`
Received time.Time `json:"received"`
Codename string `json:"codename"`
Description string `json:"description"`
Processors []ProcessInfo `json:"processors,omitempty" bson:",omitempty"`
Tags []string `json:"tags,omitempty" bson:",omitempty"`
}
// Code defines the code of the event.
type Code int32
// Level defines the level of event.
type Level int8
// Level possible values.
const (
Info Level = iota
Low
Medium
High
Critical
)
// String method to return string of ILevel.
func (l Level) String() string {
switch l {
case Info:
return "info"
case Low:
return "low"
case Medium:
return "medium"
case High:
return "high"
case Critical:
return "critical"
}
return "unknown"
}
// MarshalJSON implements interface for struct marshalling.
func (l Level) MarshalJSON() ([]byte, error) {
s := ""
switch l {
case Info:
s = "info"
case Low:
s = "low"
case Medium:
s = "medium"
case High:
s = "high"
case Critical:
s = "critical"
default:
return nil, fmt.Errorf("invalid value '%v' for level", l)
}
return json.Marshal(s)
}
// UnmarshalJSON implements interface for struct unmarshalling.
func (l *Level) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case "info":
*l = Info
return nil
case "low":
*l = Low
return nil
case "medium":
*l = Medium
return nil
case "high":
*l = High
return nil
case "critical":
*l = Critical
return nil
default:
return fmt.Errorf("cannot unmarshal level '%s'", s)
}
}
// ToEventLevel returns event level from string, raise if ok and error if bad value.
func ToEventLevel(s string) (level Level, raise bool, err error) {
switch s {
case "":
raise = false
case "none":
raise = false
case "info":
raise = true
level = Info
case "low":
raise = true
level = Low
case "medium":
raise = true
level = Medium
case "high":
raise = true
level = High
case "critical":
raise = true
level = Critical
default:
err = errors.New("invalid event level value")
}
return
}
// Type defines the type of event.
type Type int8
// Type possible values.
const (
Undefined Type = iota
Security
)
func (i Type) String() string {
switch i {
case Undefined:
return "undefined"
case Security:
return "security"
default:
return fmt.Sprintf("%#v(unknown)", i)
}
}
// MarshalJSON implements interface for struct marshalling.
func (i Type) MarshalJSON() ([]byte, error) {
s := ""
switch i {
case Undefined:
s = "undefined"
case Security:
s = "security"
default:
return nil, fmt.Errorf("invalid value '%v' for security", i)
}
return json.Marshal(s)
}
// UnmarshalJSON implements interface for struct unmarshalling.
func (i *Type) UnmarshalJSON(data []byte) error {
var s string
if err := json.Unmarshal(data, &s); err != nil {
return err
}
switch s {
case "undefined":
*i = Undefined
return nil
case "security":
*i = Security
return nil
default:
return fmt.Errorf("cannot unmarshal type '%s'", s)
}
}
// ProcessInfo stores event processing info.
type ProcessInfo struct {
Received time.Time `json:"received"`
Processor Source `json:"processor"`
}
// Source stores event source information.
type Source struct {
Hostname string `json:"hostname"`
Program string `json:"program"`
Instance string `json:"instance"`
PID int `json:"pid"`
}
func (s Source) String() string {
if s.Instance == "" {
return fmt.Sprintf("%s.%s[%v]", s.Hostname, s.Program, s.PID)
}
return fmt.Sprintf("%s.%s.%s[%v]", s.Hostname, s.Program, s.Instance, s.PID)
}
// Equals returns true if sources are equals.
func (s Source) Equals(o Source) bool {
if s.Hostname != o.Hostname || s.Program != o.Program || s.Instance != o.Instance || s.PID != o.PID {
return false
}
return true
}
// New event with default source and now timestamp.
func New(c Code, l Level) Event {
return Event{
Code: c,
Level: l,
Created: time.Now(),
Source: defaultSource,
Data: make(map[string]interface{}),
}
}
// Set appends data to an event.
func (e *Event) Set(field string, v interface{}) error {
if !fieldRegExp.MatchString(field) {
return errors.New("invalid field")
}
e.Data[field] = v
return nil
}
// Get gets data from an event.
func (e *Event) Get(field string) (v interface{}, ok bool) {
v, ok = e.Data[field]
return
}
// Fields returns the fields in the event. It returns always a sorted list.
func (e *Event) Fields() []string {
fields := make([]string, 0, len(e.Data))
for k := range e.Data {
fields = append(fields, k)
}
sort.Strings(fields)
return fields
}
// PrintFields returns an string.
func (e *Event) PrintFields() string {
s := ""
first := true
for _, field := range e.Fields() {
if first {
first = false
} else {
s = s + ";"
}
value := e.Data[field]
s = s + fmt.Sprintf("%s=%v", field, value)
}
return s
}
var fieldRegExp, _ = regexp.Compile(`^[A-Za-z][A-Za-z0-9_\.]*$`)
var instanceRegExp, _ = regexp.Compile(`^[A-Za-z][A-Za-z0-9_]*$`)