diff --git a/strongswan/collector.go b/strongswan/collector.go index 73e0f6e..bf3766e 100644 --- a/strongswan/collector.go +++ b/strongswan/collector.go @@ -27,6 +27,7 @@ type viciClientFn func() (ViciClient, error) type Collector struct { viciClientFn viciClientFn + up *prometheus.Desc ikeCnt *prometheus.Desc ikeVersion *prometheus.Desc ikeStatus *prometheus.Desc @@ -62,6 +63,11 @@ func NewCollector(viciClientFn viciClientFn) *Collector { return &Collector{ viciClientFn: viciClientFn, + up: prometheus.NewDesc( + prefix+"up", + "Whether scraping metrics was successful.", + nil, nil, + ), ikeCnt: prometheus.NewDesc( prefix+"ike_count", "Number of known IKEs", @@ -202,6 +208,7 @@ func NewCollector(viciClientFn viciClientFn) *Collector { } func (c *Collector) Describe(ch chan<- *prometheus.Desc) { + ch <- c.up ch <- c.ikeCnt ch <- c.ikeVersion ch <- c.ikeStatus @@ -240,6 +247,11 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { prometheus.GaugeValue, float64(0), ) + ch <- prometheus.MustNewConstMetric( + c.up, + prometheus.GaugeValue, + float64(0), + ) return } ch <- prometheus.MustNewConstMetric( @@ -247,6 +259,11 @@ func (c *Collector) Collect(ch chan<- prometheus.Metric) { prometheus.GaugeValue, float64(len(sas)), ) + ch <- prometheus.MustNewConstMetric( + c.up, + prometheus.GaugeValue, + float64(1), + ) for _, ikeSa := range sas { c.collectIkeMetrics(ikeSa, ch) for _, child := range ikeSa.Children { diff --git a/strongswan/collector_test.go b/strongswan/collector_test.go index 576b805..34346aa 100644 --- a/strongswan/collector_test.go +++ b/strongswan/collector_test.go @@ -42,6 +42,25 @@ func TestCollector_Metrics(t *testing.T) { wantMetricsValue int wantMetricsCount int }{ + { + name: "Strongswan status without IKEs", + metricName: "strongswan_up", + wantMetricsHelp: "Whether scraping metrics was successful.", + wantMetricsType: "gauge", + wantMetricsValue: 0, + wantMetricsCount: 1, + }, + { + name: "Strongswan status with IKEs", + msgsModifierFn: func(msgs *vici.Message) { + msgs.Set("ike-name", vici.NewMessage()) + }, + metricName: "strongswan_up", + wantMetricsHelp: "Whether scraping metrics was successful.", + wantMetricsType: "gauge", + wantMetricsValue: 1, + wantMetricsCount: 1, + }, { name: "connection error", viciClientErr: errors.New("some error"),