Skip to content
Merged
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 .golangci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ version: "2"
linters:
enable:
- misspell
- modernize
- revive
- sloglint
exclusions:
generated: lax
Expand All @@ -15,5 +17,20 @@ linters:
- errcheck
path: _test.go
formatters:
enable:
- gci
- gofumpt
- goimports
exclusions:
generated: lax
settings:
gci:
sections:
- standard
- default
- prefix(github.com/prometheus-community/fortigate_exporter)
gofumpt:
extra-rules: true
goimports:
local-prefixes:
- github.com/prometheus-community/fortigate_exporter
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2025 The Prometheus Authors
# Copyright The Prometheus Authors
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
Expand Down
12 changes: 6 additions & 6 deletions fortigate_exporter.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -37,13 +37,13 @@ import (
"runtime/debug"
"strings"

"github.com/prometheus-community/fortigate_exporter/pkg/probe"

"github.com/prometheus-community/fortigate_exporter/internal/config"
fortiHTTP "github.com/prometheus-community/fortigate_exporter/pkg/http"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
"github.com/prometheus/client_golang/prometheus/promhttp"

"github.com/prometheus-community/fortigate_exporter/internal/config"
fortiHTTP "github.com/prometheus-community/fortigate_exporter/pkg/http"
"github.com/prometheus-community/fortigate_exporter/pkg/probe"
)

