#!/usr/bin/perl

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


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


# gather @found from system
foreach (`mpt-status -s 2>&1`)
{
    #log_id 0 OPTIMAL
    #phys_id 1 ONLINE
    if(/^(log|phys)_id\s+(\d+)\s+(\S+)\s*$/) { push(@found, { 'type' => $1, 'id' => $2, 'status' => $3 }); }
}




# check found against requested
foreach $f (@found)
{
    foreach $r (@requested)
    {
        if($f->{'type'} eq $r->{'type'} && $f->{'id'} eq $r->{'id'})
        {
            if($f->{'status'} !~ /(OPTIMAL|ONLINE)/) { print STDERR $f->{'type'} . ':' . $f->{'id'} . ' is in status: ' . $f->{'status'} . "\n"; failraid(); }
        }
    }
}


# check requested againt found
foreach $r (@requested)
{
    $matched = 0;
    foreach $f (@found)
    {
        if($f->{'type'} eq $r->{'type'} && $f->{'id'} eq $r->{'id'}) { $matched = 1; }
    }
    
    if(! $matched) { print STDERR $r->{'type'} . ':' . $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);
}
