Skip to content

Commit 3d0cbc2

Browse files
anomaliesCollector: Add wanguard_anomaly_latest_value metric
1 parent 6cd9c27 commit 3d0cbc2

File tree

5 files changed

+31
-13
lines changed

5 files changed

+31
-13
lines changed

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
EXPORTER_VERSION=1.7
1+
EXPORTER_VERSION=1.8
22
PACKAGES_DIR=compiled_packages
33

44
all: test build clean

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,13 @@ wanguard_announcements_finished 1
7676
Metric | Type | Description | Labels
7777
-------|------|-------------|-------
7878
wanguard_anomalies_active | gauge | Active anomalies at the moment | anomaly, anomaly_id, bits, bits_s, duration, latest_value, packets, pkts_s, prefix, sensor_interface_name
79+
wanguard_anomaly_latest_value | gauge | Latest measurement value for active anomaly | anomaly, anomaly_id, prefix, sensor_interface_name
7980
wanguard_anomalies_finished | gauge | Number of finished anomalies |
8081

8182
Example:
8283
```
8384
wanguard_anomalies_active{anomaly="ICMP pkts/s > 1",anomaly_id="1",bits="169576384000",bits_s="9014400",duration="60",latest_value="17500",packets="320020500",pkts_s="17500",prefix="10.10.10.10/32",sensor_interface_name="Interface 1"} 1
85+
wanguard_anomaly_latest_value{anomaly="ICMP pkts/s > 1",anomaly_id="1",prefix="10.10.10.10/32",sensor_interface_name="Interface 1"} 17500
8486
wanguard_anomalies_finished 1
8587
```
8688

collectors/anomalies_collector.go

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,10 @@ import (
99
)
1010

1111
type AnomaliesCollector struct {
12-
wgClient *wgc.Client
13-
AnomalyActive *prometheus.Desc
14-
AnomaliesFinished *prometheus.Desc
12+
wgClient *wgc.Client
13+
AnomalyActive *prometheus.Desc
14+
AnomalyLatestValue *prometheus.Desc
15+
AnomaliesFinished *prometheus.Desc
1516
}
1617

