Add support for LTSV query logging
This commit is contained in:
parent
5870ffa7d0
commit
796186a078
|
@ -93,7 +93,7 @@ func ConfigLoad(proxy *Proxy, config_file string) error {
|
||||||
} else {
|
} else {
|
||||||
config.QueryLog.Format = strings.ToLower(config.QueryLog.Format)
|
config.QueryLog.Format = strings.ToLower(config.QueryLog.Format)
|
||||||
}
|
}
|
||||||
if config.QueryLog.Format != "tsv" {
|
if config.QueryLog.Format != "tsv" && config.QueryLog.Format != "ltsv" {
|
||||||
return errors.New("Unsupported query log format")
|
return errors.New("Unsupported query log format")
|
||||||
}
|
}
|
||||||
proxy.queryLogFile = config.QueryLog.File
|
proxy.queryLogFile = config.QueryLog.File
|
||||||
|
|
|
@ -56,7 +56,7 @@ block_ipv6 = false
|
||||||
### Full path to the query log file
|
### Full path to the query log file
|
||||||
file = "/tmp/query.log"
|
file = "/tmp/query.log"
|
||||||
|
|
||||||
### Query log format (currently supported: tsv)
|
### Query log format (currently supported: tsv and ltsv)
|
||||||
format = "tsv"
|
format = "tsv"
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -12,6 +12,7 @@ import (
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
lru "github.com/hashicorp/golang-lru"
|
lru "github.com/hashicorp/golang-lru"
|
||||||
|
"github.com/jedisct1/dlog"
|
||||||
"github.com/miekg/dns"
|
"github.com/miekg/dns"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -226,6 +227,7 @@ func (plugin *PluginBlockIPv6) Eval(pluginsState *PluginsState, msg *dns.Msg) er
|
||||||
type PluginQueryLog struct {
|
type PluginQueryLog struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
outFd *os.File
|
outFd *os.File
|
||||||
|
format string
|
||||||
}
|
}
|
||||||
|
|
||||||
func (plugin *PluginQueryLog) Name() string {
|
func (plugin *PluginQueryLog) Name() string {
|
||||||
|
@ -244,6 +246,7 @@ func (plugin *PluginQueryLog) Init(proxy *Proxy) error {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
plugin.outFd = outFd
|
plugin.outFd = outFd
|
||||||
|
plugin.format = proxy.queryLogFormat
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -262,10 +265,6 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
question := questions[0]
|
question := questions[0]
|
||||||
now := time.Now()
|
|
||||||
year, month, day := now.Date()
|
|
||||||
hour, minute, second := now.Clock()
|
|
||||||
tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second)
|
|
||||||
var clientIPStr string
|
var clientIPStr string
|
||||||
if pluginsState.clientProto == "udp" {
|
if pluginsState.clientProto == "udp" {
|
||||||
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
|
clientIPStr = (*pluginsState.clientAddr).(*net.UDPAddr).IP.String()
|
||||||
|
@ -280,7 +279,19 @@ func (plugin *PluginQueryLog) Eval(pluginsState *PluginsState, msg *dns.Msg) err
|
||||||
if !ok {
|
if !ok {
|
||||||
qType = string(qType)
|
qType = string(qType)
|
||||||
}
|
}
|
||||||
line := fmt.Sprintf("%s\t%s\t%s\t%s\n", tsStr, clientIPStr, qName, qType)
|
var line string
|
||||||
|
if plugin.format == "tsv" {
|
||||||
|
now := time.Now()
|
||||||
|
year, month, day := now.Date()
|
||||||
|
hour, minute, second := now.Clock()
|
||||||
|
tsStr := fmt.Sprintf("[%d-%02d-%02d %02d:%02d:%02d]", year, int(month), day, hour, minute, second)
|
||||||
|
line = fmt.Sprintf("%s\t%s\t%s\t%s\n", tsStr, clientIPStr, qName, qType)
|
||||||
|
} else if plugin.format == "ltsv" {
|
||||||
|
line = fmt.Sprintf("time:%d\thost:%s\tmessage:%s\ttype:%s\n",
|
||||||
|
time.Now().Unix(), clientIPStr, qName, qType)
|
||||||
|
} else {
|
||||||
|
dlog.Fatalf("Unexpected log format: [%s]", plugin.format)
|
||||||
|
}
|
||||||
plugin.Lock()
|
plugin.Lock()
|
||||||
if plugin.outFd == nil {
|
if plugin.outFd == nil {
|
||||||
return errors.New("Log file not initialized")
|
return errors.New("Log file not initialized")
|
||||||
|
|
Loading…
Reference in New Issue