-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
158 lines (137 loc) · 3.62 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
/*
* Copyright (c) 2017, 2019 Farsight Security, Inc.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package main
import (
"io"
"log"
"net"
"os"
"strings"
"time"
"github.com/farsightsec/go-nmsg"
"github.com/farsightsec/sielink/client"
)
// Context stores state and configuration referenced by multiple functions.
type Context struct {
*Config
client.Client
nmsg.Output
stats
}
func traceMsg(ctx *Context, fmt string, args ...interface{}) {
if !ctx.Trace {
return
}
log.Printf(fmt, args...)
}
type statCounter struct {
Bytes, Messages uint64
}
func (sc *statCounter) Add(n uint64) {
if sc == nil {
return
}
sc.Bytes += n
sc.Messages++
}
type statWriter struct {
wstats *statCounter
errstats *statCounter
io.Writer
}
func (sw *statWriter) Write(p []byte) (n int, err error) {
n, err = sw.Writer.Write(p)
if err != nil {
sw.errstats.Add(uint64(n))
return
}
sw.wstats.Add(uint64(n))
return
}
type stats struct {
StartTime time.Time
DnstapIn, DnstapError, DnstapFiltered statCounter
QnameFiltered statCounter
NmsgOut statCounter
NmsgUp, NmsgError, NmsgDiscard statCounter
}
func (s *stats) Log() {
log.Printf("Uptime: %s dnstap-input %d bytes / %d msgs; "+
"dnstap-error %d bytes / %d msgs; "+
"dnstap-filtered %d bytes / %d msgs; "+
"qname-filtered %d bytes / %d msgs; "+
"nmsg-out %d bytes / %d msgs; "+
"nmsg-up %d bytes / %d msgs; "+
"nmsg-error %d bytes / %d msgs; "+
"nmsg-discard %d bytes / %d msgs; ",
time.Duration(time.Since(s.StartTime).Seconds())*time.Second,
s.DnstapIn.Bytes, s.DnstapIn.Messages,
s.DnstapError.Bytes, s.DnstapError.Messages,
s.DnstapFiltered.Bytes, s.DnstapFiltered.Messages,
s.QnameFiltered.Bytes, s.QnameFiltered.Messages,
s.NmsgOut.Bytes, s.NmsgOut.Messages,
s.NmsgUp.Bytes, s.NmsgUp.Messages,
s.NmsgError.Bytes, s.NmsgError.Messages,
s.NmsgDiscard.Bytes, s.NmsgDiscard.Messages,
)
}
func main() {
var err error
// leave date stamp to external logger.
log.SetFlags(0)
ctx := new(Context)
ctx.Config, err = parseConfig(os.Args[1:])
if err != nil {
log.Fatal(err)
}
cconfig := &client.Config{
Heartbeat: ctx.Config.Heartbeat.Duration,
URL: "http://localhost/dnstap-client",
APIKey: ctx.Config.APIKey.String(),
}
ctx.stats.StartTime = time.Now()
if len(ctx.Config.Servers) > 0 {
ctx.Client = client.NewClient(cconfig)
}
for _, s := range ctx.Config.Servers {
if !strings.HasPrefix(s.Path, "/session/") {
s.Path = "/session/dnstap-sensor-upload"
}
go func(uri string) {
for {
log.Printf("Connecting to %s", uri)
log.Printf("%s: connection closed: %v", uri, ctx.Client.DialAndHandle(uri))
if ctx.Config.Retry.Duration == 0 {
log.Printf("No retry specified. Abandoning %s", uri)
return
}
<-time.After(ctx.Config.Retry.Duration)
}
}(s.String())
}
if ctx.Config.UDPOutput.UDPAddr != nil {
conn, err := net.DialUDP("udp", nil, ctx.Config.UDPOutput.UDPAddr)
if err != nil {
log.Fatalf("Failed to dial %s: %v", ctx.Config.UDPOutput, err)
}
statConn := &statWriter{
Writer: conn,
wstats: &ctx.NmsgOut,
}
ctx.Output = nmsg.TimedBufferedOutput(statConn, ctx.Config.Flush.Duration)
ctx.Output.SetSequenced(true)
ctx.Output.SetMaxSize(ctx.Config.MTU, ctx.Config.MTU)
}
ticker := time.NewTicker(ctx.Config.StatsInterval.Duration)
go func() {
for _ = range ticker.C {
ctx.stats.Log()
}
}()
ctx.Config.DnstapInput.run(ctx)
}