1
0
Fork 0
mirror of https://github.com/alice-lg/birdwatcher.git synced 2025-03-09 00:00:05 +01:00
birdwatcher/birdwatcher.go

131 lines
3.8 KiB
Go
Raw Permalink Normal View History

2016-10-23 09:44:42 +02:00
package main
2016-10-23 09:51:40 +02:00
import (
"flag"
2016-10-23 09:51:40 +02:00
"log"
"net/http"
2016-11-30 15:19:01 +01:00
"strings"
"github.com/ecix/birdwatcher/bird"
2016-11-11 15:33:08 +01:00
"github.com/ecix/birdwatcher/endpoints"
2016-11-30 13:40:34 +01:00
"github.com/julienschmidt/httprouter"
2016-10-23 09:51:40 +02:00
)
//go:generate versionize
2017-02-22 16:09:54 +01:00
var VERSION = "1.7.11"
2016-12-06 13:17:43 +01:00
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
2016-11-11 14:14:38 +01:00
r := httprouter.New()
2016-12-06 13:17:43 +01:00
if isModuleEnabled("status", whitelist) {
2017-02-15 12:20:55 +01:00
r.GET("/version", endpoints.Version(VERSION))
2016-12-06 13:17:43 +01:00
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))
}
2016-12-06 17:20:27 +01:00
if isModuleEnabled("routes_filtered", whitelist) {
r.GET("/routes/filtered/:protocol", endpoints.Endpoint(endpoints.RoutesFiltered))
2016-12-06 17:20:27 +01:00
}
2016-12-08 11:09:25 +01:00
if isModuleEnabled("routes_prefixed", whitelist) {
r.GET("/routes/prefix", endpoints.Endpoint(endpoints.RoutesPrefixed))
2016-12-08 11:09:25 +01:00
}
2016-12-06 13:17:43 +01:00
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))
}
2017-02-15 12:20:55 +01:00
if isModuleEnabled("routes_peer", whitelist) {
r.GET("/routes/peer", endpoints.Endpoint(endpoints.RoutesPeer))
}
2016-11-11 14:14:38 +01:00
return r
}
// Print service information like, listen address,
// access restrictions and configuration flags
2016-11-30 13:40:34 +01:00
func PrintServiceInfo(conf *Config, birdConf bird.BirdConfig) {
// General Info
log.Println("Starting Birdwatcher")
2016-12-06 13:17:43 +01:00
log.Println(" Using:", birdConf.BirdCmd)
log.Println(" Listen:", birdConf.Listen)
2016-11-30 15:19:01 +01:00
// Endpoint Info
if len(conf.Server.AllowFrom) == 0 {
2016-12-06 13:17:43 +01:00
log.Println(" AllowFrom: ALL")
2016-11-30 15:19:01 +01:00
} else {
2016-12-06 13:17:43 +01:00
log.Println(" AllowFrom:", strings.Join(conf.Server.AllowFrom, ", "))
}
log.Println(" ModulesEnabled:")
for _, m := range conf.Server.ModulesEnabled {
log.Println(" -", m)
2016-11-30 15:19:01 +01:00
}
}
2016-10-23 09:44:42 +02:00
func main() {
bird6 := flag.Bool("6", false, "Use bird6 instead of bird")
2017-02-22 18:49:17 +01:00
configfile := flag.String("config", "./etc/ecix/birdwatcher.conf", "Configuration file location")
flag.Parse()
endpoints.VERSION = VERSION
2016-11-25 15:50:59 +01:00
bird.InstallRateLimitReset()
// Load configurations
2017-02-22 18:49:17 +01:00
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
}
PrintServiceInfo(conf, birdConf)
2016-11-30 15:19:01 +01:00
// Configuration
bird.ClientConf = birdConf
bird.StatusConf = conf.Status
2016-12-13 11:01:28 +01:00
bird.RateLimitConf.Conf = conf.Ratelimit
2016-11-30 15:19:01 +01:00
endpoints.Conf = conf.Server
2016-11-11 16:00:36 +01:00
2016-11-30 13:21:40 +01:00
// Make server
2016-12-06 13:17:43 +01:00
r := makeRouter(conf.Server)
2016-11-30 13:21:40 +01:00
log.Fatal(http.ListenAndServe(birdConf.Listen, r))
2016-10-23 09:44:42 +02:00
}