go-bgp

a collection of golang BGP tools to monitor, archive and serve
git clone git://git.2f30.org/go-bgp
Log | Files | Refs | README

bgpmon.go (4036B)


      1 package main
      2 
      3 import (
      4 	log "github.com/Sirupsen/logrus"
      5 	"github.com/Sirupsen/logrus/hooks/syslog"
      6 	"github.com/jessevdk/go-flags"
      7 	"github.com/osrg/gobgp/api"
      8 	"github.com/osrg/gobgp/config"
      9 	"github.com/osrg/gobgp/packet"
     10 	"github.com/osrg/gobgp/server"
     11 	"io/ioutil"
     12 	"log/syslog"
     13 	"os"
     14 	"os/signal"
     15 	"runtime"
     16 	"strings"
     17 	"syscall"
     18 )
     19 
     20 func main() {
     21 	runtime.GOMAXPROCS(runtime.NumCPU())
     22 
     23 	sigCh := make(chan os.Signal, 1)
     24 	signal.Notify(sigCh, syscall.SIGHUP)
     25 
     26 	var opts struct {
     27 		ConfigFile    string `short:"f" long:"config-file" description:"specifying a config file"`
     28 		LogLevel      string `short:"l" long:"log-level" description:"specifying log level"`
     29 		LogPlain      bool   `short:"p" long:"log-plain" description:"use plain format for logging (json by default)"`
     30 		UseSyslog     string `short:"s" long:"syslog" description:"use syslogd"`
     31 		Facility      string `long:"syslog-facility" description:"specify syslog facility"`
     32 		DisableStdlog bool   `long:"disable-stdlog" description:"disable standard logging"`
     33 	}
     34 	_, err := flags.Parse(&opts)
     35 	if err != nil {
     36 		os.Exit(1)
     37 	}
     38 
     39 	switch opts.LogLevel {
     40 	case "debug":
     41 		log.SetLevel(log.DebugLevel)
     42 	case "info":
     43 		log.SetLevel(log.InfoLevel)
     44 	default:
     45 		log.SetLevel(log.InfoLevel)
     46 	}
     47 
     48 	if opts.DisableStdlog == true {
     49 		log.SetOutput(ioutil.Discard)
     50 	} else {
     51 		log.SetOutput(os.Stdout)
     52 	}
     53 
     54 	if opts.UseSyslog != "" {
     55 		dst := strings.SplitN(opts.UseSyslog, ":", 2)
     56 		network := ""
     57 		addr := ""
     58 		if len(dst) == 2 {
     59 			network = dst[0]
     60 			addr = dst[1]
     61 		}
     62 
     63 		facility := syslog.Priority(0)
     64 		switch opts.Facility {
     65 		case "kern":
     66 			facility = syslog.LOG_KERN
     67 		case "user":
     68 			facility = syslog.LOG_USER
     69 		case "mail":
     70 			facility = syslog.LOG_MAIL
     71 		case "daemon":
     72 			facility = syslog.LOG_DAEMON
     73 		case "auth":
     74 			facility = syslog.LOG_AUTH
     75 		case "syslog":
     76 			facility = syslog.LOG_SYSLOG
     77 		case "lpr":
     78 			facility = syslog.LOG_LPR
     79 		case "news":
     80 			facility = syslog.LOG_NEWS
     81 		case "uucp":
     82 			facility = syslog.LOG_UUCP
     83 		case "cron":
     84 			facility = syslog.LOG_CRON
     85 		case "authpriv":
     86 			facility = syslog.LOG_AUTHPRIV
     87 		case "ftp":
     88 			facility = syslog.LOG_FTP
     89 		case "local0":
     90 			facility = syslog.LOG_LOCAL0
     91 		case "local1":
     92 			facility = syslog.LOG_LOCAL1
     93 		case "local2":
     94 			facility = syslog.LOG_LOCAL2
     95 		case "local3":
     96 			facility = syslog.LOG_LOCAL3
     97 		case "local4":
     98 			facility = syslog.LOG_LOCAL4
     99 		case "local5":
    100 			facility = syslog.LOG_LOCAL5
    101 		case "local6":
    102 			facility = syslog.LOG_LOCAL6
    103 		case "local7":
    104 			facility = syslog.LOG_LOCAL7
    105 		}
    106 
    107 		hook, err := logrus_syslog.NewSyslogHook(network, addr, syslog.LOG_INFO|facility, "bgpd")
    108 		if err != nil {
    109 			log.Error("Unable to connect to syslog daemon, ", opts.UseSyslog)
    110 			os.Exit(1)
    111 		} else {
    112 			log.AddHook(hook)
    113 		}
    114 	}
    115 
    116 	if opts.LogPlain == false {
    117 		log.SetFormatter(&log.JSONFormatter{})
    118 	}
    119 
    120 	if opts.ConfigFile == "" {
    121 		opts.ConfigFile = "gobgpd.conf"
    122 	}
    123 
    124 	configCh := make(chan config.BgpType)
    125 	reloadCh := make(chan bool)
    126 	go config.ReadConfigfileServe(opts.ConfigFile, configCh, reloadCh)
    127 	reloadCh <- true
    128 
    129 	bgpServer := server.NewBgpServer(bgp.BGP_PORT)
    130 	go bgpServer.Serve()
    131 
    132 	// start Rest Server
    133 	restServer := api.NewRestServer(api.REST_PORT, bgpServer.RestReqCh)
    134 	go restServer.Serve()
    135 
    136 	var bgpConfig *config.BgpType = nil
    137 	for {
    138 		select {
    139 		case newConfig := <-configCh:
    140 			var added []config.NeighborType
    141 			var deleted []config.NeighborType
    142 
    143 			if bgpConfig == nil {
    144 				bgpServer.SetGlobalType(newConfig.Global)
    145 				bgpConfig = &newConfig
    146 				added = newConfig.NeighborList
    147 				deleted = []config.NeighborType{}
    148 			} else {
    149 				bgpConfig, added, deleted = config.UpdateConfig(bgpConfig, &newConfig)
    150 			}
    151 
    152 			for _, p := range added {
    153 				log.Infof("Peer %v is added", p.NeighborAddress)
    154 				bgpServer.PeerAdd(p)
    155 			}
    156 			for _, p := range deleted {
    157 				log.Infof("Peer %v is deleted", p.NeighborAddress)
    158 				bgpServer.PeerDelete(p)
    159 			}
    160 		case sig := <-sigCh:
    161 			switch sig {
    162 			case syscall.SIGHUP:
    163 				log.Info("reload the config file")
    164 				reloadCh <- true
    165 			}
    166 		}
    167 	}
    168 }
    169