/
syslog_output.go
185 lines (152 loc) · 4.39 KB
/
syslog_output.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
package outputs
import (
"errors"
"fmt"
"os"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
syslog "github.com/RackSec/srslog"
. "github.com/carbonblack/cb-event-forwarder/pkg/config"
log "github.com/sirupsen/logrus"
)
type SyslogOutput struct {
Config *Configuration
protocol string
hostnamePort string
tag string
outputSocket *syslog.Writer
connectTime time.Time
reconnectTime time.Time
connected bool
droppedEventCount int64
droppedEventSinceConnection int64
sync.RWMutex
}
func NewSyslogOutputFromConfig(cfg *Configuration) *SyslogOutput {
return &SyslogOutput{Config: cfg}
}
type SyslogStatistics struct {
LastOpenTime time.Time `json:"last_open_time"`
Protocol string `json:"protocol"`
RemoteHostnamePort string `json:"remote_hostname_port"`
DroppedEventCount int64 `json:"dropped_event_count"`
Connected bool `json:"connected"`
}
// Initialize expects a connection string in the following format: [protocol]:host[:port]
// For example: tcp+tls:destination.server.example.com:512
// - protocol is optional (but the colon before host is not) and should be something like:
// - tcp, udp, tcp+tls
// - port is optional and if not provided will default to 514.
func (o *SyslogOutput) Initialize(netConn string) error {
o.Lock()
defer o.Unlock()
if o.connected {
o.outputSocket.Close()
o.connected = false
}
connSpecification := strings.SplitN(netConn, ":", 2)
if len(connSpecification) < 2 {
return fmt.Errorf("Invalid connection string: '%s'.", netConn)
}
o.protocol = connSpecification[0]
o.hostnamePort = connSpecification[1]
if !strings.Contains(connSpecification[1], ":") {
log.Infof("No port specified; defaulting to '514'")
o.hostnamePort += ":514"
}
var err error
o.outputSocket, err = syslog.DialWithTLSConfig(o.protocol, o.hostnamePort, syslog.LOG_INFO, o.tag, o.Config.TLSConfig)
if err != nil {
return fmt.Errorf("Error connecting to '%s': %s", netConn, err)
}
o.markConnected()
return nil
}
func (o *SyslogOutput) Key() string {
return o.String()
}
func (o *SyslogOutput) String() string {
o.RLock()
defer o.RUnlock()
return fmt.Sprintf("%s:%s", o.protocol, o.hostnamePort)
}
func (o *SyslogOutput) Statistics() interface{} {
o.RLock()
defer o.RUnlock()
return SyslogStatistics{
LastOpenTime: o.connectTime,
Protocol: o.protocol,
RemoteHostnamePort: o.hostnamePort,
DroppedEventCount: o.droppedEventCount,
Connected: o.connected,
}
}
func (o *SyslogOutput) markConnected() {
o.connectTime = time.Now()
log.Infof("Connected to %s at %s.", o.hostnamePort, o.connectTime)
o.connected = true
if o.droppedEventCount != o.droppedEventSinceConnection {
log.Infof("Dropped %d events since the last reconnection.",
o.droppedEventCount-o.droppedEventSinceConnection)
o.droppedEventSinceConnection = o.droppedEventCount
}
}
func (o *SyslogOutput) closeAndScheduleReconnection() {
o.Lock()
defer o.Unlock()
if o.connected {
o.outputSocket.Close()
o.connected = false
}
// try reconnecting in 5 seconds
o.reconnectTime = time.Now().Add(time.Duration(5 * time.Second))
log.Infof("Lost connection to %s. Will try to reconnect at %s.", o.hostnamePort, o.reconnectTime)
}
func (o *SyslogOutput) output(m string) error {
if !o.connected {
// drop this event on the floor...
atomic.AddInt64(&o.droppedEventCount, 1)
return nil
}
err := o.outputSocket.Info(m)
if err != nil {
o.closeAndScheduleReconnection()
atomic.AddInt64(&o.droppedEventCount, 1)
}
return err
}
func (o *SyslogOutput) Go(messages <-chan string, signals <-chan os.Signal, exitCond *sync.Cond) error {
if o.outputSocket == nil {
return errors.New("Output socket not open")
}
go func() {
refreshTicker := time.NewTicker(1 * time.Second)
defer SignalExitCond(exitCond)
defer refreshTicker.Stop()
for {
select {
case message := <-messages:
if err := o.output(message); err != nil && !o.Config.DryRun {
log.Errorf("%s", err)
}
case <-refreshTicker.C:
if !o.connected && time.Now().After(o.reconnectTime) {
err := o.Initialize(o.String())
if err != nil {
o.closeAndScheduleReconnection()
}
}
case signal := <-signals:
switch signal {
case syscall.SIGTERM, syscall.SIGINT:
log.Infof("Syslog output handling SIGTERM")
return
}
}
}
}()
return nil
}