discosrv: Better statistics

This commit is contained in:
Jakob Borg 2014-04-19 23:14:56 +02:00
parent 44a194d226
commit 39be6932b5

View File

@ -4,6 +4,7 @@ import (
"encoding/binary" "encoding/binary"
"encoding/hex" "encoding/hex"
"flag" "flag"
"fmt"
"log" "log"
"net" "net"
"os" "os"
@ -26,22 +27,28 @@ type Address struct {
} }
var ( var (
nodes = make(map[string]Node) nodes = make(map[string]Node)
lock sync.Mutex lock sync.Mutex
queries = 0 queries = 0
answered = 0 announces = 0
limited = 0 answered = 0
debug = false limited = 0
limiter = lru.New(1024) unknowns = 0
debug = false
limiter = lru.New(1024)
) )
func main() { func main() {
var listen string var listen string
var timestamp bool var timestamp bool
var statsIntv int
var statsFile string
flag.StringVar(&listen, "listen", ":22025", "Listen address") flag.StringVar(&listen, "listen", ":22025", "Listen address")
flag.BoolVar(&debug, "debug", false, "Enable debug output") flag.BoolVar(&debug, "debug", false, "Enable debug output")
flag.BoolVar(&timestamp, "timestamp", true, "Timestamp the log output") flag.BoolVar(&timestamp, "timestamp", true, "Timestamp the log output")
flag.IntVar(&statsIntv, "stats-intv", 0, "Statistics output interval (s)")
flag.StringVar(&statsFile, "stats-file", "/var/log/discosrv.stats", "Statistics file name")
flag.Parse() flag.Parse()
log.SetOutput(os.Stdout) log.SetOutput(os.Stdout)
@ -55,7 +62,9 @@ func main() {
log.Fatal(err) log.Fatal(err)
} }
go logStats() if statsIntv > 0 {
go logStats(statsFile, statsIntv)
}
var buf = make([]byte, 1024) var buf = make([]byte, 1024)
for { for {
@ -85,6 +94,11 @@ func main() {
case discover.QueryMagicV2: case discover.QueryMagicV2:
handleQueryV2(conn, addr, buf) handleQueryV2(conn, addr, buf)
default:
lock.Lock()
unknowns++
lock.Unlock()
} }
} }
} }
@ -129,6 +143,10 @@ func handleAnnounceV2(addr *net.UDPAddr, buf []byte) {
log.Printf("<- %v %#v", addr, pkt) log.Printf("<- %v %#v", addr, pkt)
} }
lock.Lock()
announces++
lock.Unlock()
ip := addr.IP.To4() ip := addr.IP.To4()
if ip == nil { if ip == nil {
ip = addr.IP.To16() ip = addr.IP.To16()
@ -197,9 +215,21 @@ func handleQueryV2(conn *net.UDPConn, addr *net.UDPAddr, buf []byte) {
} }
} }
func logStats() { func next(intv int) time.Time {
d := time.Duration(intv) * time.Second
t0 := time.Now()
t1 := t0.Add(d).Truncate(d)
time.Sleep(t1.Sub(t0))
return t1
}
func logStats(file string, intv int) {
f, err := os.OpenFile(file, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0644)
if err != nil {
log.Fatal(err)
}
for { for {
time.Sleep(600 * time.Second) t := next(intv)
lock.Lock() lock.Lock()
@ -211,11 +241,15 @@ func logStats() {
} }
} }
log.Printf("Expired %d nodes; %d nodes in registry; %d queries (%d answered)", deleted, len(nodes), queries, answered) fmt.Fprintf(f, "%d Nr:%d Ne:%d Qt:%d Qa:%d A:%d U:%d Lq:%d Lc:%d\n",
log.Printf("Limited %d queries; %d entries in limiter cache", limited, limiter.Len()) t.Unix(), len(nodes), deleted, queries, answered, announces, unknowns, limited, limiter.Len())
f.Sync()
queries = 0 queries = 0
announces = 0
answered = 0 answered = 0
limited = 0 limited = 0
unknowns = 0
lock.Unlock() lock.Unlock()
} }