-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathstatstee.go
84 lines (70 loc) · 1.88 KB
/
statstee.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package main
import (
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"github.com/rodaine/statstee/datagram"
"github.com/rodaine/statstee/router"
"github.com/rodaine/statstee/streams"
"github.com/rodaine/statstee/views"
"golang.org/x/net/context"
)
const (
logFile = "statstee.log"
)
var (
logWriter io.WriteCloser
streamError error
deviceInterface string = streams.LoopbackAbbr
sniffedPort int = streams.DefaultStatsDPort
outputDebug bool = false
listenMode bool = false
captureMode bool = false
)
func init() {
flag.StringVar(&deviceInterface, "d", deviceInterface, "network device to capture on")
flag.IntVar(&sniffedPort, "p", sniffedPort, "port to capture on")
flag.BoolVar(&listenMode, "l", listenMode, "force listen mode, error if the port cannot be bound")
flag.BoolVar(&captureMode, "c", captureMode, "force capture mode, even if StatsD is not present")
flag.BoolVar(&outputDebug, "v", outputDebug, "display debug output to "+logFile)
flag.Parse()
log.SetOutput(ioutil.Discard)
}
func main() {
if outputDebug {
logWriter, _ = os.OpenFile(logFile, os.O_TRUNC|os.O_CREATE|os.O_WRONLY, 0644)
log.SetFlags(log.Lmicroseconds | log.LstdFlags | log.Lshortfile)
log.SetOutput(logWriter)
defer logWriter.Close()
}
mode := streams.DefaultMode
switch {
case listenMode:
mode = streams.ListenMode
case captureMode:
mode = streams.CaptureMode
}
stream, err := streams.ResolveStream(mode, deviceInterface, sniffedPort)
fatalIfError(err)
parser := datagram.NewParser()
router := router.New(parser.Chan())
go captureMetrics(router)
go parser.Parse(stream.Chan())
go stream.Listen(context.TODO())
fatalIfError(views.Loop(router))
fatalIfError(streamError)
}
func captureMetrics(r *router.Router) {
r.Listen()
views.Quit()
}
func fatalIfError(err error) {
if err != nil {
log.Println(err)
fmt.Fprintln(os.Stderr, err)
os.Exit(1)
}
}