|         |      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 %cf; | 
|         |     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                 # "system('perldoc -V &>/dev/null')" appears shorter, but may not | 
|         |     27                 # do what you expect ( it still returns 0 on debian squeeze with | 
|         |     28                 # dash as system shell even if cannot find the command in $PATH) | 
|         |     29                 -noperldoc => system('perldoc -V >/dev/null 2>&1') | 
|         |     30             ); | 
|         |     31         }, | 
|         |     32     ) or pod2usage; | 
|         |     33  | 
|         |     34     %cf = get_config(); | 
|         |     35     die "$cf{master_dir}: $!\n" if not -d $cf{master_dir}; | 
|         |     36  | 
|         |     37     foreach my $dir (grep { -d } glob "$cf{master_dir}/*") { | 
|         |     38  | 
|         |     39         my $zone = basename($dir); | 
|         |     40         $info{$zone} = { status => "OK" }; | 
|         |     41  | 
|         |     42         if (not -f "$dir/.index.zsk") { | 
|         |     43             $info{$zone}{zsk}    = 0; | 
|         |     44             $info{$zone}{ksk}    = 0; | 
|         |     45             $info{$zone}{kc}     = 0; | 
|         |     46             $info{$zone}{end}    = "-"; | 
|         |     47             $info{$zone}{expiry} = undef; | 
|         |     48             next; | 
|         |     49         } | 
|         |     50  | 
|         |     51         # prueft wie viele zsks genutzt werden | 
|         |     52         { | 
|         |     53             open(my ($fh), $_ = "<$dir/.index.zsk") | 
|         |     54               or die "Can't open $_: $!\n"; | 
|         |     55             () = <$fh>; | 
|         |     56             $info{$zone}{zsk} = $. | 
|         |     57         } | 
|         |     58  | 
|         |     59         # prueft wie viele ksks genutzt werden | 
|         |     60         { | 
|         |     61             open(my ($fh), $_ = "<$dir/.index.ksk") | 
|         |     62               or die "Can't open $_: $!\n"; | 
|         |     63             () = <$fh>; | 
|         |     64             $info{$zone}{ksk} = $. | 
|         |     65         } | 
|         |     66  | 
|         |     67         # prueft wie oft die schluessel zum signieren genutzt wurden | 
|         |     68         { | 
|         |     69             open(my ($fh), $_ = "<$dir/.keycounter") | 
|         |     70               or die "Can't open $_: $!\n"; | 
|         |     71             chomp($info{$zone}{kc} = <$fh>); | 
|         |     72         } | 
|         |     73  | 
|         |     74         # prueft das ablaufdatum | 
|         |     75         if (!-f "$dir/$zone.signed") { | 
|         |     76             $info{$zone}{end} = "-"; | 
|         |     77             next; | 
|         |     78         } | 
|         |     79  | 
|         |     80         open(my ($fh), $_ = "<$dir/$zone.signed") or die "Can't open $_: $!\n"; | 
|         |     81         while (<$fh>) { | 
|         |     82             next if not /RSIG\s+SOA\s.*\s | 
|         |     83 				(?<year>\d\d\d\d) | 
|         |     84 				(?<mon>\d\d) | 
|         |     85 				(?<day>\d\d) | 
|         |     86 				(?<hour>\d\d) | 
|         |     87 				(?<min>\d\d)\d+\s\(/ix; | 
|         |     88             $info{$zone}{end} = "$+{day}.$+{mon}.$+{year} $+{hour}:$+{min}"; | 
|         |     89             $info{$zone}{expiry} = | 
|         |     90               timelocal(0, $+{min}, $+{hour}, $+{day}, $+{mon} - 1, $+{year}); | 
|         |     91         } | 
|         |     92     } | 
|         |     93  | 
|         |     94     {    # output | 
|         |     95  | 
|         |     96         my $sort_by = | 
|         |     97           $opt_expiry | 
|         |     98           ? sub { ($info{$a}{expiry} // 2**64) <=> ($info{$b}{expiry} // 2**64) } | 
|         |     99           : sub { $a cmp $b }; | 
|         |    100  | 
|         |    101         my $format_h = "%-35s %-8s %1s/%1s %3s %7s\n"; | 
|         |    102         my $format_l = "%-35s %-8s %1d/%1d %5d %19s\n"; | 
|         |    103  | 
|         |    104         printf $format_h => qw(Domain Status ZSK KSK Used Sig-end); | 
|         |    105  | 
|         |    106         foreach my $zone (sort $sort_by keys %info) { | 
|         |    107             printf $format_l => $zone, | 
|         |    108               @{ $info{$zone} }{qw(status zsk ksk kc end)}; | 
|         |    109         } | 
|         |    110     } | 
|         |    111 } | 
|         |    112  | 
|         |    113 __END__ | 
|         |    114  | 
|         |    115 =head1 NAME | 
|         |    116  | 
|         |    117  zone-ls -- lists all zones | 
|         |    118  | 
|         |    119 =head1 SYNOPSIS | 
|         |    120  | 
|         |    121  zone-ls [-e|--expiry] | 
|         |    122  | 
|         |    123 =head1 DESCRIPTION | 
|         |    124  | 
|         |    125 This B<zone-ls> lists all zones under control of our dnstools suite. The output is ordered by domain name. | 
|         |    126  | 
|         |    127 =head1 OPTIONS | 
|         |    128  | 
|         |    129 =over | 
|         |    130  | 
|         |    131 =item B<-e>|B<--expiry> | 
|         |    132  | 
|         |    133 Order the output by expiry date. The sooner the key expires, the more top the | 
|         |    134 domain is listed. | 
|         |    135  | 
|         |    136 =back | 
|         |    137  | 
|         |    138 Additionally the common B<-h>|B<--help>|B<-m>|B<--man> options, which should be | 
|         |    139 self explanatory. | 
|         |    140  | 
|         |    141 =head1 AUTHORS | 
|         |    142  | 
|         |    143 L<andre.suess@pipkin.cc> | 
|         |    144  | 
|         |    145 =cut | 
|         |    146  | 
|         |    147 # vim:ts=4 sw=4 ai si aw: |