Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions strongswan/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -240,13 +247,23 @@ 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(
c.ikeCnt,
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 {
Expand Down
19 changes: 19 additions & 0 deletions strongswan/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"),
Expand Down