-
Notifications
You must be signed in to change notification settings - Fork 10
/
drainer.go
91 lines (79 loc) · 1.78 KB
/
drainer.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
package syslog
import (
"crypto/x509"
"errors"
"io/ioutil"
"log"
"time"
)
type Drain struct {
Transport string `yaml:"transport"`
Address string `yaml:"address"`
CA string `yaml:"ca"`
}
type Drainer interface {
Drain(line string, tag string) error
}
const ServerPollingInterval = 5 * time.Second
type drainer struct {
errorLogger *log.Logger
logger *Logger
hostname string
structuredData string
}
func NewDrainer(errorLogger *log.Logger, drain Drain, hostname, structuredData string) (*drainer, error) {
err := errors.New("non-nil")
var logger *Logger
var certPool *x509.CertPool
if len(drain.CA) != 0 {
ca, err := ioutil.ReadFile(drain.CA)
if err != nil {
errorLogger.Printf("Error reading ca certificate: %s \n", err.Error())
return nil, err
}
certPool = x509.NewCertPool()
certPool.AppendCertsFromPEM(ca)
}
for err != nil {
logger, err = Dial(
hostname,
drain.Transport,
drain.Address,
certPool,
30*time.Second,
30*time.Second,
99990,
)
if err != nil {
errorLogger.Printf("Connection error: %s, Will retry in %d \n", err.Error(), ServerPollingInterval)
time.Sleep(ServerPollingInterval)
}
}
if err != nil {
return nil, err
}
return &drainer{
logger: logger,
hostname: hostname,
structuredData: structuredData,
errorLogger: errorLogger,
}, nil
}
func (d *drainer) Drain(line string, tag string) error {
d.logger.Packets <- Packet{
Severity: SevInfo,
Facility: LogUser,
StructuredData: d.structuredData,
Hostname: d.hostname,
Tag: tag,
Time: time.Now(),
Message: line,
}
select {
case err := <-d.logger.Errors:
d.errorLogger.Printf("Error sending syslog packet: %s", err)
return err
default:
return nil
}
}