refactoring (resolves #9)
This commit is contained in:
parent
a9d6003ed4
commit
3b3e9431a7
33
bgp/bgp_collector.go
Normal file
33
bgp/bgp_collector.go
Normal file
@ -0,0 +1,33 @@
|
||||
package bgp
|
||||
|
||||
import (
|
||||
"github.com/czerwonk/bird_exporter/protocol"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var exporter map[int]*protocol.GenericProtocolMetricExporter
|
||||
|
||||
type BgpCollector struct {
|
||||
protocols []*protocol.Protocol
|
||||
}
|
||||
|
||||
func init() {
|
||||
exporter = make(map[int]*protocol.GenericProtocolMetricExporter)
|
||||
exporter[4] = protocol.NewGenericProtocolMetricExporter("bgp4_session")
|
||||
exporter[6] = protocol.NewGenericProtocolMetricExporter("bgp6_session")
|
||||
}
|
||||
|
||||
func NewCollector(p []*protocol.Protocol) prometheus.Collector {
|
||||
return &BgpCollector{protocols: p}
|
||||
}
|
||||
|
||||
func (m *BgpCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
exporter[4].Describe(ch)
|
||||
exporter[6].Describe(ch)
|
||||
}
|
||||
|
||||
func (m *BgpCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
for _, p := range m.protocols {
|
||||
exporter[p.IpVersion].Export(p, ch)
|
||||
}
|
||||
}
|
@ -1,59 +0,0 @@
|
||||
package bgp
|
||||
|
||||
import (
|
||||
"github.com/czerwonk/bird_exporter/protocol"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var descriptions map[int]*desc
|
||||
|
||||
type desc struct {
|
||||
upDesc *prometheus.Desc
|
||||
importCountDesc *prometheus.Desc
|
||||
exportCountDesc *prometheus.Desc
|
||||
filterCountDesc *prometheus.Desc
|
||||
preferredCountDesc *prometheus.Desc
|
||||
uptimeDesc *prometheus.Desc
|
||||
}
|
||||
|
||||
type BgpMetric struct {
|
||||
Protocol *protocol.Protocol
|
||||
}
|
||||
|
||||
func init() {
|
||||
descriptions = make(map[int]*desc)
|
||||
descriptions[4] = getDesc("bgp4")
|
||||
descriptions[6] = getDesc("bgp6")
|
||||
}
|
||||
|
||||
func getDesc(prefix string) *desc {
|
||||
labels := []string{"name"}
|
||||
|
||||
d := &desc{}
|
||||
d.upDesc = prometheus.NewDesc(prefix+"_session_up", "Protocol is up", labels, nil)
|
||||
d.importCountDesc = prometheus.NewDesc(prefix+"_session_prefix_count_import", "Number of imported routes", labels, nil)
|
||||
d.exportCountDesc = prometheus.NewDesc(prefix+"_session_prefix_count_export", "Number of exported routes", labels, nil)
|
||||
d.filterCountDesc = prometheus.NewDesc(prefix+"_session_prefix_count_filter", "Number of filtered routes", labels, nil)
|
||||
d.preferredCountDesc = prometheus.NewDesc(prefix+"_session_prefix_count_preferred", "Number of preferred routes", labels, nil)
|
||||
d.uptimeDesc = prometheus.NewDesc(prefix+"_session_uptime", "Uptime of the protocol in seconds", labels, nil)
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (m *BgpMetric) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- descriptions[m.Protocol.IpVersion].upDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].importCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].exportCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].filterCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].preferredCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].uptimeDesc
|
||||
}
|
||||
|
||||
func (m *BgpMetric) GetMetrics(ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].upDesc, prometheus.GaugeValue, float64(m.Protocol.Up), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].importCountDesc, prometheus.GaugeValue, float64(m.Protocol.Imported), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].exportCountDesc, prometheus.GaugeValue, float64(m.Protocol.Exported), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].filterCountDesc, prometheus.GaugeValue, float64(m.Protocol.Filtered), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].preferredCountDesc, prometheus.GaugeValue, float64(m.Protocol.Preferred), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].uptimeDesc, prometheus.GaugeValue, float64(m.Protocol.Uptime), m.Protocol.Name)
|
||||
}
|
@ -1,31 +1,47 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/czerwonk/bird_exporter/bgp"
|
||||
"github.com/czerwonk/bird_exporter/ospf"
|
||||
"github.com/czerwonk/bird_exporter/protocol"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
type MetricCollector struct {
|
||||
Protocols []ProtocolMetric
|
||||
collectors []prometheus.Collector
|
||||
}
|
||||
|
||||
func NewMetricCollectorForProtocols(p []*protocol.Protocol) *MetricCollector {
|
||||
m := make([]ProtocolMetric, 0)
|
||||
for _, x := range p {
|
||||
m = append(m, NewProtocolMetricFromProtocol(x))
|
||||
func NewMetricCollectorForProtocols(protocols []*protocol.Protocol) *MetricCollector {
|
||||
b := make([]*protocol.Protocol, 0)
|
||||
o := make([]*protocol.Protocol, 0)
|
||||
|
||||
for _, p := range protocols {
|
||||
if p.Proto == protocol.BGP {
|
||||
b = append(b, p)
|
||||
} else if p.Proto == protocol.OSPF {
|
||||
o = append(o, p)
|
||||
}
|
||||
}
|
||||
|
||||
return &MetricCollector{Protocols: m}
|
||||
c := make([]prometheus.Collector, 0)
|
||||
if len(b) > 0 {
|
||||
c = append(c, bgp.NewCollector(b))
|
||||
}
|
||||
if len(o) > 0 {
|
||||
c = append(c, ospf.NewCollector(o))
|
||||
}
|
||||
|
||||
return &MetricCollector{collectors: c}
|
||||
}
|
||||
|
||||
func (m *MetricCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
for _, p := range m.Protocols {
|
||||
p.Describe(ch)
|
||||
for _, c := range m.collectors {
|
||||
c.Describe(ch)
|
||||
}
|
||||
}
|
||||
|
||||
func (m *MetricCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
for _, p := range m.Protocols {
|
||||
p.GetMetrics(ch)
|
||||
for _, c := range m.collectors {
|
||||
c.Collect(ch)
|
||||
}
|
||||
}
|
||||
|
54
ospf/ospf_collector.go
Normal file
54
ospf/ospf_collector.go
Normal file
@ -0,0 +1,54 @@
|
||||
package ospf
|
||||
|
||||
import (
|
||||
"github.com/czerwonk/bird_exporter/protocol"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var descriptions map[int]*desc
|
||||
var exporter map[int]*protocol.GenericProtocolMetricExporter
|
||||
|
||||
type desc struct {
|
||||
runningDesc *prometheus.Desc
|
||||
}
|
||||
|
||||
type OspfCollector struct {
|
||||
protocols []*protocol.Protocol
|
||||
}
|
||||
|
||||
func init() {
|
||||
exporter = make(map[int]*protocol.GenericProtocolMetricExporter)
|
||||
exporter[4] = protocol.NewGenericProtocolMetricExporter("ospf")
|
||||
exporter[6] = protocol.NewGenericProtocolMetricExporter("ospfv3")
|
||||
|
||||
descriptions = make(map[int]*desc)
|
||||
descriptions[4] = getDesc("ospf")
|
||||
descriptions[6] = getDesc("ospfv3")
|
||||
}
|
||||
|
||||
func getDesc(prefix string) *desc {
|
||||
labels := []string{"name"}
|
||||
|
||||
d := &desc{}
|
||||
d.runningDesc = prometheus.NewDesc(prefix+"_running", "State of OSPF: 0 = Alone, 1 = Running (Neighbor-Adjacencies established)", labels, nil)
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func NewCollector(p []*protocol.Protocol) prometheus.Collector {
|
||||
return &OspfCollector{protocols: p}
|
||||
}
|
||||
|
||||
func (m *OspfCollector) Describe(ch chan<- *prometheus.Desc) {
|
||||
exporter[4].Describe(ch)
|
||||
exporter[6].Describe(ch)
|
||||
ch <- descriptions[4].runningDesc
|
||||
ch <- descriptions[6].runningDesc
|
||||
}
|
||||
|
||||
func (m *OspfCollector) Collect(ch chan<- prometheus.Metric) {
|
||||
for _, p := range m.protocols {
|
||||
exporter[p.IpVersion].Export(p, ch)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[p.IpVersion].runningDesc, prometheus.GaugeValue, p.Attributes["running"], p.Name)
|
||||
}
|
||||
}
|
@ -1,63 +0,0 @@
|
||||
package ospf
|
||||
|
||||
import (
|
||||
"github.com/czerwonk/bird_exporter/protocol"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
var descriptions map[int]*desc
|
||||
|
||||
type desc struct {
|
||||
upDesc *prometheus.Desc
|
||||
importCountDesc *prometheus.Desc
|
||||
exportCountDesc *prometheus.Desc
|
||||
filterCountDesc *prometheus.Desc
|
||||
preferredCountDesc *prometheus.Desc
|
||||
uptimeDesc *prometheus.Desc
|
||||
runningDesc *prometheus.Desc
|
||||
}
|
||||
|
||||
type OspfMetric struct {
|
||||
Protocol *protocol.Protocol
|
||||
}
|
||||
|
||||
func init() {
|
||||
descriptions = make(map[int]*desc)
|
||||
descriptions[4] = getDesc("ospf")
|
||||
descriptions[6] = getDesc("ospfv3")
|
||||
}
|
||||
|
||||
func getDesc(prefix string) *desc {
|
||||
labels := []string{"name"}
|
||||
|
||||
d := &desc{}
|
||||
d.upDesc = prometheus.NewDesc(prefix+"_up", "Protocol is up", labels, nil)
|
||||
d.importCountDesc = prometheus.NewDesc(prefix+"_prefix_count_import", "Number of imported routes", labels, nil)
|
||||
d.exportCountDesc = prometheus.NewDesc(prefix+"_prefix_count_export", "Number of exported routes", labels, nil)
|
||||
d.filterCountDesc = prometheus.NewDesc(prefix+"_prefix_count_filter", "Number of filtered routes", labels, nil)
|
||||
d.preferredCountDesc = prometheus.NewDesc(prefix+"_prefix_count_preferred", "Number of preferred routes", labels, nil)
|
||||
d.uptimeDesc = prometheus.NewDesc(prefix+"_uptime", "Uptime of the protocol in seconds", labels, nil)
|
||||
d.runningDesc = prometheus.NewDesc(prefix+"_running", "State of OSPF: 0 = Alone, 1 = Running (Neighbor-Adjacencies established)", labels, nil)
|
||||
|
||||
return d
|
||||
}
|
||||
|
||||
func (m *OspfMetric) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- descriptions[m.Protocol.IpVersion].upDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].importCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].exportCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].filterCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].preferredCountDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].uptimeDesc
|
||||
ch <- descriptions[m.Protocol.IpVersion].runningDesc
|
||||
}
|
||||
|
||||
func (m *OspfMetric) GetMetrics(ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].upDesc, prometheus.GaugeValue, float64(m.Protocol.Up), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].importCountDesc, prometheus.GaugeValue, float64(m.Protocol.Imported), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].exportCountDesc, prometheus.GaugeValue, float64(m.Protocol.Exported), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].filterCountDesc, prometheus.GaugeValue, float64(m.Protocol.Filtered), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].preferredCountDesc, prometheus.GaugeValue, float64(m.Protocol.Preferred), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].uptimeDesc, prometheus.GaugeValue, float64(m.Protocol.Uptime), m.Protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(descriptions[m.Protocol.IpVersion].runningDesc, prometheus.GaugeValue, m.Protocol.Attributes["running"], m.Protocol.Name)
|
||||
}
|
47
protocol/generic_exporter.go
Normal file
47
protocol/generic_exporter.go
Normal file
@ -0,0 +1,47 @@
|
||||
package protocol
|
||||
|
||||
import "github.com/prometheus/client_golang/prometheus"
|
||||
|
||||
type GenericProtocolMetricExporter struct {
|
||||
upDesc *prometheus.Desc
|
||||
importCountDesc *prometheus.Desc
|
||||
exportCountDesc *prometheus.Desc
|
||||
filterCountDesc *prometheus.Desc
|
||||
preferredCountDesc *prometheus.Desc
|
||||
uptimeDesc *prometheus.Desc
|
||||
}
|
||||
|
||||
func NewGenericProtocolMetricExporter(prefix string) *GenericProtocolMetricExporter {
|
||||
m := &GenericProtocolMetricExporter{}
|
||||
m.initDesc(prefix)
|
||||
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *GenericProtocolMetricExporter) initDesc(prefix string) {
|
||||
labels := []string{"name"}
|
||||
m.upDesc = prometheus.NewDesc(prefix+"_up", "Protocol is up", labels, nil)
|
||||
m.importCountDesc = prometheus.NewDesc(prefix+"_prefix_count_import", "Number of imported routes", labels, nil)
|
||||
m.exportCountDesc = prometheus.NewDesc(prefix+"_prefix_count_export", "Number of exported routes", labels, nil)
|
||||
m.filterCountDesc = prometheus.NewDesc(prefix+"_prefix_count_filter", "Number of filtered routes", labels, nil)
|
||||
m.preferredCountDesc = prometheus.NewDesc(prefix+"_prefix_count_preferred", "Number of preferred routes", labels, nil)
|
||||
m.uptimeDesc = prometheus.NewDesc(prefix+"_uptime", "Uptime of the protocol in seconds", labels, nil)
|
||||
}
|
||||
|
||||
func (m *GenericProtocolMetricExporter) Describe(ch chan<- *prometheus.Desc) {
|
||||
ch <- m.upDesc
|
||||
ch <- m.importCountDesc
|
||||
ch <- m.exportCountDesc
|
||||
ch <- m.filterCountDesc
|
||||
ch <- m.preferredCountDesc
|
||||
ch <- m.uptimeDesc
|
||||
}
|
||||
|
||||
func (m *GenericProtocolMetricExporter) Export(protocol *Protocol, ch chan<- prometheus.Metric) {
|
||||
ch <- prometheus.MustNewConstMetric(m.upDesc, prometheus.GaugeValue, float64(protocol.Up), protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(m.importCountDesc, prometheus.GaugeValue, float64(protocol.Imported), protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(m.exportCountDesc, prometheus.GaugeValue, float64(protocol.Exported), protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(m.filterCountDesc, prometheus.GaugeValue, float64(protocol.Filtered), protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(m.preferredCountDesc, prometheus.GaugeValue, float64(protocol.Preferred), protocol.Name)
|
||||
ch <- prometheus.MustNewConstMetric(m.uptimeDesc, prometheus.GaugeValue, float64(protocol.Uptime), protocol.Name)
|
||||
}
|
@ -1,25 +0,0 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/czerwonk/bird_exporter/bgp"
|
||||
"github.com/czerwonk/bird_exporter/ospf"
|
||||
"github.com/czerwonk/bird_exporter/protocol"
|
||||
"github.com/prometheus/client_golang/prometheus"
|
||||
)
|
||||
|
||||
type ProtocolMetric interface {
|
||||
Describe(ch chan<- *prometheus.Desc)
|
||||
GetMetrics(ch chan<- prometheus.Metric)
|
||||
}
|
||||
|
||||
func NewProtocolMetricFromProtocol(p *protocol.Protocol) ProtocolMetric {
|
||||
if p.Proto == protocol.BGP {
|
||||
return &bgp.BgpMetric{Protocol: p}
|
||||
}
|
||||
|
||||
if p.Proto == protocol.OSPF {
|
||||
return &ospf.OspfMetric{Protocol: p}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user