#!/usr/bin/perl
use strict;
use Socket;
use Sys::Hostname;
my $host = hostname();

my $phost_ip =  gethostbyname($host);
my $host_ip;
if (defined $phost_ip) {
    $host_ip = inet_ntoa($phost_ip);
} else {
    $host_ip = "";
}

#usage eoshapl manager-node1 manager-node2 alias failovertime [debug]

$| = 1;

system("echo $$ >& /var/run/eosha.pid");

my $logfile = (shift @ARGV) or exit -1;
my $target1 = (shift @ARGV) or exit -1;
my $target2 = (shift @ARGV) or exit -1;
my $aliasname = (shift @ARGV) or exit -1;
my $failovertime = (shift @ARGV) or exit -1;
my $debug = (shift @ARGV or 0);

my $now;

sub HUP_handler {
    close STDOUT;
    close STDERR;

    open STDOUT, ">> $logfile";
    open STDERR,  ">> $logfile";

    $SIG{'HUP'} = 'HUP_handler';
    now();
    printf STDOUT "$now log rotated ...";

}

$SIG{'HUP'} = 'HUP_handler';

my $mgmstatus="";
my $mqstatus="";
my $syncstatus="";
my $eossyncstatus="";
my $eosshtimeout=10;

my $mastermgm=0;
my $mastermq=0;
my $mastersync=0;
my $masterfed=0;
my $eossync=0;
my $confdir="";
my $metadir="";
my $md1file = "";
my $md2file = "";
my $md1dirfile = "";
my $md2dirfile = "";
my $md1failoverfile = "";
my $md2failoverfile = "";
my $md1failoverdirfile = "";
my $md2failoverdirfile = "";
my $mdfailoverdir = "";
my $md1confdir = "";
my $md2confdir = "";
my $configname = "";

sub now {
    $now = `date`;
    chomp $now;
    $now .= " ";
    $now .= `date +%s`;
    chomp $now;
    return $now;
}

sub getconfig {
    $confdir = `cat /etc/xrd.cf.mgm | grep -v "^#" | grep mgmofs.configdir | awk '{print \$2}' 2>/dev/null`;
    $metadir = `cat /etc/xrd.cf.mgm | grep -v "^#" | grep mgmofs.metalog   | awk '{print \$2}' 2>/dev/null`;
    $configname = `cat /etc/xrd.cf.mgm | grep -v "^#" | grep mgmofs.autoloadconfig | awk '{print \$2}' 2>/dev/null`;

    if ( $ENV{'EOS_AUTOLOAD_CONFIG'} ne "" ) {
        $configname = $ENV{'EOS_AUTOLOAD_CONFIG'};
    }
    
    if ( $ENV{'EOS_MGM_TIMEOUT'} ne "" ) {
	$eosshtimeout = $ENV{'EOS_MGM_TIMEOUT'};
    }
 
    chomp $confdir;
    chomp $metadir;
    chomp $configname;

    if ($confdir eq "") {
        $confdir = "/var/eos/config/";
    }
    
    if ($metadir eq "") {
        $metadir = "/var/eos/md/";
    }
    
    now();
    
    $md1file = "$metadir/files.$target1.mdlog";
    $md2file = "$metadir/files.$target2.mdlog";
    $md1failoverfile = "$metadir/failover/files.$target1.mdlog";
    $md2failoverfile = "$metadir/failover/files.$target2.mdlog";

    $md1dirfile = "$metadir/directories.$target1.mdlog";
    $md2dirfile = "$metadir/directories.$target2.mdlog";
    $md1failoverdirfile = "$metadir/failover/directories.$target1.mdlog";
    $md2failoverdirfile = "$metadir/failover/directories.$target2.mdlog";
    $mdfailoverdir = "$metadir/failover";

    $md1confdir = "$confdir/$target1/";
    $md2confdir = "$confdir/$target2/";
}


my $cnt=0;
printf "$now ===================================================================\n";
printf "$now Started EOSHA - EOS High Availability\n";
printf "$now ___________________________________________________________________\n";

