-
Notifications
You must be signed in to change notification settings - Fork 10
/
main.go
209 lines (184 loc) · 5 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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
package main
import (
"container/heap"
"flag"
"fmt"
"github.com/google/gopacket"
"github.com/google/gopacket/pcap"
"io/ioutil"
"time"
)
const CAPTURE_SIZE = 9000
// startReportingLoop starts a loop that will periodically output statistics
// on the hottest keys, and optionally, errors that occured in parsing.
func startReportingLoop(config Config, hot_keys *HotKeyPool, errors *HotKeyPool) {
sleep_duration := time.Duration(config.Interval) * time.Second
time.Sleep(sleep_duration)
for {
st := time.Now()
rotated_keys := hot_keys.Rotate()
top_keys := rotated_keys.GetTopKeys()
rotated_errors := errors.Rotate()
top_errors := rotated_errors.GetTopKeys()
// Build output
output := ""
/* Show keys */
i := 0
for {
if top_keys.Len() == 0 {
break
}
/* Check if we've reached the specified key limit, but only if
* the user didn't specify regular expressions to match on. */
if len(config.Regexps) == 0 && i >= config.NumItemsToReport {
break
}
key := heap.Pop(top_keys)
output += fmt.Sprintf("mcsauna.keys.%s %d\n", key.(*Key).Name, key.(*Key).Hits)
i += 1
}
/* Show errors */
if config.ShowErrors {
for top_errors.Len() > 0 {
err := heap.Pop(top_errors)
output += fmt.Sprintf(
"mcsauna.errors.%s %d\n", err.(*Key).Name, err.(*Key).Hits)
}
}
// Write to stdout
if !config.Quiet {
fmt.Print(output)
}
// Write to file
if config.OutputFile != "" {
err := ioutil.WriteFile(config.OutputFile, []byte(output), 0666)
if err != nil {
panic(err)
}
}
elapsed := time.Now().Sub(st)
time.Sleep(sleep_duration - elapsed)
}
}
func main() {
config_file := flag.String("c", "", "config file")
interval := flag.Int("n", 0, "reporting interval (seconds, default 5)")
network_interface := flag.String("i", "", "capture interface (default any)")
port := flag.Int("p", 0, "capture port (default 11211)")
num_items_to_report := flag.Int("r", 0, "number of items to report (default 20)")
quiet := flag.Bool("q", false, "suppress stdout output (default false)")
output_file := flag.String("w", "", "file to write output to")
show_errors := flag.Bool("e", true, "show errors in parsing as a metric")
flag.Parse()
// Parse Config
var config Config
var err error
if *config_file != "" {
config_data, _ := ioutil.ReadFile(*config_file)
config, err = NewConfig(config_data)
if err != nil {
panic(err)
}
} else {
config, err = NewConfig([]byte{})
}
// Parse CLI Args
if *interval != 0 {
config.Interval = *interval
}
if *network_interface != "" {
config.Interface = *network_interface
}
if *port != 0 {
config.Port = *port
}
if *num_items_to_report != 0 {
config.NumItemsToReport = *num_items_to_report
}
if *quiet != false {
config.Quiet = *quiet
}
if *output_file != "" {
config.OutputFile = *output_file
}
if *show_errors != true {
config.ShowErrors = *show_errors
}
// Build Regexps
regexp_keys := NewRegexpKeys()
for _, re := range config.Regexps {
regexp_key, err := NewRegexpKey(re.Re, re.Name)
if err != nil {
panic(err)
}
regexp_keys.Add(regexp_key)
}
hot_keys := NewHotKeyPool()
errors := NewHotKeyPool()
// Setup pcap
handle, err := pcap.OpenLive(config.Interface, CAPTURE_SIZE, true, pcap.BlockForever)
if err != nil {
panic(err)
}
filter := fmt.Sprintf("tcp and dst port %d", config.Port)
err = handle.SetBPFFilter(filter)
if err != nil {
panic(err)
}
packetSource := gopacket.NewPacketSource(handle, handle.LinkType())
go startReportingLoop(config, hot_keys, errors)
// Grab a packet
var (
payload []byte
keys []string
cmd_err int
)
for packet := range packetSource.Packets() {
app_data := packet.ApplicationLayer()
if app_data == nil {
continue
}
payload = app_data.Payload()
// Process data
prev_payload_len := 0
for len(payload) > 0 {
_, keys, payload, cmd_err = parseCommand(payload)
// ... We keep track of the payload length to make sure we don't end
// ... up in an infinite loop if one of the processors repeatedly
// ... sends us the same remainder. This should never happen, but
// ... if it does, it would be better to move on to the next packet
// ... rather than spin CPU doing nothing.
if len(payload) == prev_payload_len {
break
}
prev_payload_len = len(payload)
if cmd_err == ERR_NONE {
// Raw key
if len(config.Regexps) == 0 {
hot_keys.Add(keys)
} else {
// Regex
matches := []string{}
match_errors := []string{}
for _, key := range keys {
matched_regex, err := regexp_keys.Match(key)
if err != nil {
match_errors = append(match_errors, "match_error")
// The user has requested that we also show keys that
// weren't matched at all, probably for debugging.
if config.ShowUnmatched {
matches = append(matches, key)
}
} else {
matches = append(matches, matched_regex)
}
}
hot_keys.Add(matches)
errors.Add(match_errors)
}
} else {
errors.Add([]string{ERR_TO_STAT[cmd_err]})
}
}
}
}