1718
type AnomaliesCount struct {
@@ -36,23 +37,25 @@ type Anomaly struct {
3637
func NewAnomaliesCollector(wgclient *wgc.Client) *AnomaliesCollector {
3738
prefix := "wanguard_anomalies_"
3839
return &AnomaliesCollector{
39-
wgClient: wgclient,
40-
AnomalyActive: prometheus.NewDesc(prefix+"active", "Active anomalies at the moment", []string{"prefix", "anomaly", "anomaly_id", "duration", "pkts_s", "packets", "bits_s", "bits", "latest_value", "sensor_interface_name"}, nil),
41-
AnomaliesFinished: prometheus.NewDesc(prefix+"finished", "Number of finished anomalies", nil, nil),
40+
wgClient: wgclient,
41+
AnomalyActive: prometheus.NewDesc(prefix+"active", "Active anomalies at the moment", []string{"prefix", "anomaly", "anomaly_id", "duration", "pkts_s", "packets", "bits_s", "bits", "latest_value", "sensor_interface_name"}, nil),
42+
AnomalyLatestValue: prometheus.NewDesc("wanguard_anomaly_latest_value", "Latest measurement value for active anomaly", []string{"prefix", "anomaly", "anomaly_id", "sensor_interface_name"}, nil),
43+
AnomaliesFinished: prometheus.NewDesc(prefix+"finished", "Number of finished anomalies", nil, nil),
4244
}
4345
}
4446

4547
func (c *AnomaliesCollector) Describe(ch chan<- *prometheus.Desc) {
4648
ch <- c.AnomalyActive
49+
ch <- c.AnomalyLatestValue
4750
ch <- c.AnomaliesFinished
4851
}
4952

5053
func (c *AnomaliesCollector) Collect(ch chan<- prometheus.Metric) {
51-
collectActiveAnomalies(c.AnomalyActive, c.wgClient, ch)
54+
collectActiveAnomalies(c.AnomalyActive, c.AnomalyLatestValue, c.wgClient, ch)
5255
collectFinishedAnomaliesTotal(c.AnomaliesFinished, c.wgClient, ch)
5356
}
5457

55-
func collectActiveAnomalies(desc *prometheus.Desc, wgclient *wgc.Client, ch chan<- prometheus.Metric) {
58+
func collectActiveAnomalies(desc *prometheus.Desc, latestValueDesc *prometheus.Desc, wgclient *wgc.Client, ch chan<- prometheus.Metric) {
5659
var anomalies []Anomaly
5760

5861
err := wgclient.GetParsed("anomalies?status=Active&fields=anomaly_id,anomaly,prefix,duration,pkts/s,packets,bits/s,bits,latest_value,sensor", &anomalies)
@@ -72,6 +75,14 @@ func collectActiveAnomalies(desc *prometheus.Desc, wgclient *wgc.Client, ch chan
7275
anomaly.Bits,
7376
anomaly.LatestValue,
7477
anomaly.Sensor.SensorInterfaceName)
78+
79+
if v, err := strconv.ParseFloat(anomaly.LatestValue, 64); err == nil {
80+
ch <- prometheus.MustNewConstMetric(latestValueDesc, prometheus.GaugeValue, v,
81+
anomaly.Prefix,
82+
anomaly.Anomaly,
83+
anomaly.AnomalyId,
84+
anomaly.Sensor.SensorInterfaceName)
85+
}
7586
}
7687
}
7788

collectors/anomalies_collector_test.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ func TestAnomaliesCollector(t *testing.T) {
1414
AnomaliesCollector := NewAnomaliesCollector(wgcClient)
1515

1616
metricsCount := testutil.CollectAndCount(AnomaliesCollector)
17-
if metricsCount != 2 {
18-
t.Errorf("Expected 2 metrics, got %d", metricsCount)
17+
if metricsCount != 3 {
18+
t.Errorf("Expected 3 metrics, got %d", metricsCount)
1919
}
2020

2121
lintErrors, err := testutil.CollectAndLint(AnomaliesCollector)
@@ -30,6 +30,7 @@ func TestAnomaliesCollector(t *testing.T) {
3030
expectedMetrics := anomaliesExpectedMetrics()
3131
err = testutil.CollectAndCompare(AnomaliesCollector, strings.NewReader(expectedMetrics),
3232
"wanguard_anomalies_active",
33+
"wanguard_anomaly_latest_value",
3334
"wanguard_anomalies_finished")
3435
if err != nil {
3536
t.Errorf("Expected no error, got %s", err)
@@ -41,7 +42,11 @@ func anomaliesExpectedMetrics() string {
4142
# HELP wanguard_anomalies_active Active anomalies at the moment
4243
# TYPE wanguard_anomalies_active gauge
4344
wanguard_anomalies_active{anomaly="ICMP pkts/s > 1",anomaly_id="1",bits="169576384000",bits_s="9014400",duration="60",latest_value="130961807",packets="320020500",pkts_s="17500",prefix="10.10.10.10/32",sensor_interface_name="sFlows Filter Cluster"} 1
44-
45+
46+
# HELP wanguard_anomaly_latest_value Latest measurement value for active anomaly
47+
# TYPE wanguard_anomaly_latest_value gauge
48+
wanguard_anomaly_latest_value{anomaly="ICMP pkts/s > 1",anomaly_id="1",prefix="10.10.10.10/32",sensor_interface_name="sFlows Filter Cluster"} 1.30961807e+08
49+
4550
# HELP wanguard_anomalies_finished Number of finished anomalies
4651
# TYPE wanguard_anomalies_finished gauge
4752
wanguard_anomalies_finished 1

wanguard_exporter.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ import (
1414
)
1515

1616
const (
17-
version string = "1.7"
17+
version string = "1.8"
1818
)
1919

2020
type collectorsList struct {

0 commit comments

Comments
 (0)