|
| 1 | +package collector |
| 2 | + |
| 3 | +import ( |
| 4 | + "encoding/json" |
| 5 | + "fmt" |
| 6 | + "log/slog" |
| 7 | + "strconv" |
| 8 | + |
| 9 | + "github.com/prometheus/client_golang/prometheus" |
| 10 | +) |
| 11 | + |
| 12 | +var rpkiSubsystem = "rpki" |
| 13 | + |
| 14 | +func init() { |
| 15 | + registerCollector(rpkiSubsystem, disabledByDefault, NewRPKICollector) |
| 16 | +} |
| 17 | + |
| 18 | +type rpkiCollector struct { |
| 19 | + logger *slog.Logger |
| 20 | + descriptions map[string]*prometheus.Desc |
| 21 | +} |
| 22 | + |
| 23 | +// NewRPKICollector collects RPKI cache-connection metrics, implemented as per the Collector interface. |
| 24 | +func NewRPKICollector(logger *slog.Logger) (Collector, error) { |
| 25 | + return &rpkiCollector{logger: logger, descriptions: getRPKIDesc()}, nil |
| 26 | +} |
| 27 | + |
| 28 | +func getRPKIDesc() map[string]*prometheus.Desc { |
| 29 | + labels := []string{"vrf", "mode", "host", "port"} |
| 30 | + return map[string]*prometheus.Desc{ |
| 31 | + "cacheState": colPromDesc(rpkiSubsystem, "cache_state", "State of the RPKI cache connection (1 = connected, 0 = disconnected).", labels), |
| 32 | + "cachePreference": colPromDesc(rpkiSubsystem, "cache_preference", "Preference value of the RPKI cache connection.", labels), |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Update implemented as per the Collector interface. |
| 37 | +func (c *rpkiCollector) Update(ch chan<- prometheus.Metric) error { |
| 38 | + vrfs, err := getVRFs() |
| 39 | + if err != nil { |
| 40 | + return err |
| 41 | + } |
| 42 | + |
| 43 | + for _, vrf := range vrfs { |
| 44 | + var cmd string |
| 45 | + if vrf == "default" { |
| 46 | + cmd = "show rpki cache-connection json" |
| 47 | + } else { |
| 48 | + cmd = fmt.Sprintf("show rpki cache-connection vrf %s json", vrf) |
| 49 | + } |
| 50 | + |
| 51 | + output, err := executeBGPCommand(cmd) |
| 52 | + if err != nil { |
| 53 | + return err |
| 54 | + } |
| 55 | + if len(output) == 0 { |
| 56 | + continue |
| 57 | + } |
| 58 | + |
| 59 | + if err := processRPKICacheConnection(ch, output, vrf, c.descriptions); err != nil { |
| 60 | + return cmdOutputProcessError(cmd, string(output), err) |
| 61 | + } |
| 62 | + } |
| 63 | + return nil |
| 64 | +} |
| 65 | + |
| 66 | +func processRPKICacheConnection(ch chan<- prometheus.Metric, jsonRPKI []byte, vrf string, rpkiDesc map[string]*prometheus.Desc) error { |
| 67 | + var cacheConn rpkiCacheConnection |
| 68 | + if err := json.Unmarshal(jsonRPKI, &cacheConn); err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + |
| 72 | + for _, conn := range cacheConn.Connections { |
| 73 | + labels := []string{vrf, conn.Mode, conn.Host, strconv.Itoa(conn.Port)} |
| 74 | + |
| 75 | + state := 0.0 |
| 76 | + if conn.State == "connected" { |
| 77 | + state = 1.0 |
| 78 | + } |
| 79 | + |
| 80 | + newGauge(ch, rpkiDesc["cacheState"], state, labels...) |
| 81 | + newGauge(ch, rpkiDesc["cachePreference"], float64(conn.Preference), labels...) |
| 82 | + } |
| 83 | + return nil |
| 84 | +} |
| 85 | + |
| 86 | +type rpkiCacheConnection struct { |
| 87 | + ConnectedGroup int `json:"connectedGroup"` |
| 88 | + Connections []rpkiConnection `json:"connections"` |
| 89 | +} |
| 90 | + |
| 91 | +type rpkiConnection struct { |
| 92 | + Mode string `json:"mode"` |
| 93 | + Host string `json:"host"` |
| 94 | + Port int `json:"port,string"` |
| 95 | + Preference int `json:"preference"` |
| 96 | + State string `json:"state"` |
| 97 | +} |
0 commit comments