-
Notifications
You must be signed in to change notification settings - Fork 0
/
rispeers.go
82 lines (72 loc) · 1.98 KB
/
rispeers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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))
}
}
}
}