[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Deleted network not available again?



-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Thursday 23 May 2002 05:43 pm, Hays, Kelly wrote:
> Hitesh,
> Is this bug still in 1.6? If so, I think I just exercised it. Did you ever
> write the script to go thru and relink 'lost' blocks? If so where can I
> find it?

I've attached the script to this message.  Just run it and follow the prompts.  
There are two modes to run in.  'relink' more will relink lost children to 
the root of the tree, 'delete' will just remove them.

This only needs to be run if you use MySQL since Postgres handles these things 
correctly.

Let me know if you have any problems with it.. make sure to post the output 
from the program with your message.

> Thanks,
>   Kelly Hays
>
> >-----Original Message-----
>
> From: Hitesh Patel [mailto:hitesh@presys.com]
>
> >Sent: Friday, April 19, 2002 12:14 AM
> >To: Adam "Tauvix" Debus; northstar-general@brownkid.net
> >Subject: Re: Deleted network not available again?
> >
> >-----BEGIN PGP SIGNED MESSAGE-----
> >Hash: SHA1
> >
> >On Thursday 18 April 2002 17:24 pm, Adam \"Tauvix\" Debus wrote:
> >> MySQL
> >
> >Ah hah.. i knew MySQL was going to bite me in the a@@ sooner or later..
> >ok..
> >here we go
> >
> >Turns out that MySQL doesn't do REFERENCES right.. this is a problem that
> > I never realized was there because mysql doesn't output any errors on it.
> >  So I
> >had to implement recursive delete functions in NorthStar to handle the
> >removal in MySQL.. PITA if you ask me ;-)
> >
> >I've committed the changes to CVS along with some other fixes and will
> >probably whip up a script to go thru and relink 'lost' blocks sometime
> >tomorrow.
> >
> >> ----- Original Message -----
> >> From: "Hitesh Patel" <hitesh@presys.com>
> >> To: "Adam "Tauvix" Debus" <northstar@delsol.net>;
> >> <northstar-general@brownkid.net>
> >> Sent: Thursday, April 18, 2002 3:18 PM
> >> Subject: Re: Deleted network not available again?
> >>
> >> > On Thursday 18 April 2002 13:42 pm, Adam \"Tauvix\" Debus wrote:
> >> > > I have found a problem where if you delete a subnet, and it has
> >>
> >> children,
> >>
> >> > > that it doesn't delete the children. Look in the database for
> >> > > individual
> >>
> >> IP
> >>
> >> > > addresses, or subnets, that came from that /24.
> >> >
> >> > I'm looking into why this is happening.  The rules in the db should
> >
> >make
> >
> >> this
> >>
> >> > automatically happen.  Are you using MySQL or Postgres?
> >> >
> >> > > Thanks,
> >> > >
> >> > > Adam "Tauvix" Debus
> >> > > Network Administrator, ReachONE Internet
> >> > > adam@reachone.com
> >> > >
> >> > > ----- Original Message -----
> >> > > From: "Cooper, Steve" <SCooper@newskies.com>
> >> > > To: <northstar-general@brownkid.net>
> >> > > Sent: Thursday, April 18, 2002 11:34 AM
> >> > > Subject: Deleted network not available again?
> >> > >
> >> > > > Hi
> >> > > >
> >> > > > I deleted a subnet (a /24) and then tried to add it again, but now
> >> > > > its not available in the 'Network Address' drop-down (after
> >
> >selecting
> >
> >> > > > the
> >> > >
> >> > > netmask).
> >> > >
> >> > > > Any ideas where to start looking?
> >> > > >
> >> > > > Excellent program BTW.
> >> > > >
> >> > > > Steven Cooper
> >> > > > Senior Network Implementation Engineer
> >> > > > New Skies Satellites UK Ltd
> >> > > > email: scooper@newskies.com
> >> >
> >> > --
> >> > +---------------------------------+----------------------------+
> >> >
> >> > | Hitesh Patel                    |  Voice: (541) 759-3126     |
> >> > | Network Engineering Manager     |  Fax:   (541) 759-3214     |
> >> > | Preferred Communications Inc.   |  Email: hitesh@pciwest.net |
> >> >
> >> > +---------------------------------+----------------------------+
> >
> >- --
> >+---------------------------------+----------------------------+
> >
> >| Hitesh Patel                    |  Voice: (541) 759-3126     |
> >| Network Engineering Manager     |  Fax:   (541) 759-3214     |
> >| Preferred Communications Inc.	  |  Email: hitesh@pciwest.net |
> >
> >+---------------------------------+----------------------------+
> >
> >-----BEGIN PGP SIGNATURE-----
> >Version: GnuPG v1.0.6 (GNU/Linux)
> >Comment: For info see http://www.gnupg.org
> >
> >iD8DBQE8v6eGCws8KqPtd2URAhQyAJwKXfdL2CvUa0w7aX+mvBh0MJoIUACeKHel
> >EAd3nzubkciOdsYO2uLKgEU=
> >=l31B
> >-----END PGP SIGNATURE-----

