This repository has been archived by the owner on Sep 8, 2018. It is now read-only.
/
main.go
204 lines (181 loc) · 5.39 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
package main
import (
"flag"
"fmt"
"net"
"net/http"
"net/http/pprof"
"net/url"
"os"
"os/signal"
"strconv"
"strings"
"syscall"
"text/tabwriter"
"github.com/pkg/errors"
"github.com/prometheus/client_golang/prometheus/promhttp"
)
var version = "dev" // set by release script
func usage() {
fmt.Fprintf(os.Stderr, "USAGE\n")
fmt.Fprintf(os.Stderr, " %s <mode> [flags]\n", os.Args[0])
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "MODES\n")
fmt.Fprintf(os.Stderr, " forward Forwarding agent\n")
fmt.Fprintf(os.Stderr, " ingest Ingester node\n")
fmt.Fprintf(os.Stderr, " store Storage node\n")
fmt.Fprintf(os.Stderr, " ingeststore Combination ingest+store node, for small installations\n")
fmt.Fprintf(os.Stderr, " query Querying commandline tool\n")
fmt.Fprintf(os.Stderr, " testsvc Test service, emits log lines at a fixed rate\n")
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "VERSION\n")
fmt.Fprintf(os.Stderr, " %s\n", version)
fmt.Fprintf(os.Stderr, "\n")
}
func main() {
if len(os.Args) < 2 {
usage()
os.Exit(1)
}
var run func([]string) error
switch strings.ToLower(os.Args[1]) {
case "forward":
run = runForward
case "ingest":
run = runIngest
case "store":
run = runStore
case "ingeststore":
run = runIngestStore
case "query":
run = runQuery
case "testsvc":
run = runTestService
default:
usage()
os.Exit(1)
}
if err := run(os.Args[2:]); err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
os.Exit(1)
}
}
const (
defaultAPIPort = 7650
defaultClusterPort = 7659
defaultFilesystem = "real"
)
var (
defaultAPIAddr = fmt.Sprintf("tcp://0.0.0.0:%d", defaultAPIPort)
defaultClusterAddr = fmt.Sprintf("tcp://0.0.0.0:%d", defaultClusterPort)
)
type stringset map[string]struct{}
func (ss stringset) Set(s string) error {
ss[s] = struct{}{}
return nil
}
func (ss stringset) String() string {
if len(ss) <= 0 {
return "<addr>"
}
return strings.Join(ss.slice(), ", ")
}
func (ss stringset) slice() (res []string) {
res = make([]string, 0, len(ss))
for s := range ss {
res = append(res, s)
}
return res
}
func interrupt(cancel <-chan struct{}) error {
c := make(chan os.Signal, 1)
signal.Notify(c, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-c:
return fmt.Errorf("received signal %s", sig)
case <-cancel:
return errors.New("canceled")
}
}
// "udp://host:1234", 80 => udp host:1234 host 1234
// "host:1234", 80 => tcp host:1234 host 1234
// "host", 80 => tcp host:80 host 80
func parseAddr(addr string, defaultPort int) (network, address, host string, port int, err error) {
u, err := url.Parse(strings.ToLower(addr))
if err != nil {
return network, address, host, port, err
}
switch {
case u.Scheme == "" && u.Opaque == "" && u.Host == "" && u.Path != "": // "host"
u.Scheme, u.Opaque, u.Host, u.Path = "tcp", "", net.JoinHostPort(u.Path, strconv.Itoa(defaultPort)), ""
case u.Scheme != "" && u.Opaque != "" && u.Host == "" && u.Path == "": // "host:1234"
u.Scheme, u.Opaque, u.Host, u.Path = "tcp", "", net.JoinHostPort(u.Scheme, u.Opaque), ""
case u.Scheme != "" && u.Opaque == "" && u.Host != "" && u.Path == "": // "tcp://host[:1234]"
if _, _, err := net.SplitHostPort(u.Host); err != nil {
u.Host = net.JoinHostPort(u.Host, strconv.Itoa(defaultPort))
}
default:
return network, address, host, port, errors.Errorf("%s: unsupported address format", addr)
}
host, portStr, err := net.SplitHostPort(u.Host)
if err != nil {
return network, address, host, port, err
}
port, err = strconv.Atoi(portStr)
if err != nil {
return network, address, host, port, err
}
return u.Scheme, u.Host, host, port, nil
}
func registerMetrics(mux *http.ServeMux) {
mux.Handle("/metrics", promhttp.Handler())
}
func registerProfile(mux *http.ServeMux) {
mux.HandleFunc("/debug/pprof/", pprof.Index)
mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
mux.HandleFunc("/debug/pprof/trace", pprof.Trace)
mux.Handle("/debug/pprof/block", pprof.Handler("block"))
mux.Handle("/debug/pprof/goroutine", pprof.Handler("goroutine"))
mux.Handle("/debug/pprof/heap", pprof.Handler("heap"))
mux.Handle("/debug/pprof/threadcreate", pprof.Handler("threadcreate"))
}
func usageFor(fs *flag.FlagSet, short string) func() {
return func() {
fmt.Fprintf(os.Stderr, "USAGE\n")
fmt.Fprintf(os.Stderr, " %s\n", short)
fmt.Fprintf(os.Stderr, "\n")
fmt.Fprintf(os.Stderr, "FLAGS\n")
w := tabwriter.NewWriter(os.Stderr, 0, 2, 2, ' ', 0)
fs.VisitAll(func(f *flag.Flag) {
fmt.Fprintf(w, "\t-%s %s\t%s\n", f.Name, f.DefValue, f.Usage)
})
w.Flush()
fmt.Fprintf(os.Stderr, "\n")
}
}
func hasNonlocal(clusterPeers stringset) bool {
for peer := range clusterPeers {
if host, _, err := net.SplitHostPort(peer); err == nil {
peer = host
}
if ip := net.ParseIP(peer); ip != nil && !ip.IsLoopback() {
return true
} else if ip == nil && strings.ToLower(peer) != "localhost" {
return true
}
}
return false
}
func isUnroutable(clusterHost string) bool {
if host, _, err := net.SplitHostPort(clusterHost); err == nil {
clusterHost = host
}
if ip := net.ParseIP(clusterHost); ip != nil && (ip.IsUnspecified() || ip.IsLoopback()) {
return true // typically 0.0.0.0 or localhost
} else if ip == nil && strings.ToLower(clusterHost) == "localhost" {
return true
}
return false
}