-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
90 lines (75 loc) · 1.89 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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package main
import (
"encoding/csv"
"encoding/json"
"os"
"sort"
"github.com/procore/pgnetdetective/metrics"
"github.com/procore/pgnetdetective/processing"
"github.com/codegangsta/cli"
"github.com/google/gopacket/pcap"
)
func main() {
app := cli.NewApp()
app.Name = "pgnetdetective"
app.Version = "0.1"
app.Usage = "Analyze Postgres Network Traffic Captures"
app.Flags = []cli.Flag{
cli.BoolFlag{
Name: "bytes",
Usage: "Display bytes instead of as Human-Readable",
},
cli.StringFlag{
Name: "output",
Value: "text",
Usage: "Specify output file format: [text|json|csv]",
},
cli.IntFlag{
Name: "limit",
Value: 0,
Usage: "Limit output based on NetworkLoad size in kilobytes",
},
}
app.Action = func(c *cli.Context) {
if len(c.Args()) != 1 {
cli.ShowAppHelp(c)
os.Exit(0)
}
path := c.Args()[0]
// Open the .cap file
handle, err := pcap.OpenOffline(path)
if err != nil {
panic(err)
}
combinedQueryMetrics, responses := processing.ExtractPGPackets(handle)
processing.AssociatePGPackets(combinedQueryMetrics, responses)
if c.Int("limit") > 0 {
limitedQueryMetrics := metrics.NewQueryMetrics()
limit := uint64(c.Int("limit"))
for _, m := range combinedQueryMetrics.List {
if m.TotalNetworkLoad/1000 >= limit {
limitedQueryMetrics.List = append(limitedQueryMetrics.List, m)
}
}
combinedQueryMetrics = limitedQueryMetrics
}
combinedQueryMetrics.DisplayBytes = c.Bool("bytes")
sort.Sort(combinedQueryMetrics)
if c.String("output") == "json" {
out, err := json.Marshal(combinedQueryMetrics)
if err != nil {
panic(err)
}
os.Stdout.Write(out)
} else if c.String("output") == "csv" {
w := csv.NewWriter(os.Stdout)
w.WriteAll(combinedQueryMetrics.CsvString())
if err := w.Error(); err != nil {
panic(err)
}
} else {
combinedQueryMetrics.PrintText()
}
}
app.Run(os.Args)
}