- -- 
+---------------------------------+----------------------------+
| Hitesh Patel                    |  Voice: (541) 759-3126     |
| Network Engineering Manager     |  Fax:   (541) 759-3214     |
| Preferred Communications Inc.	  |  Email: hitesh@pciwest.net |
+---------------------------------+----------------------------+

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.0.6 (GNU/Linux)
Comment: For info see http://www.gnupg.org

iD8DBQE87oYoCws8KqPtd2URAuKrAJ9zFaUdaIhQ6MlxkbfJZSrTqaX3WQCfVhND
enxiDbsGWekDTI5JSOZKDTw=
=RCZ1
-----END PGP SIGNATURE-----
#!/usr/bin/perl
package NorthStarRelinker;

use DBI;

$VERSION = "1.0";
my(@searchdir) = qw(/opt/NorthStar /usr/NorthStar /usr/local/NorthStar);
my($t);

foreach(@searchdir) {
    if(-f "$_/NorthStar.conf") {
	print "config in $_\n";
        ConfigInit("$_/NorthStar.conf");
        $t = 1;
        last;
    }
}

if(!$t) {
    die("Unable to find the configuration file.\n");
}

my(%config) = ConfigGetSection('Global');
my(%dbconfig) = ConfigGetSection('Database');
my($lib_dir) = $config{'InstallDir'} . "/lib/";

require $lib_dir . 'dbutil.lib';

my(%dbinfo) = (
  'fh'      => 'db',		              # Database FH, DONT change this
  'driver'	=> $dbconfig{'DatabaseDriver'},# DBI database driver
  'host'    => $dbconfig{'DatabaseHost'}, # The host address of your db server
  'port'    => $dbconfig{'DatabasePort'}, # Database port
  'options' => '',		                  # Unused
  'tty'     => '',		                  # Unused
  'dbname'  => $dbconfig{'DatabaseName'}, # The database to connect to
  'dbuser'  => $dbconfig{'DatabaseUser'}, # The user to connect as
  'dbpass'  => $dbconfig{'DatabasePass'}  # The password to use
);

if(db_connect(\%dbinfo) != 0) {
	ExitError("db_connect: " . $DBI::errstr);
}

if($dbinfo{'driver'} !~ /mysql/i) {
    print "\nThis script only needs to be run on MySQL databases\n\n";
   exit(0);
}

main();

sub main {
    my($c, $sth, $sql);

    print 
"





Welcome to the NorthStar Database Relinker.  This program
will relink or delete 'lost' children in the networks table.

If you choose to relink the netblock will be placed under
the root of the tree and you can properly delete it from there.

If you choose to delete the lost blocks will be deleted from 
the database.

This script should only be used with versions 1.4 to 1.6 of NorthStar

Please make sure you have backed up your existing database
before running this program... we wouldn't want you to 
loose all your data would we? ;-)

";
    print "Would you like to continue with the update [y/n]? ";
    $c = <STDIN>;
    chop($c);
    lc($c);
    if($c ne 'y') {
        ExitError("Aborting update");
    }

    print "\nWould you like to Relink or Delete lost children [r/d]? ";
    $c = <STDIN>;
    chop($c);
    lc($c);
    if($c eq 'd') {
	DeleteLost();
    } else {
	RelinkLost();
    }

    print << 'EOF';


Ok, were done.  Give it a run and see how it
works!!

EOF

}

sub RelinkLost {
    my(@lost) = GetLostChildren();
    print "Relink... ";
    foreach(@lost) {
       print "$_ ";
       my($sql) = "UPDATE networks SET parent = '0' WHERE id = '$_';";
       my($sth) = $db->prepare($sql);
       $sth->execute();
       db_rescheck(\$sth,'relink $_','1');
    }
}

sub DeleteLost {
    my(@lost) = GetLostChildren();
    print "Delete... ";
    foreach(@lost) {
       print "$_ ";
       my($sql) = "DELETE FROM networks WHERE id = '$_';";
       my($sth) = $db->prepare($sql);
       $sth->execute();
       db_rescheck(\$sth,'delete $_','1');
    }
}

sub GetLostChildren {
    my(%id, %parent, @ret);
    print "\nLoading networks cache...\n";
    my($sql) = "SELECT id,parent FROM networks;";
    my($sth) = $db->prepare($sql);
    $sth->execute();
    db_rescheck(\$sth, 'getcache','1');
    my($i);
    for($i = 0; $i < $sth->rows ; $i++) {
	if($i % 10) { print "."; }
	my(@row) = $sth->fetchrow_array;
        $id{$row[0]} = 1;
        $parent{$row[0]} = $row[1];
    }
    print "done (loaded " . $sth->rows . ")\n"; 
    print "Finding lost children...\n";
    while(($k,$v) = each %id) {
       if(!$id{$parent{$k}}) {
       	print "$k->$parent{$k}\n";
	push(@ret, $k);
       }
    } 
    return(@ret);
}

