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
16 changes: 15 additions & 1 deletion collector/collector.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ package collector

import (
"bufio"
"errors"
"fmt"
"log"
"os"
Expand Down Expand Up @@ -256,6 +257,13 @@ func question(name string) dns.Question {
}

func parseLease(line string) (*lease, error) {
if strings.TrimSpace(line) == "" {
return nil, errSkipLease
}
if strings.HasPrefix(line, "duid ") || strings.HasPrefix(line, "vendorclass ") {
return nil, errSkipLease
}

arr := strings.Fields(line)
if got, want := len(arr), 5; got != want {
return nil, fmt.Errorf("illegal lease: expected %d fields, got %d", want, got)
Expand All @@ -275,6 +283,8 @@ func parseLease(line string) (*lease, error) {
}, nil
}

var errSkipLease = errors.New("skip lease line")

// Read the DHCP lease file with the given path and return a list of leases.
//
// The format of the DHCP lease file written by dnsmasq is not formally
Expand Down Expand Up @@ -303,7 +313,11 @@ func readLeaseFile(path string) ([]lease, error) {
activeLeases := []lease{}
for i := 1; scanner.Scan(); i++ {
leaseLine := scanner.Text()
if activeLease, err := parseLease(leaseLine); err == nil {
activeLease, err := parseLease(leaseLine)
if errors.Is(err, errSkipLease) {
continue
}
if err == nil {
activeLeases = append(activeLeases, *activeLease)
} else {
log.Printf("Error parsing lease (%d, %q): %s", i, leaseLine, err)
Expand Down
15 changes: 15 additions & 0 deletions collector/collector_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
package collector

import (
"errors"
"fmt"
"io"
"net"
Expand Down Expand Up @@ -222,3 +223,17 @@ func fetchMetrics(t *testing.T, c *Collector) map[string]string {
}
return metrics
}

func TestParseLeaseSkipsMetadataLines(t *testing.T) {
t.Parallel()

for _, line := range []string{
"duid 00:01:00:01:ff:6f:ff:6e:aa:cf:ff:af:4a:8b",
"vendorclass something",
" ",
} {
if lease, err := parseLease(line); !errors.Is(err, errSkipLease) || lease != nil {
t.Fatalf("parseLease(%q) = (%v, %v), want skip error", line, lease, err)
}
}
}
5 changes: 4 additions & 1 deletion collector/testdata/dnsmasq.leases
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
duid 00:01:00:01:ff:6f:ff:6e:aa:cf:ff:af:4a:8b
vendorclass some-vendor-info
1625595932 00:00:00:00:00:00 10.10.10.10 host-1 00:00:00:00:00:00
0 00:00:00:00:00:01 10.10.10.11 host-2 00:00:00:00:00:01
0 00:00:00:00:00:01 10.10.10.11 host-2 00:00:00:00:00:01