#!/usr/bin/perl

# misc variables
my (@requested, @found);


# extract @requested from arugments
foreach (@ARGV)
{
    if ( /^rsf:(\d+)$/ ) { push(@requested, { 'id' => $1 }); }
    else
    {
        print STDERR 'Invalid syntax for parameter (' . $_ . '), examples of valid values: "rsf:0" "rsf:42"' . "\n";
        failraid();
    }
}


# gather @found from system
foreach (`areca-cli rsf info 2>&1`)
{
    # 1  Raid Set # 000       2  600.0GB    0.0GB    300.0GB         Normal
    if(/^\s*\d+\s+Raid Set # (\d+)\s+.+\s+(\S+)\s*$/) { push(@found, { 'id' => $1, 'status' => $2 }); }
}




# check found against requested
foreach $f (@found)
{
    foreach $r (@requested)
    {
        if($f->{'id'} == $r->{'id'})
        {
            if($f->{'status'} !~ /(Normal)/) { print STDERR 'rsf:' . $f->{'id'} . ' is in status: ' . $f->{'status'} . "\n"; failraid(); }
        }
    }
}


# check requested againt found
foreach $r (@requested)
{
    $matched = 0;
    foreach $f (@found)
    {
        if($f->{'id'} == $r->{'id'}) { $matched = 1; }
    }
    
    if(! $matched) { print STDERR 'rsf:' . $r->{'id'} . ' was requested but not found on the system' . "\n"; failraid(); }
}


# if we are here, all is ok
okraid();




sub okraid
{
    print 'RAIDOK';
    exit(0);
}

sub failraid
{
    print 'FAIL';
    exit(0);
}

