This repository has been archived by the owner on Nov 5, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
176 lines (147 loc) · 4.32 KB
/
main.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
package main
import (
"bytes"
"fmt"
"log"
"net"
"strings"
"sync"
"time"
"github.com/Luzifer/rconfig"
"github.com/fluent/fluent-logger-golang/fluent"
"github.com/fsouza/go-dockerclient"
"github.com/robertkrimen/otto"
)
var (
version = "dev"
logstream chan *message
cfg config
client *docker.Client
err error
jsVM = otto.New()
jsLineConverter *otto.Script
containerCache = map[string]*docker.Container{}
containerCacheLock sync.RWMutex
)
type config struct {
PapertrailEndpoint string `flag:"papertrail-endpoint" description:"[DEPRECATED] Logging target in PaperTrail (TCP, Plain)"`
DockerAPI string `flag:"docker-endpoint" default:"/var/run/docker.sock" description:"Address of docker endpoint to use"`
Testing bool `flag:"testing" default:"false" description:"Do not stream but write to STDOUT"`
LineConverter string `flag:"line-converter" default:"lineconverter.js" description:"Sets the JavaScript to compile the log line to be sent"`
SysLogEndpoint string `flag:"endpoint" description:"TCP/plain capable syslog endpoint (PaperTrail, Loggly, ...)"`
ListenAddress string `flag:"listen" default:"localhost:24224" description:"Listen address for fluentd protocol"`
}
func main() {
rconfig.Parse(&cfg)
// Transistion for deprecated --papertrail-endpoint parameter
if cfg.SysLogEndpoint == "" && cfg.PapertrailEndpoint != "" {
cfg.SysLogEndpoint = cfg.PapertrailEndpoint
}
logstream = make(chan *message, 5000)
jsLineConverter, err = jsVM.Compile(cfg.LineConverter, nil)
if err != nil {
log.Fatalf("Unable to parse line converter script: %s", err)
}
log.Printf("Started dockerlogstream %s", version)
// Connect to Docker socket
client, err = docker.NewClient("unix://" + cfg.DockerAPI)
if err != nil {
log.Fatalf("Unable to connect to Docker daemon: %s", err)
}
if cfg.Testing {
// Log to STDOUT instead of streaming
ta := TestAdapter{}
go ta.Stream(logstream)
} else {
// Create sending part of the logger
sl, err := NewSyslogAdapter(cfg.SysLogEndpoint)
if err != nil {
log.Fatalf("Unable to create logging adapter: %s", err)
}
go sl.Stream(logstream)
}
fluentServer, err := net.Listen("tcp", cfg.ListenAddress)
if err != nil {
log.Fatalf("Unable to listen: %s", err)
}
for {
conn, err := fluentServer.Accept()
if err != nil {
log.Fatalf("Unable to accept tcp connection: %s", err)
}
go handleFluentdForwardConnection(conn)
}
}
func handleFluentdForwardConnection(c net.Conn) {
defer c.Close()
buffer := bytes.NewBuffer([]byte{})
tmp := make([]byte, 256)
for {
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
n, err := c.Read(tmp)
if err != nil {
if oe, ok := err.(*net.OpError); ok {
if oe.Temporary() {
continue
}
}
break
}
buffer.Write(tmp[:n])
for {
var (
msg fluent.Message
newBytes []byte
storedBytes = buffer.Bytes()
)
newBytes, err = msg.UnmarshalMsg(storedBytes)
if err != nil {
buffer = bytes.NewBuffer(storedBytes)
break
}
buffer = bytes.NewBuffer(newBytes)
if err := handleLogMessage(msg); err != nil {
log.Printf("Unable to process log message: %#v -- ERR=%s", msg, err)
}
}
}
}
func handleLogMessage(msg fluent.Message) error {
/*
* fluent.Message{
* Tag:"docker.51081fbd2352",
* Time:1457009998,
* Record:map[string]interface {}{
* "log":"foobar\r",
* "container_id":"51081fbd2352f70ddba441d6b2f91d24bdddc9a5ac32e82c5c5893ff9bf0de6b",
* "container_name":"/tiny_williams",
* "source":"stdout"
* },
* Option:interface {}(nil)
* }
*/
data := msg.Record.(map[string]interface{})
container, err := getContainerInformation(data["container_id"].(string))
if err != nil {
return fmt.Errorf("Unable to fetch container information: %s", err)
}
logstream <- &message{
Container: container,
Data: strings.TrimSpace(data["log"].(string)),
Time: time.Unix(msg.Time, 0),
}
return nil
}
func getContainerInformation(id string) (*docker.Container, error) {
containerCacheLock.RLock()
if c, ok := containerCache[id]; ok {
containerCacheLock.RUnlock()
return c, nil
}
containerCacheLock.RUnlock()
container, err := client.InspectContainer(id)
containerCacheLock.Lock()
containerCache[id] = container
containerCacheLock.Unlock()
return container, err
}