| 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 | var ( |
| 11 | bfdUpDesc *prometheus.Desc |
| 12 | bfdUptimeDesc *prometheus.Desc |
| 13 | bfdIntervalDesc *prometheus.Desc |
| 14 | bfdTimoutDesc *prometheus.Desc |
| 15 | ) |
| 16 | |
| 17 | func init() { |
| 18 | l := []string{"name", "ip", "interface"} |
| 19 | prefix := "bird_bfd_session_" |
| 20 | bfdUpDesc = prometheus.NewDesc(prefix+"up", "Session is up", l, nil) |
| 21 | bfdUptimeDesc = prometheus.NewDesc(prefix+"uptime_seconds", "Session uptime in seconds", l, nil) |
| 22 | bfdIntervalDesc = prometheus.NewDesc(prefix+"interval_seconds", "Session uptime in seconds", l, nil) |
| 23 | bfdTimoutDesc = prometheus.NewDesc(prefix+"timeout_seconds", "Session timeout in seconds", l, nil) |
| 24 | } |
| 25 | |
| 26 | type bfdMetricExporter struct { |
| 27 | client client.Client |
| 28 | } |
| 29 | |
| 30 | // NewBFDExporter creates a new MetricExporter for BFD metrics |
| 31 | func NewBFDExporter(client client.Client) MetricExporter { |
| 32 | return &bfdMetricExporter{client: client} |
| 33 | } |
| 34 | |
| 35 | func (m *bfdMetricExporter) Describe(ch chan<- *prometheus.Desc) { |
| 36 | ch <- bfdUpDesc |
| 37 | ch <- bfdUptimeDesc |
| 38 | ch <- bfdIntervalDesc |
| 39 | ch <- bfdTimoutDesc |
| 40 | } |
| 41 | |
| 42 | func (m *bfdMetricExporter) Export(p *protocol.Protocol, ch chan<- prometheus.Metric, newFormat bool) { |
| 43 | if p.Proto != protocol.BFD { |
| 44 | return |
| 45 | } |
| 46 | |
| 47 | sessions, err := m.client.GetBFDSessions(p) |
| 48 | if err != nil { |
| 49 | log.Errorln(err) |
| 50 | return |
| 51 | } |
| 52 | |
| 53 | for _, s := range sessions { |
| 54 | m.exportSession(s, p.Name, ch) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | func (m *bfdMetricExporter) exportSession(s *protocol.BFDSession, protocolName string, ch chan<- prometheus.Metric) { |
| 59 | l := []string{protocolName, s.IP, s.Interface} |
| 60 | |
| 61 | var up float64 |
| 62 | var uptime float64 |
| 63 | |
| 64 | if s.Up { |
| 65 | up = 1 |
| 66 | uptime = float64(s.Since) |
| 67 | } |
| 68 | |
| 69 | ch <- prometheus.MustNewConstMetric(bfdUpDesc, prometheus.GaugeValue, up, l...) |
| 70 | ch <- prometheus.MustNewConstMetric(bfdUptimeDesc, prometheus.GaugeValue, uptime, l...) |
| 71 | ch <- prometheus.MustNewConstMetric(bfdIntervalDesc, prometheus.GaugeValue, s.Interval, l...) |
| 72 | ch <- prometheus.MustNewConstMetric(bfdTimoutDesc, prometheus.GaugeValue, s.Timeout, l...) |
| 73 | } |