From 56b493283e28c9a383bbe7dc359add9e2ba4f081 Mon Sep 17 00:00:00 2001 From: maride Date: Wed, 8 Jan 2020 08:56:11 +0100 Subject: [PATCH] Add statistics about how many packets were processed --- analyze/analyzer.go | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/analyze/analyzer.go b/analyze/analyzer.go index 0f7b0de..44e1410 100644 --- a/analyze/analyzer.go +++ b/analyze/analyzer.go @@ -1,11 +1,19 @@ package analyze import ( + "fmt" + "git.darknebu.la/maride/pancap/output" "git.darknebu.la/maride/pancap/protocol" "github.com/google/gopacket" "log" ) +var ( + // Store total amount and amount of visited packets + totalPackets int + processedPackets int +) + func Analyze(source *gopacket.PacketSource) error { // Loop over all packets now for { @@ -19,13 +27,23 @@ func Analyze(source *gopacket.PacketSource) error { continue } + // Track if we didn't process a packet + processed := false + // Iterate over all possible protocols for _, p := range protocol.Protocols { // Check if this protocol can handle this packet if p.CanAnalyze(packet) { handleErr(p.Analyze(packet)) + processed = true } } + + // Raise statistics + totalPackets += 1 + if processed { + processedPackets += 1 + } } return nil @@ -33,6 +51,11 @@ func Analyze(source *gopacket.PacketSource) error { // Prints all the summaries. func PrintSummary() { + // First, print base information collected while analyzing + content := fmt.Sprintf("Processed %d out of %d packets (%d%%)", processedPackets, totalPackets, processedPackets*100/totalPackets) + output.PrintBlock("Overall statistics", content) + + // Print summary of each protocol for _, p := range protocol.Protocols { p.PrintSummary() }