This repository has been archived by the owner on May 19, 2022. It is now read-only.
forked from carbonblack/cb-event-forwarder
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathsyslog_output.go
166 lines (133 loc) · 3.63 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
package main
import (
"errors"
"fmt"
syslog "github.com/RackSec/srslog"
log "github.com/sirupsen/logrus"
"os"
"os/signal"
"strings"
"sync"
"sync/atomic"
"syscall"
"time"
)
type SyslogOutput struct {
protocol string
hostnamePort string
tag string
outputSocket *syslog.Writer
connectTime time.Time
reconnectTime time.Time
connected bool
droppedEventCount int64
droppedEventSinceConnection int64
sync.RWMutex
}
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):(hostname/IP):(port)
// for example: tcp+tls:destination.server.example.com:512
func (o *SyslogOutput) Initialize(netConn string) error {
o.Lock()
defer o.Unlock()
if o.connected {
o.outputSocket.Close()
}
connSpecification := strings.SplitN(netConn, ":", 2)
o.protocol = connSpecification[0]
o.hostnamePort = connSpecification[1]
var err error
o.outputSocket, err = syslog.DialWithTLSConfig(o.protocol, o.hostnamePort, syslog.LOG_INFO, o.tag, config.TLSConfig)
if err != nil {
return errors.New(fmt.Sprintf("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()
}
return err
}
func (o *SyslogOutput) Go(messages <-chan string, errorChan chan<- error) error {
if o.outputSocket == nil {
return errors.New("Output socket not open")
}
go func() {
refreshTicker := time.NewTicker(1 * time.Second)
defer refreshTicker.Stop()
hup := make(chan os.Signal, 1)
signal.Notify(hup, syscall.SIGHUP)
defer signal.Stop(hup)
for {
select {
case message := <-messages:
if err := o.output(message); err != nil {
errorChan <- err
}
case <-refreshTicker.C:
if !o.connected && time.Now().After(o.reconnectTime) {
err := o.Initialize(o.String())
if err != nil {
o.closeAndScheduleReconnection()
}
}
}
}
}()
return nil
}