From 854cf36c7460f3240a0126322e5d03b929a1c3d0 Mon Sep 17 00:00:00 2001 From: ISHIDA Wataru Date: Fri, 12 Jun 2015 13:02:34 +0900 Subject: cli: add a command to monitor changes of best paths $ gobgp monitor global rib [ROUTE] 10.0.1.0/24 via 192.168.10.3 aspath [65001] attrs [{Origin: IGP} {Med: 0}] [ROUTE] 10.0.2.0/24 via 192.168.10.4 aspath [65002] attrs [{Origin: IGP} {Med: 0}] [ROUTE] 10.0.3.0/24 via 192.168.10.5 aspath [65003] attrs [{Origin: IGP} {Med: 0}] [ROUTE] 10.0.4.0/24 via 192.168.10.6 aspath [65004] attrs [{Origin: IGP} {Med: 0}] [DELROUTE] 10.0.4.0/24 via 192.168.10.6 aspath [65004] attrs [{Origin: IGP} {Med: 0}] [DELROUTE] 10.0.2.0/24 via 192.168.10.4 aspath [65002] attrs [{Origin: IGP} {Med: 0}] [DELROUTE] 10.0.3.0/24 via 192.168.10.5 aspath [65003] attrs [{Origin: IGP} {Med: 0}] [ROUTE] 10.0.3.0/24 via 192.168.10.5 aspath [65003] attrs [{Origin: IGP} {Med: 100}] [ROUTE] 10.0.6.0/24 via 192.168.10.5 aspath [65003 65005] attrs [{Origin: IGP} {Med: 100}] [ROUTE] 10.0.2.0/24 via 192.168.10.4 aspath [65002] attrs [{Origin: IGP} {Med: 200}] Signed-off-by: ISHIDA Wataru --- gobgp/common.go | 1 + gobgp/global.go | 2 +- gobgp/main.go | 3 ++- gobgp/monitor.go | 72 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ gobgp/neighbor.go | 17 +++++++++---- 5 files changed, 88 insertions(+), 7 deletions(-) create mode 100644 gobgp/monitor.go diff --git a/gobgp/common.go b/gobgp/common.go index d7d9e2c4..0ae61af7 100644 --- a/gobgp/common.go +++ b/gobgp/common.go @@ -52,6 +52,7 @@ const ( CMD_ACTIONS = "actions" CMD_IMPORT = "import" CMD_EXPORT = "export" + CMD_MONITOR = "monitor" ) var subOpts struct { diff --git a/gobgp/global.go b/gobgp/global.go index b5f19ad3..93bd1fde 100644 --- a/gobgp/global.go +++ b/gobgp/global.go @@ -70,7 +70,7 @@ func showGlobalRib() error { sort.Sort(ps) - showRoute(ps, true, true) + showRoute(ps, true, true, false) return nil } diff --git a/gobgp/main.go b/gobgp/main.go index ba6c8d5f..79629380 100644 --- a/gobgp/main.go +++ b/gobgp/main.go @@ -58,9 +58,10 @@ func main() { globalCmd := NewGlobalCmd() neighborCmd := NewNeighborCmd() policyCmd := NewPolicyCmd() + monitorCmd := NewMonitorCmd() rootCmd.AddCommand(globalCmd) rootCmd.AddCommand(neighborCmd) rootCmd.AddCommand(policyCmd) - + rootCmd.AddCommand(monitorCmd) rootCmd.Execute() } diff --git a/gobgp/monitor.go b/gobgp/monitor.go new file mode 100644 index 00000000..b57b1451 --- /dev/null +++ b/gobgp/monitor.go @@ -0,0 +1,72 @@ +// Copyright (C) 2015 Nippon Telegraph and Telephone Corporation. +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or +// implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package main + +import ( + "fmt" + "github.com/osrg/gobgp/api" + "github.com/spf13/cobra" + "golang.org/x/net/context" + "io" + "net" + "os" +) + +func NewMonitorCmd() *cobra.Command { + ribCmd := &cobra.Command{ + Use: CMD_RIB, + Run: func(cmd *cobra.Command, args []string) { + rt, err := checkAddressFamily(net.IP{}) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + arg := &api.Arguments{ + Resource: api.Resource_GLOBAL, + Af: rt, + } + + stream, err := client.MonitorBestChanged(context.Background(), arg) + if err != nil { + fmt.Println(err) + os.Exit(1) + } + for { + p, err := stream.Recv() + if err == io.EOF { + break + } else if err != nil { + fmt.Println(err) + os.Exit(1) + } + showRoute([]*api.Path{p}, false, false, true) + } + + }, + } + + globalCmd := &cobra.Command{ + Use: CMD_GLOBAL, + } + globalCmd.AddCommand(ribCmd) + + monitorCmd := &cobra.Command{ + Use: CMD_MONITOR, + } + monitorCmd.AddCommand(globalCmd) + + return monitorCmd +} diff --git a/gobgp/neighbor.go b/gobgp/neighbor.go index 8d311f9b..c478f7a1 100644 --- a/gobgp/neighbor.go +++ b/gobgp/neighbor.go @@ -223,7 +223,7 @@ type AsPathFormat struct { separator string } -func showRoute(pathList []*api.Path, showAge bool, showBest bool) { +func showRoute(pathList []*api.Path, showAge bool, showBest bool, isMonitor bool) { var pathStrs [][]interface{} maxPrefixLen := len("Network") @@ -352,8 +352,13 @@ func showRoute(pathList []*api.Path, showAge bool, showBest bool) { if maxAsPathLen < len(aspath(p.Attrs)) { maxAsPathLen = len(aspath(p.Attrs)) } - - if showAge { + if isMonitor { + title := "ROUTE" + if p.IsWithdraw { + title = "DELROUTE" + } + pathStrs = append(pathStrs, []interface{}{title, p.Nlri.Prefix, p.Nexthop, aspath(p.Attrs), formatAttrs(p.Attrs)}) + } else if showAge { pathStrs = append(pathStrs, []interface{}{best, p.Nlri.Prefix, p.Nexthop, aspath(p.Attrs), formatTimedelta(p.Age), formatAttrs(p.Attrs)}) } else { pathStrs = append(pathStrs, []interface{}{best, p.Nlri.Prefix, p.Nexthop, aspath(p.Attrs), formatAttrs(p.Attrs)}) @@ -361,7 +366,9 @@ func showRoute(pathList []*api.Path, showAge bool, showBest bool) { } var format string - if showAge { + if isMonitor { + format = "[%s] %s via %s aspath [%s] attrs %s\n" + } else if showAge { format = fmt.Sprintf("%%-2s %%-%ds %%-%ds %%-%ds %%-10s %%-s\n", maxPrefixLen, maxNexthopLen, maxAsPathLen) fmt.Printf(format, "", "Network", "Next Hop", "AS_PATH", "Age", "Attrs") } else { @@ -455,7 +462,7 @@ func showNeighborRib(r string, remoteIP net.IP) error { } sort.Sort(ps) - showRoute(ps, showAge, showBest) + showRoute(ps, showAge, showBest, false) return nil } -- cgit v1.2.3