Skip to content

Commit

Permalink
fix: custom collector for dhcp leases to remove them when they expire
Browse files Browse the repository at this point in the history
  • Loading branch information
henrywhitaker3 committed Mar 28, 2024
1 parent 6a5c84a commit 16c83c2
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 19 deletions.
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,5 +59,4 @@ scrape_configs:
| adguard_top_upstreams | The number of repsonses for the top upstream servers |
| adguard_top_upstreams_avg_response_time_seconds | The average response time for each of the top upstream servers |
| adguard_dhcp_enabled | Whether dhcp is enabled |
| adguard_dhcp_leases | The number of dhcp leases |
| adguard_dhcp_static_leases | The number of static dhcp leases |
| adguard_dhcp_leases | The dhcp leases |
20 changes: 19 additions & 1 deletion internal/adguard/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"io"
"net/http"
"net/url"
"slices"

"github.com/henrywhitaker3/adguard-exporter/internal/config"
)
Expand Down Expand Up @@ -70,7 +71,24 @@ func (c *Client) GetStatus(ctx context.Context) (*Status, error) {
func (c *Client) GetDhcp(ctx context.Context) (*DhcpStatus, error) {
out := &DhcpStatus{}
err := c.do(ctx, http.MethodGet, "/control/dhcp/status", out)
return out, err
if err != nil {
return nil, err
}

for i := range out.DynamicLeases {
l := out.DynamicLeases[i]
l.Type = "dynamic"
out.DynamicLeases[i] = l
}
for i := range out.StaticLeases {
l := out.StaticLeases[i]
l.Type = "static"
out.StaticLeases[i] = l
}

out.Leases = slices.Concat(out.DynamicLeases, out.StaticLeases)

return out, nil
}

func (c *Client) Url() string {
Expand Down
8 changes: 5 additions & 3 deletions internal/adguard/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,12 @@ type DhcpLease struct {
IP string `json:"ip"`
Hostname string `json:"hostname"`
Expires string `json:"expires,omitempty"`
Type string
}

type DhcpStatus struct {
Enabled Bool ` json:"enabled"`
Leases []DhcpLease `json:"leases"`
StaticLeases []DhcpLease `json:"static_leases"`
Enabled Bool ` json:"enabled"`
DynamicLeases []DhcpLease `json:"leases"`
StaticLeases []DhcpLease `json:"static_leases"`
Leases []DhcpLease
}
59 changes: 48 additions & 11 deletions internal/metrics/metrics.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package metrics

import (
"sync"

"github.com/henrywhitaker3/adguard-exporter/internal/adguard"
"github.com/prometheus/client_golang/prometheus"
)

Expand Down Expand Up @@ -81,18 +84,53 @@ var (
Namespace: "adguard",
Help: "Whether dhcp is enabled",
}, []string{"server"})
DhcpLeases = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "dhcp_leases",
Namespace: "adguard",
Help: "The number of dhcp leases",
}, []string{"server"})
DhcpStaticLeases = prometheus.NewGaugeVec(prometheus.GaugeOpts{
Name: "dhcp_static_leases",
Namespace: "adguard",
Help: "The number of static dhcp leases",
}, []string{"server"})
DhcpLeasesMetric = prometheus.NewDesc(
"adguard_dchp_leases",
"The dhcp leases",
[]string{"server", "type", "ip", "mac", "hostname"},
nil,
)
DhcpLeases = NewDhcpLeasesServer(DhcpLeasesMetric)
)

type DhcpLeasesServer struct {
mu *sync.Mutex
Desc *prometheus.Desc
leases map[string][]adguard.DhcpLease
}

func NewDhcpLeasesServer(desc *prometheus.Desc) *DhcpLeasesServer {
return &DhcpLeasesServer{
mu: &sync.Mutex{},
leases: map[string][]adguard.DhcpLease{},
Desc: desc,
}
}

func (d *DhcpLeasesServer) Record(server string, leases []adguard.DhcpLease) {
d.mu.Lock()
defer d.mu.Unlock()

d.leases[server] = leases
}

func (d *DhcpLeasesServer) Collect(ch chan<- prometheus.Metric) {
for server, leases := range d.leases {
for _, lease := range leases {
ch <- prometheus.MustNewConstMetric(
d.Desc,
prometheus.CounterValue,
1,
server, lease.Type, lease.IP, lease.Mac, lease.Hostname,
)
}
}
}

func (d *DhcpLeasesServer) Describe(ch chan<- *prometheus.Desc) {
ch <- d.Desc
}

func Init() {
prometheus.MustRegister(ScrapeErrors)

Expand All @@ -115,5 +153,4 @@ func Init() {
// DHCP
prometheus.MustRegister(DhcpEnabled)
prometheus.MustRegister(DhcpLeases)
prometheus.MustRegister(DhcpStaticLeases)
}
3 changes: 1 addition & 2 deletions internal/worker/worker.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,5 @@ func collectDhcp(ctx context.Context, client *adguard.Client) {
return
}
metrics.DhcpEnabled.WithLabelValues(client.Url()).Set(float64(dhcp.Enabled.Int()))
metrics.DhcpLeases.WithLabelValues(client.Url()).Set(float64(len(dhcp.Leases)))
metrics.DhcpStaticLeases.WithLabelValues(client.Url()).Set(float64(len(dhcp.StaticLeases)))
metrics.DhcpLeases.Record(client.Url(), dhcp.Leases)
}

0 comments on commit 16c83c2

Please sign in to comment.