Skip to content
Open
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
55 changes: 33 additions & 22 deletions dhcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package main

import (
"fmt"
"net"
"time"

dhcp "github.com/insomniacslk/dhcp/dhcpv4"
Expand All @@ -12,52 +13,62 @@ import (

var (
dhcpRequests = promauto.NewCounter(prometheus.CounterOpts{
Name: "observer_dhcp_requests",
Help: "Total number of sent DHCP requests",
Name: "observer_dhcp_requests",
Help: "Total number of sent DHCPv4 requests",
})
dhcpOffers = promauto.NewCounter(prometheus.CounterOpts{
Name: "observer_dhcp_offers",
Help: "Total number of received DHCP offers",
Name: "observer_dhcp_offers",
Help: "Total number of received DHCPv4 offers",
})
dhcpFailures = promauto.NewCounter(prometheus.CounterOpts{
Name: "observer_dhcp_failures",
Help: "Total number of failed DHCP handshakes",
Help: "Total number of failed DHCPv4 handshakes",
})
dchpHandshakeLatency = prometheus.NewGauge(prometheus.GaugeOpts{
dhcpHandshakeLatency = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "observer_dhcp_latency",
Help: "Time between dhcp request and dhcp offer.",
Help: "Time between DHCPv4 request and offer.",
})
dchpLeaseTime = prometheus.NewGauge(prometheus.GaugeOpts{
dhcpLeaseTime = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "observer_dhcp_lease_time",
Help: "Time until DHCP lease will be renewed.",
Help: "Time until DHCPv4 lease will be renewed.",
})
)

func init() {
prometheus.MustRegister(dchpHandshakeLatency)
prometheus.MustRegister(dchpLeaseTime)
prometheus.MustRegister(dhcpHandshakeLatency)
prometheus.MustRegister(dhcpLeaseTime)
}

func sampleDhcp(iface string, verbose bool) {
func sampleDhcp(iface string, verbose bool) (error, net.IP, int) {
dhcpRequests.Inc()
client := client.NewClient()
start := time.Now()
conversation, err := client.Exchange(iface)
if err != nil {
dhcpFailures.Inc()
fmt.Printf("DHCP Failed with %s.\n", err)
return
return err, nil, 0
}
dchpHandshakeLatency.Set(time.Since(start).Seconds())
dhcpHandshakeLatency.Set(time.Since(start).Seconds())

var yourIPAddr net.IP
var prefixBits int
for _, packet := range conversation {
if packet.MessageType() == dhcp.MessageTypeOffer {
dhcpOffers.Inc()
}
if packet.Options.Has(dhcp.OptionIPAddressLeaseTime) {
dchpLeaseTime.Set(packet.IPAddressLeaseTime(0).Seconds())
}
if verbose {
fmt.Println(packet.Summary())
}

if packet.MessageType() == dhcp.MessageTypeOffer {
dhcpOffers.Inc()
}

if packet.MessageType() == dhcp.MessageTypeAck {
if packet.Options.Has(dhcp.OptionIPAddressLeaseTime) {
dhcpLeaseTime.Set(packet.IPAddressLeaseTime(0).Seconds())
}

prefixBits, _ = packet.SubnetMask().Size()
yourIPAddr = packet.YourIPAddr
}
}
}
return nil, yourIPAddr, prefixBits
}
77 changes: 77 additions & 0 deletions dhcp6.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package main

import (
"fmt"
"net"
"time"

dhcp "github.com/insomniacslk/dhcp/dhcpv6"
client "github.com/insomniacslk/dhcp/dhcpv6/client6"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/promauto"
)

var (
dhcp6Requests = promauto.NewCounter(prometheus.CounterOpts{
Name: "observer_dhcp6_requests",
Help: "Total number of sent DHCPv6 requests",
})
dhcp6Replies = promauto.NewCounter(prometheus.CounterOpts{
Name: "observer_dhcp6_offers",
Help: "Total number of received DHCPv6 replies(offers)",
})
dhcp6Failures = promauto.NewCounter(prometheus.CounterOpts{
Name: "observer_dhcp6_failures",
Help: "Total number of failed DHCPv6 handshakes",
})
dhcp6HandshakeLatency = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "observer_dhcp6_latency",
Help: "Time between DHCPv6 request and offer.",
})
dhcp6Lifetime = prometheus.NewGauge(prometheus.GaugeOpts{
Name: "observer_dhcp6_lifetime",
Help: "Duration that DHCPv6 obtained address is valid.",
})
)

