-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: report bgp states of connected neighbors #136
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package frr | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
"os" | ||
"time" | ||
|
||
"github.com/metal-stack/metal-go/api/models" | ||
) | ||
|
||
type Vrf struct { | ||
VrfID int | ||
VrfName string | ||
Ports Ports | ||
} | ||
|
||
type Port struct { | ||
Hostname string `json:"hostname"` | ||
PeerGroup string `json:"peerGroup"` | ||
BgpState string `json:"bgpState"` | ||
BgpTimerUpEstablished int64 `json:"bgpTimerUpEstablishedEpoch"` | ||
|
||
AddressFamilyInfo struct { | ||
IPv4UnicastCumulus struct { | ||
SentPrefixCounter int64 `json:"sentPrefixCounter"` | ||
AcceptedPrefixCounter int64 `json:"acceptedPrefixCounter"` | ||
} `json:"IPv4 Unicast"` | ||
IPv6UnicastCumulus struct { | ||
SentPrefixCounter int64 `json:"sentPrefixCounter"` | ||
AcceptedPrefixCounter int64 `json:"acceptedPrefixCounter"` | ||
} `json:"IPv6 Unicast"` | ||
IPv4UnicastSonic struct { | ||
SentPrefixCounter int64 `json:"sentPrefixCounter"` | ||
AcceptedPrefixCounter int64 `json:"acceptedPrefixCounter"` | ||
} `json:"ipv4Unicast"` | ||
IPv6UnicastSonic struct { | ||
SentPrefixCounter int64 `json:"sentPrefixCounter"` | ||
AcceptedPrefixCounter int64 `json:"acceptedPrefixCounter"` | ||
} `json:"ipv6Unicast"` | ||
} `json:"addressFamilyInfo"` | ||
} | ||
|
||
type Vrfs map[string]Vrf | ||
type Ports map[string]Port | ||
|
||
func GetBGPStates(filepath string) (map[string]models.V1SwitchBGPPortState, error) { | ||
|
||
fileInfo, err := os.Stat(filepath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error getting file info for %s: %w", filepath, err) | ||
} | ||
|
||
if time.Since(fileInfo.ModTime()) > time.Hour { | ||
return nil, fmt.Errorf("file %s is too old", filepath) | ||
} | ||
|
||
file, err := os.Open(filepath) | ||
if err != nil { | ||
return nil, fmt.Errorf("error opening frr bgp state json file %s: %w", filepath, err) | ||
} | ||
defer file.Close() | ||
|
||
byteValue, err := io.ReadAll(file) | ||
if err != nil { | ||
return nil, fmt.Errorf("error reading frr bgp state json file %s: %w", filepath, err) | ||
} | ||
|
||
var tempData map[string]map[string]json.RawMessage | ||
if err := json.Unmarshal(byteValue, &tempData); err != nil { | ||
return nil, fmt.Errorf("error unmarshalling bgp vrf all neigh output: %w", err) | ||
} | ||
|
||
bgpstates := make(map[string]models.V1SwitchBGPPortState) | ||
for _, vrfData := range tempData { | ||
|
||
var VrfName string | ||
if err := json.Unmarshal(vrfData["vrfName"], &VrfName); err != nil { | ||
return nil, fmt.Errorf("error parsing vrfName: %w", err) | ||
} | ||
|
||
for key, value := range vrfData { | ||
if key == "vrfId" || key == "vrfName" { | ||
continue | ||
} | ||
var port Port | ||
if err := json.Unmarshal(value, &port); err != nil { | ||
return nil, fmt.Errorf("error parsing port info for %s: %w", key, err) | ||
} | ||
bgptimerup := port.BgpTimerUpEstablished | ||
sentPrefixCounter := port.AddressFamilyInfo.IPv4UnicastCumulus.SentPrefixCounter + | ||
port.AddressFamilyInfo.IPv6UnicastCumulus.SentPrefixCounter + | ||
port.AddressFamilyInfo.IPv4UnicastSonic.SentPrefixCounter + | ||
port.AddressFamilyInfo.IPv6UnicastSonic.SentPrefixCounter | ||
|
||
acceptedPrefixCounter := port.AddressFamilyInfo.IPv4UnicastCumulus.AcceptedPrefixCounter + | ||
port.AddressFamilyInfo.IPv6UnicastCumulus.AcceptedPrefixCounter + | ||
port.AddressFamilyInfo.IPv4UnicastSonic.AcceptedPrefixCounter + | ||
port.AddressFamilyInfo.IPv6UnicastSonic.AcceptedPrefixCounter | ||
|
||
bgpstates[key] = models.V1SwitchBGPPortState{ | ||
Neighbor: &port.Hostname, | ||
PeerGroup: &port.PeerGroup, | ||
BgpState: &port.BgpState, | ||
BgpTimerUpEstablished: &bgptimerup, | ||
VrfName: &VrfName, | ||
SentPrefixCounter: &sentPrefixCounter, | ||
AcceptedPrefixCounter: &acceptedPrefixCounter, | ||
} | ||
} | ||
} | ||
|
||
return bgpstates, nil | ||
} | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you please add a test for parsing the JSON output from FRR?