Skip to content

Commit

Permalink
monitor ris peers
Browse files Browse the repository at this point in the history
  • Loading branch information
ezrizhu committed Oct 29, 2024
1 parent 7f30ac2 commit 96e1f02
Show file tree
Hide file tree
Showing 3 changed files with 146 additions and 0 deletions.
3 changes: 3 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ func updateStates() {
upstreamsGauge.Reset()
upstreams2Gauge.Reset()
bgpCommunitiesGauge.Reset()
ripeRISPeerRouteCount.Reset()

fetchRisPeer()

for _, prefix := range monitorState {
prefix.checkVisState()
Expand Down
61 changes: 61 additions & 0 deletions ripeStruct.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,64 @@ type RIPEStatLookingGlassResp struct {
StatusCode int `json:"status_code"`
Time string `json:"time"`
}

type risResponse struct {
Messages [][]string `json:"messages"`
SeeAlso []string `json:"see_also"`
Version string `json:"version"`
DataCall struct {
Name string `json:"data_call_name"`
Status string `json:"data_call_status"`
} `json:"data_call"`
Cached bool `json:"cached"`
Data struct {
Peers struct {
RRC00 []risPeer `json:"rrc00"`
RRC01 []risPeer `json:"rrc01"`
RRC03 []risPeer `json:"rrc03"`
RRC04 []risPeer `json:"rrc04"`
RRC05 []risPeer `json:"rrc05"`
RRC06 []risPeer `json:"rrc06"`
RRC07 []risPeer `json:"rrc07"`
RRC10 []risPeer `json:"rrc10"`
RRC11 []risPeer `json:"rrc11"`
RRC13 []risPeer `json:"rrc13"`
RRC14 []risPeer `json:"rrc14"`
RRC15 []risPeer `json:"rrc15"`
RRC16 []risPeer `json:"rrc16"`
RRC18 []risPeer `json:"rrc18"`
RRC19 []risPeer `json:"rrc19"`
RRC20 []risPeer `json:"rrc20"`
RRC21 []risPeer `json:"rrc21"`
RRC22 []risPeer `json:"rrc22"`
RRC23 []risPeer `json:"rrc23"`
RRC24 []risPeer `json:"rrc24"`
RRC25 []risPeer `json:"rrc25"`
RRC26 []risPeer `json:"rrc26"`
} `json:"peers"`
LatestTime string `json:"latest_time"`
EarliestTime string `json:"earliest_time"`
Parameters risParameters `json:"parameters"`
} `json:"data"`
QueryID string `json:"query_id"`
ProcessTime int `json:"process_time"`
ServerID string `json:"server_id"`
BuildVersion string `json:"build_version"`
Status string `json:"status"`
StatusCode int `json:"status_code"`
Time string `json:"time"`
}

// Peer represents an individual peer in the RIS system
type risPeer struct {
ASN string `json:"asn"`
IP string `json:"ip"`
V4PrefixCount int `json:"v4_prefix_count"`
V6PrefixCount int `json:"v6_prefix_count"`
}

// Parameters represents the query parameters
type risParameters struct {
QueryTime string `json:"query_time"`
Cache interface{} `json:"cache"`
}
82 changes: 82 additions & 0 deletions rispeers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package main

import (
"encoding/json"
"io/ioutil"
"net/http"
"reflect"
"strings"

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

var (
ripeRISPeerRouteCount = promauto.NewGaugeVec(prometheus.GaugeOpts{
Name: "rispeer",
Help: "RIPE RIS Peer Route Count",
}, []string{"ip", "asn", "rrc", "stack"})
ripeRISPeerErr = promauto.NewCounter(prometheus.CounterOpts{
Name: "rispeer_err",
Help: "error count for ripe ris peer endpoint",
})
)

func fetchRisPeer() {
log.Trace().Msg("checking ris peers state")
ripeRISPeerRouteCount.Reset()
url := ripestatBase + "/data/ris-peers/data.json?sourceapp=" + appId
resp, err := http.Get(url)
if err != nil {
log.Error().Err(err).Msg("Fetching ripestat")
ripeRISPeerErr.Inc()
return
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("reading ripestat resp")
return
}
defer resp.Body.Close()

var response risResponse
if err := json.Unmarshal(body, &response); err != nil {
log.Error().Err(err).Msg("err unmarhsalling resp")
return
}

// Use reflection to iterate through all RRCs
peersValue := reflect.ValueOf(response.Data.Peers)
peersType := peersValue.Type()

for i := 0; i < peersValue.NumField(); i++ {
field := peersType.Field(i)
rrc := strings.ToLower(field.Name) // Get RRC name from struct field
peers := peersValue.Field(i).Interface().([]risPeer)

// Process peers for this RRC
for _, peer := range peers {
// Set IPv4 prefix count
if peer.V4PrefixCount > 0 {
ripeRISPeerRouteCount.With(prometheus.Labels{
"ip": peer.IP,
"asn": peer.ASN,
"rrc": rrc,
"stack": "ipv4",
}).Set(float64(peer.V4PrefixCount))
}

// Set IPv6 prefix count
if peer.V6PrefixCount > 0 {
ripeRISPeerRouteCount.With(prometheus.Labels{
"ip": peer.IP,
"asn": peer.ASN,
"rrc": rrc,
"stack": "ipv6",
}).Set(float64(peer.V6PrefixCount))
}
}
}
}

0 comments on commit 96e1f02

Please sign in to comment.