Skip to content

Commit

Permalink
upstream labeling and improvments
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrizhu committed Jul 29, 2024
1 parent b6e5e09 commit b550b8a
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 10 deletions.
63 changes: 54 additions & 9 deletions lg.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ var (
},
[]string{"prefix", "city", "mux", "upstreams", "available", "origin"},
)
ripeStatLGErr = promauto.NewCounter(prometheus.CounterOpts{
Name: "ripestatlg_err",
Help: "error count for ripestat lg endpoint",
})
possibleHijack = promauto.NewCounter(prometheus.CounterOpts{
Name: "possible_hijack",
Help: "upstream mismatch, possible hijack",
})
//bgpCommunitiesGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
// Name: "bgp_communities",
// Help: "BGP Communities",
Expand All @@ -40,6 +48,7 @@ func (p *Prefix) checkLGState() {
resp, err := http.Get(url)
if err != nil {
log.Error().Err(err).Msg("Fetching ripestat")
ripeStatLGErr.Inc()
return
}

Expand All @@ -57,6 +66,7 @@ func (p *Prefix) checkLGState() {
log.Error().Int("status code", statusCode).
Str("status", ripeStatLookingGlassResp.Status).
Msg("ripestat(lg) resp status code != 200")
ripeStatLGErr.Inc()
return
}

Expand All @@ -65,6 +75,15 @@ func (p *Prefix) checkLGState() {
availableStr = "n"
}

var UFMGOrigin bool
if p.origin == 61574 {
UFMGOrigin = true
}

if p.origin == 13335 {
return
}

origin := strconv.Itoa(p.origin)

for _, rrc := range ripeStatLookingGlassResp.Data.Rrcs {
Expand All @@ -76,21 +95,47 @@ func (p *Prefix) checkLGState() {
asPathSplit := strings.Split(peer.AsPath, " ")
upstream := ""
upstream2 := ""
if len(asPathSplit) >= 4 {
upstream = asPathSplit[len(asPathSplit)-4]
if err != nil {
log.Err(err).Msg("atoi fail")
}
offset := 2
if UFMGOrigin {
offset += 2
}
if len(asPathSplit) >= 5 {
upstream2 = asPathSplit[len(asPathSplit)-5]
if err != nil {
log.Err(err).Msg("atoi fail")
if len(asPathSplit) < offset+1 {
return
}
upstream = asPathSplit[len(asPathSplit)-offset]
if err != nil {
log.Error().Err(err).Msg("atoi fail")
return
}
matched := false
for _, dbUpstream := range dbUpstreams {
if dbUpstream.name == upstream {
matched = true
break
}
}
if !matched {
log.Info().
Str("prefix", p.prefix).
Str("upstream", upstream).
Str("path", peer.AsPath).
Msg("upstream mismatch, hijack possible")
possibleHijack.Inc()
return
}
if !slices.Contains(upstreams, upstream) {
upstreams = append(upstreams, upstream)
}

// second upstream
if len(asPathSplit) < offset+2 {
return
}
upstream2 = asPathSplit[len(asPathSplit)-offset-1]
if err != nil {
log.Error().Err(err).Msg("atoi fail")
return
}
if !slices.Contains(upstreams2, upstream2) {
upstreams2 = append(upstreams2, upstream2)
}
Expand Down
9 changes: 8 additions & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
var port int
var appId string
var debug bool
var jsonLog bool

const ripestatBase = "https://stat.ripe.net"

Expand All @@ -33,16 +34,20 @@ func updateStates() {

func init() {
zerolog.TimeFieldFormat = zerolog.TimeFormatUnix
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})

flag.StringVar(&appId, "appid", "exporter", "provide a unique identifier to every data call")
flag.IntVar(&port, "port", 2112, "port")
flag.BoolVar(&debug, "debug", false, "debug")
flag.BoolVar(&jsonLog, "json", false, "json logging")
}

func main() {
flag.Parse()

if !jsonLog {
log.Logger = log.Output(zerolog.ConsoleWriter{Out: os.Stdout})
}

if debug {
zerolog.SetGlobalLevel(zerolog.DebugLevel)
log.Debug().Msg("Debug log enabled")
Expand All @@ -57,6 +62,8 @@ func main() {

updateStates()

setUpstreamGauge()

go func() {
ticker := time.NewTicker(1 * time.Minute)
defer ticker.Stop()
Expand Down
25 changes: 25 additions & 0 deletions prefixes.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,31 @@ type Prefix struct {
origin int
}

type Upstream struct {
asn string
name string
}

var dbUpstreams = []Upstream{
Upstream{"Northeastern University", "156"},
Upstream{"FABRIC Testbed", "398900"},
Upstream{"GRNet", "5408"},
Upstream{"Bit BV", "12859"},
Upstream{"Netwerkvereniging Coloclue", "8283"},
Upstream{"Stony Brook University", "5719"},
Upstream{"Clemson University", "12148"},
Upstream{"Utah Education Network", "210"},
Upstream{"Georgia Institute of Technology", "2637"},
Upstream{"University of Wisconsin - Madison", "3128"},
Upstream{"Rede Nacional de Ensino e Pesquisa (RNP)", "1916"},
Upstream{"Cornell University", "26"},
Upstream{"psg.com RGNet", "3130"},
Upstream{"Los Nettos Regional Network", "226"},
Upstream{"UW at PNW GigaPoP", "101"},
Upstream{"vultr", "20473"},
Upstream{"HE", "6939"},
}

var monitorState = []Prefix{
Prefix{prefix: "2804:269c:fe01::/48", pop: "seattle01", available: true, origin: 47065},
Prefix{prefix: "2804:269c:fe02::/48", pop: "isi01", available: true, origin: 47065},
Expand Down
24 changes: 24 additions & 0 deletions upstreams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package main

import (
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/rs/zerolog/log"
)

var (
upstreamGauge = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "defined_upstreams",
Help: "defined upstreams pulled from the db",
}, []string{"asn", "name"})
)

func setUpstreamGauge() {
log.Trace().Msg("setting upstreeams gauge")
for _, dbUpstream := range dbUpstreams {
upstreamGauge.WithLabelValues(
dbUpstream.asn,
dbUpstream.name,
).Set(1)
}
}
6 changes: 6 additions & 0 deletions vis.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ var (
Name: "prefix_visibility",
Help: "Visibility of the prefix",
}, []string{"prefix", "city", "mux", "available", "origin"})
ripeStatVisibilityErr = promauto.NewCounter(prometheus.CounterOpts{
Name: "ripestatvis_err",
Help: "error count for ripestat vis endpoint",
})
)

func (p *Prefix) checkVisState() {
Expand All @@ -25,6 +29,7 @@ func (p *Prefix) checkVisState() {
resp, err := http.Get(url)
if err != nil {
log.Error().Err(err).Msg("Fetching ripestat")
ripeStatVisibilityErr.Inc()
return
}

Expand All @@ -42,6 +47,7 @@ func (p *Prefix) checkVisState() {
log.Error().Int("status code", statusCode).
Str("status", ripeStatVisibilityResp.Status).
Msg("ripestat(vis) resp status code != 200")
ripeStatVisibilityErr.Inc()
return
}

Expand Down

0 comments on commit b550b8a

Please sign in to comment.