forked from alice-lg/birdwatcher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
birdwatcher.go
211 lines (182 loc) · 6.8 KB
/
birdwatcher.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
210
211
package main
import (
"flag"
"log"
"net/http"
"os"
"strings"
"github.com/alice-lg/birdwatcher/bird"
"github.com/alice-lg/birdwatcher/endpoints"
"github.com/gorilla/handlers"
"github.com/julienschmidt/httprouter"
)
//go:generate versionize
var VERSION = "2.0.0"
func isModuleEnabled(module string, modulesEnabled []string) bool {
for _, enabled := range modulesEnabled {
if enabled == module {
return true
}
}
return false
}
func makeRouter(config endpoints.ServerConfig) *httprouter.Router {
whitelist := config.ModulesEnabled
r := httprouter.New()
if isModuleEnabled("status", whitelist) {
r.GET("/version", endpoints.Version(VERSION))
r.GET("/status", endpoints.Endpoint(endpoints.Status))
}
if isModuleEnabled("protocols", whitelist) {
r.GET("/protocols", endpoints.Endpoint(endpoints.Protocols))
}
if isModuleEnabled("protocols_bgp", whitelist) {
r.GET("/protocols/bgp", endpoints.Endpoint(endpoints.Bgp))
}
if isModuleEnabled("protocols_short", whitelist) {
r.GET("/protocols/short", endpoints.Endpoint(endpoints.ProtocolsShort))
}
if isModuleEnabled("symbols", whitelist) {
r.GET("/symbols", endpoints.Endpoint(endpoints.Symbols))
}
if isModuleEnabled("symbols_tables", whitelist) {
r.GET("/symbols/tables", endpoints.Endpoint(endpoints.SymbolTables))
}
if isModuleEnabled("symbols_protocols", whitelist) {
r.GET("/symbols/protocols", endpoints.Endpoint(endpoints.SymbolProtocols))
}
if isModuleEnabled("routes_protocol", whitelist) {
r.GET("/routes/protocol/:protocol", endpoints.Endpoint(endpoints.ProtoRoutes))
}
if isModuleEnabled("routes_peer", whitelist) {
r.GET("/routes/peer/:peer", endpoints.Endpoint(endpoints.PeerRoutes))
}
if isModuleEnabled("routes_table", whitelist) {
r.GET("/routes/table/:table", endpoints.Endpoint(endpoints.TableRoutes))
}
if isModuleEnabled("routes_table_filtered", whitelist) {
r.GET("/routes/table/:table/filtered", endpoints.Endpoint(endpoints.TableRoutesFiltered))
}
if isModuleEnabled("routes_table_peer", whitelist) {
r.GET("/routes/table/:table/peer/:peer", endpoints.Endpoint(endpoints.TableAndPeerRoutes))
}
if isModuleEnabled("routes_count_protocol", whitelist) {
r.GET("/routes/count/protocol/:protocol", endpoints.Endpoint(endpoints.ProtoCount))
}
if isModuleEnabled("routes_count_table", whitelist) {
r.GET("/routes/count/table/:table", endpoints.Endpoint(endpoints.TableCount))
}
if isModuleEnabled("routes_count_primary", whitelist) {
r.GET("/routes/count/primary/:protocol", endpoints.Endpoint(endpoints.ProtoPrimaryCount))
}
if isModuleEnabled("routes_filtered", whitelist) {
r.GET("/routes/filtered/:protocol", endpoints.Endpoint(endpoints.RoutesFiltered))
}
if isModuleEnabled("routes_export", whitelist) {
r.GET("/routes/export/:protocol", endpoints.Endpoint(endpoints.RoutesExport))
}
if isModuleEnabled("routes_noexport", whitelist) {
r.GET("/routes/noexport/:protocol", endpoints.Endpoint(endpoints.RoutesNoExport))
}
if isModuleEnabled("routes_prefixed", whitelist) {
r.GET("/routes/prefix", endpoints.Endpoint(endpoints.RoutesPrefixed))
}
if isModuleEnabled("route_net", whitelist) {
r.GET("/route/net/:net", endpoints.Endpoint(endpoints.RouteNet))
r.GET("/route/net/:net/table/:table", endpoints.Endpoint(endpoints.RouteNetTable))
}
if isModuleEnabled("routes_pipe_filtered_count", whitelist) {
r.GET("/routes/pipe/filtered/count", endpoints.Endpoint(endpoints.PipeRoutesFilteredCount))
}
if isModuleEnabled("routes_pipe_filtered", whitelist) {
r.GET("/routes/pipe/filtered", endpoints.Endpoint(endpoints.PipeRoutesFiltered))
}
return r
}
// Print service information like, listen address,
// access restrictions and configuration flags
func PrintServiceInfo(conf *Config, birdConf bird.BirdConfig) {
// General Info
log.Println("Starting Birdwatcher")
log.Println(" Using:", birdConf.BirdCmd)
log.Println(" Listen:", birdConf.Listen)
log.Println(" Cache TTL:", birdConf.CacheTtl)
// Endpoint Info
if len(conf.Server.AllowFrom) == 0 {
log.Println(" AllowFrom: ALL")
} else {
log.Println(" AllowFrom:", strings.Join(conf.Server.AllowFrom, ", "))
}
if conf.Cache.UseRedis {
log.Println(" Caching backend: REDIS")
log.Println(" Using server:", conf.Cache.RedisServer)
} else {
log.Println(" Caching backend: MEMORY")
}
log.Println(" ModulesEnabled:")
for _, m := range conf.Server.ModulesEnabled {
log.Println(" -", m)
}
}
// MyLogger is our own log.Logger wrapper so we can customize it
type MyLogger struct {
logger *log.Logger
}
// Write implements the Write method of io.Writer
func (m *MyLogger) Write(p []byte) (n int, err error) {
m.logger.Print(string(p))
return len(p), nil
}
func main() {
// Disable timestamps for the default logger, as they are generated by the syslog implementation
log.SetFlags(log.Flags() &^ (log.Ldate | log.Ltime))
bird6 := flag.Bool("6", false, "Use bird6 instead of bird")
workerPoolSize := flag.Int("worker-pool-size", 8, "Number of go routines used to parse routing tables concurrently")
configfile := flag.String("config", "/etc/birdwatcher/birdwatcher.conf", "Configuration file location")
flag.Parse()
bird.WorkerPoolSize = *workerPoolSize
conf, err := LoadConfigs([]string{*configfile})
if err != nil {
log.Fatal("Loading birdwatcher configuration failed:", err)
}
if conf.Server.EnableTLS {
if len(conf.Server.Crt) == 0 || len(conf.Server.Key) == 0 {
log.Fatalln("You have enabled TLS support. Please specify 'crt' and 'key' in birdwatcher config file.")
}
}
endpoints.VERSION = VERSION
bird.InstallRateLimitReset()
// Get config according to flags
birdConf := conf.Bird
if *bird6 {
birdConf = conf.Bird6
bird.IPVersion = "6"
}
PrintServiceInfo(conf, birdConf)
// Configuration
bird.ClientConf = birdConf
bird.StatusConf = conf.Status
bird.RateLimitConf.Lock()
bird.RateLimitConf.Conf = conf.Ratelimit
bird.RateLimitConf.Unlock()
bird.ParserConf = conf.Parser
bird.CacheConf = conf.Cache
bird.InitializeCache()
endpoints.Conf = conf.Server
// Make server
r := makeRouter(conf.Server)
// Set up our own custom log.Logger without a prefix
myquerylog := log.New(os.Stdout, "", 0)
// Disable timestamps, as they are contained in the query log
myquerylog.SetFlags(myquerylog.Flags() &^ (log.Ldate | log.Ltime))
mylogger := &MyLogger{myquerylog}
go Housekeeping(conf.Housekeeping, !(bird.CacheConf.UseRedis)) // expire caches only for MemoryCache
if conf.Server.EnableTLS {
if len(conf.Server.Crt) == 0 || len(conf.Server.Key) == 0 {
log.Fatalln("You have enabled TLS support but not specified both a .crt and a .key file in the config.")
}
log.Fatal(http.ListenAndServeTLS(birdConf.Listen, conf.Server.Crt, conf.Server.Key, handlers.LoggingHandler(mylogger, r)))
} else {
log.Fatal(http.ListenAndServe(birdConf.Listen, handlers.LoggingHandler(mylogger, r)))
}
}