#!/usr/bin/perl

use Net::DNS;

$mydnsserver = "ns2.colobox.com";
@goodservers = qw/ns2.colobox.com ns01.backupdns.com/;
$dir = "/var/named/rf";

$res = Net::DNS::Resolver->new;
$res->tcp_timeout(2);
$res->udp_timeout(2);
opendir(DIR, $dir);
@files = sort grep { !/^\./ && -f "$dir/$_" } readdir(DIR);
closedir(DIR);
foreach $zone (@files) {
  $zone =~ s/^db\.//;
  $zone =~ /\.(\w+)/; $tld = $1;
  @tldservers = ();
  $res->nameservers($mydnsserver);
  $query = $res->query($tld . ".", "NS");
  next unless $query;
  foreach $rr ($query->answer) {
    next unless $rr->type eq "NS";
    push(@tldservers, $rr->nsdname);
  }
  $found = 0;
  $total = 0;
  foreach $tldserver (@tldservers) {
    $total++;
    $i = 0;
    $res->nameservers($tldserver);
    $query = $res->query("$zone.", "NS");
    next unless $query;
    foreach $rr ($query->answer) {
      next unless $rr->type eq "NS";
      foreach $goodserver (@goodservers) {
        $i++ if lc($rr->nsdname) eq $goodserver;
      }
    }
    $found++ if $i == scalar @goodservers;
  }
  printf("%25s %02d/%02d\n", $zone, $found, $total);
}