func init() {
prometheus.MustRegister(dhcp6HandshakeLatency)
prometheus.MustRegister(dhcp6Lifetime)
}

func sampleDhcp6(iface string, verbose bool) (error, net.IP, int) {
dhcp6Requests.Inc()
client := client.NewClient()
start := time.Now()
conversation, err := client.Exchange(iface)
if err != nil {
dhcp6Failures.Inc()
return err, nil, 0
}
dhcp6HandshakeLatency.Set(time.Since(start).Seconds())

var yourIPAddr net.IP
var prefixBits int = 128
for _, packet := range conversation {
if verbose {
fmt.Println(packet.Summary())
}

message, err := packet.GetInnerMessage()
if err != nil {
dhcp6Failures.Inc()
return err, nil, 0
}

if message.MessageType == dhcp.MessageTypeReply {
dhcp6Replies.Inc()

iaNa := message.Options.OneIANA()
naAddr := *iaNa.Options.OneAddress()

dhcp6Lifetime.Set(naAddr.ValidLifetime.Seconds())
yourIPAddr = naAddr.IPv6Addr
}
}
return nil, yourIPAddr, prefixBits
}
76 changes: 0 additions & 76 deletions dns.go

This file was deleted.

40 changes: 20 additions & 20 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,29 +1,29 @@
module github.com/dhtech/observer

go 1.19
go 1.23.0

toolchain go1.24.4

require (
github.com/go-ping/ping v1.1.0
github.com/insomniacslk/dhcp v0.0.0-20221001123530-5308ebe5334c
github.com/miekg/dns v1.1.50
github.com/prometheus/client_golang v1.14.0
github.com/insomniacslk/dhcp v0.0.0-20250417080101-5f8cf70e8c5f
github.com/prometheus-community/pro-bing v0.7.0
github.com/prometheus/client_golang v1.22.0
github.com/vishvananda/netlink v1.3.1
)

require (
github.com/beorn7/perks v1.0.1 // indirect
github.com/cespare/xxhash/v2 v2.1.2 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.2.0 // indirect
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
github.com/prometheus/client_model v0.3.0 // indirect
github.com/prometheus/common v0.37.0 // indirect
github.com/prometheus/procfs v0.8.0 // indirect
github.com/u-root/uio v0.0.0-20210528114334-82958018845c // indirect
golang.org/x/mod v0.4.2 // indirect
golang.org/x/net v0.0.0-20220225172249-27dd8689420f // indirect
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/tools v0.1.6-0.20210726203631-07bc1bf47fb2 // indirect
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 // indirect
google.golang.org/protobuf v1.28.1 // indirect
github.com/cespare/xxhash/v2 v2.3.0 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/munnerz/goautoneg v0.0.0-20191010083416-a7dc8b61c822 // indirect
github.com/pierrec/lz4/v4 v4.1.22 // indirect
github.com/prometheus/client_model v0.6.2 // indirect
github.com/prometheus/common v0.65.0 // indirect
github.com/prometheus/procfs v0.16.1 // indirect
github.com/u-root/uio v0.0.0-20240224005618-d2acac8f3701 // indirect
github.com/vishvananda/netns v0.0.5 // indirect
golang.org/x/net v0.41.0 // indirect
golang.org/x/sync v0.15.0 // indirect
golang.org/x/sys v0.33.0 // indirect
google.golang.org/protobuf v1.36.6 // indirect
)
Loading