Changeset 368 for branches


Ignore:
Timestamp:
11/26/07 17:54:21 (17 years ago)
Author:
Kris Deugau
Message:

/branches/stable

Fix rWHOIS export. Again. (Previous changes caused the net-data file
to grow without limit by appending the data instead of always
overwriting the file. Ooops.)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/stable/cgi-bin/extras/db2rwhois.pl

    r367 r368  
    5050closedir RWHOISROOT;
    5151
     52# prefetch alloctype data
     53my $sth = $dbh->prepare("select type,def_custid,arin_netname from alloctypes");
     54$sth->execute;
     55while (my @data = $sth->fetchrow_array) {
     56  $netnameprefix{$data[0]} = $data[2];
     57}
     58
     59# Get the list of masters to export
     60my $msth = $dbh->prepare("select cidr,ctime,mtime from masterblocks where rwhois='y'");
     61$msth->execute;
     62
     63# Prepare to select subblocks for each master
     64# Make sure to remove the private netblocks from this,
     65# no use or point in broadcasting our use of them.
     66# Also remove the details of our "reserved CORE/WAN" blocks;  they're not critical.
     67my $ssth = $dbh->prepare("select cidr,custid,type,city,description,createstamp,modifystamp,swip ".
     68        "from allocations where ".
     69        "not (cidr <<= '192.168.0.0/16') and ".
     70        "not (cidr <<= '172.16.0.0/12') and ".
     71        "not (cidr <<= '10.0.0.0/8') and ".
     72        "not (type = 'wr') and ".
     73        "masklen(cidr) <=30 and ".
     74        "cidr <<= ?");
     75
     76# Customer data, for those rare blocks we really need to delegate.
     77my $custsth = $dbh->prepare("select name,street,city,province,country,pocode,phone,tech_handle,special ".
     78        "from customers where custid=?");
     79
    5280# Fill in data about our master blocks as allocated from ARIN
    5381# We open separate files for each of these as appropriate.
    5482# Changes in master blocks are treated as complete new masters - since we're exporting
    5583# all data every time, this isn't so terrible as it might seem.
    56 my $sth = $dbh->prepare("select cidr,ctime,mtime from masterblocks where rwhois='y'");
    57 $sth->execute;
    5884my $i=0;
    59 GETMASTERS: while (my @data = $sth->fetchrow_array()) {
     85while (my @data = $msth->fetchrow_array()) {
    6086
    6187  $masterblocks[$i] = new NetAddr::IP $data[0];
     
    153179);
    154180
    155 # Whew!  Ugly little varmint.
     181  # Recreate the net-nnn.nnn.nnn.nnn-nn.txt data file
    156182  my $masterfilename = "$rwnet/data/network/$rwnet.txt";
    157183
     
    174200        "Updated-By: noc\@example.com\n";
    175201
    176   close MASTERFILE;
    177   $i++;
    178 }
    179 
    180 # prefetch alloctype data
    181 $sth = $dbh->prepare("select type,def_custid,arin_netname from alloctypes");
    182 $sth->execute;
    183 while (my @data = $sth->fetchrow_array) {
    184   $netnameprefix{$data[0]} = $data[2];
    185 }
    186 
    187 # Now read out the data in the "main" delegation list, and check it
    188 # with the master blocks.  We need to do this to decide which rWHOIS
    189 # "net-xxx.xxx.xxx.xxx-mask" tree the data belongs in.
    190 
    191 # Make sure to remove the private netblocks from this.
    192 # No use or point in broadcasting our use of them.
    193 # Also remove the details of our "reserved CORE/WAN" blocks;  they're not critical.
    194 $sth = $dbh->prepare("select cidr,custid,type,city,description,createstamp,modifystamp,swip ".
    195         "from allocations where ".
    196         "not (cidr <<= '192.168.0.0/16') and ".
    197         "not (cidr <<= '172.16.0.0/12') and ".
    198         "not (cidr <<= '10.0.0.0/8') and ".
    199         "not (type = 'wr') and ".
    200         "masklen(cidr) <=30");
    201 #" and (custid='6750400' or custid like '%-RES' or custid like '%-BUS')");
    202 $sth->execute;
    203 
    204 my $custsth = $dbh->prepare("select name,street,city,province,country,pocode,phone,tech_handle,special from customers where custid=?");
    205 
    206 $i=0;
    207 while (my ($cidr, $custid, $type, $city, $desc, $ctime, $mtime, $swip) = $sth->fetchrow_array) {
     202  # And now the subblocks
     203  $ssth->execute("$masterblocks[$i]");
     204  while (my ($cidr, $custid, $type, $city, $desc, $ctime, $mtime, $swip) = $ssth->fetchrow_array) {
    208205
    209206# We get master block info from @masterblocks.
     
    220217 # Updated-By: noc@example.com
    221218
    222   # Get the "full" network number
    223   my $net = new NetAddr::IP $cidr;
     219    # Get the "full" network number
     220    my $net = new NetAddr::IP $cidr;
    224221
    225222# Assumptions:  All data in ipdb is public
    226223# If not, we need another field to indicate "public/private".
    227224
    228   foreach my $master (@masterblocks) {
    229     if ($master->contains($net)) {
    230 
    231 # Whew!  Ugly little varmint.
    232       my $masterfilename = "net-".$master->addr."-".$master->masklen.
    233         "/data/network/".$master->addr."-".$master->masklen.".txt";
    234 
    235       open MASTERFILE,">>$rwhoisDataPath/$masterfilename"
    236         or print "File open error: '$!' on '$rwhoisDataPath/$masterfilename'\n";
    237 
    238225# cidr custid type city description notes maskbits
    239226
     
    241228if ($desc =~ /^\s*$/) { $desc = 'Friendly ISP'; }
    242229
    243       # Fix up datestamps.  We don't *really* need sub-microsecond resolution on our exports...
    244       ($ctime) = ($ctime =~ /^(\d+-\d+-\d+)\s+/);
    245       ($mtime) = ($mtime =~ /^(\d+-\d+-\d+)\s+/);
     230    # Fix up datestamps.  We don't *really* need sub-microsecond resolution on our exports...
     231    ($ctime) = ($ctime =~ /^(\d+-\d+-\d+)\s+/);
     232    ($mtime) = ($mtime =~ /^(\d+-\d+-\d+)\s+/);
    246233
    247234# Notes:
     
    262249#  network:Class-Name:network                   [Provided by rWHOIS protocol]
    263250
    264       my $netname = $netnameprefix{$type};
    265 
    266       if ($swip eq 'n') {
    267         print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
    268                 "Auth-Area: $master\n".
    269                 "Network-Name: $netname-".$net->network."\n".
    270                 "IP-Network: $net\n".
    271                 "IP-Network-Block: ".$net->range."\n".
    272                 "Org-Name: Friendly ISP\n".
    273                 "Street-Address: 123 4th Street\n".
    274                 "City: Anytown\n".
    275                 "StateProv: Ontario\n".
    276                 "Postal-Code: H0H 0H0\n".
    277                 "Country-Code: CA\n".
    278                 "Tech-Contact: ISP-ARIN-HANDLE\n".
    279                 "Created: $ctime\n".
    280                 "Updated: $mtime\n".
    281                 "Updated-By: noc\@example.com\n";
     251    my $netname = $netnameprefix{$type};
     252
     253    if ($swip eq 'n') {
     254      print MASTERFILE "---\nID: NETBLK-ISP.$masterblocks[$i]\n".
     255        "Auth-Area: $masterblocks[$i]\n".
     256        "Network-Name: $netname-".$net->network."\n".
     257        "IP-Network: $net\n".
     258        "IP-Network-Block: ".$net->range."\n".
     259        "Org-Name: Friendly ISP\n".
     260        "Street-Address: 123 4th Street\n".
     261        "City: Anytown\n".
     262        "StateProv: Ontario\n".
     263        "Postal-Code: H0H 0H0\n".
     264        "Country-Code: CA\n".
     265        "Tech-Contact: ISP-ARIN-HANDLE\n".
     266        "Created: $ctime\n".
     267        "Updated: $mtime\n".
     268        "Updated-By: noc\@example.com\n";
     269    } else {
     270      $custsth->execute($custid);
     271      my ($name, $street, $city, $prov, $country, $pocode, $phone, $tech, $special) = $custsth->fetchrow_array;
     272      $custsth->finish;
     273      if ($special && $special =~ /NetName/ && $special =~ /$cidr/) {
     274        ($netname) = ($special =~ /NetName$cidr: ([A-Z0-9_-]+)/);
    282275      } else {
    283         $custsth->execute($custid);
    284         my ($name, $street, $city, $prov, $country, $pocode, $phone, $tech, $special) = $custsth->fetchrow_array;
    285         $custsth->finish;
    286         if ($special && $special =~ /NetName/ && $special =~ /$cidr/) {
    287           ($netname) = ($special =~ /NetName$cidr: ([A-Z0-9_-]+)/);
    288         } else {
    289           $netname .= "-".$net->network;
    290         }
    291         print MASTERFILE "---\nID: NETBLK-ISP.$master\n".
    292                 "Auth-Area: $master\n".
    293                 "Network-Name: $netname\n".
    294                 "IP-Network: $net\n".
    295                 "IP-Network-Block: ".$net->range."\n".
    296                 "Org-Name: $name\n".
    297                 "Street-Address: $street\n".
    298                 "City: $city\n".
    299                 "StateProv: $prov\n".
    300                 "Postal-Code: $pocode\n".
    301                 "Country-Code: $country\n".
    302                 "Tech-Contact: $tech\n".
    303                 "Created: $ctime\n".
    304                 "Updated: $mtime\n".
    305                 "Updated-By: noc\@example.com\n";
    306       } # swip
    307 
    308     } # net in master
    309   } # foreach master
     276        $netname .= "-".$net->network;
     277      }
     278      print MASTERFILE "---\nID: NETBLK-ISP.$masterblocks[$i]\n".
     279        "Auth-Area: $masterblocks[$i]\n".
     280        "Network-Name: $netname\n".
     281        "IP-Network: $net\n".
     282        "IP-Network-Block: ".$net->range."\n".
     283        "Org-Name: $name\n".
     284        "Street-Address: $street\n".
     285        "City: $city\n".
     286        "StateProv: $prov\n".
     287        "Postal-Code: $pocode\n".
     288        "Country-Code: $country\n".
     289        "Tech-Contact: $tech\n".
     290        "Created: $ctime\n".
     291        "Updated: $mtime\n".
     292        "Updated-By: noc\@example.com\n";
     293    } # swip
     294
     295  } # while $ssth->fetchrow_array()
     296
     297  close MASTERFILE;
    310298
    311299  $i++;
    312 } # while fetchrow_array()
    313 
     300} # while $msth->fetchrow_array()
     301
     302# Now we see if there's obsolete netdata directories to be deleted,
     303# and therefore an auth-area file to regenerate
    314304foreach my $netdir (keys %rwhoisdirs) {
    315305  print "deleting obsolete directory $netdir...\n";
Note: See TracChangeset for help on using the changeset viewer.