bin/zone-ls
changeset 88 0e1e5027e9c0
parent 85 c47953192c5c
child 95 cb92affcf59f
equal deleted inserted replaced
52:53c95f2ff0ac 88:0e1e5027e9c0
       
     1 #! /usr/bin/perl
       
     2 
       
     3 use v5.10;
       
     4 use strict;
       
     5 use warnings;
       
     6 use Pod::Usage;
       
     7 use File::Basename;
       
     8 use Time::Local;
       
     9 use Getopt::Long;
       
    10 use if $ENV{DEBUG} => "Smart::Comments";
       
    11 use DNStools::Config qw(get_config);
       
    12 
       
    13 my %config;
       
    14 my $opt_expiry = undef;
       
    15 
       
    16 MAIN: {
       
    17     my %info;    # will hold the information we collected
       
    18 
       
    19     GetOptions(
       
    20         "e|expiry" => \$opt_expiry,
       
    21         "h|help"   => sub { pod2usage(-exit => 0, -verbose => 1) },
       
    22         "m|man"    => sub {
       
    23             pod2usage(
       
    24                 -exit      => 0,
       
    25                 -verbose   => 2,
       
    26                 -noperldoc => system("perldoc -V &>/dev/null")
       
    27             );
       
    28         },
       
    29     ) or pod2usage;
       
    30 
       
    31     %config = get_config();
       
    32     die "$config{master_dir}: $!\n" if not -d $config{master_dir};
       
    33 
       
    34     foreach my $dir (grep { -d } glob "$config{master_dir}/*") {
       
    35 
       
    36         my $zone = basename($dir);
       
    37         $info{$zone} = { status => "OK" };
       
    38 
       
    39         if (not -f "$dir/.index.zsk") {
       
    40             $info{$zone}{zsk}    = 0;
       
    41             $info{$zone}{ksk}    = 0;
       
    42             $info{$zone}{kc}     = 0;
       
    43             $info{$zone}{end}    = "-";
       
    44             $info{$zone}{expiry} = undef;
       
    45             next;
       
    46         }
       
    47 
       
    48         # prueft wie viele zsks genutzt werden
       
    49         {
       
    50             open(my ($fh), $_ = "<$dir/.index.zsk")
       
    51               or die "Can't open $_: $!\n";
       
    52             () = <$fh>;
       
    53             $info{$zone}{zsk} = $.
       
    54         }
       
    55 
       
    56         # prueft wie viele ksks genutzt werden
       
    57         {
       
    58             open(my ($fh), $_ = "<$dir/.index.ksk")
       
    59               or die "Can't open $_: $!\n";
       
    60             () = <$fh>;
       
    61             $info{$zone}{ksk} = $.
       
    62         }
       
    63 
       
    64         # prueft wie oft die schluessel zum signieren genutzt wurden
       
    65         {
       
    66             open(my ($fh), $_ = "<$dir/.keycounter")
       
    67               or die "Can't open $_: $!\n";
       
    68             chomp($info{$zone}{kc} = <$fh>);
       
    69         }
       
    70 
       
    71         # prueft das ablaufdatum
       
    72         if (!-f "$dir/$zone.signed") {
       
    73             $info{$zone}{end} = "-";
       
    74             next;
       
    75         }
       
    76 
       
    77         open(my ($fh), $_ = "<$dir/$zone.signed") or die "Can't open $_: $!\n";
       
    78         while (<$fh>) {
       
    79             next if not /RSIG\s+SOA\s.*\s
       
    80 				(?<year>\d\d\d\d)
       
    81 				(?<mon>\d\d)
       
    82 				(?<day>\d\d)
       
    83 				(?<hour>\d\d)
       
    84 				(?<min>\d\d)\d+\s\(/ix;
       
    85             $info{$zone}{end} = "$+{day}.$+{mon}.$+{year} $+{hour}:$+{min}";
       
    86             $info{$zone}{expiry} =
       
    87               timelocal(0, $+{min}, $+{hour}, $+{day}, $+{mon} - 1, $+{year});
       
    88         }
       
    89     }
       
    90 
       
    91     {    # output
       
    92 
       
    93         my $sort_by =
       
    94           $opt_expiry
       
    95           ? sub { ($info{$a}{expiry} // 2**64) <=> ($info{$b}{expiry} // 2**64) }
       
    96           : sub { $a cmp $b };
       
    97 
       
    98         my $format_h = "%-35s %-8s %1s/%1s %3s %7s\n";
       
    99         my $format_l = "%-35s %-8s %1d/%1d %5d %19s\n";
       
   100 
       
   101         printf $format_h => qw(Domain Status ZSK KSK Used Sig-end);
       
   102 
       
   103         foreach my $zone (sort $sort_by keys %info) {
       
   104             printf $format_l => $zone,
       
   105               @{ $info{$zone} }{qw(status zsk ksk kc end)};
       
   106         }
       
   107     }
       
   108 }
       
   109 
       
   110 __END__
       
   111 
       
   112 =head1 NAME
       
   113 
       
   114  zone-ls -- lists all zones
       
   115 
       
   116 =head1 SYNOPSIS
       
   117 
       
   118  zone-ls [-e|--expiry]
       
   119 
       
   120 =head1 DESCRIPTION
       
   121 
       
   122 This B<zone-ls> lists all zones under control of our dnstools suite. The output is ordered by domain name.
       
   123 
       
   124 =head1 OPTIONS
       
   125 
       
   126 =over
       
   127 
       
   128 =item B<-e>|B<--expiry>
       
   129 
       
   130 Order the output by expiry date. The sooner the key expires, the more top the
       
   131 domain is listed.
       
   132 
       
   133 =back
       
   134 
       
   135 Additionally the common B<-h>|B<--help>|B<-m>|B<--man> options, which should be
       
   136 self explanatory.
       
   137 
       
   138 =head1 AUTHORS
       
   139 
       
   140 L<andre.suess@pipkin.cc>
       
   141 
       
   142 =cut
       
   143 
       
   144 # vim:ts=4 sw=4 ai si aw: