From dd262484a573f0d9ccf6cb1c0163431ca33ab0d9 Mon Sep 17 00:00:00 2001 From: FriendlyButFire Date: Fri, 1 Dec 2023 17:14:40 +0100 Subject: [PATCH] feat: added ARP Table informatio and corresponding testcase --- README.md | 3 +++ pkg/probe/probe.go | 1 + pkg/probe/system_arp.go | 45 ++++++++++++++++++++++++++++++++++ pkg/probe/system_arp_test.go | 28 +++++++++++++++++++++ pkg/probe/testdata/arp.jsonnet | 28 +++++++++++++++++++++ 5 files changed, 105 insertions(+) create mode 100644 pkg/probe/system_arp.go create mode 100644 pkg/probe/system_arp_test.go create mode 100644 pkg/probe/testdata/arp.jsonnet diff --git a/README.md b/README.md index 22a3e81..3564e71 100755 --- a/README.md +++ b/README.md @@ -73,6 +73,9 @@ Per-VDOM: * _System/Fortimanager/Status_ * `fortigate_fortimanager_connection_status` * `fortigate_fortimanager_registration_status` + * `fortigate_ippool_total_items` + * _System/Network/Arp + * `fortigate_arp_entry_age_seconds` * _System/Interface_ * `fortigate_interface_link_up` * `fortigate_interface_speed_bps` diff --git a/pkg/probe/probe.go b/pkg/probe/probe.go index c79a7f6..0fe76d7 100644 --- a/pkg/probe/probe.go +++ b/pkg/probe/probe.go @@ -128,6 +128,7 @@ func (p *ProbeCollector) Probe(ctx context.Context, target map[string]string, hc {"Log/Fortianalyzer/Queue", probeLogAnalyzerQueue}, {"Log/DiskUsage", probeLogCurrentDiskUsage}, {"System/AvailableCertificates", probeSystemAvailableCertificates}, + {"System/ArpTable", probeSystemArpTable}, {"System/Fortimanager/Status", probeSystemFortimanagerStatus}, {"System/HAStatistics", probeSystemHAStatistics}, {"System/Interface", probeSystemInterface}, diff --git a/pkg/probe/system_arp.go b/pkg/probe/system_arp.go new file mode 100644 index 0000000..f1b4c80 --- /dev/null +++ b/pkg/probe/system_arp.go @@ -0,0 +1,45 @@ +package probe + +import ( + "log" + + "github.com/bluecmd/fortigate_exporter/pkg/http" + "github.com/prometheus/client_golang/prometheus" +) + +func probeSystemArpTable(c http.FortiHTTP, meta *TargetMetadata) ([]prometheus.Metric, bool) { + var ( + mArpEntryAge = prometheus.NewDesc( + "fortigate_arp_entry_age_seconds", + "Age of ARP table entry in seconds", + []string{"ip", "mac", "interface"}, nil, + ) + ) + + type arpEntry struct { + IP string `json:"ip"` + Age int `json:"age"` + MAC string `json:"mac"` + Interface string `json:"interface"` + } + + type arpResponse struct { + Results []arpEntry `json:"results"` + VDOM string `json:"vdom"` + } + + var r arpResponse + + if err := c.Get("api/v2/monitor/network/arp/select", "", &r); err != nil { + log.Printf("Error: %v", err) + return nil, false + } + + metrics := []prometheus.Metric{} + for _, entry := range r.Results { + metrics = append(metrics, prometheus.MustNewConstMetric(mArpEntryAge, prometheus.GaugeValue, float64(entry.Age), entry.IP, entry.MAC, entry.Interface)) + // Add more metrics if needed + } + + return metrics, true +} diff --git a/pkg/probe/system_arp_test.go b/pkg/probe/system_arp_test.go new file mode 100644 index 0000000..a4054f3 --- /dev/null +++ b/pkg/probe/system_arp_test.go @@ -0,0 +1,28 @@ +package probe + +import ( + "strings" + "testing" + + "github.com/prometheus/client_golang/prometheus" + "github.com/prometheus/client_golang/prometheus/testutil" +) + +func TestSystemArpTable(t *testing.T) { + c := newFakeClient() + c.prepare("api/v2/monitor/network/arp/select", "testdata/arp.jsonnet") + r := prometheus.NewPedanticRegistry() + if !testProbe(probeSystemArpTable, c, r) { + t.Errorf("probeSystemArpTable() returned non-success") + } + + em := ` + # HELP fortigate_arp_entry_age_seconds Age of ARP table entry in seconds + # TYPE fortigate_arp_entry_age_seconds gauge + fortigate_arp_entry_age_seconds{ip="192.168.1.23",mac="aa:05:f3:a6:33:2a",interface="port1"} 0 + fortigate_arp_entry_age_seconds{ip="192.168.1.1",mac="80:16:05:f8:71:10",interface="port1"} 35 + ` + if err := testutil.GatherAndCompare(r, strings.NewReader(em)); err != nil { + t.Fatalf("metric compare: err %v", err) + } +} diff --git a/pkg/probe/testdata/arp.jsonnet b/pkg/probe/testdata/arp.jsonnet new file mode 100644 index 0000000..81c7cbf --- /dev/null +++ b/pkg/probe/testdata/arp.jsonnet @@ -0,0 +1,28 @@ +# api/v2/monitor/network/arp/select?vdom=* +[ + { + "http_method": "GET", + "results": [ + { + "ip": "192.168.1.23", + "age": 0, + "mac": "aa:05:f3:a6:33:2a", + "interface": "port1" + }, + { + "ip": "192.168.1.1", + "age": 35, + "mac": "80:16:05:f8:71:10", + "interface": "port1" + } + ], + "vdom": "root", + "path": "network", + "name": "arp", + "action": "", + "status": "success", + "serial": "FGVMEVHX1X8UNJ34", + "version": "v7.0.5", + "build": 304 + } +] \ No newline at end of file