Sophie

Sophie

distrib > Mandriva > 10.2 > x86_64 > by-pkgid > ff3ee324c0567e1c25614ef618e7dd7f > files > 19

clusterautosetup-2.1-5mdk.src.rpm

#!/usr/bin/perl -w

# setup_add_nodes_to_dhcpd

# Copyright (C) 1999 MandrakeSoft (glebouder@mandrakesoft.com)
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.

#V0.10 : bug fix

use strict;
use File::Copy;
use Getopt::Std;
use Term::ANSIColor;

my $dhcpnode  = "/usr/bin/dhcpnode";
my $syslog    = "/var/log/syslog";
my $dhcpdconf = "/etc/dhcpd.conf";
my @my_mac;
my @mac;
my $node_nbre = 0;
my @mac_in_dhcp;

getopts("n:") || &usage;
use vars qw($opt_n);

if ($opt_n) {
    if ( $opt_n < 1 ) {
        print "can detect less than 1 node !\n";
        &usage;
    }

    sub usage {
        die "$0 [-n Nombre_of_Nodes ]
	-n          : number of nodes, \n!!!this option change the original $dhcpdconf file!!!.
	
    ";
    }
}

if ( !-f $dhcpnode ) {
    print "$dhcpnode doesn't exist please change the \$dhcpnode location\n";
    die;
}

if ( !-f $dhcpdconf ) {
    print "$dhcpdconf doesn't exist\n";
    die;
}

copy $dhcpdconf , $dhcpdconf . "\.add_nodes";

print "This script works on a backup of $dhcpdconf\n";
print "it can be found in  $dhcpdconf.add_nodes\n";
print "\n\nThe script must be run on the dhcp server !\n";

if ($opt_n) {
    print "\nwaiting for $opt_n node(s)...\n";
}
else {
    print "\nMove $dhcpdconf.add_nodes to $dhcpdconf and do a\n
    /etc/init.d/dhcpd restart\n
after to valid changes.\n\n";

    print "\nwaiting for dhcp request\n\n";
    print "Press Ctrl C to stop collect MAC addresse\n";
}

sub log_scan {
    open MESSAGE, $syslog or die "Can't open /var/log/messages !!!\n";
    my @mac;

    seek( MESSAGE, 0, 2 );

    for ( ; ; ) {
	    sleep 1;
        while (<MESSAGE>) {

            if ( /DHCPACK on \S+ to (\S+) via \S+/ )
            {
                if ( !&is_it_my_MAC($1) == 1 ) {

                    if ( !( &is_it_in_dhcp($1) ) ) {
                        if ( !( &is_it_allready_know($1) ) ) {

                            print "DHCP REQUEST FROM A NEW NODE : $1";
                            if ($opt_n) {
                                $node_nbre++;
                                print "     [ $node_nbre / $opt_n ]";
                            }
                            print "\n";
                            system "$dhcpnode -C $dhcpdconf.add_nodes -a $1 -b -s";
                        }
                        else {
                            print "dhcp request from a registred node : $1\n";
                        }

                    }
                    else {
                        print
"dhcp request from a node that is already in $dhcpdconf : $1\n";

                    }

                }

                # I test if we have enough addresses
                &should_i_leave;
                sleep 1;

            }
        }

    }

}

# Get the MAC addresses of the dhcp server
sub my_MAC {

    my $output;
    my $mac;
    my @my_mac;
    my $line;
    my $minuscule;
    my $cmd = "/sbin/ifconfig";

    open( IFCONFIG, "$cmd | " ) or ( carp("Could not run $cmd"), return undef );

    while ( defined( $line = <IFCONFIG> ) ) {
        chomp $line;

        next unless ( $line =~ /^eth/ );
        if ( $line =~ /\S+\s+\S+\s+\S+\s+\S+\s+(\S+)/ ) {

            $minuscule = $1;
            $minuscule =~ s/(\S)/\L$1/gi;
            push @my_mac, $minuscule;
        }
    }

    close IFCONFIG;
    foreach (@my_mac) {
    }
    @my_mac;

}

sub is_it_my_MAC {
    my $it_s_my_mac = 0;

    my $mac_test = $_[0];
    foreach (&my_MAC) {

        if ( "0" . $mac_test eq $_ ) {
            $it_s_my_mac = 1;
        }
    }

    $it_s_my_mac;
}

sub is_it_allready_know {

    my $new_mac   = $_[0];
    my $registred = 0;
    foreach (@mac) {

        if ( $new_mac eq $_ ) {
            $registred = 1;
        }

    }

    if ( !($registred) ) {
        push @mac, $new_mac;
    }

    $registred;
}

sub is_it_in_dhcp {

    my $new_mac = $_[0];
    my $in_dhcp = 0;
    foreach (@mac_in_dhcp) {
        if ( $new_mac eq $_ ) {
            $in_dhcp = 1;
        }

    }

    $in_dhcp;
}

sub read_mac_from_dhcpd {
    my $cmd = "/usr/bin/dhcpnode -l";
    my $line;

    open( DHCPNODE, "$cmd | " ) or ( carp("Could not run $cmd"), return undef );

    while ( defined( $line = <DHCPNODE> ) ) {
        next unless ( $line =~ /\S+\s+(\S\S:\S\S:\S\S:\S\S:\S\S:\S\S)/ );
        push @mac_in_dhcp, $1;
    }
    close DHCPNODE;

}

# I test if we have enough addresses
sub should_i_leave {

    if ($opt_n) {
        if ( @mac >= $opt_n ) {
            close MESSAGE;
            print "Moving dhcpd.conf in /root...\n";
            move $dhcpdconf , "/root/";
            print "Moving $dhcpdconf" . "\.add_nodes in $dhcpdconf\n";
            move $dhcpdconf . "\.add_nodes", $dhcpdconf;
            system "/etc/init.d/dhcpd", "restart";
            die "dhcpd.conf wrote. dhcpd restarted.\n";
        }

    }

}
&read_mac_from_dhcpd;
&log_scan;