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