while(1) {
    now();


    if ($cnt ==0) {
        getconfig();
        print "$now MGM1-METALOG: fmd=$md1file dmd=$md1dirfile cfg=$md1confdir\n";
        print "$now MGM2-METALOG: fmd=$md2file dmd=$md2dirfile cfg=$md2confdir\n";
    }

    $mastermgm =system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status mgm >&/dev/null");
    $mastermq  =system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status mq >&/dev/null");
    $mastersync=system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status sync >&/dev/null");
    $masterfed =system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status fed >&/dev/null");
    $eossync=system("env CONSOLETYPE=serial /etc/rc.d/init.d/eossync status >&/dev/null");
    $mastermgm = ($mastermgm >> 8);
    $mastermq  = ($mastermq >> 8);
    $mastersync= ($mastersync >> 8);
    $masterfed = ($masterfed >> 8);
    $eossync= ($eossync >> 8);

    if (!$mastermgm) {
        $mgmstatus="<up>";
    } else {
        $mgmstatus="<down>";
    }

    if (!$mastermq) {
        $mqstatus="<up>";
    } else {
        $mqstatus="<down>";
    }

    if (!$mastersync) {
        $syncstatus="<up>";
    } else {
        $syncstatus="<down>";
    }

    if (!$eossync) {
        $eossyncstatus="<up>";
    } else {
        $eossyncstatus="<down>";
    }

    if ($debug || (!$cnt)) {print "$now MGM status is $mgmstatus - MQ status is $mqstatus - SYNC status is $syncstatus - EOSSYNC status is $eossyncstatus\n";}

    my $palias_ip =  gethostbyname("$aliasname");
    my $alias_ip;
    if (defined $palias_ip) {
        $alias_ip = inet_ntoa($palias_ip);
    } else {
	if (!$mastermgm) {
	    # if we are the master we stay the master
	    $alias_ip = $host_ip;
	} else {
	    # if we are a slave and cannot resolve we can set it to undef
	    $alias_ip = "-undef-";
	}
    }

    if ($debug || (!$cnt)) {print "$now hostname=$host [$host_ip]  aliastarget=$alias_ip aliases=$aliasname\n";}
    
    if ( $eossync ) {
	now();
	printf "$now ===================================================================\n";
	printf("$now (re-)start EOS SYNC ...\n");
	printf "$now ___________________________________________________________________\n";
	# service dead ... let's restart
	my $startupsync = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eossync restart >> $logfile 2>&1 ");
    }  

    if ("$host_ip" ne "$alias_ip") {
        if ( $mastersync ) {
            now();
            printf "$now ===================================================================\n";
            printf("$now (re-)start SYNC ...\n");
            printf "$now ___________________________________________________________________\n";
            # service dead ... let's restart
            my $startupsync = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos restart sync >> $logfile 2>&1 ");
        }

        if (!$mastermq) {
            now();
            printf "$now ===================================================================\n";
            printf "$now shut down running MQ ...\n";
            printf "$now ___________________________________________________________________\n";
            getconfig();
            
            my $shutdownmq  = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos stop mq >> $logfile 2>&1 ");
        }

        if (!$mastermgm) {
            now();
            my $stime = `date +%s`; chomp $stime;
            print "$now hostname=$host [$host_ip]  aliastarget=$alias_ip aliases=$aliasname\n";
            printf "$now ===================================================================\n";
            printf "$now shut down running mgm ...\n";
            printf "$now ___________________________________________________________________\n";           
            my $shutdownmgm = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos stop mgm");
            printf "$now ___________________________________________________________________\n";
            printf "$now Finished FAILOVER procedure ... $host(me) is now the SLAVE ...\n";
            printf "$now ===================================================================\n";
            if ( $ENV{'EOS_NOTIFY'} ne "" ) {
                system("echo $now Finished FAILOVER procedure ... $host\\(me\\) is now the SLAVE ... | $ENV{EOS_NOTIFY}");
            }

            my $ttime = `date +%s`; chomp $ttime;
            $ttime = int($ttime);
            $stime = int($stime);

            print "$ttime $stime $failovertime\n";
            if ( ($ttime - $stime) < $failovertime) {
                print "$ttime $stime $failovertime\n";
                for (my $i=0; $i< ($failovertime-($ttime-$stime)); $i+=10) {
                    now();
                    printf "$now ... allowing to become master again in %d seconds ...\n", $failovertime - $i;
                    sleep 10;
                }
            }
        } 
    } else {
        my $isfailover=0;
        # check if this is really a failover case or just a startup after a complete shutdown
        # for this we compare the timestamps of the files changelog files
        getconfig();
        my ($dev1,$ino1,$mode1,$nlink1,$uid1,$gid1,$rdev1,$size1,$atime1,$mtime1,$ctime1,$blksize1,$blocks1) = stat($md1file);
        my ($dev2,$ino2,$mode2,$nlink2,$uid2,$gid2,$rdev2,$size2,$atime2,$mtime2,$ctime2,$blksize2,$blocks2) = stat($md2file);

        if ( "$host" eq "$target1" ) {
            # the remote master has never written anything, we don't need to do a failover operation
            if ( $mtime2 eq "") {
                $isfailover=0;
            }
            # the local master is uninitialized, the remote master is not - this is a failover situation
            if ( ($mtime1 eq "") && ($mtime2 ne "")) {
                $isfailover=1;
            }
            # local and remote master have a changelog file, so let's judge by the modification time of the synced files
            if ( ($mtime1 ne "") && ($mtime2 ne "") && ($mtime1 <= $mtime2)) {
                $isfailover=1;
            }
        }

        if ( "$host" eq "$target2" ) {
            # the remote master has never written anything, we don't need to do a failover operation
            if ( $mtime1 eq "") {
                $isfailover=0;
            }
            # the local master is uninitalized, the remote master is not - this is a failover situation
            if ( ($mtime2 eq "") && ($mtime1 ne "")) {
                $isfailover=1;
            }
            # local and remote master have a changelog file, so let's judge by the modification time of the synced files
            if ( ($mtime2 ne "") && ($mtime1 ne "") && ($mtime2 <= $mtime1)) {
                $isfailover=1;
            }
        }

        if ( (!$isfailover) && (($mastermgm ==2) || ($mastermq == 2)) ) {
            now();
            printf "$now ===================================================================\n";
            printf "$now Skipping FAILOVER procedure (probably we have been the master before or the system is new) ... starting MASTER ...\n";
            printf "$now ___________________________________________________________________\n";
            my $startupmq   = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos start mq >> $logfile 2>&1 ");
            my $startupmgm  = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos start mgm >> $logfile 2>&1 ");
            my $startupsync = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos restart sync >> $logfile 2>&1 ");
            my $startupfed  = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos restart fed >> $logfile 2>&1 ");
        }
            
        if ( ($isfailover) && (($mastermgm == 2) || ($mastermq == 2)) ) {
            now();
            printf "$now ===================================================================\n";
            printf "$now Initiating FAILOVER procedure ... becoming MASTER ...\n";
            printf "$now ___________________________________________________________________\n";
            getconfig();

            my $stime = `date +%s`; chomp $stime;

            ###################### CONFIGURATION FAILOVER ############################
            # create backup name
            my $backupconfigname = $configname;
            my $backupconfigname = "$configname.failover.$stime";
            system("mkdir -p $md1confdir/backup");
            system("mkdir -p $md2confdir/backup");
            system("mkdir -p $mdfailoverdir");

            printf("backupname = $backupconfigname\n");
            if ( "$host" eq "$target1" ) {
                now();
                printf("$now backup config and copy from $target2 ...\n");
                printf "____________________________________________________________________________________________________________\n";
                system("cp -v $md1confdir/$configname.eoscf $md1confdir/backup/$backupconfigname.eoscf >> $logfile 2>&1 ");
                system("cp -v $md2confdir/$configname.eoscf $md1confdir/$configname.eoscf >> $logfile 2>&1 ");
            }
            if ( "$host" eq "$target2" ) {
                now();
                printf("$now backup config and copy from $target1 ...\n");
                printf "____________________________________________________________________________________________________________\n";
                system("cp -v $md2confdir/$configname.eoscf $md2confdir/backup/$backupconfigname.eoscf >> $logfile 2>&1 ");
                system("cp -v $md1confdir/$configname.eoscf $md2confdir/$configname.eoscf >> $logfile 2>&1 ");
            }

            # shutdown sync
            system("env CONSOLETYPE=serial /etc/rc.d/init.d/eossync stop >> $logfile 2>&1 ");
            ###################### METADATA -  FAILOVER ############################
            if ( "$host" eq "$target1" ) {
                now();
                printf "____________________________________________________________________________________________________________\n";
                system("test -e $md1file && mv -v $md1file $md1failoverfile.$stime >> $logfile 2>&1 ");
                system("test -e $md1failoverfile.$stime && bzip2 -v $md1failoverfile.$stime >> $logfile 2>&1 &");
                printf "____________________________________________________________________________________________________________\n";
                my $retc = system("echo := eos-log-compact $md2file $md1file ; test -e $md2file && eos-log-compact $md2file $md1file >> $logfile 2>&1 ");
                $retc = ($retc >> 8);
                if ( -e "$md2file" && $retc) {
                    printf("$now WARNING : eos-log-compact failed - trying eos-log-repair\n");
                    my $retrepair = system("echo := eos-log-repair $md2file $md1file >> $logfile 2>&1 ; test -e $md2file && eos-log-repair $md2file $md1file >> $logfile 2>&1 ");
                    $retrepair = ($retrepair >> 8);
                    if ($retrepair) {
                        # oh oh, that is quite fatal
                        printf("$now FATAL cannot repair changelog\n");
                        # we stop ourself and send an emergency email
                        if ( $ENV{'EOS_NOTIFY'} ne "" ) {
                            system("echo $now Failed FAILOVER procedure ... $host\\(me\\) is now the MASTER ... but eos-log-repair failed | $ENV{'EOS_NOTIFY'}");                    
                            system("env CONSOLETYPE=serial /etc/rc.d/init.d/eosha stop >> $logfile 2>&1 ");
                        }
                    }
                }

                printf "____________________________________________________________________________________________________________\n";
                system("test -e $md1dirfile && mv -v $md1dirfile $md1failoverdirfile.$stime >> $logfile 2>&1 ");
                system("test -e $md1failoverdirfile.$stime && bzip2 -v $md1failoverdirfile.$stime >> $logfile 2>&1 &");
                printf "____________________________________________________________________________________________________________\n";
                $retc = system("echo := eos-log-compact $md2dirfile $md1dirfile >> $logfile 2>&1  ; eos-log-compact $md2dirfile $md1dirfile >> $logfile 2>&1 ");
                $retc = ($retc >> 8);
                if ( -e "$md2dirfile" && $retc) {
                    printf("$now WARNING : eos-log-compact failed - trying eos-log-repair\n");
                    my $retrepair = system("echo := eos-log-repair $md2dirfile $md1dirfile >> $logfile 2>&1 ; test -e $md2dirfile && eos-log-repair $md2dirfile $md1dirfile >> $logfile 2>&1 ");
                    $retrepair = ($retrepair >> 8);
                    if ($retrepair) {
                        # oh oh, that is quite fatal
                        printf("$now FATAL cannot repair changelog\n");
                        # we stop ourself and send an emergency email
                        if ( $ENV{'EOS_NOTIFY'} ne "" ) {
                            system("echo $now Failed FAILOVER procedure ... $host\\(me\\) is now the MASTER ... but eos-log-repair failed | $ENV{'EOS_NOTIFY'}");                    
                        }
                        system("env CONSOLETYPE=serial /etc/rc.d/init.d/eosha stop >> $logfile 2>&1 ");
                    }
                }

                printf "____________________________________________________________________________________________________________\n";
            }

            if ( "$host" eq "$target2" ) {
                printf "____________________________________________________________________________________________________________\n";
                system("test -e $md2file && mv -v $md2file $md2failoverfile.$stime >> $logfile 2>&1 ");
                system("test -e $md2failoverfile.$stime && bzip2 -v $md2failoverfile.$stime >> $logfile 2>&1 & ");
                printf "____________________________________________________________________________________________________________\n";
                my $retc = system("echo := eos-log-compact $md1file $md2file >> $logfile 2>&1 ; test -e $md1file && eos-log-compact $md1file $md2file >> $logfile 2>&1 ");
                $retc = ($retc >> 8);
                if ( -e "$md1file" && $retc) {
                    printf("$now WARNING : eos-log-compact failed - trying eos-log-repair\n");
                    my $retrepair = system("echo := eos-log-repair $md1file $md2file >> $logfile 2>&1  ; test -e $md1file && eos-log-repair $md1file $md2file >> $logfile 2>&1 ");
                    $retrepair = ($retrepair >> 8);
                    if ($retrepair) {
                        # oh oh, that is quite fatal
                        printf("$now FATAL cannot repair changelog\n");
                        # we stop ourself and send an emergency email
                        if ( $ENV{'EOS_NOTIFY'} ne "" ) {
                            system("echo $now Failed FAILOVER procedure ... $host\\(me\\) is now the MASTER ... but eos-log-repair failed | $ENV{'EOS_NOTIFY'}");                    
                        }
                        system("env CONSOLETYPE=serial /etc/rc.d/init.d/eosha stop >> $logfile 2>&1 ");
                    }
                }

                printf "____________________________________________________________________________________________________________\n";
                system("test -e $md2dirfile && mv -v $md2dirfile $md2failoverdirfile.$stime >> $logfile 2>&1 ");
                system("test -e $md2failoverdirfile.$stime && bzip2 -v $md2failoverdirfile.$stime >> $logfile 2>&1 &");
                printf "____________________________________________________________________________________________________________\n";
                $retc = system("echo := eos-log-compact $md1dirfile $md2dirfile >> $logfile 2>&1 ; test -e $md1dirfile && eos-log-compact $md1dirfile $md2dirfile >> $logfile 2>&1 ");
                $retc = ($retc >> 8);
                if ( -e "$md1dirfile" && $retc) {
                    printf("$now WARNING : eos-log-compact failed - trying eos-log-repair\n");
                    my $retrepair = system("echo := eos-log-repair $md1dirfile $md2dirfile >> $logfile 2>&1 ; test -e $md1dirfile && eos-log-repair $md1dirfile $md2dirfile >> $logfile 2>&1 ");
                    $retrepair = ($retrepair >> 8);
                    if ($retrepair) {
                        # oh oh, that is quite fatal
                        printf("$now FATAL cannot repair changelog\n");
                        # we stop ourself and send an emergency email
                        if ( $ENV{'EOS_NOTIFY'} ne "" ) {
                            system("echo $now Failed FAILOVER procedure ... $host\\(me\\) is now the MASTER ... but eos-log-repair failed | $ENV{'EOS_NOTIFY'}");                    
                        }
                        system("env CONSOLETYPE=serial /etc/rc.d/init.d/eosha stop >> $logfile 2>&1 ");
                    }
                }

                printf "____________________________________________________________________________________________________________\n";
            }

            my $ttime = `date +%s`; chomp $ttime;
            $ttime = int($ttime);
            $stime = int($stime);

            print "$ttime $stime $failovertime\n";
            if ( ($ttime - $stime) < $failovertime) {
                print "$ttime $stime $failovertime\n";
                for (my $i=0; $i< ($failovertime-($ttime-$stime)); $i+=10) {
                    now();
                    printf "$now ... starting service in %d seconds ...\n", $failovertime-($ttime-$stime) - $i;
                    sleep 10;
                }
            }

            printf("\n");
            print "$now Bring up MGM + MQ\n";
            my $startupmq  = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos start mq >> $logfile 2>&1 ");
            my $startupmgm = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos start mgm >> $logfile 2>&1 ");
            
            sleep 5;

            # restart sync
            system("env CONSOLETYPE=serial /etc/rc.d/init.d/eossync start >> $logfile 2>&1 ");

            $mastermgm =system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status mgm >&/dev/null");
            $mastermq  =system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status mq >&/dev/null");
            $mastersync=system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status sync >&/dev/null");
            $masterfed =system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos status fed >&/dev/null");
            $eossync=system("env CONSOLETYPE=serial /etc/rc.d/init.d/eossync status >&/dev/null");
            $mastermgm = ($mastermgm >> 8);
            $mastermq  = ($mastermq >> 8);
            $mastersync= ($mastersync >>8);
	    $masterfed = ($masterfed >>8);
            $eossync = ($eossync >>8);
            
            if ( (!$mastermgm) && (!$mastermq) &&(!$mastersync) ) {
                now();
                printf "$now ___________________________________________________________________\n";
                printf "$now Finished FAILOVER procedure ... $host(me) is now the MASTER ...\n";
                printf "$now ===================================================================\n";
                if ( $ENV{'EOS_NOTIFY'} ne "" ) {
                    system("echo $now Finished FAILOVER procedure ... $host\\(me\\) is now the MASTER ... | $ENV{'EOS_NOTIFY'}");                    
                }
            }
        }
        if ( $mastermgm && ($mastermgm != 2)) {
            now();
            printf "$now ===================================================================\n";
            printf("$now restart dead MGM ...\n");
            printf "$now ___________________________________________________________________\n";
            # service dead ... let's restart
            my $startupmgm = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos restart mgm >> $logfile 2>&1 ");
        }
        if ( $mastermq && ($mastermq != 2)) {
            now();
            printf "$now ===================================================================\n";
            printf("$now restart dead MQ ...\n");
            printf "$now ___________________________________________________________________\n";
            # service dead ... let's restart
            my $startupmq = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos restart mq >> $logfile 2>&1 ");
        }
        if ( $mastersync ) {
            now();
            printf "$now ===================================================================\n";
            printf("$now (re-)start SYNC ...\n");
            printf "$now ___________________________________________________________________\n";
            # service dead ... let's restart
            my $startupsync = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos restart sync >> $logfile 2>&1 ");
        }

	if ( $masterfed ) {
	    now();
#            printf "$now ===================================================================\n";
#            printf("$now (re-)start FED ...\n");
#            printf "$now ___________________________________________________________________\n";
            # service dead ... let's restart
            my $startupfed = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eos restart fed >> /dev/null 2>&1 ");
	}
        if ( $eossync ) {
            now();
            printf "$now ===================================================================\n";
            printf("$now (re-)start EOS SYNC ...\n");
            printf "$now ___________________________________________________________________\n";
            # service dead ... let's restart
            my $startupsync = system("env CONSOLETYPE=serial /etc/rc.d/init.d/eossync restart >> $logfile 2>&1 ");
         }  
	# check that the MGM is operational
	if (!$mastermgm) {
	    my $mgmdead=system("eossh-timeout -t $eosshtimeout xrd localhost stat / >& /dev/null");
	    $mgmdead = ($mgmdead >> 8);
	    if ($mgmdead) {
		now();
		printf "$now ===================================================================\n";
		printf "$now local alive test gave a timeout after $eosshtimeout seconds ...\n";
		printf "$now ___________________________________________________________________\n";
		if ( $ENV{'EOS_NOTIFY'} ne "" ) {
		    my $timefile=`find /var/lock/subsys/eos.mgmtimeout -mmin -10 -mmin +0 2>/dev/null`;
		    if ( "$timefile" eq "/var/lock/subsys/eos.mgmtimeout" ) {
			printf "$now MGM didn't respond within $eosshtimeout seconds .. [ not sending mail notification for some time ... ]\n";
		    } else {
			system("echo $now MGM on $host\\(me\\) did not reply within timeout of $eosshtimeout seconds ... | $ENV{EOS_NOTIFY}");
			system("touch /var/lock/subsys/eos.mgmtimeout");
			printf "$now MGM didn't respond within $eosshtimeout seconds .. [ sending mail via $ENV{'EOS_NOTIFY'} ]\n";
		    }
		}
	    }
	}
    }

    sleep(5);
    $cnt++;
}
