package birdfmt import ( "strings" ) // ProtocolsSummary is a lightweight parse of `birdc show protocols all` (BIRD 2). type ProtocolsSummary struct { BGPSessionsTotal int BGPEstablished int RawLineCount int } // isBGPProtocolSummaryRow is true for BIRD "show protocols" summary rows where the // second column (Proto) is BGP. Substring checks are unsafe: names like evobgp_* contain "bgp". func isBGPProtocolSummaryRow(line string) bool { line = strings.TrimSpace(line) if line == "" { return false } low := strings.ToLower(line) if strings.HasPrefix(low, "name") || strings.HasPrefix(low, "table") { return false } fields := strings.Fields(line) if len(fields) < 2 { return false } return strings.EqualFold(fields[1], "BGP") } // SummarizeProtocolsOutput extracts BGP session heuristics from birdc output. func SummarizeProtocolsOutput(output string) ProtocolsSummary { var s ProtocolsSummary lines := strings.Split(output, "\n") s.RawLineCount = len(lines) for _, line := range lines { line = strings.TrimSpace(line) if line == "" { continue } low := strings.ToLower(line) if !isBGPProtocolSummaryRow(line) { continue } s.BGPSessionsTotal++ if strings.Contains(low, "established") { s.BGPEstablished++ } } return s }