bird_exporter/parser/bgp_state.go
zhanghao b09cde3210 Add bgp state metrics
This patch adds bgp state metrics as follows:
http://XXX:9324/metrics

bird_bgp_state{name="uplink0",proto="BGP",state="Active"} 1
bird_bgp_state{name="uplink1",proto="BGP",state="Idle"} 1
2021-07-02 05:14:30 -04:00

50 lines
1.0 KiB
Go

package parser
import (
"bufio"
"bytes"
"regexp"
"strings"
"github.com/czerwonk/bird_exporter/protocol"
)
var (
nameRegex *regexp.Regexp
bgpStateRegex *regexp.Regexp
)
type bgpStateContext struct {
line string
current *protocol.BgpState
}
func init() {
bgpStateRegex = regexp.MustCompile(`^(?:1002\-)?([^\s]+)\s+(MRT|BGP|BFD|OSPF|RPKI|RIP|RAdv|Pipe|Perf|Direct|Babel|Device|Kernel|Static)\s+([^\s]+)\s+([^\s]+)\s+(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}|[^\s]+)\s+(Idle|Connect|Active|OpenSent|OpenConfirm|Established|Close)(?:\s+(.*?))?$`)
}
func ParseBgpState(data []byte) *protocol.BgpState {
reader := bytes.NewReader(data)
scanner := bufio.NewScanner(reader)
c := &bgpStateContext{
current: nil,
}
for scanner.Scan() {
c.line = strings.TrimRight(scanner.Text(), " ")
if c.line == "" {
c.current = nil
}
m := bgpStateRegex.FindStringSubmatch(c.line)
if m != nil {
s := &protocol.BgpState{Name: m[1]}
c.current = s
c.current.State = m[6]
break
}
}
return c.current
}