File tree Expand file tree Collapse file tree 2 files changed +53
-2
lines changed Expand file tree Collapse file tree 2 files changed +53
-2
lines changed Original file line number Diff line number Diff line change @@ -3,6 +3,7 @@ package capture
3
3
import (
4
4
"context"
5
5
"errors"
6
+ "expvar"
6
7
"fmt"
7
8
"io"
8
9
"log"
@@ -22,9 +23,20 @@ import (
22
23
"github.com/google/gopacket/pcap"
23
24
)
24
25
26
+ var stats * expvar.Map
27
+
28
+ func init () {
29
+ stats = expvar .NewMap ("raw" )
30
+ stats .Init ()
31
+ }
32
+
25
33
// PacketHandler is a function that is used to handle packets
26
34
type PacketHandler func (* tcp.Packet )
27
35
36
+ type PcapStatProvider interface {
37
+ Stats () (* pcap.Stats , error )
38
+ }
39
+
28
40
// PcapOptions options that can be set on a pcap capture handle,
29
41
// these options take effect on inactive pcap handles
30
42
type PcapOptions struct {
@@ -339,10 +351,19 @@ func (l *Listener) read(handler PacketHandler) {
339
351
}
340
352
}
341
353
354
+ timer := time .NewTicker (1 * time .Second )
355
+
342
356
for {
343
357
select {
344
358
case <- l .quit :
345
359
return
360
+ case <- timer .C :
361
+ if h , ok := hndl .handler .(PcapStatProvider ); ok {
362
+ s , _ := h .Stats ()
363
+ stats .Add ("packets_received" , int64 (s .PacketsReceived ))
364
+ stats .Add ("packets_dropped" , int64 (s .PacketsDropped ))
365
+ stats .Add ("packets_if_dropped" , int64 (s .PacketsIfDropped ))
366
+ }
346
367
default :
347
368
data , ci , err := hndl .handler .ReadPacketData ()
348
369
if err == nil {
Original file line number Diff line number Diff line change 3
3
package main
4
4
5
5
import (
6
- _ "expvar"
6
+ "expvar"
7
7
"flag"
8
+ "fmt"
8
9
"log"
9
10
"net/http"
10
11
"net/http/httputil"
11
- _ "net/http/pprof"
12
+ httppptof "net/http/pprof"
12
13
"os"
13
14
"os/signal"
14
15
"runtime"
22
23
memprofile = flag .String ("memprofile" , "" , "write memory profile to this file" )
23
24
)
24
25
26
+ func init () {
27
+ var defaultServeMux http.ServeMux
28
+ http .DefaultServeMux = & defaultServeMux
29
+
30
+ http .HandleFunc ("/debug/vars" , func (w http.ResponseWriter , r * http.Request ) {
31
+ w .Header ().Set ("Content-Type" , "application/json; charset=utf-8" )
32
+ fmt .Fprintf (w , "{\n " )
33
+ first := true
34
+ expvar .Do (func (kv expvar.KeyValue ) {
35
+ if kv .Key == "memstats" || kv .Key == "cmdline" {
36
+ return
37
+ }
38
+
39
+ if ! first {
40
+ fmt .Fprintf (w , ",\n " )
41
+ }
42
+ first = false
43
+ fmt .Fprintf (w , "%q: %s" , kv .Key , kv .Value )
44
+ })
45
+ fmt .Fprintf (w , "\n }\n " )
46
+ })
47
+
48
+ http .HandleFunc ("/debug/pprof/" , httppptof .Index )
49
+ http .HandleFunc ("/debug/pprof/cmdline" , httppptof .Cmdline )
50
+ http .HandleFunc ("/debug/pprof/profile" , httppptof .Profile )
51
+ http .HandleFunc ("/debug/pprof/symbol" , httppptof .Symbol )
52
+ http .HandleFunc ("/debug/pprof/trace" , httppptof .Trace )
53
+ }
54
+
25
55
func loggingMiddleware (addr string , next http.Handler ) http.Handler {
26
56
return http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
27
57
if r .URL .Path == "/loop" {
You can’t perform that action at this time.
0 commit comments