-
Notifications
You must be signed in to change notification settings - Fork 1
/
stats_postgres.go
60 lines (52 loc) · 1.31 KB
/
stats_postgres.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
package main
import (
"database/sql"
cdns "github.com/niclabs/dnszeppelin"
// dns "github.com/miekg/dns"
"log"
"strings"
// "time"
)
func PGWrite(db *sql.DB, batch []cdns.DNSResult, size int) error {
l := len(batch)
for i := 0; i < l; i = i + size {
s := size
if (i > l - size) { s = l-i }
go func() {
vs := make([]string,s)
va := []interface{}{}
tx,err := db.Begin()
if err != nil {
log.Fatal("Error TX Begin ", err)
}
for j := 0; j < size; j++ {
vs[j] = "(?, ?, ?, ?, ?, ?, ?)"
ts := batch[i + j].Timestamp
src := batch[i + j].SrcIP.String()
dst := batch[i + j].DstIP.String()
qname := strings.ToLower(batch[i + j].DNS.Question[0].Name)
isQuestion := ! batch[i + j].DNS.Response
var code int
if isQuestion {
code = int(batch[i + j].DNS.Question[0].Qtype)
} else {
code = int(batch[i + j].DNS.Rcode)
}
pl := int(batch[i + j].PacketLength)
va = append(va, ts, src, dst, qname, isQuestion, code, pl)
}
str := "INSERT INTO msg (time,src,dst,qname,iq,code,len) VALUES "
str = str + strings.Join(vs,",")
_, err = tx.Exec(str, va...)
if err != nil {
tx.Rollback()
log.Fatal("Error Exec ", err)
}
err = tx.Commit()
if err != nil {
log.Fatal("Error db TX ",err)
}
} () // End go func()
}
return nil
}