forked from cilium/cilium
-
Notifications
You must be signed in to change notification settings - Fork 0
/
logstash.go
132 lines (121 loc) · 3.32 KB
/
logstash.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
// Copyright 2016-2017 Authors of Cilium
//
// 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 main
import (
"encoding/json"
"net"
"strconv"
"strings"
"time"
"github.com/cilium/cilium/pkg/endpointmanager"
"github.com/cilium/cilium/pkg/maps/policymap"
"github.com/cilium/cilium/pkg/policy"
log "github.com/Sirupsen/logrus"
)
// LogstashStat is used to collect stats from the policy dumps.
type LogstashStat struct {
FromID uint32
From string
ToID string
Bytes uint64
Packets uint64
Action string
}
func newLogstashClient(addr string) net.Conn {
i := 3
for {
c, err := net.Dial("tcp", addr)
if err != nil {
if i >= 0 {
log.Errorf("Error while connecting to Logstash address %s: %s", addr, err)
if i == 0 {
log.Info("Mutting Logstash connection errors but still retrying...")
}
i--
}
} else {
log.Infof("Connection to Logstash %s successfully made", addr)
return c
}
time.Sleep(10 * time.Second)
}
}
// EnableLogstash is used to start collecting statistics with Logstash.
func (d *Daemon) EnableLogstash(LogstashAddr string, refreshTime int) {
readStats := func(c net.Conn) {
defer func() {
recover()
}()
for {
timeToProcess1 := time.Now()
allPes := map[uint16][]policymap.PolicyEntryDump{}
endpointmanager.Mutex.RLock()
for _, ep := range endpointmanager.Endpoints {
ep.Mutex.RLock()
pes, err := ep.PolicyMap.DumpToSlice()
if err != nil {
continue
}
allPes[ep.ID] = pes
ep.Mutex.RUnlock()
}
endpointmanager.Mutex.RUnlock()
lss := d.processStats(allPes)
for _, ls := range lss {
if err := json.NewEncoder(c).Encode(ls); err != nil {
log.Errorf("Error while sending data to Logstash: %s", err)
timeToProcess2 := time.Now()
time.Sleep(time.Second*time.Duration(refreshTime) - timeToProcess2.Sub(timeToProcess1))
return
}
}
timeToProcess2 := time.Now()
time.Sleep(time.Second*time.Duration(refreshTime) - timeToProcess2.Sub(timeToProcess1))
}
}
for {
c := newLogstashClient(LogstashAddr)
readStats(c)
}
}
func (d *Daemon) getInlineLabelStr(id policy.NumericIdentity) string {
l, err := d.GetCachedLabelList(id)
if err != nil {
return ""
}
inlineLblSlice := []string{}
for _, lbl := range l {
inlineLblSlice = append(inlineLblSlice, lbl.String())
}
return strings.Join(inlineLblSlice, "\n")
}
func (d *Daemon) processStats(allPes map[uint16][]policymap.PolicyEntryDump) []LogstashStat {
lss := []LogstashStat{}
for k, v := range allPes {
if len(v) == 0 {
continue
}
for _, stat := range v {
lss = append(lss, LogstashStat{
FromID: stat.ID,
From: d.getInlineLabelStr(policy.NumericIdentity(stat.ID)),
ToID: strconv.FormatUint(uint64(k), 10),
Bytes: stat.Bytes,
Packets: stat.Packets,
Action: stat.PolicyEntry.String(),
})
}
}
return lss
}