diff options
-rwxr-xr-x | cli/gobgpcli | 44 | ||||
-rw-r--r-- | config/bgp_configs.go | 3 | ||||
-rw-r--r-- | server/peer.go | 16 | ||||
-rw-r--r-- | table/table_manager.go | 8 |
4 files changed, 66 insertions, 5 deletions
diff --git a/cli/gobgpcli b/cli/gobgpcli index fada58e1..a6fdfd5a 100755 --- a/cli/gobgpcli +++ b/cli/gobgpcli @@ -77,7 +77,7 @@ class Show(object): return f[1]() return 1 - def _neighbors(self, neighbor=None): + def _neighbor(self, neighbor=None): capdict = {1: "MULTIPROTOCOL", 2: "ROUTE_REFRESH", 4: "CARRYING_LABEL_INFO", @@ -122,10 +122,48 @@ class Show(object): print("") return 0 + @staticmethod + def format_timedelta(sec): + days = timedelta(seconds=sec).days + t = timedelta(seconds=timedelta(seconds=sec).seconds) + if days == 0: + up = t + else: + up = str(days) + "d" + " " + t + return up + def do_neighbors(self): if len(self.args) != 1: return 1 - return self._neighbors() + r = requests.get(self.base_url + "/neighbors") + neighbors = r.json() + if self.options.debug: + print neighbors + return 0 + print("{:40s} {:5s} {:11s}{:11s} {:8s} |#{:8s} {:8s} {:8s}".format("Peer", "AS", "Up", "Down", "State", "Advertised", "Received", "Accepted")) + for n in sorted(neighbors, key=lambda n: n["conf"]["remote_ip"]): + if n["info"]["uptime"] == 0: + up = "never" + else: + up = self.format_timedelta(n["info"]["uptime"]) + down = self.format_timedelta(n["info"]["downtime"]) + + s = n["info"]["bgp_state"] + if s == "BGP_FSM_IDLE": + state = "Idle" + elif s == "BGP_FSM_CONNECT": + state = "Connect" + elif s == "BGP_FSM_ACTIVE": + state = "Active" + elif s == "BGP_FSM_OPENSENT": + state = "Sent" + elif s == "BGP_FSM_OPENCONFIRM": + state = "Confirm" + elif s == "BGP_FSM_ESTABLISHED": + state = "Establ" + + print("{:40s} {:5d} {:11s}{:11s} {:8s} | {:>8d} {:>8d} {:>8d}".format(n["conf"]["remote_ip"], n["conf"]["remote_as"], up, down, state, n["info"]["Advertized"], n["info"]["Received"], n["info"]["Accepted"])) + return 0 def _format_attrs(self, attrlist): attrs = [] @@ -184,7 +222,7 @@ class Show(object): if len(self.args) != 2 and len(self.args) != 3: return 1 if len(self.args) == 2: - return self._neighbors(neighbor=self.args[1]) + return self._neighbor(neighbor=self.args[1]) if self.args[2] == "local": self.args[2] = "local-rib" r = requests.get(self.base_url + "/neighbor/" + self.args[1] + "/" + self.args[2]) diff --git a/config/bgp_configs.go b/config/bgp_configs.go index de3e1f97..ca2bafd2 100644 --- a/config/bgp_configs.go +++ b/config/bgp_configs.go @@ -378,7 +378,8 @@ type BgpNeighborCommonStateType struct { // peer-state State uint32 // peer-uptime - Uptime time.Time + Uptime time.Time + Downtime time.Time // BGP statistics // Open message input count diff --git a/server/peer.go b/server/peer.go index 437ea38c..9fa6961c 100644 --- a/server/peer.go +++ b/server/peer.go @@ -78,6 +78,7 @@ func NewPeer(g config.GlobalType, peer config.NeighborType, serverMsgCh chan *se } p.fsm = NewFSM(&g, &peer, p.acceptedConnCh, p.incoming, p.outgoing) peer.BgpNeighborCommonState.State = uint32(bgp.BGP_FSM_IDLE) + peer.BgpNeighborCommonState.Downtime = time.Now() if peer.NeighborAddress.To4() != nil { p.rf = bgp.RF_IPv4_UC } else { @@ -281,7 +282,7 @@ func (peer *Peer) loop() error { peer.fsm.peerConfig.BgpNeighborCommonState.EstablishedCount++ } if oldState == bgp.BGP_FSM_ESTABLISHED { - peer.fsm.peerConfig.BgpNeighborCommonState.Uptime = time.Time{} + peer.fsm.peerConfig.BgpNeighborCommonState.Downtime = time.Now() peer.adjRib.DropAllIn(peer.rf) pm := &peerMsg{ msgType: PEER_MSG_PEER_DOWN, @@ -356,6 +357,11 @@ func (peer *Peer) MarshalJSON() ([]byte, error) { if !s.Uptime.IsZero() { uptime = time.Now().Sub(s.Uptime).Seconds() } + downtime := float64(0) + if !s.Downtime.IsZero() { + downtime = time.Now().Sub(s.Downtime).Seconds() + } + p["info"] = struct { BgpState string `json:"bgp_state"` FsmEstablishedTransitions uint32 `json:"fsm_established_transitions"` @@ -372,7 +378,11 @@ func (peer *Peer) MarshalJSON() ([]byte, error) { RefreshMessageOut uint32 `json:"refresh_message_out"` RefreshMessageIn uint32 `json:"refresh_message_in"` Uptime float64 `json:"uptime"` + Downtime float64 `json:"downtime"` LastError string `json:"last_error"` + Received uint32 + Accepted uint32 + Advertized uint32 }{ BgpState: f.state.String(), @@ -390,6 +400,10 @@ func (peer *Peer) MarshalJSON() ([]byte, error) { RefreshMessageOut: s.RefreshOut, RefreshMessageIn: s.RefreshIn, Uptime: uptime, + Downtime: downtime, + Received: uint32(peer.adjRib.GetInCount(peer.rf)), + Accepted: uint32(peer.adjRib.GetInCount(peer.rf)), + Advertized: uint32(peer.adjRib.GetOutCount(peer.rf)), } return json.Marshal(p) diff --git a/table/table_manager.go b/table/table_manager.go index 6ee96518..109cd004 100644 --- a/table/table_manager.go +++ b/table/table_manager.go @@ -281,6 +281,14 @@ func (adj *AdjRib) GetOutPathList(rf bgp.RouteFamily) []Path { return adj.getPathList(adj.adjRibOut[rf]) } +func (adj *AdjRib) GetInCount(rf bgp.RouteFamily) int { + return len(adj.adjRibIn[rf]) +} + +func (adj *AdjRib) GetOutCount(rf bgp.RouteFamily) int { + return len(adj.adjRibOut[rf]) +} + func (adj *AdjRib) DropAllIn(rf bgp.RouteFamily) { // replace old one adj.adjRibIn[rf] = make(map[string]*ReceivedRoute) |