| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "github.com/czerwonk/bird_exporter/client" |
| 5 | "github.com/czerwonk/bird_exporter/protocol" |
| 6 | "github.com/prometheus/client_golang/prometheus" |
| 7 | log "github.com/sirupsen/logrus" |
| 8 | ) |
| 9 | |
| 10 | type ospfDesc struct { |
| 11 | runningDesc *prometheus.Desc |
| 12 | interfaceCountDesc *prometheus.Desc |
| 13 | neighborCountDesc *prometheus.Desc |
| 14 | neighborAdjacentCountDesc *prometheus.Desc |
| 15 | } |
| 16 | |
| 17 | type ospfMetricExporter struct { |
| 18 | descriptions map[string]*ospfDesc |
| 19 | client client.Client |
| 20 | } |
| 21 | |
| 22 | // NewOSPFExporter creates a new MetricExporter for OSPF metrics |
| 23 | func NewOSPFExporter(prefix string, client client.Client) MetricExporter { |
| 24 | d := make(map[string]*ospfDesc) |
| 25 | d["4"] = getDesc(prefix + "ospf") |
| 26 | d["6"] = getDesc(prefix + "ospfv3") |
| 27 | |
| 28 | return &ospfMetricExporter{descriptions: d, client: client} |
| 29 | } |
| 30 | |
| 31 | func getDesc(prefix string) *ospfDesc { |
| 32 | labels := []string{"name"} |
| 33 | |
| 34 | d := &ospfDesc{} |
| 35 | d.runningDesc = prometheus.NewDesc(prefix+"_running", "State of OSPF: 0 = Alone, 1 = Running (Neighbor-Adjacencies established)", labels, nil) |
| 36 | |
| 37 | labels = append(labels, "area") |
| 38 | d.interfaceCountDesc = prometheus.NewDesc(prefix+"_interface_count", "Number of interfaces in the area", labels, nil) |
| 39 | d.neighborCountDesc = prometheus.NewDesc(prefix+"_neighbor_count", "Number of neighbors in the area", labels, nil) |
| 40 | d.neighborAdjacentCountDesc = prometheus.NewDesc(prefix+"_neighbor_adjacent_count", "Number of adjacent neighbors in the area", labels, nil) |
| 41 | |
| 42 | return d |
| 43 | } |
| 44 | |
| 45 | func (m *ospfMetricExporter) Describe(ch chan<- *prometheus.Desc) { |
| 46 | m.describe("4", ch) |
| 47 | m.describe("6", ch) |
| 48 | } |
| 49 | |
| 50 | func (m *ospfMetricExporter) describe(ipVersion string, ch chan<- *prometheus.Desc) { |
| 51 | d := m.descriptions[ipVersion] |
| 52 | ch <- d.runningDesc |
| 53 | ch <- d.interfaceCountDesc |
| 54 | ch <- d.neighborCountDesc |
| 55 | ch <- d.neighborAdjacentCountDesc |
| 56 | } |
| 57 | |
| 58 | func (m *ospfMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric, newFormat bool) { |
| 59 | d := m.descriptions[p.IPVersion] |
| 60 | |
| 61 | var running float64 |
| 62 | if p.State == "Running" { |
| 63 | running = 1 |
| 64 | } |
| 65 | |
| 66 | ch <- prometheus.MustNewConstMetric(d.runningDesc, prometheus.GaugeValue, running, p.Name) |
| 67 | |
| 68 | areas, err := m.client.GetOSPFAreas(p) |
| 69 | if err != nil { |
| 70 | log.Errorln(err) |
| 71 | return |
| 72 | } |
| 73 | |
| 74 | for _, area := range areas { |
| 75 | l := []string{p.Name, area.Name} |
| 76 | ch <- prometheus.MustNewConstMetric(d.interfaceCountDesc, prometheus.GaugeValue, float64(area.InterfaceCount), l...) |
| 77 | ch <- prometheus.MustNewConstMetric(d.neighborCountDesc, prometheus.GaugeValue, float64(area.NeighborCount), l...) |
| 78 | ch <- prometheus.MustNewConstMetric(d.neighborAdjacentCountDesc, prometheus.GaugeValue, float64(area.NeighborAdjacentCount), l...) |
| 79 | } |
| 80 | } |