+ dhcp: /dhcp/status: return static leases

This commit is contained in:
Simon Zolin 2019-05-14 13:54:35 +03:00
parent 342699d933
commit 763b986955
3 changed files with 26 additions and 3 deletions

View File

@ -42,9 +42,11 @@ func convertLeases(inputLeases []dhcpd.Lease, includeExpires bool) []map[string]
func handleDHCPStatus(w http.ResponseWriter, r *http.Request) {
log.Tracef("%s %v", r.Method, r.URL)
leases := convertLeases(dhcpServer.Leases(), true)
staticLeases := convertLeases(dhcpServer.StaticLeases(), false)
status := map[string]interface{}{
"config": config.DHCP,
"leases": leases,
"config": config.DHCP,
"leases": leases,
"static_leases": staticLeases,
}
w.Header().Set("Content-Type", "application/json")

View File

@ -55,7 +55,9 @@ func (s *Server) dbLoad() {
numLeases := len(obj)
for i := range obj {
if !ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) {
if obj[i].Expiry != leaseExpireStatic &&
!ipInRange(s.leaseStart, s.leaseStop, obj[i].IP) {
log.Tracef("Skipping a lease with IP %s: not within current IP range", obj[i].IP)
continue
}

View File

@ -606,6 +606,25 @@ func (s *Server) Leases() []Lease {
return result
}
// StaticLeases returns the list of statically-configured DHCP leases (thread-safe)
func (s *Server) StaticLeases() []Lease {
s.Lock()
if s.IPpool == nil {
s.dbLoad()
}
s.Unlock()
var result []Lease
s.RLock()
for _, lease := range s.leases {
if lease.Expiry.Unix() == 1 {
result = append(result, *lease)
}
}
s.RUnlock()
return result
}
// Print information about the current leases
func (s *Server) printLeases() {
log.Tracef("Leases:")