diff --git a/capture/capture_dnstap.go b/capture/capture_dnstap.go index 754a089..b106837 100644 --- a/capture/capture_dnstap.go +++ b/capture/capture_dnstap.go @@ -11,6 +11,7 @@ import ( "github.com/mosajjal/dnsmonster/types" "github.com/mosajjal/dnsmonster/util" + "github.com/rcrowley/go-metrics" log "github.com/sirupsen/logrus" dnstap "github.com/dnstap/golang-dnstap" @@ -86,19 +87,23 @@ func dnsTapMsgToDNSResult(msg []byte) types.DNSResult { func StartDNSTap(resultChannel chan types.DNSResult) { log.Info("Starting DNStap capture") + + packetsCaptured := metrics.GetOrRegisterGauge("packetsCaptured", metrics.DefaultRegistry) + packetsDropped := metrics.GetOrRegisterGauge("packetsDropped", metrics.DefaultRegistry) + packetLossPercent := metrics.GetOrRegisterGaugeFloat64("packetLossPercent", metrics.DefaultRegistry) + input := parseDnstapSocket(util.CaptureFlags.DnstapSocket, util.CaptureFlags.DnstapPermission) buf := make(chan []byte, 1024) ratioCnt := 0 - totalCnt := uint(0) + totalCnt := int64(0) // Setup SIGINT handling handleDNSTapInterrupt(done) // Set up various tickers for different tasks captureStatsTicker := time.NewTicker(util.GeneralFlags.CaptureStatsDelay) - printStatsTicker := time.NewTicker(util.GeneralFlags.PrintStatsDelay) for { @@ -128,11 +133,10 @@ func StartDNSTap(resultChannel chan types.DNSResult) { case <-done: return case <-captureStatsTicker.C: - pcapStats.PacketsGot = totalCnt - pcapStats.PacketsLost = 0 - pcapStats.PacketLossPercent = (float32(pcapStats.PacketsLost) * 100.0 / float32(pcapStats.PacketsGot)) - case <-printStatsTicker.C: - log.Infof("%+v", pcapStats) + packetsCaptured.Update(totalCnt) + packetsDropped.Update(0) //todo: this is not correct, need to fix + packetLossPercent.Update(float64(packetsDropped.Value()) * 100.0 / float64(packetsCaptured.Value())) + } } } diff --git a/capture/capture_pcap.go b/capture/capture_pcap.go index 45ffd0d..fef93aa 100644 --- a/capture/capture_pcap.go +++ b/capture/capture_pcap.go @@ -4,14 +4,13 @@ import ( "time" "github.com/mosajjal/dnsmonster/util" + "github.com/rcrowley/go-metrics" log "github.com/sirupsen/logrus" "os" "os/signal" ) -var pcapStats captureStats - func handleInterrupt() { c := make(chan os.Signal, 1) signal.Notify(c, os.Interrupt) @@ -67,6 +66,9 @@ func NewDNSCapturer(options CaptureOptions) DNSCapturer { } func (capturer *DNSCapturer) Start() { + packetsCaptured := metrics.GetOrRegisterGauge("packetsCaptured", metrics.DefaultRegistry) + packetsDropped := metrics.GetOrRegisterGauge("packetsDropped", metrics.DefaultRegistry) + packetLossPercent := metrics.GetOrRegisterGaugeFloat64("packetLossPercent", metrics.DefaultRegistry) var myHandler genericHandler @@ -99,10 +101,9 @@ func (capturer *DNSCapturer) Start() { // Set up various tickers for different tasks captureStatsTicker := time.NewTicker(util.GeneralFlags.CaptureStatsDelay) - printStatsTicker := time.NewTicker(util.GeneralFlags.PrintStatsDelay) var ratioCnt = 0 - var totalCnt = uint(0) + var totalCnt = int64(0) for { ratioCnt++ @@ -125,16 +126,12 @@ func (capturer *DNSCapturer) Start() { packets, drop := myHandler.Stat() if packets == 0 { // to make up for pcap not being able to get stats - pcapStats.PacketsGot = totalCnt + packetsCaptured.Update(totalCnt) } else { - pcapStats.PacketsGot = packets - pcapStats.PacketsLost = drop + packetsCaptured.Update(int64(packets)) + packetsCaptured.Update(int64(drop)) } - - pcapStats.PacketLossPercent = (float32(pcapStats.PacketsLost) * 100.0 / float32(pcapStats.PacketsGot)) - - case <-printStatsTicker.C: - log.Infof("%+v", pcapStats) + packetLossPercent.Update(float64(packetsDropped.Value()) * 100.0 / float64(packetsCaptured.Value())) } diff --git a/capture/types.go b/capture/types.go index fefacfd..2d26a6f 100644 --- a/capture/types.go +++ b/capture/types.go @@ -101,13 +101,6 @@ type DNSCapturer struct { processing chan rawPacketBytes } -// captureStats is capturing statistics about our current live captures. At this point it's not accurate for PCAP files. -type captureStats struct { - PacketsGot uint - PacketsLost uint - PacketLossPercent float32 -} - // ipv6 is a struct to be used as a key. type ipv6 struct { ip4 gopacket.Flow