| 1 | package metrics |
| 2 | |
| 3 | import ( |
| 4 | "github.com/czerwonk/bird_exporter/client" |
| 5 | "github.com/prometheus/client_golang/prometheus" |
| 6 | ) |
| 7 | |
| 8 | type StatusExporter struct { |
| 9 | client *client.BirdClient |
| 10 | socketPath string |
| 11 | |
| 12 | upDesc *prometheus.Desc |
| 13 | infoDesc *prometheus.Desc |
| 14 | lastRebootDesc *prometheus.Desc |
| 15 | lastReconfigDesc *prometheus.Desc |
| 16 | serverTimeDesc *prometheus.Desc |
| 17 | } |
| 18 | |
| 19 | func NewStatusExporter(c *client.BirdClient, socketPath string) *StatusExporter { |
| 20 | return &StatusExporter{ |
| 21 | client: c, |
| 22 | socketPath: socketPath, |
| 23 | |
| 24 | upDesc: prometheus.NewDesc( |
| 25 | "bird_daemon_up", |
| 26 | "Whether the BIRD daemon is up (1) or down (0)", |
| 27 | nil, nil, |
| 28 | ), |
| 29 | infoDesc: prometheus.NewDesc( |
| 30 | "bird_daemon_info", |
| 31 | "Static information about BIRD", |
| 32 | []string{"router_id", "version"}, nil, |
| 33 | ), |
| 34 | lastRebootDesc: prometheus.NewDesc( |
| 35 | "bird_last_reboot_timestamp_seconds", |
| 36 | "Timestamp of the last BIRD reboot", |
| 37 | nil, nil, |
| 38 | ), |
| 39 | lastReconfigDesc: prometheus.NewDesc( |
| 40 | "bird_last_reconfig_timestamp_seconds", |
| 41 | "Timestamp of the last BIRD reconfiguration", |
| 42 | nil, nil, |
| 43 | ), |
| 44 | serverTimeDesc: prometheus.NewDesc( |
| 45 | "bird_server_time_timestamp_seconds", |
| 46 | "Server time reported by BIRD", |
| 47 | nil, nil, |
| 48 | ), |
| 49 | } |
| 50 | } |
| 51 | |
| 52 | func (e *StatusExporter) Describe(ch chan<- *prometheus.Desc) { |
| 53 | ch <- e.upDesc |
| 54 | ch <- e.infoDesc |
| 55 | ch <- e.lastRebootDesc |
| 56 | ch <- e.lastReconfigDesc |
| 57 | ch <- e.serverTimeDesc |
| 58 | } |
| 59 | |
| 60 | func (e *StatusExporter) Collect(ch chan<- prometheus.Metric) { |
| 61 | s, err := e.client.StatusFromSocket(e.socketPath) |
| 62 | if err != nil || s == nil { |
| 63 | ch <- prometheus.MustNewConstMetric(e.upDesc, prometheus.GaugeValue, 0) |
| 64 | return |
| 65 | } |
| 66 | |
| 67 | ch <- prometheus.MustNewConstMetric(e.upDesc, prometheus.GaugeValue, 1) |
| 68 | ch <- prometheus.MustNewConstMetric(e.infoDesc, prometheus.GaugeValue, 1, s.RouterID, s.Version) |
| 69 | |
| 70 | if !s.LastReboot.IsZero() { |
| 71 | ch <- prometheus.MustNewConstMetric(e.lastRebootDesc, prometheus.GaugeValue, float64(s.LastReboot.Unix())) |
| 72 | } |
| 73 | if !s.LastReconfig.IsZero() { |
| 74 | ch <- prometheus.MustNewConstMetric(e.lastReconfigDesc, prometheus.GaugeValue, float64(s.LastReconfig.Unix())) |
| 75 | } |
| 76 | if !s.ServerTime.IsZero() { |
| 77 | ch <- prometheus.MustNewConstMetric(e.serverTimeDesc, prometheus.GaugeValue, float64(s.ServerTime.Unix())) |
| 78 | } |
| 79 | } |