Skip to content

Commit

Permalink
Add packet capture debug stats
Browse files Browse the repository at this point in the history
Also remove potentially sensitive memstat and cmdargs from /debug/vars endopint
  • Loading branch information
buger committed Jul 26, 2021
1 parent c01be50 commit 79ff882
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 2 deletions.
21 changes: 21 additions & 0 deletions capture/capture.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package capture
import (
"context"
"errors"
"expvar"
"fmt"
"io"
"log"
Expand All @@ -22,9 +23,20 @@ import (
"github.com/google/gopacket/pcap"
)

var stats *expvar.Map

func init() {
stats = expvar.NewMap("raw")
stats.Init()
}

// PacketHandler is a function that is used to handle packets
type PacketHandler func(*tcp.Packet)

type PcapStatProvider interface {
Stats() (*pcap.Stats, error)
}

// PcapOptions options that can be set on a pcap capture handle,
// these options take effect on inactive pcap handles
type PcapOptions struct {
Expand Down Expand Up @@ -339,10 +351,19 @@ func (l *Listener) read(handler PacketHandler) {
}
}

timer := time.NewTicker(1 * time.Second)

for {
select {
case <-l.quit:
return
case <-timer.C:
if h, ok := hndl.handler.(PcapStatProvider); ok {
s, _ := h.Stats()
stats.Add("packets_received", int64(s.PacketsReceived))
stats.Add("packets_dropped", int64(s.PacketsDropped))
stats.Add("packets_if_dropped", int64(s.PacketsIfDropped))
}
default:
data, ci, err := hndl.handler.ReadPacketData()
if err == nil {
Expand Down
34 changes: 32 additions & 2 deletions gor.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
package main

import (
_ "expvar"
"expvar"
"flag"
"fmt"
"log"
"net/http"
"net/http/httputil"
_ "net/http/pprof"
httppptof "net/http/pprof"
"os"
"os/signal"
"runtime"
Expand All @@ -22,6 +23,35 @@ var (
memprofile = flag.String("memprofile", "", "write memory profile to this file")
)

func init() {
var defaultServeMux http.ServeMux
http.DefaultServeMux = &defaultServeMux

http.HandleFunc("/debug/vars", func(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json; charset=utf-8")
fmt.Fprintf(w, "{\n")
first := true
expvar.Do(func(kv expvar.KeyValue) {
if kv.Key == "memstats" || kv.Key == "cmdline" {
return
}

if !first {
fmt.Fprintf(w, ",\n")
}
first = false
fmt.Fprintf(w, "%q: %s", kv.Key, kv.Value)
})
fmt.Fprintf(w, "\n}\n")
})

http.HandleFunc("/debug/pprof/", httppptof.Index)
http.HandleFunc("/debug/pprof/cmdline", httppptof.Cmdline)
http.HandleFunc("/debug/pprof/profile", httppptof.Profile)
http.HandleFunc("/debug/pprof/symbol", httppptof.Symbol)
http.HandleFunc("/debug/pprof/trace", httppptof.Trace)
}

func loggingMiddleware(addr string, next http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.URL.Path == "/loop" {
Expand Down

0 comments on commit 79ff882

Please sign in to comment.