| 1 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 2 | |
| 3 | package cato_networks |
| 4 | |
| 5 | import "github.com/netdata/netdata/go/plugins/pkg/metrix" |
| 6 | |
| 7 | type collectorMetrics struct { |
| 8 | site siteMetricInstruments |
| 9 | device deviceMetricInstruments |
| 10 | iface interfaceMetricInstruments |
| 11 | bgp bgpMetricInstruments |
| 12 | } |
| 13 | |
| 14 | type siteMetricInstruments struct { |
| 15 | connectivityStatus metrix.SnapshotStateSetVec |
| 16 | operationalStatus metrix.SnapshotStateSetVec |
| 17 | hosts metrix.SnapshotGaugeVec |
| 18 | traffic trafficMetricWriters |
| 19 | } |
| 20 | |
| 21 | type deviceMetricInstruments struct { |
| 22 | connectionStatus metrix.SnapshotStateSetVec |
| 23 | } |
| 24 | |
| 25 | type interfaceMetricInstruments struct { |
| 26 | connectionStatus metrix.SnapshotStateSetVec |
| 27 | tunnelUptime metrix.SnapshotGaugeVec |
| 28 | traffic trafficMetricWriters |
| 29 | } |
| 30 | |
| 31 | type bgpMetricInstruments struct { |
| 32 | sessionStatus metrix.SnapshotStateSetVec |
| 33 | routes metrix.SnapshotGaugeVec |
| 34 | routesLimit metrix.SnapshotGaugeVec |
| 35 | routesLimitState metrix.SnapshotStateSetVec |
| 36 | ribOutRoutes metrix.SnapshotGaugeVec |
| 37 | } |
| 38 | |
| 39 | type trafficMetricWriters struct { |
| 40 | bytesUp metrix.SnapshotGaugeVec |
| 41 | bytesDown metrix.SnapshotGaugeVec |
| 42 | lostUp metrix.SnapshotGaugeVec |
| 43 | lostDown metrix.SnapshotGaugeVec |
| 44 | jitterUp metrix.SnapshotGaugeVec |
| 45 | jitterDown metrix.SnapshotGaugeVec |
| 46 | discardUp metrix.SnapshotGaugeVec |
| 47 | discardDown metrix.SnapshotGaugeVec |
| 48 | rtt metrix.SnapshotGaugeVec |
| 49 | lastMileLatency metrix.SnapshotGaugeVec |
| 50 | lastMileLoss metrix.SnapshotGaugeVec |
| 51 | } |
| 52 | |
| 53 | func (w trafficMetricWriters) withHostScope(scope metrix.HostScope) trafficMetricWriters { |
| 54 | return trafficMetricWriters{ |
| 55 | bytesUp: gaugeVecWithHostScope(w.bytesUp, scope), |
| 56 | bytesDown: gaugeVecWithHostScope(w.bytesDown, scope), |
| 57 | lostUp: gaugeVecWithHostScope(w.lostUp, scope), |
| 58 | lostDown: gaugeVecWithHostScope(w.lostDown, scope), |
| 59 | jitterUp: gaugeVecWithHostScope(w.jitterUp, scope), |
| 60 | jitterDown: gaugeVecWithHostScope(w.jitterDown, scope), |
| 61 | discardUp: gaugeVecWithHostScope(w.discardUp, scope), |
| 62 | discardDown: gaugeVecWithHostScope(w.discardDown, scope), |
| 63 | rtt: gaugeVecWithHostScope(w.rtt, scope), |
| 64 | lastMileLatency: gaugeVecWithHostScope(w.lastMileLatency, scope), |
| 65 | lastMileLoss: gaugeVecWithHostScope(w.lastMileLoss, scope), |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | func gaugeVecWithHostScope(vec metrix.SnapshotGaugeVec, scope metrix.HostScope) metrix.SnapshotGaugeVec { |
| 70 | if vec == nil { |
| 71 | return nil |
| 72 | } |
| 73 | return vec.WithHostScope(scope) |
| 74 | } |
| 75 | |
| 76 | var siteConnectivityStates = []string{"connected", "disconnected", "degraded", "unknown"} |
| 77 | var siteOperationalStates = []string{"active", "disabled", "locked", "unknown"} |
| 78 | var deviceConnectionStates = []string{"connected", "disconnected"} |
| 79 | var interfaceConnectionStates = []string{"connected", "disconnected"} |
| 80 | var bgpSessionStates = []string{"up", "down", "unknown"} |
| 81 | var bgpRoutesLimitStates = []string{"ok", "exceeded"} |
| 82 | |
| 83 | func newCollectorMetrics(store metrix.CollectorStore) *collectorMetrics { |
| 84 | meter := store.Write().SnapshotMeter("") |
| 85 | |
| 86 | siteVec := meter.Vec("site_id", "site_name", "pop_name") |
| 87 | deviceVec := meter.Vec("site_id", "site_name", "device_id", "device_name") |
| 88 | ifaceVec := meter.Vec("site_id", "site_name", "device_id", "device_name", "interface_id", "interface_name") |
| 89 | bgpVec := meter.Vec("site_id", "site_name", "peer_ip", "peer_asn") |
| 90 | |
| 91 | return &collectorMetrics{ |
| 92 | site: siteMetricInstruments{ |
| 93 | connectivityStatus: newStateSetVec(siteVec, "site_connectivity_status", siteConnectivityStates), |
| 94 | operationalStatus: newStateSetVec(siteVec, "site_operational_status", siteOperationalStates), |
| 95 | hosts: siteVec.Gauge("site_hosts"), |
| 96 | traffic: trafficMetricWriters{ |
| 97 | bytesUp: siteVec.Gauge("site_bytes_upstream_max"), |
| 98 | bytesDown: siteVec.Gauge("site_bytes_downstream_max"), |
| 99 | lostUp: siteVec.Gauge("site_lost_upstream_percent"), |
| 100 | lostDown: siteVec.Gauge("site_lost_downstream_percent"), |
| 101 | jitterUp: siteVec.Gauge("site_jitter_upstream_ms"), |
| 102 | jitterDown: siteVec.Gauge("site_jitter_downstream_ms"), |
| 103 | discardUp: siteVec.Gauge("site_packets_discarded_upstream"), |
| 104 | discardDown: siteVec.Gauge("site_packets_discarded_downstream"), |
| 105 | rtt: siteVec.Gauge("site_rtt_ms"), |
| 106 | lastMileLatency: siteVec.Gauge("site_last_mile_latency_ms"), |
| 107 | lastMileLoss: siteVec.Gauge("site_last_mile_packet_loss_percent"), |
| 108 | }, |
| 109 | }, |
| 110 | device: deviceMetricInstruments{ |
| 111 | connectionStatus: newStateSetVec(deviceVec, "device_connection_status", deviceConnectionStates), |
| 112 | }, |
| 113 | iface: interfaceMetricInstruments{ |
| 114 | connectionStatus: newStateSetVec(ifaceVec, "interface_connection_status", interfaceConnectionStates), |
| 115 | tunnelUptime: ifaceVec.Gauge("interface_tunnel_uptime_seconds"), |
| 116 | traffic: trafficMetricWriters{ |
| 117 | bytesUp: ifaceVec.Gauge("interface_bytes_upstream_max"), |
| 118 | bytesDown: ifaceVec.Gauge("interface_bytes_downstream_max"), |
| 119 | lostUp: ifaceVec.Gauge("interface_lost_upstream_percent"), |
| 120 | lostDown: ifaceVec.Gauge("interface_lost_downstream_percent"), |
| 121 | jitterUp: ifaceVec.Gauge("interface_jitter_upstream_ms"), |
| 122 | jitterDown: ifaceVec.Gauge("interface_jitter_downstream_ms"), |
| 123 | discardUp: ifaceVec.Gauge("interface_packets_discarded_upstream"), |
| 124 | discardDown: ifaceVec.Gauge("interface_packets_discarded_downstream"), |
| 125 | rtt: ifaceVec.Gauge("interface_rtt_ms"), |
| 126 | }, |
| 127 | }, |
| 128 | bgp: bgpMetricInstruments{ |
| 129 | sessionStatus: newStateSetVec(bgpVec, "bgp_session_status", bgpSessionStates), |
| 130 | routes: bgpVec.Gauge("bgp_routes"), |
| 131 | routesLimit: bgpVec.Gauge("bgp_routes_limit"), |
| 132 | routesLimitState: newStateSetVec(bgpVec, "bgp_routes_limit_status", bgpRoutesLimitStates), |
| 133 | ribOutRoutes: bgpVec.Gauge("bgp_rib_out_routes"), |
| 134 | }, |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | func newStateSetVec(meter metrix.SnapshotVecMeter, name string, states []string) metrix.SnapshotStateSetVec { |
| 139 | return meter.StateSet(name, metrix.WithStateSetMode(metrix.ModeEnum), metrix.WithStateSetStates(states...)) |
| 140 | } |