-
Notifications
You must be signed in to change notification settings - Fork 1
/
clickhouse.go
166 lines (144 loc) · 3.83 KB
/
clickhouse.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
package main
import (
"fmt"
cdns "github.com/niclabs/dnszeppelin"
"github.com/ClickHouse/clickhouse-go"
data "github.com/ClickHouse/clickhouse-go/lib/data"
"log"
"sync"
"time"
)
func connectClickhouseRetry(exiting chan bool, clickhouseHost string) clickhouse.Clickhouse {
tick := time.NewTicker(5 * time.Second)
defer tick.Stop()
for {
c, err := connectClickhouse(exiting, clickhouseHost)
if err == nil {
return c
}
// Error getting connection, wait the timer or check if we are exiting
select {
case <-exiting:
// When exiting, return immediately
return nil
case <-tick.C:
continue
}
}
}
func connectClickhouse(exiting chan bool, clickhouseHost string) (clickhouse.Clickhouse, error) {
connection, err := clickhouse.OpenDirect(fmt.Sprintf("tcp://%v?debug=false", clickhouseHost))
if err != nil {
log.Println(err)
return nil, err
}
return connection, err
}
func min(a, b int) int {
if a > b {
return a
}
return b
}
func ClickHouseCollector(resultChannel chan cdns.DNSResult, exiting chan bool, wg *sync.WaitGroup, clickhouseHost string, batchSize, batchDelay uint, limit int, server string) {
wg.Add(1)
defer wg.Done()
serverByte := []byte(server)
connect := connectClickhouseRetry(exiting, clickhouseHost)
batch := make([]cdns.DNSResult, 0, batchSize)
ticker := time.Tick(time.Duration(batchDelay) * time.Second)
for {
select {
case data := <-resultChannel:
if limit == 0 || len(batch) < limit {
batch = append(batch, data)
}
case <-ticker:
if err := SendData(connect, batch, serverByte); err != nil {
log.Println(err)
connect = connectClickhouseRetry(exiting, clickhouseHost)
} else {
batch = make([]cdns.DNSResult, 0, batchSize)
}
case <-exiting:
return
}
}
}
func SendData(connect clickhouse.Clickhouse, batch []cdns.DNSResult, server []byte) error {
if len(batch) == 0 {
return nil
}
// Return if the connection is null, we are exiting
if connect == nil {
return nil
}
// log.Println("Sending ", len(batch))
_, err := connect.Begin()
if err != nil {
return err
}
_, err = connect.Prepare("INSERT INTO DNS_LOG (DnsDate, timestamp, Server, IPVersion, IP, Protocol, QR, OpCode, Class, Type, ResponseCode, Question, Size, Edns0Present, DoBit, IPdst) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)")
if err != nil {
return err
}
block, err := connect.Block()
if err != nil {
return err
}
blocks := []*data.Block{block}
count := len(blocks)
var wg sync.WaitGroup
wg.Add(len(blocks))
for i := range blocks {
b := blocks[i]
start := i * (len(batch)) / count
end := min((i+1)*(len(batch))/count, len(batch))
go func() {
defer wg.Done()
b.Reserve()
for k := start; k < end; k++ {
for _, dnsQuery := range batch[k].DNS.Question {
b.NumRows++
b.WriteDate(0, batch[k].Timestamp)
b.WriteDateTime(1, batch[k].Timestamp)
b.WriteBytes(2, server)
b.WriteUInt8(3, batch[k].IPVersion)
ip := batch[k].SrcIP.String()
ipdst := batch[k].DstIP.String()
b.WriteString(4, string(ip))
b.WriteFixedString(5, []byte(batch[k].Protocol))
QR := uint8(0)
if batch[k].DNS.Response {
QR = 1
}
b.WriteUInt8(6, QR)
b.WriteUInt8(7, uint8(batch[k].DNS.Opcode))
b.WriteUInt16(8, uint16(dnsQuery.Qclass))
b.WriteUInt16(9, uint16(dnsQuery.Qtype))
b.WriteUInt8(10, uint8(batch[k].DNS.Rcode))
b.WriteString(11, string(dnsQuery.Name))
b.WriteUInt16(12, batch[k].PacketLength)
edns, doBit := uint8(0), uint8(0)
if edns0 := batch[k].DNS.IsEdns0(); edns0 != nil {
edns = 1
if edns0.Do() {
doBit = 1
}
}
b.WriteUInt8(13, edns)
b.WriteUInt8(14, doBit)
b.WriteString(15, ipdst)
}
}
if err := connect.WriteBlock(b); err != nil {
return
}
}()
}
wg.Wait()
if err := connect.Commit(); err != nil {
return err
}
return nil
}