#!/usr/bin/perl

use strict;

print "\nEnter absolute path for mountpoint #1: ";
my $mount1=<STDIN>;
chomp $mount1;
print "\nEnter absolute path for mountpoint #2: ";
my $mount2=<STDIN>;
chomp $mount2;
print "\nEnter absolute path for mountpoint #3: ";
my $mount3=<STDIN>;
chomp $mount3;

print "\nEnter FILER-A IP: ";
my $filer_a = <STDIN>;
chomp $filer_a;

print "\nEnter FILER-B IP: ";
my $filer_b = <STDIN>;
chomp $filer_b;

if (-e "/root/io_log") {
    print "Directory /root/io_log already exists.\n";
} else {
    `mkdir /root/io_log`;
    print "Created Directory /root/io_log for dt logs\n";
}

open (myfile, '>/root/io_log/noprogmade.pl');
print myfile "#!/usr/bin/perl\n\n";
print myfile '`echo "======================================" >> /root/io_log/Status`;';
print myfile "\n";
print myfile '`date >> /root/io_log/Status`;';
print myfile "\n";
print myfile '`echo "No IO progress for > 120 sec" >> /root/io_log/Status`;';
print myfile "\n";
print myfile '`echo "======================================" >> /root/io_log/Status`;';
print myfile "\n\n";
close (myfile);

`chmod 777 /root/io_log/noprogmade.pl`;

`echo "nohup dt of=$mount1/a limit=500m oncerr=abort disable=pstats disable=fsync oflags=trunc errors=1 incr=var min=2k max=64k dispose=keep pattern=iot iotype=random enable=noprog noprogt=120s noprogtt=120s alarm=3s trigger="cmd:/root/noprogmade.pl" history=3 enable=syslog log=/root/io_log/log1 runtime=50h &" > /root/io_log/io_script.sh`;

`echo "nohup dt of=$mount2/b limit=500m oncerr=abort disable=pstats disable=fsync oflags=trunc errors=1 incr=var min=2k max=64k dispose=keep pattern=iot iotype=random enable=noprog noprogt=120s noprogtt=120s alarm=3s trigger="cmd:/root/noprogmade.pl" history=3 enable=syslog log=/root/io_log/log2 runtime=50h &" >> /root/io_log/io_script.sh`;

`echo "nohup dt of=$mount3/c limit=500m oncerr=abort disable=pstats disable=fsync oflags=trunc errors=1 incr=var min=2k max=64k dispose=keep pattern=iot iotype=random enable=noprog noprogt=120s noprogtt=120s alarm=3s trigger="cmd:/root/noprogmade.pl" history=3 enable=syslog log=/root/io_log/log3 runtime=50h &" >> /root/io_log/io_script.sh`;

`chmod 777 /root/io_log/io_script.sh`;
system("/root/io_log/io_script.sh &");
print "Started dt on mountpoints...\n";

print "\nWaiting 600S before starting faults on $filer_a and $filer_b...\n";

&wait_delay();

print "\n";

for my $i (1 .. 30) {

    `echo "Iteration no. $i" >> /root/io_log/Status`;
    &command_run($filer_a, "cf takeover");
    &wait_delay();

    &command_run($filer_a, "cf giveback");
    &wait_delay();

    &command_run($filer_b, "cf takeover");
    &wait_delay();

    &command_run($filer_b, "cf giveback");
    &wait_delay();
    
    &command_run($filer_a, "cf takeover -f");
    &wait_delay();

    &command_run($filer_a, "cf giveback");
    &wait_delay();

    &command_run($filer_b, "cf takeover -f");
    &wait_delay();

    &command_run($filer_b, "cf giveback");
    &wait_delay();

    &command_run($filer_a, "fcp stop");
    sleep 30;
    &command_run($filer_a, "fcp start");
    &wait_delay();
    
    &command_run($filer_b, "fcp stop");
    sleep 30;
    &command_run($filer_b, "fcp start");
    &wait_delay();
}

sub command_run {

    my ($filer,$cmd) = @_;
    print "\n";
    `date`;
    `date >> /root/io_log/Status`;
    `echo "Performing $cmd on filer $filer" >> /root/io_log/Status`;
    system ("rsh -l root: $filer \"$cmd\"");
    print "\n";
    return 1;
}

sub wait_delay {
    print "\n\nWaiting for 600 Seconds...\n";
    for my $i (1 .. 600)
    {
        print ".";
        `sleep 1`;
    }
    return 1;
}
