summaryrefslogtreecommitdiff
path: root/contrib
diff options
context:
space:
mode:
authorShawn Routhier <sar@isc.org>2016-07-26 11:22:23 -0700
committerShawn Routhier <sar@isc.org>2016-07-26 11:22:23 -0700
commit841d9a18add5ade4a6f04bc6f05ea2e632d58d97 (patch)
treef33e85dd8bc72a29b3eb47f926ae4df10bc8f88a /contrib
parent03f36ddf99567c364da98e65a885b8751f4eda2f (diff)
downloadisc-dhcp-841d9a18add5ade4a6f04bc6f05ea2e632d58d97.tar.gz
[master] Enhance the script to show leases from the lease file
Squashed commit of the following: commit ed86441135b2dd001c1175abeaddc7fbfa5b3feb Author: Shawn Routhier <sar@isc.org> Date: Tue Jul 19 20:31:42 2016 -0700 [rt42113] Enhance the script to look at leases
Diffstat (limited to 'contrib')
-rw-r--r--contrib/dhcp-lease-list.pl40
1 files changed, 28 insertions, 12 deletions
diff --git a/contrib/dhcp-lease-list.pl b/contrib/dhcp-lease-list.pl
index 3e6fd729..aa6372df 100644
--- a/contrib/dhcp-lease-list.pl
+++ b/contrib/dhcp-lease-list.pl
@@ -17,12 +17,15 @@
# optional, sar
#
# 2016-01-19 - updated to better trim the manu string and output the hostnames, sar
+#
+# 2016-01-18 - Mainly cosmetics. Eliminated spurious output in "parsable" mode.
+# Provided for the various conventional lease file locations. (cbp)
use strict;
use warnings;
use POSIX qw(strftime);
-my $LEASES = '/var/db/dhcpd.leases';
+my @LEASES = ('/var/db/dhcpd.leases', '/var/lib/dhcp/dhcpd.leases', '/var/lib/dhcp3/dhcpd.leases');
my @all_leases;
my @leases;
@@ -69,7 +72,16 @@ sub check_oui_file() {
## Read current leases file into array.
sub read_dhcpd_leases() {
- open(F, $LEASES) or die("Cannot open $LEASES: $!");
+ my $db;
+ for my $db_cand (@LEASES) {
+ if ( -r $db_cand) {
+ $db = $db_cand;
+ last;
+ }
+ }
+ die("Cannot find leases db") unless defined $db;
+ open(F, $db) or die("Cannot open $db: $!");
+ print("Reading leases from $db\n") if $opt_format eq 'human';
my $content = join('', <F>);
close(F);
@all_leases = split(/lease/, $content);
@@ -86,7 +98,7 @@ sub process_leases() {
my $gm_now = strftime("%Y/%m/%d %H:%M:%S", gmtime());
my %tmp_leases; # for sorting and filtering
- my $counter = 1;
+ my $counter = $opt_format eq 'human' ? 1 : 0;
# parse entries
foreach my $lease (@all_leases) {
@@ -95,14 +107,16 @@ sub process_leases() {
# skip outdated lines
next if ($opt_keep eq 'active' and $3 lt $gm_now);
- my $percent = (($counter / $total_leases)*100);
- printf "Processing: %2d%% complete\r", $percent;
- ++$counter;
+ if ($counter) {
+ my $percent = (($counter / $total_leases)*100);
+ printf "Processing: %2d%% complete\r", $percent;
+ ++$counter;
+ }
- my $hostname = "-NA-";
- if ($6) {
- $hostname = $6;
- }
+ my $hostname = "-NA-";
+ if ($6) {
+ $hostname = $6;
+ }
my $mac = $4;
my $date_end = $3;
@@ -175,7 +189,9 @@ sub cli_processing() {
" --last prints the last (even if end<now) entry for every MAC\n".
" --all prints all entries i.e. more than one per MAC\n".
" --lease uses the next argument as the name of the lease file\n".
- " the default is /var/db/dhcpd.leases\n".
+ " the default is to try /var/db/dhcpd.leases then\n".
+ " /var/lib/dhcp/dhcpd.leases then\n".
+ " /var/lib/dhcp3/dhcpd.leases\n".
"\n");
exit(0);
} elsif ($arg eq '--parsable') {
@@ -185,7 +201,7 @@ sub cli_processing() {
} elsif ($arg eq '--all') {
$opt_keep = 'all';
} elsif ($arg eq '--lease') {
- $LEASES = shift(@ARGV);
+ unshift @LEASES, shift(@ARGV);
} else {
die("Unknown option $arg");
}