forked from gliderlabs/logspout
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogspout.go
353 lines (304 loc) · 9.34 KB
/
logspout.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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
package main
import (
"encoding/json"
"fmt"
"io"
"log"
"log/syslog"
"net"
"net/http"
"net/url"
"os"
"strconv"
"strings"
"time"
"code.google.com/p/go.net/websocket"
"github.com/fsouza/go-dockerclient"
"github.com/go-martini/martini"
"github.com/streadway/amqp"
)
var debugMode bool
func debug(v ...interface{}) {
if debugMode {
log.Println(v...)
}
}
func assert(err error, context string) {
if err != nil {
log.Fatal(context+": ", err)
}
}
func getopt(name, dfault string) string {
value := os.Getenv(name)
if value == "" {
value = dfault
}
return value
}
type Colorizer map[string]int
// returns up to 14 color escape codes (then repeats) for each unique key
func (c Colorizer) Get(key string) string {
i, exists := c[key]
if !exists {
c[key] = len(c)
i = c[key]
}
bright := "1;"
if i%14 > 6 {
bright = ""
}
return "\x1b[" + bright + "3" + strconv.Itoa(7-(i%7)) + "m"
}
func rabbitmqStreamer(target Target, types []string, logstream chan *Log) {
typestr := "," + strings.Join(types, ",") + ","
log.Printf("Ehrmegerd")
// Connects opens an AMQP connection from the credentials in the URL.
conn, err := amqp.Dial(os.Args[1])
if err != nil {
log.Fatalf("connection.open: %s", err)
}
// This waits for a server acknowledgment which means the sockets will have
// flushed all outbound publishings prior to returning. It's important to
// block on Close to not lose any publishings.
defer conn.Close()
c, err := conn.Channel()
if err != nil {
log.Fatalf("channel.open: %s", err)
}
for logline := range logstream {
if typestr != ",," && !strings.Contains(typestr, logline.Type) {
continue
}
// We declare our topology on both the publisher and consumer to ensure they
// are the same. This is part of AMQP being a programmable messaging model.
//
// See the Channel.Consume example for the complimentary declare.
// UNCOMMENT ME WHEN THIS STUFF IS READY
err = c.ExchangeDeclarePassive("logstash_exchange", "direct", true, false, false, false, nil)
if err != nil {
log.Fatalf("exchange.declare: %v", err)
}
// Prepare this message to be persistent. Your publishing requirements may
// be different.
msg := amqp.Publishing{
//Headers: amqp.Table{},
//ContentType: "text/plain",
//ContentEncoding: "UTF-8",
// DeliveryMode: amqp.Transient,
DeliveryMode: amqp.Persistent,
Priority: 0,
Timestamp: time.Now(),
Body: []byte(logline.Data),
}
// This is not a mandatory delivery, so it will be dropped if there are no
// queues bound to the logstash exchange.
err = c.Publish("logstash_exchange", "logstash", false, false, msg)
if err != nil {
// Since publish is asynchronous this can happen if the network connection
// is reset or if the server has run out of resources.
log.Fatalf("basic.publish: %v", err)
}
}
}
func syslogStreamer(target Target, types []string, logstream chan *Log) {
typestr := "," + strings.Join(types, ",") + ","
for logline := range logstream {
if typestr != ",," && !strings.Contains(typestr, logline.Type) {
continue
}
tag := logline.Name + target.AppendTag
remote, err := syslog.Dial("udp", target.Addr, syslog.LOG_USER|syslog.LOG_INFO, tag)
assert(err, "syslog")
io.WriteString(remote, logline.Data)
}
}
func udpStreamer(target Target, types []string, logstream chan *Log) {
typestr := "," + strings.Join(types, ",") + ","
addr, err := net.ResolveUDPAddr("udp", target.Addr)
assert(err, "resolve udp failed")
conn, err := net.DialUDP("udp", nil, addr)
assert(err, "connect udp failed")
encoder := json.NewEncoder(&IgnorantWriter{conn})
defer conn.Close()
for logline := range logstream {
if typestr != ",," && !strings.Contains(typestr, logline.Type) {
continue
}
encoder.Encode(logline)
}
}
func rfc5424Streamer(target Target, types []string, logstream chan *Log) {
typestr := "," + strings.Join(types, ",") + ","
pri := syslog.LOG_USER | syslog.LOG_INFO
hostname, _ := os.Hostname()
c, err := net.Dial("udp", target.Addr)
assert(err, "net dial rfc5424")
if hostname == "" {
hostname = c.LocalAddr().String()
}
for logline := range logstream {
if typestr != ",," && !strings.Contains(typestr, logline.Type) {
continue
}
tag := logline.Name + target.AppendTag
nl := ""
if !strings.HasSuffix(logline.Data, "\n") {
nl = "\n"
}
timestamp := time.Now().Format(time.RFC3339)
_, err := fmt.Fprintf(c, "<%d>1 %s %s %s %d - [%s] %s%s", pri, timestamp, hostname, tag, os.Getpid(), target.StructuredData, logline.Data, nl)
assert(err, "rfc5424")
}
}
func websocketStreamer(w http.ResponseWriter, req *http.Request, logstream chan *Log, closer chan bool) {
websocket.Handler(func(conn *websocket.Conn) {
for logline := range logstream {
if req.URL.Query().Get("type") != "" && logline.Type != req.URL.Query().Get("type") {
continue
}
_, err := conn.Write(append(marshal(logline), '\n'))
if err != nil {
closer <- true
return
}
}
}).ServeHTTP(w, req)
}
func httpStreamer(w http.ResponseWriter, req *http.Request, logstream chan *Log, multi bool) {
var colors Colorizer
var usecolor, usejson bool
nameWidth := 16
if req.URL.Query().Get("colors") != "off" {
colors = make(Colorizer)
usecolor = true
}
if req.Header.Get("Accept") == "application/json" {
w.Header().Add("Content-Type", "application/json")
usejson = true
} else {
w.Header().Add("Content-Type", "text/plain")
}
for logline := range logstream {
if req.URL.Query().Get("types") != "" && logline.Type != req.URL.Query().Get("types") {
continue
}
if usejson {
w.Write(append(marshal(logline), '\n'))
} else {
if multi {
if len(logline.Name) > nameWidth {
nameWidth = len(logline.Name)
}
if usecolor {
w.Write([]byte(fmt.Sprintf(
"%s%"+strconv.Itoa(nameWidth)+"s|%s\x1b[0m\n",
colors.Get(logline.Name), logline.Name, logline.Data,
)))
} else {
w.Write([]byte(fmt.Sprintf(
"%"+strconv.Itoa(nameWidth)+"s|%s\n", logline.Name, logline.Data,
)))
}
} else {
w.Write(append([]byte(logline.Data), '\n'))
}
}
w.(http.Flusher).Flush()
}
}
func main() {
debugMode = getopt("DEBUG", "") != ""
port := getopt("PORT", "8000")
endpoint := getopt("DOCKER_HOST", "unix:///var/run/docker.sock")
routespath := getopt("ROUTESPATH", "/var/lib/logspout")
client, err := docker.NewClient(endpoint)
assert(err, "docker")
attacher := NewAttachManager(client)
router := NewRouteManager(attacher)
if len(os.Args) > 1 {
u, err := url.Parse(os.Args[1])
assert(err, "url")
log.Println("routing all to " + os.Args[1])
r := Route{
Target: Target{
Type: u.Scheme,
Addr: u.Host,
},
}
if u.RawQuery != "" {
v, err := url.ParseQuery(u.RawQuery)
assert(err, "query")
if v.Get("filter") != "" || v.Get("types") != "" {
r.Source = &Source{
Filter: v.Get("filter"),
Types: strings.Split(v.Get("types"), ","),
}
}
r.Target.StructuredData = v.Get("structuredData")
r.Target.AppendTag = v.Get("appendTag")
}
router.Add(&r)
}
if _, err := os.Stat(routespath); err == nil {
log.Println("loading and persisting routes in " + routespath)
assert(router.Load(RouteFileStore(routespath)), "persistor")
}
m := martini.Classic()
m.Get("/logs(?:/(?P<predicate>[a-zA-Z]+):(?P<value>.+))?", func(w http.ResponseWriter, req *http.Request, params martini.Params) {
source := new(Source)
switch {
case params["predicate"] == "id" && params["value"] != "":
source.ID = params["value"][:12]
case params["predicate"] == "name" && params["value"] != "":
source.Name = params["value"]
case params["predicate"] == "filter" && params["value"] != "":
source.Filter = params["value"]
}
if source.ID != "" && attacher.Get(source.ID) == nil {
http.NotFound(w, req)
return
}
logstream := make(chan *Log)
defer close(logstream)
var closer <-chan bool
if req.Header.Get("Upgrade") == "websocket" {
closerBi := make(chan bool)
go websocketStreamer(w, req, logstream, closerBi)
closer = closerBi
} else {
go httpStreamer(w, req, logstream, source.All() || source.Filter != "")
closer = w.(http.CloseNotifier).CloseNotify()
}
attacher.Listen(source, logstream, closer)
})
m.Get("/routes", func(w http.ResponseWriter, req *http.Request) {
w.Header().Add("Content-Type", "application/json")
routes, _ := router.GetAll()
w.Write(append(marshal(routes), '\n'))
})
m.Post("/routes", func(w http.ResponseWriter, req *http.Request) (int, string) {
route := new(Route)
if err := unmarshal(req.Body, route); err != nil {
return http.StatusBadRequest, "Bad request: " + err.Error()
}
// TODO: validate?
router.Add(route)
w.Header().Add("Content-Type", "application/json")
return http.StatusCreated, string(append(marshal(route), '\n'))
})
m.Get("/routes/:id", func(w http.ResponseWriter, req *http.Request, params martini.Params) {
route, _ := router.Get(params["id"])
if route == nil {
http.NotFound(w, req)
return
}
w.Write(append(marshal(route), '\n'))
})
m.Delete("/routes/:id", func(w http.ResponseWriter, req *http.Request, params martini.Params) {
if ok := router.Remove(params["id"]); !ok {
http.NotFound(w, req)
}
})
log.Println("logspout serving http on :" + port)
log.Fatal(http.ListenAndServe(":"+port, m))
}