#! /usr/bin/perl

#
# Author: Kevin C. Miller <kevinm@andrew.cmu.edu>
# http://www.andrew.cmu.edu/~kevinm/dhcp/failover.html
#
# The copyright is with the author. We (SuSE) include this script
# with his permission.
#

use strict;

my %LeaseBS;
my %LeaseNBS;
my %Update;
my $level = 1;
my $IP = '';
my ($ip, $bs, $nbs, $join);
open(FILE, "/var/lib/dhcp/dhcpd.leases");
while(my $line = <FILE>) {
  next if ($line =~ /^\#/);
  if ($level == 1) {
    if ($line =~ /lease ([\d\.]+) \{/) {
      $level++;
      $IP = $1;
      $LeaseBS{$IP} = '';
      $LeaseNBS{$IP} = '';
      $Update{$IP} = 0;
    }
  }elsif($level == 2) {
    if ($line =~ /next binding state ([^\;]+)/) {
      $LeaseNBS{$IP} = $1;
    }elsif($line =~ /binding state ([^\;]+)/) {
      $LeaseBS{$IP} = $1;
    }elsif($line =~ /f_lupdate/) {
      $Update{$IP} = 1;
    }elsif($line =~ /\}/) {
      $level--;
#      print "$IP: $Leases{$IP}->[0] $Leases{$IP}->[1]\n";
      $IP = '';
    }
  }
}
close(FILE);

format IP =
@<<<<<<<<<<<<<<<<<< @<<<<<<<<< @ @<<<<<<<<<
$ip, $bs, $join, $nbs
.

$~ = "IP";
print 
"IP Address        Binding      Next Binding\n";
foreach $ip (sort keys %LeaseBS) {
  $bs = $LeaseBS{$ip};
  $nbs = $LeaseNBS{$ip};
  $join = '>'; 
  $join = '*' if ($Update{$ip});
  write(STDOUT);
}