forked from elastic/beats
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publish.go
380 lines (314 loc) · 10.6 KB
/
publish.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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package main
import (
"encoding/json"
"fmt"
"github.com/packetbeat/elastigo/api"
"github.com/packetbeat/elastigo/core"
"labix.org/v2/mgo/bson"
"os"
"strconv"
"strings"
"time"
)
type PublisherType struct {
name string
disabled bool
Index string
RefreshTopologyTimer <-chan time.Time
TopologyMap map[string]string
}
var Publisher PublisherType
// Config
type tomlAgent struct {
Name string
Refresh_topology_freq int
Ignore_outgoing bool
}
type tomlMothership struct {
Host string
Port int
Protocol string
Username string
Password string
Index string
Path string
}
type Event struct {
Timestamp time.Time `json:"@timestamp"`
Type string `json:"type"`
Agent string `json:"agent"`
Src_ip string `json:"src_ip"`
Src_port uint16 `json:"src_port"`
Src_proc string `json:"src_proc"`
Src_country string `json:"src_country"`
Src_server string `json:"src_server"`
Dst_ip string `json:"dst_ip"`
Dst_port uint16 `json:"dst_port"`
Dst_proc string `json:"dst_proc"`
Dst_server string `json:"dst_server"`
ResponseTime int32 `json:"responsetime"`
Status string `json:"status"`
RequestRaw string `json:"request_raw"`
ResponseRaw string `json:"response_raw"`
Mysql bson.M `json:"mysql"`
Http bson.M `json:"http"`
Redis bson.M `json:"redis"`
Pgsql bson.M `json:"pgsql"`
}
type Topology struct {
Name string `json:"name"`
Ip string `json:"ip"`
}
func PrintPublishEvent(event *Event) {
json, err := json.MarshalIndent(event, "", " ")
if err != nil {
ERR("json.Marshal: %s", err)
} else {
DEBUG("publish", "Publish: %s", string(json))
}
}
const (
OK_STATUS = "OK"
ERROR_STATUS = "Error"
)
func (publisher *PublisherType) GetServerName(ip string) string {
// in case the IP is localhost, return current agent name
islocal, err := IsLoopback(ip)
if err != nil {
ERR("Parsing IP %s fails with: %s", ip, err)
return ""
} else {
if islocal {
return publisher.name
}
}
// find the agent with the desired IP
name, exists := publisher.TopologyMap[ip]
if !exists {
return ""
}
return name
}
func (publisher *PublisherType) PublishHttpTransaction(t *HttpTransaction) error {
event := Event{}
event.Type = "http"
response := t.Http["response"].(bson.M)
code := response["code"].(uint16)
if code < 400 {
event.Status = OK_STATUS
} else {
event.Status = ERROR_STATUS
}
event.ResponseTime = t.ResponseTime
event.RequestRaw = t.Request_raw
event.ResponseRaw = t.Response_raw
event.Http = t.Http
return publisher.PublishEvent(t.ts, &t.Src, &t.Dst, &event)
}
func (publisher *PublisherType) PublishMysqlTransaction(t *MysqlTransaction) error {
event := Event{}
event.Type = "mysql"
if t.Mysql["iserror"].(bool) {
event.Status = ERROR_STATUS
} else {
event.Status = OK_STATUS
}
event.ResponseTime = t.ResponseTime
event.RequestRaw = t.Request_raw
event.ResponseRaw = t.Response_raw
event.Mysql = t.Mysql
return publisher.PublishEvent(t.ts, &t.Src, &t.Dst, &event)
}
func (publisher *PublisherType) PublishRedisTransaction(t *RedisTransaction) error {
event := Event{}
event.Type = "redis"
event.Status = OK_STATUS
event.ResponseTime = t.ResponseTime
event.RequestRaw = t.Request_raw
event.ResponseRaw = t.Response_raw
event.Redis = t.Redis
return publisher.PublishEvent(t.ts, &t.Src, &t.Dst, &event)
}
func (publisher *PublisherType) PublishEvent(ts time.Time, src *Endpoint, dst *Endpoint, event *Event) error {
index := fmt.Sprintf("%s-%d.%02d.%02d", publisher.Index, ts.Year(), ts.Month(), ts.Day())
event.Src_server = publisher.GetServerName(src.Ip)
event.Dst_server = publisher.GetServerName(dst.Ip)
if _Config.Agent.Ignore_outgoing && event.Dst_server != "" &&
event.Dst_server != publisher.name {
// duplicated transaction -> ignore it
DEBUG("publish", "Ignore duplicated REDIS transaction on %s: %s -> %s", publisher.name, event.Src_server, event.Dst_server)
return nil
}
event.Timestamp = ts
event.Agent = publisher.name
event.Src_ip = src.Ip
event.Src_port = src.Port
event.Src_proc = src.Proc
event.Dst_ip = dst.Ip
event.Dst_port = dst.Port
event.Dst_proc = dst.Proc
// set src_country if no src_server is set
event.Src_country = ""
if _GeoLite != nil {
if len(event.Src_server) == 0 { // only for external IP addresses
loc := _GeoLite.GetLocationByIP(src.Ip)
if loc != nil {
event.Src_country = loc.CountryCode
}
}
}
if IS_DEBUG("publish") {
PrintPublishEvent(event)
}
// add Redis transaction
var err error
if !publisher.disabled {
_, err = core.Index(index, event.Type, "", nil, event)
}
return err
}
func (publisher *PublisherType) PublishPgsqlTransaction(t *PgsqlTransaction) error {
event := Event{}
event.Type = "pgsql"
if t.Pgsql["iserror"].(bool) {
event.Status = ERROR_STATUS
} else {
event.Status = OK_STATUS
}
event.ResponseTime = t.ResponseTime
event.RequestRaw = t.Request_raw
event.ResponseRaw = t.Response_raw
event.Pgsql = t.Pgsql
return publisher.PublishEvent(t.ts, &t.Src, &t.Dst, &event)
}
func (publisher *PublisherType) UpdateTopologyPeriodically() {
for _ = range publisher.RefreshTopologyTimer {
publisher.UpdateTopology()
}
}
func (publisher *PublisherType) UpdateTopology() {
DEBUG("publish", "Updating Topology")
// get all agents IPs from Elasticsearch
TopologyMapTmp := make(map[string]string)
res, err := core.SearchUri("packetbeat-topology", "server-ip", nil)
if err == nil {
for _, server := range res.Hits.Hits {
var top Topology
err = json.Unmarshal([]byte(*server.Source), &top)
if err != nil {
ERR("json.Unmarshal fails with: %s", err)
}
// add mapping
TopologyMapTmp[top.Ip] = top.Name
}
} else {
ERR("core.SearchRequest fails with: %s", err)
}
// update topology map
publisher.TopologyMap = TopologyMapTmp
DEBUG("publish", "[%s] Map: %s", publisher.name, publisher.TopologyMap)
}
func (publisher *PublisherType) PublishTopology(params ...string) error {
var localAddrs []string = params
if len(params) == 0 {
addrs, err := LocalIpAddrsAsStrings(false)
if err != nil {
ERR("Getting local IP addresses fails with: %s", err)
return err
}
localAddrs = addrs
}
DEBUG("publish", "Local IP addresses (without loopbacks): %s", localAddrs)
// delete old IP addresses
searchJson := fmt.Sprintf("{query: {term: {name: %s}}}", strconv.Quote(publisher.name))
res, err := core.SearchRequest("packetbeat-topology", "server-ip", nil, searchJson)
if err == nil {
for _, server := range res.Hits.Hits {
var top Topology
err = json.Unmarshal([]byte(*server.Source), &top)
if err != nil {
ERR("Failed to unmarshal json data: %s", err)
}
if !stringInSlice(top.Ip, localAddrs) {
res, err := core.Delete("packetbeat-topology", "server-ip" /*id*/, top.Ip, nil)
if err != nil {
ERR("Failed to delete the old IP address from packetbeat-topology")
}
if !res.Ok {
ERR("Fail to delete old topology entry")
}
}
}
}
// add new IP addresses
for _, addr := range localAddrs {
// check if the IP is already in the elasticsearch, before adding it
found, err := core.Exists("packetbeat-topology", "server-ip" /*id*/, addr, nil)
if err != nil {
ERR("core.Exists fails with: %s", err)
} else {
if !found {
res, err := core.Index("packetbeat-topology", "server-ip" /*id*/, addr, nil,
Topology{publisher.name, addr})
if err != nil {
return err
}
if !res.Ok {
ERR("Fail to add new topology entry")
}
}
}
}
DEBUG("publish", "Topology: name=%s, ips=%s", publisher.name, strings.Join(localAddrs, " "))
// initialize local topology map
publisher.TopologyMap = make(map[string]string)
return nil
}
func (publisher *PublisherType) Init(publishDisabled bool) error {
var err error
// Set the Elasticsearch Host to Connect to
api.Domain = _Config.Elasticsearch.Host
api.Port = fmt.Sprintf("%d", _Config.Elasticsearch.Port)
api.Username = _Config.Elasticsearch.Username
api.Password = _Config.Elasticsearch.Password
api.BasePath = _Config.Elasticsearch.Path
if _Config.Elasticsearch.Protocol != "" {
api.Protocol = _Config.Elasticsearch.Protocol
}
if _Config.Elasticsearch.Index != "" {
publisher.Index = _Config.Elasticsearch.Index
} else {
publisher.Index = "packetbeat"
}
INFO("Using %s://%s:%s%s as publisher", api.Protocol, api.Domain, api.Port, api.BasePath)
INFO("Using index pattern [%s-]YYYY.MM.DD", publisher.Index)
publisher.name = _Config.Agent.Name
if len(publisher.name) == 0 {
// use the hostname
publisher.name, err = os.Hostname()
if err != nil {
return err
}
INFO("No agent name configured, using hostname '%s'", publisher.name)
}
publisher.disabled = publishDisabled
if publisher.disabled {
INFO("Dry run mode. Elasticsearch won't be updated or queried.")
}
RefreshTopologyFreq := 10 * time.Second
if _Config.Agent.Refresh_topology_freq != 0 {
RefreshTopologyFreq = time.Duration(_Config.Agent.Refresh_topology_freq) * time.Second
}
publisher.RefreshTopologyTimer = time.Tick(RefreshTopologyFreq)
if !publisher.disabled {
// register agent and its public IP addresses
err = publisher.PublishTopology()
if err != nil {
ERR("Failed to publish topology: %s", err)
return err
}
// update topology periodically
go publisher.UpdateTopologyPeriodically()
}
return nil
}