This repository has been archived by the owner on Feb 22, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
165 lines (138 loc) · 3.47 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
package main
import (
"bytes"
"flag"
"fmt"
"log"
"net"
"os"
"os/signal"
"regexp"
"runtime"
"strings"
"syscall"
"time"
)
const VERSION = "0.3.1"
type Destination struct {
Address string
Regex *regexp.Regexp
Replace []byte
}
type Packet struct {
Key []byte
Body []byte
}
type StringArray []string
func (a *StringArray) Set(s string) error {
*a = append(*a, s)
return nil
}
func (a *StringArray) String() string {
return fmt.Sprint(*a)
}
var (
address = flag.String("address", ":8125", "UDP listening address")
destinationAddresses = StringArray{}
showVersion = flag.Bool("version", false, "print version info")
)
func init() {
flag.Var(&destinationAddresses, "destination-address", "destination address (may be given multiple times)")
}
var packetRegexp = regexp.MustCompile("^([^:]+):(.*)$")
func parseMessage(data []byte) []*Packet {
var output []*Packet
for _, line := range bytes.Split(data, []byte("\n")) {
if len(line) == 0 {
continue
}
item := packetRegexp.FindSubmatch(line)
if len(item) == 0 {
continue
}
packet := &Packet{
Key: item[1],
Body: item[2],
}
output = append(output, packet)
}
return output
}
func processData(dataCh chan []byte, destinations []Destination) {
var destConns []net.Conn
for _, destination := range destinations {
conn, err := net.DialTimeout("udp", destination.Address, time.Second)
if err != nil {
log.Fatalf("ERROR: UDP connection failed - %s", err)
}
destConns = append(destConns, conn)
}
for data := range dataCh {
for _, p := range parseMessage(data) {
for i, destination := range destinations {
key := destination.Regex.ReplaceAll(p.Key, destination.Replace)
packet := fmt.Sprintf("%s:%s", key, p.Body)
conn := destConns[i]
_, err := conn.Write([]byte(packet))
if err != nil {
log.Printf("ERROR: writing to UDP socket - %s", err)
conn.Close()
// reconnect
conn, err := net.DialTimeout("udp", destination.Address, time.Second)
if err != nil {
log.Fatalf("ERROR: UDP connection failed - %s", err)
}
destConns[i] = conn
}
}
}
}
}
func udpListener(dataCh chan []byte) {
addr, _ := net.ResolveUDPAddr("udp", *address)
log.Printf("listening on %s", addr)
listener, err := net.ListenUDP("udp", addr)
if err != nil {
log.Fatalf("ERROR: ListenUDP - %s", err)
}
defer listener.Close()
err = listener.SetReadBuffer(1024 * 1024)
if err != nil {
log.Printf("ERROR: SetReadBuffer - %s", err)
}
for {
message := make([]byte, 512)
n, remaddr, err := listener.ReadFromUDP(message)
if err != nil {
log.Printf("ERROR: reading UDP packet from %+v - %s", remaddr, err)
continue
}
log.Printf("msg: %s (%d)", message[:n], n)
dataCh <- message[:n]
}
}
func main() {
flag.Parse()
if *showVersion {
fmt.Printf("statsdtee v%s (built w/%s)\n", VERSION, runtime.Version())
return
}
var destinations []Destination
for _, destinationAddress := range destinationAddresses {
parts := strings.Split(destinationAddress, ":")
destinations = append(destinations, Destination{
Address: fmt.Sprintf("%s:%s", parts[0], parts[1]),
Regex: regexp.MustCompile(parts[2]),
Replace: []byte(parts[3]),
})
}
if len(destinations) == 0 {
log.Fatalf("must specify at least one --destination-address")
}
runtime.GOMAXPROCS(2)
signalchan := make(chan os.Signal, 1)
signal.Notify(signalchan, syscall.SIGTERM)
dataCh := make(chan []byte, 1000)
go udpListener(dataCh)
processData(dataCh, destinations)
}