renamed session to protocol
This commit is contained in:
parent
97a971c795
commit
cf7d4bd13e
@ -2,29 +2,29 @@ package main
|
||||
|
||||
import "os/exec"
|
||||
|
||||
func getSessions() ([]*session, error) {
|
||||
sessions := make([]*session, 0)
|
||||
func getProtocols() ([]*protocol, error) {
|
||||
protocols := make([]*protocol, 0)
|
||||
|
||||
if *birdEnabled {
|
||||
s, err := getSessionsFromBird(4)
|
||||
s, err := getProtocolsFromBird(4)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessions = append(sessions, s...)
|
||||
protocols = append(protocols, s...)
|
||||
}
|
||||
|
||||
if *bird6Enabled {
|
||||
s, err := getSessionsFromBird(6)
|
||||
s, err := getProtocolsFromBird(6)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
sessions = append(sessions, s...)
|
||||
protocols = append(protocols, s...)
|
||||
}
|
||||
|
||||
return sessions, nil
|
||||
return protocols, nil
|
||||
}
|
||||
|
||||
func getSessionsFromBird(ipVersion int) ([]*session, error) {
|
||||
func getProtocolsFromBird(ipVersion int) ([]*protocol, error) {
|
||||
client := *birdClient
|
||||
|
||||
if ipVersion == 6 {
|
||||
|
23
main.go
23
main.go
@ -11,24 +11,15 @@ import (
|
||||
"os"
|
||||
)
|
||||
|
||||
type session struct {
|
||||
name string
|
||||
ipVersion int
|
||||
established int
|
||||
imported int64
|
||||
exported int64
|
||||
uptime int
|
||||
}
|
||||
|
||||
const version string = "0.4"
|
||||
const version string = "0.5"
|
||||
|
||||
var (
|
||||
showVersion = flag.Bool("version", false, "Print version information.")
|
||||
listenAddress = flag.String("web.listen-address", ":9200", "Address on which to expose metrics and web interface.")
|
||||
metricsPath = flag.String("web.telemetry-path", "/metrics", "Path under which to expose metrics.")
|
||||
birdClient = flag.String("bird.client", "birdc", "Binary to communicate with the bird routing daemon")
|
||||
birdEnabled = flag.Bool("bird.ipv4", true, "Get sessions from bird")
|
||||
bird6Enabled = flag.Bool("bird.ipv6", true, "Get sessions from bird6")
|
||||
birdEnabled = flag.Bool("bird.ipv4", true, "Get protocols from bird")
|
||||
bird6Enabled = flag.Bool("bird.ipv6", true, "Get protocols from bird6")
|
||||
)
|
||||
|
||||
func main() {
|
||||
@ -77,19 +68,19 @@ func errorHandler(f func(io.Writer, *http.Request) error) http.HandlerFunc {
|
||||
}
|
||||
|
||||
func handleMetricsRequest(w io.Writer, r *http.Request) error {
|
||||
sessions, err := getSessions()
|
||||
protocols, err := getProtocols()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
for _, s := range sessions {
|
||||
writeForSession(s, w)
|
||||
for _, s := range protocols {
|
||||
writeForBgpSession(s, w)
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func writeForSession(s *session, w io.Writer) {
|
||||
func writeForBgpSession(s *protocol, w io.Writer) {
|
||||
fmt.Fprintf(w, "bgp%d_session_up{name=\"%s\"} %d\n", s.ipVersion, s.name, s.established)
|
||||
fmt.Fprintf(w, "bgp%d_session_prefix_count_import{name=\"%s\"} %d\n", s.ipVersion, s.name, s.imported)
|
||||
fmt.Fprintf(w, "bgp%d_session_prefix_count_export{name=\"%s\"} %d\n", s.ipVersion, s.name, s.exported)
|
||||
|
26
parser.go
26
parser.go
@ -22,18 +22,18 @@ func init() {
|
||||
uptimeRegex, _ = regexp.Compile("^(?:((\\d+):(\\d{2}):(\\d{2}))|\\d+)$")
|
||||
}
|
||||
|
||||
func parseOutput(data []byte, ipVersion int) []*session {
|
||||
sessions := make([]*session, 0)
|
||||
func parseOutput(data []byte, ipVersion int) []*protocol {
|
||||
protocols := make([]*protocol, 0)
|
||||
|
||||
reader := bytes.NewReader(data)
|
||||
scanner := bufio.NewScanner(reader)
|
||||
var current *session = nil
|
||||
var current *protocol = nil
|
||||
|
||||
for scanner.Scan() {
|
||||
line := scanner.Text()
|
||||
if session, ok := parseLineForSession(line, ipVersion); ok {
|
||||
current = session
|
||||
sessions = append(sessions, current)
|
||||
if p, ok := parseLineForProtocol(line, ipVersion); ok {
|
||||
current = p
|
||||
protocols = append(protocols, current)
|
||||
}
|
||||
|
||||
if current != nil {
|
||||
@ -45,26 +45,26 @@ func parseOutput(data []byte, ipVersion int) []*session {
|
||||
}
|
||||
}
|
||||
|
||||
return sessions
|
||||
return protocols
|
||||
}
|
||||
|
||||
func parseLineForSession(line string, ipVersion int) (*session, bool) {
|
||||
func parseLineForProtocol(line string, ipVersion int) (*protocol, bool) {
|
||||
match := protoRegex.FindStringSubmatch(line)
|
||||
|
||||
if match == nil {
|
||||
return nil, false
|
||||
}
|
||||
|
||||
session := session{name: match[1], ipVersion: ipVersion, established: parseState(match[5]), uptime: parseUptime(match[4])}
|
||||
return &session, true
|
||||
p := protocol{name: match[1], ipVersion: ipVersion, established: parseState(match[5]), uptime: parseUptime(match[4])}
|
||||
return &p, true
|
||||
}
|
||||
|
||||
func parseLineForRoutes(line string, session *session) {
|
||||
func parseLineForRoutes(line string, p *protocol) {
|
||||
match := routeRegex.FindStringSubmatch(line)
|
||||
|
||||
if match != nil {
|
||||
session.imported, _ = strconv.ParseInt(match[1], 10, 64)
|
||||
session.exported, _ = strconv.ParseInt(match[2], 10, 64)
|
||||
p.imported, _ = strconv.ParseInt(match[1], 10, 64)
|
||||
p.exported, _ = strconv.ParseInt(match[2], 10, 64)
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,10 +8,10 @@ import (
|
||||
|
||||
func TestEstablishedBirdOldFormat(t *testing.T) {
|
||||
data := "foo BGP master up 1481973060 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx"
|
||||
s := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("sessions", 1, len(s), t)
|
||||
p := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("protocols", 1, len(p), t)
|
||||
|
||||
x := s[0]
|
||||
x := p[0]
|
||||
assert.StringEqual("name", "foo", x.name, t)
|
||||
assert.IntEqual("established", 1, x.established, t)
|
||||
assert.Int64Equal("imported", 12, x.imported, t)
|
||||
@ -21,10 +21,10 @@ func TestEstablishedBirdOldFormat(t *testing.T) {
|
||||
|
||||
func TestEstablishedBirdCurrentFormat(t *testing.T) {
|
||||
data := "foo BGP master up 00:01:00 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx"
|
||||
s := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("sessions", 1, len(s), t)
|
||||
p := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("protocols", 1, len(p), t)
|
||||
|
||||
x := s[0]
|
||||
x := p[0]
|
||||
assert.StringEqual("name", "foo", x.name, t)
|
||||
assert.IntEqual("established", 1, x.established, t)
|
||||
assert.Int64Equal("imported", 12, x.imported, t)
|
||||
@ -35,19 +35,19 @@ func TestEstablishedBirdCurrentFormat(t *testing.T) {
|
||||
|
||||
func TestIpv6(t *testing.T) {
|
||||
data := "foo BGP master up 00:01:00 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx"
|
||||
s := parseOutput([]byte(data), 6)
|
||||
assert.IntEqual("sessions", 1, len(s), t)
|
||||
p := parseOutput([]byte(data), 6)
|
||||
assert.IntEqual("protocols", 1, len(p), t)
|
||||
|
||||
x := s[0]
|
||||
x := p[0]
|
||||
assert.IntEqual("ipVersion", 6, x.ipVersion, t)
|
||||
}
|
||||
|
||||
func TestActive(t *testing.T) {
|
||||
data := "bar BGP master start 2016-01-01 Active\ntest\nbar"
|
||||
s := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("sessions", 1, len(s), t)
|
||||
p := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("protocols", 1, len(p), t)
|
||||
|
||||
x := s[0]
|
||||
x := p[0]
|
||||
assert.StringEqual("name", "bar", x.name, t)
|
||||
assert.IntEqual("established", 0, x.established, t)
|
||||
assert.IntEqual("imported", 0, int(x.imported), t)
|
||||
@ -58,6 +58,6 @@ func TestActive(t *testing.T) {
|
||||
|
||||
func Test2Sessions(t *testing.T) {
|
||||
data := "foo BGP master up 00:01:00 Established\ntest\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nbar BGP master start 2016-01-01 Active\nxxx"
|
||||
s := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("sessions", 2, len(s), t)
|
||||
p := parseOutput([]byte(data), 4)
|
||||
assert.IntEqual("protocols", 2, len(p), t)
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user