var (
Expand Down Expand Up @@ -105,7 +105,7 @@ func main() {
}

http.Handle("/metrics", promhttp.Handler())
http.HandleFunc("/probe", probe.ProbeHandler)
http.HandleFunc("/probe", probe.Handler)
go func() {
if err := http.ListenAndServe(savedConfig.Listen, nil); err != nil {
log.Fatalf("Unable to serve: %v", err)
Expand Down
21 changes: 12 additions & 9 deletions internal/config/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -28,7 +28,7 @@ type FortiExporterParameter struct {
ScrapeTimeout *int
TLSTimeout *int
TLSInsecure *bool
TlsExtraCAs *string
TLSExtraCAs *string
MaxBGPPaths *int
MaxVPNUsers *int
}
Expand All @@ -39,16 +39,18 @@ type FortiExporterConfig struct {
ScrapeTimeout int
TLSTimeout int
TLSInsecure bool
TlsExtraCAs []LocalCert
TLSExtraCAs []LocalCert
MaxBGPPaths int
MaxVPNUsers int
}

type AuthKeys map[Target]TargetAuth

type Target string
type Token string
type ProbeList []string
type (
Target string
Token string
ProbeList []string
)

type Probes struct {
Include ProbeList
Expand All @@ -72,7 +74,7 @@ var (
ScrapeTimeout: flag.Int("scrape-timeout", 30, "max seconds to allow a scrape to take"),
TLSTimeout: flag.Int("https-timeout", 10, "TLS Handshake timeout in seconds"),
TLSInsecure: flag.Bool("insecure", false, "Allow insecure certificates"),
TlsExtraCAs: flag.String("extra-ca-certs", "", "comma-separated files containing extra PEMs to trust for TLS connections in addition to the system trust store"),
TLSExtraCAs: flag.String("extra-ca-certs", "", "comma-separated files containing extra PEMs to trust for TLS connections in addition to the system trust store"),
MaxBGPPaths: flag.Int("max-bgp-paths", 10000, "How many BGP Paths to receive when counting routes, needs to be greater than or equal to the number of routes or metrics will not be generated"),
MaxVPNUsers: flag.Int("max-vpn-users", 0, "How many VPN Users to receive when counting users, needs to be greater than or equal the number of users or metrics will not be generated (0 eq. none by default)"),
}
Expand All @@ -93,6 +95,7 @@ func MustReInit() {
log.Fatalf("config.ReInit failed: %+v", err)
}
}

func ReInit() error {
flag.Parse()

Expand Down Expand Up @@ -120,7 +123,7 @@ func ReInit() error {
log.Printf("Loaded %d API keys", len(savedConfig.AuthKeys))

// parse ExtraCAs
for _, eca := range strings.Split(*parameter.TlsExtraCAs, ",") {
for eca := range strings.SplitSeq(*parameter.TLSExtraCAs, ",") {
if eca == "" {
continue
}
Expand All @@ -135,7 +138,7 @@ func ReInit() error {
Path: eca,
Content: certs,
}
savedConfig.TlsExtraCAs = append(savedConfig.TlsExtraCAs, certObject)
savedConfig.TLSExtraCAs = append(savedConfig.TLSExtraCAs, certObject)
}

return nil
Expand Down
2 changes: 1 addition & 1 deletion internal/utils/files/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
2 changes: 1 addition & 1 deletion internal/version/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down
18 changes: 9 additions & 9 deletions internal/version/main_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -36,24 +36,24 @@ import (

func TestVersionParseOK(t *testing.T) {
for _, tv := range []struct {
v string
maj int
min int
ok bool
v string
major int
minor int
ok bool
}{
{v: "v6.4.4", maj: 6, min: 4, ok: true},
{v: "v6.4.4", major: 6, minor: 4, ok: true},
{v: "1.0.0", ok: false},
} {
t.Run(tv.v, func(t *testing.T) {
maj, min, ok := ParseVersion(tv.v)
major, minor, ok := ParseVersion(tv.v)
if !tv.ok {
if ok {
t.Errorf("Expected %q to fail to parse, succeeded", tv.v)
}
return
}
if maj != tv.maj || min != tv.min {
t.Errorf("Expected %q to be (%d, %d), was (%d, %d)", tv.v, tv.maj, tv.min, maj, min)
if major != tv.major || minor != tv.minor {
t.Errorf("Expected %q to be (%d, %d), was (%d, %d)", tv.v, tv.major, tv.minor, major, minor)
}
})
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/http/forti_token_client.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -41,13 +41,13 @@ import (
"github.com/prometheus-community/fortigate_exporter/internal/config"
)

type HTTPClient interface {
type Client interface {
Do(req *http.Request) (*http.Response, error)
}

type fortiTokenClient struct {
tgt url.URL
hc HTTPClient
hc Client
ctx context.Context
tok config.Token
}
Expand All @@ -61,7 +61,7 @@ func (c *fortiTokenClient) newGetRequest(url string) (*http.Request, error) {
return r, nil
}

func (c *fortiTokenClient) Get(path string, query string, obj interface{}) error {
func (c *fortiTokenClient) Get(path, query string, obj any) error {
u := c.tgt
u.Path = path
u.RawQuery = query
Expand Down Expand Up @@ -91,6 +91,6 @@ func (c *fortiTokenClient) String() string {
return c.tgt.String()
}

func newFortiTokenClient(ctx context.Context, tgt url.URL, hc HTTPClient, token config.Token) (*fortiTokenClient, error) {
func newFortiTokenClient(ctx context.Context, tgt url.URL, hc Client, token config.Token) (*fortiTokenClient, error) {
return &fortiTokenClient{tgt, hc, ctx, token}, nil
}
8 changes: 4 additions & 4 deletions pkg/http/forti_token_client_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -40,12 +40,12 @@ import (
"testing"
)

type fakeHTTPClient struct {
type fakeClient struct {
status int
body string
}

func (c *fakeHTTPClient) Do(r *http.Request) (*http.Response, error) {
func (c *fakeClient) Do(_ *http.Request) (*http.Response, error) {
return &http.Response{
Body: io.NopCloser(strings.NewReader(c.body)),
StatusCode: c.status,
Expand All @@ -56,7 +56,7 @@ func newClient(sc int, b string) (*fortiTokenClient, error) {
return newFortiTokenClient(
context.Background(),
url.URL{Scheme: "https", Host: "localhost"},
&fakeHTTPClient{sc, b},
&fakeClient{sc, b},
"TEST-TOKEN",
)
}
Expand Down
8 changes: 3 additions & 5 deletions pkg/http/main.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand Down Expand Up @@ -27,11 +27,10 @@ import (
)

type FortiHTTP interface {
Get(path string, query string, obj interface{}) error
Get(path, query string, obj any) error
}

func NewFortiClient(ctx context.Context, tgt url.URL, hc *http.Client, aConfig config.FortiExporterConfig) (FortiHTTP, error) {

auth, ok := aConfig.AuthKeys[config.Target(tgt.String())]
if !ok {
return nil, fmt.Errorf("no API authentication registered for %q", tgt.String())
Expand All @@ -56,8 +55,7 @@ func Configure(config config.FortiExporterConfig) error {
log.Fatalf("Unable to fetch system CA store: %v", err)
return err
}
for _, cert := range config.TlsExtraCAs {

for _, cert := range config.TLSExtraCAs {
if ok := roots.AppendCertsFromPEM(cert.Content); !ok {
return fmt.Errorf("failed to append certs from PEM %q, unknown error", cert.Path)
}
Expand Down
13 changes: 7 additions & 6 deletions pkg/probe/bgp_neighbor_routes.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -17,9 +17,10 @@ import (
"fmt"
"log"

"github.com/prometheus/client_golang/prometheus"

"github.com/prometheus-community/fortigate_exporter/internal/config"
"github.com/prometheus-community/fortigate_exporter/pkg/http"
"github.com/prometheus/client_golang/prometheus"
)

type BGPPath struct {
Expand Down Expand Up @@ -85,13 +86,13 @@ func probeBGPNeighborPathsIPv4(c http.FortiHTTP, meta *TargetMetadata) ([]promet
Source: route.LearnedFrom,
VDOM: r.VDOM,
}
srMap[sr] += 1
srMap[sr]++
if route.IsBest {
sr2 := PathCount{
Source: route.LearnedFrom,
VDOM: r.VDOM,
}
sr2Map[sr2] += 1
sr2Map[sr2]++
}
}
}
Expand Down Expand Up @@ -153,13 +154,13 @@ func probeBGPNeighborPathsIPv6(c http.FortiHTTP, meta *TargetMetadata) ([]promet
Source: route.LearnedFrom,
VDOM: r.VDOM,
}
srMap[sr] += 1
srMap[sr]++
if route.IsBest {
sr2 := PathCount{
Source: route.LearnedFrom,
VDOM: r.VDOM,
}
sr2Map[sr2] += 1
sr2Map[sr2]++
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions pkg/probe/bgp_neighbor_routes_test.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2025 The Prometheus Authors
// Copyright The Prometheus Authors
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
Expand All @@ -17,9 +17,10 @@ import (
"strings"
"testing"

"github.com/prometheus-community/fortigate_exporter/internal/config"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"

"github.com/prometheus-community/fortigate_exporter/internal/config"
)

func TestBGPNeighborPathsIPv4(t *testing.T) {
Expand Down Expand Up @@ -48,7 +49,6 @@ func TestBGPNeighborPathsIPv4(t *testing.T) {
}

func TestBGPNeighborPathsIPv6(t *testing.T) {

if err := config.Init(); err != nil {
t.Fatalf("config.Init failed: %+v", err)
}
Expand Down
Loading
Loading