sub ExitError {
    my($msg) = $_[0];
    my($package, $filename, $line) = caller;
    print "\nERROR: $msg at $package:$filename:$line\n";
    exit(1);
}

sub ConfigGetSection {
        my $sec = shift;
        my(%t);

        my($h) = $NorthStar::CONFIG{$sec};
        while(my($k,$v) = each %$h) {
                $t{$k} = $v;
        }
        return(%t);
}

sub ConfigGetValue {
        if(scalar(@_) == 3) {
                return($NorthStar::CONFIG{$_[0]}{$_[1]}{$_[2]});
        } elsif(scalar(@_) == 2) {
                return($NorthStar::CONFIG{$_[0]}{$_[1]});
        } else {
                return(undef);
        }
}

sub ConfigInit {
        my $cf = shift;
        my($cs, $csv, $cn, $cv, @j, $x);
        open(_CONFIG, "<$cf") || die("config file open failed: $cf: $!\n");
        while(<_CONFIG>) {
                chomp;
                s/^[\s\t]+//g; #strip leading whitespace
                next if /^#/;
                next if /^$/;
                s/\s+$//g; #strip trailing whitespace

                if(/\#/) {
                    /(.*)\#.*/;
                    $x = $1;
                } else {
                    $x = $_;
                }

                # given:
                # <Section SectionVariable>
                #                Name = Value
                # </Section>
                # $cs  = Section                Config Section        
                # $csv = SectionVariable        Config Section Variable 
                # $cn  = Name                   Config Name
                # $cv  = Value                  Config Variable
                # 
                # $csv may not be defined if a SectionVariable was not present
                if($x =~ /^<(.*)>$/ && $x !~ /^<\/.*>/) {           # handler for section starts <Section>
                        if(!defined($cs)) {              # check to see if the previous section was closed with </Section> 
                                $cs = $1;
                        } else {
                                die("syntax error in configuration file \"$cf\" at line $.: " .
                                    "previous configuration section not closed (maybe you forgot </$cs>)\n");
                        }

                        # sub-handler for sections with a variable <Section SectionVariable>
                        if($x =~ /^<(.*) (.*)>$/ && (@j = split(/\s+/, $x)) == 2) {
                                $cs = $1;
                                $csv = $2;
                        } elsif($x =~ /^<(.*)>$/) { # dont clobber sections with no SectionVariable (<Section> ..)
                                $cs = $1;
                        } else {
                                die("syntax error in configuration file \"$cf\" at line $.: " .
                                    "configuration sections may only have one parameter (<Section Parameter>)\n");
                        }
                        next;
                } elsif($x =~ /^<\/(.*)>$/) {            #handler for section closes </Section>
                        if(defined($cs)) {         #check to see if this section was ever opened
                                undef($cs);
                                undef($csv);
                        } else {
                                $cs = $1;          #set section name for error message
                                $cs =~ s/\///g;    #strip leading '/'
                                die("syntax error in configuration file \"$cf\" at line $.: " .
                                    "configuration section not opened (maybe you forgot <$cs>)\n");
                        }
                        next;
                } else {
                        if($x =~ /(.*) \"(.*)\"/) {               #process line "Name = "Value In Here"
                                $cn = $1;                                
                                $cv = $2;
                                $cv =~ s/\\(.)/$1/g;              #normalize escaped charecters like: \"
                        } elsif((@j = split(/\s+/, $x)) > 2) {    #process line "Name = Value1 Value2..."
                                $cn = shift(@j);
                                $cv = join(',', @j);              #make a comma delimited list
                        } elsif((@j = split(/\s+/, $x)) == 2) {   #process line "Name = Value"
                                $cn = $j[0];
                                $cv = $j[1];
                        } else {
                                die("syntax error in configuration file \"$cf\" at line $.\n");
                        }

                        if($csv) { #if SectionVariable was set this as CONFIG->Section->SectionVariable->Name=Value
                                #$self->Debug("add value: CONFIG->$cs->$csv->$cn=$cv");
                                $NorthStar::CONFIG{$cs}{$csv}{$cn} = $cv;
                        } else {   #else set this value under CONFIG->Section->Name=Value
                                #$self->Debug("add value: CONFIG->$cs->$cn=$cv");
                                $NorthStar::CONFIG{$cs}{$cn} = $cv;
                        }

                        next;
                }


        }
        #$self->Debug("closing config file");
        close(_CONFIG) || die("close failed: $cf: $!\n");

        return 1;
}

sub ConfigPrint {
        if(!defined(%NorthStar::CONFIG)) {
                return;
        }

        while(my($k,$v) = each %NorthStar::CONFIG) {
                print STDERR "$k\n";
                while(my($y,$z) = each %$v) {
                        if(ref($z) eq "HASH") {
                                print STDERR " $y\n";
                                while(my($a,$b) = each %$z) {
                                        print STDERR "  $a=$b\n";
                                }
                        } else {
                                print STDERR " $y=$z\n";
                        }
                }
                print STDERR "\n";
        }
}