Example #1
0
File: common.go Project: osrg/gobgp
func newClient() *cli.GoBGPClient {
	target := net.JoinHostPort(globalOpts.Host, strconv.Itoa(globalOpts.Port))
	client, err := cli.NewGoBGPClient(target)
	if err != nil {
		exitWithError(err)
	}
	return client
}
// printGoBGPPeers queries GoBGP and displays the local peers in table format.
func printGoBGPPeers(ipv string) {
	client, err := gobgp.NewGoBGPClient("")
	if err != nil {
		fmt.Printf("Error creating gobgp client: %s\n", err)
		os.Exit(1)
	}
	defer client.Close()

	afi := bgp.AFI_IP
	if ipv == "6" {
		afi = bgp.AFI_IP6
	}

	fmt.Printf("\nIPv%s BGP status\n", ipv)

	neighbors, err := client.ListNeighborByTransport(afi)
	if err != nil {
		fmt.Printf("Error retrieving neighbor info: %s\n", err)
		os.Exit(1)
	}

	formatTimedelta := func(d int64) string {
		u := uint64(d)
		neg := d < 0
		if neg {
			u = -u
		}
		secs := u % 60
		u /= 60
		mins := u % 60
		u /= 60
		hours := u % 24
		days := u / 24

		if days == 0 {
			return fmt.Sprintf("%02d:%02d:%02d", hours, mins, secs)
		} else {
			return fmt.Sprintf("%dd ", days) + fmt.Sprintf("%02d:%02d:%02d", hours, mins, secs)
		}
	}

	now := time.Now()
	peers := make([]bgpPeer, 0, len(neighbors))

	for _, n := range neighbors {
		ipString := n.Config.NeighborAddress
		description := n.Config.Description
		adminState := string(n.State.AdminState)
		sessionState := strings.Title(string(n.State.SessionState))

		timeStr := "never"
		if n.Timers.State.Uptime != 0 {
			t := int(n.Timers.State.Downtime)
			if sessionState == "Established" {
				t = int(n.Timers.State.Uptime)
			}
			timeStr = formatTimedelta(int64(now.Sub(time.Unix(int64(t), 0)).Seconds()))
		}

		sm := bgpPeerRegex.FindStringSubmatch(description)
		if len(sm) != 3 {
			log.Debugf("Not a valid line: peer name '%s' is not recognized", description)
			continue
		}
		var ok bool
		var typ string
		if typ, ok = bgpTypeMap[sm[1]]; !ok {
			log.Debugf("Not a valid line: peer type '%s' is not recognized", sm[1])
			continue
		}

		peers = append(peers, bgpPeer{
			PeerIP:   ipString,
			PeerType: typ,
			State:    adminState,
			Since:    timeStr,
			BGPState: sessionState,
		})
	}

	// If no peers were returned then just print a message.
	if len(peers) == 0 {
		fmt.Printf("No IPv%s peers found.\n", ipv)
		return
	}

	// Finally, print the peers.
	printPeers(peers)
}