Commit e5aa3264 authored by Daniel Czerwonk's avatar Daniel Czerwonk

added support for iso date format

parent 2b5e1f0f
......@@ -12,7 +12,7 @@ import (
"github.com/prometheus/common/log"
)
const version string = "1.2.0"
const version string = "1.2.1"
var (
showVersion = flag.Bool("version", false, "Print version information.")
......
......@@ -30,9 +30,9 @@ type context struct {
}
func init() {
protocolRegex = regexp.MustCompile("^(?:1002\\-)?([^\\s]+)\\s+(BGP|OSPF|Direct|Device|Kernel)\\s+([^\\s]+)\\s+([^\\s]+)\\s+([^\\s]+)(?:\\s+(.*?))?$")
protocolRegex = regexp.MustCompile("^(?:1002\\-)?([^\\s]+)\\s+(BGP|OSPF|Direct|Device|Kernel)\\s+([^\\s]+)\\s+([^\\s]+)\\s+(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}|[^\\s]+)(?:\\s+(.*?))?$")
routeRegex = regexp.MustCompile("^\\s+Routes:\\s+(\\d+) imported, (?:(\\d+) filtered, )?(\\d+) exported(?:, (\\d+) preferred)?")
uptimeRegex = regexp.MustCompile("^(?:((\\d+):(\\d{2}):(\\d{2}))|\\d+)$")
uptimeRegex = regexp.MustCompile("^(?:((\\d+):(\\d{2}):(\\d{2}))|(\\d+)|(\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}))$")
routeChangeRegex = regexp.MustCompile("(Import|Export) (updates|withdraws):\\s+(\\d+|---)\\s+(\\d+|---)\\s+(\\d+|---)\\s+(\\d+|---)\\s+(\\d+|---)\\s*")
channelRegex = regexp.MustCompile("Channel ipv(4|6)")
}
......@@ -126,11 +126,25 @@ func parseUptime(value string) int {
return 0
}
if match[1] != "" {
if len(match[1]) > 0 {
return parseUptimeForDuration(match)
}
return parseUptimeForTimestamp(value)
if len(match[5]) > 0 {
return parseUptimeForTimestamp(value)
}
return parseUptimeForIso(value)
}
func parseUptimeForIso(s string) int {
start, err := time.Parse("2006-01-02 15:04:05", s)
if err != nil {
log.Errorln(err)
return 0
}
return int(time.Since(start).Seconds())
}
func parseUptimeForDuration(duration []string) int {
......@@ -140,7 +154,6 @@ func parseUptimeForDuration(duration []string) int {
str := fmt.Sprintf("%dh%dm%ds", h, m, s)
d, err := time.ParseDuration(str)
if err != nil {
log.Errorln(err)
return 0
......
......@@ -2,6 +2,7 @@ package main
import (
"testing"
"time"
"github.com/czerwonk/bird_exporter/parser"
"github.com/czerwonk/bird_exporter/protocol"
......@@ -9,9 +10,11 @@ import (
)
func TestEstablishedBgpOldTimeFormat(t *testing.T) {
data := "foo BGP master up 1481973060 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx"
data := "foo BGP master up 1514768400 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx"
s := time.Date(2018, time.January, 1, 1, 0, 0, 0, time.UTC)
min := int(time.Since(s).Seconds())
p := parser.ParseProtocols([]byte(data), "4")
assert.IntEqual("protocols", 1, len(p), t)
max := int(time.Since(s).Seconds())
x := p[0]
assert.StringEqual("name", "foo", x.Name, t)
......@@ -22,6 +25,7 @@ func TestEstablishedBgpOldTimeFormat(t *testing.T) {
assert.Int64Equal("filtered", 1, x.Filtered, t)
assert.Int64Equal("preferred", 100, x.Preferred, t)
assert.StringEqual("ipVersion", "4", x.IpVersion, t)
assert.That("uptime", "uptime is feasable", func() bool { return x.Uptime >= min && max <= x.Uptime }, t)
}
func TestEstablishedBgpCurrentTimeFormat(t *testing.T) {
......@@ -41,6 +45,27 @@ func TestEstablishedBgpCurrentTimeFormat(t *testing.T) {
assert.IntEqual("uptime", 60, x.Uptime, t)
}
func TestEstablishedBgpIsoLongTimeFormat(t *testing.T) {
data := "foo BGP master up 2018-01-01 01:00:00 Established\ntest\nbar\n Routes: 12 imported, 1 filtered, 34 exported, 100 preferred\nxxx"
s := time.Date(2018, time.January, 1, 1, 0, 0, 0, time.UTC)
min := int(time.Since(s).Seconds())
p := parser.ParseProtocols([]byte(data), "4")
max := int(time.Since(s).Seconds())
assert.IntEqual("protocols", 1, len(p), t)
x := p[0]
assert.StringEqual("name", "foo", x.Name, t)
assert.IntEqual("proto", protocol.BGP, x.Proto, t)
assert.IntEqual("established", 1, x.Up, t)
assert.Int64Equal("imported", 12, x.Imported, t)
assert.Int64Equal("exported", 34, x.Exported, t)
assert.Int64Equal("filtered", 1, x.Filtered, t)
assert.Int64Equal("preferred", 100, x.Preferred, t)
assert.StringEqual("ipVersion", "4", x.IpVersion, t)
assert.That("uptime", "uptime is feasable", func() bool { return x.Uptime >= min && max <= x.Uptime }, t)
}
func TestIpv6Bgp(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"
p := parser.ParseProtocols([]byte(data), "6")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment