forked from mchackorg/birdwatcher
-
Notifications
You must be signed in to change notification settings - Fork 7
/
birdwatcher.go
141 lines (122 loc) · 4.12 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
package main
import (
"flag"
"log"
"net/http"
"strings"
"github.com/ecix/birdwatcher/bird"
"github.com/ecix/birdwatcher/endpoints"
"github.com/julienschmidt/httprouter"
)
//go:generate versionize
var VERSION = "1.11.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("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_table", whitelist) {
r.GET("/routes/table/:table", endpoints.Endpoint(endpoints.TableRoutes))
}
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_filtered", whitelist) {
r.GET("/routes/filtered/:protocol", endpoints.Endpoint(endpoints.RoutesFiltered))
}
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_peer", whitelist) {
r.GET("/routes/peer", endpoints.Endpoint(endpoints.RoutesPeer))
}
if isModuleEnabled("routes_dump", whitelist) {
r.GET("/routes/dump", endpoints.Endpoint(endpoints.RoutesDump))
}
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)
// Endpoint Info
if len(conf.Server.AllowFrom) == 0 {
log.Println(" AllowFrom: ALL")
} else {
log.Println(" AllowFrom:", strings.Join(conf.Server.AllowFrom, ", "))
}
log.Println(" ModulesEnabled:")
for _, m := range conf.Server.ModulesEnabled {
log.Println(" -", m)
}
log.Println(" Per Peer Tables:", conf.Parser.PerPeerTables)
}
func main() {
bird6 := flag.Bool("6", false, "Use bird6 instead of bird")
configfile := flag.String("config", "./etc/ecix/birdwatcher.conf", "Configuration file location")
flag.Parse()
endpoints.VERSION = VERSION
bird.InstallRateLimitReset()
// Load configurations
conf, err := LoadConfigs(ConfigOptions(*configfile))
if err != nil {
log.Fatal("Loading birdwatcher configuration failed:", err)
}
// 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.Conf = conf.Ratelimit
bird.ParserConf = conf.Parser
endpoints.Conf = conf.Server
// Make server
r := makeRouter(conf.Server)
log.Fatal(http.ListenAndServe(birdConf.Listen, r))
}