Skip to content

Commit

Permalink
capture uses metrics too
Browse files Browse the repository at this point in the history
  • Loading branch information
mosajjal committed Jan 12, 2022
1 parent a0eeab9 commit 9bdfa6a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 26 deletions.
18 changes: 11 additions & 7 deletions capture/capture_dnstap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {

Expand Down Expand Up @@ -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()))

}
}
}
21 changes: 9 additions & 12 deletions capture/capture_pcap.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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++

Expand All @@ -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()))

}

Expand Down
7 changes: 0 additions & 7 deletions capture/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 9bdfa6a

Please sign in to comment.