-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
67 lines (54 loc) · 1.21 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
package main
import (
"flag"
"fmt"
"log"
"net"
"github.com/olegfedoseev/pinba"
)
var (
in_addr = flag.String("in", "0.0.0.0:30002", "incoming socket")
raw = flag.Bool("raw", false, "dump raw request object")
rawBytes = flag.Bool("bytes", false, "dump raw request bytes in hex format")
)
func main() {
flag.Parse()
addr, err := net.ResolveUDPAddr("udp4", *in_addr)
if err != nil {
log.Fatalf("Can't resolve address: '%v'", err)
}
sock, err := net.ListenUDP("udp4", addr)
if err != nil {
log.Fatalf("Can't open UDP socket: '%v'", err)
}
log.Printf("Start listening on udp://%v\n", *in_addr)
defer sock.Close()
for {
var buf = make([]byte, 65536)
rlen, _, err := sock.ReadFromUDP(buf)
if err != nil {
log.Fatalf("Error on sock.ReadFrom, %v", err)
}
if rlen == 0 {
continue
}
if *rawBytes {
fmt.Printf("%#v\n", buf[0:rlen])
}
request, err := pinba.NewRequest(buf[0:rlen])
if err != nil {
fmt.Printf("Error: %v", err)
continue
}
if *raw {
fmt.Printf("%#v\n", request)
}
log.Printf("%s %s: %3.2f %s\n",
request.ServerName,
request.ScriptName,
request.RequestTime,
request.Tags.String(),
)
fmt.Printf("\t%s\n", request.Timers.String())
}
}