-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathdhcp6.go
More file actions
76 lines (66 loc) · 1.9 KB
/
dhcp6.go
File metadata and controls
76 lines (66 loc) · 1.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
package main
import (
"fmt"
"net"
"time"
"github.com/insomniacslk/dhcp/dhcpv6"
"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(client client6.Client, verbose bool) (net.IP, int, error) {
dhcp6Requests.Inc()
start := time.Now()
conversation, err := client.Exchange(iface)
if err != nil {
dhcp6Failures.Inc()
return nil, 0, err
}
dhcp6HandshakeLatency.Set(time.Since(start).Seconds())
var yourIPAddr net.IP
var prefixBits = 128
for _, packet := range conversation {
if verbose {
fmt.Println(packet.Summary())
}
message, err := packet.GetInnerMessage()
if err != nil {
dhcp6Failures.Inc()
return nil, 0, err
}
if message.MessageType == dhcpv6.MessageTypeReply {
dhcp6Replies.Inc()
iaNa := message.Options.OneIANA()
naAddr := *iaNa.Options.OneAddress()
dhcp6Lifetime.Set(naAddr.ValidLifetime.Seconds())
yourIPAddr = naAddr.IPv6Addr
}
}
return yourIPAddr, prefixBits, nil
}