Skip to content

Commit

Permalink
feat: added ARP Table informatio and corresponding testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
FriendlyButFire committed Dec 1, 2023
1 parent 9d74331 commit dd26248
Show file tree
Hide file tree
Showing 5 changed files with 105 additions and 0 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand Down
1 change: 1 addition & 0 deletions pkg/probe/probe.go
Original file line number Diff line number Diff line change
Expand Up @@ -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},
Expand Down
45 changes: 45 additions & 0 deletions pkg/probe/system_arp.go
Original file line number Diff line number Diff line change
@@ -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
}
28 changes: 28 additions & 0 deletions pkg/probe/system_arp_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
}
28 changes: 28 additions & 0 deletions pkg/probe/testdata/arp.jsonnet
Original file line number Diff line number Diff line change
@@ -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
}
]

0 comments on commit dd26248

Please sign in to comment.