#!/usr/bin/perl
#
use strict;

my %ERRORS=('DEPENDENT'=>4,'UNKNOWN'=>3,'OK'=>0,'WARNING'=>1,'CRITICAL'=>2);

my @arrays = ("md0" , "md1" , "md2" , "md3" , "md4" , "md5");
my @states = ();

my $file = "/proc/mdstat";
my $found = 0;
my $comment = "";
my $name;
my $state;
my $disk;
my $critical;
my $notif = "OK";

open(FILE , "$file") || die("Can't open $file for reading\n");

while(<FILE>)
{
        chomp;
        if($found)
        {
                if(/\[U+\]/)
                {
                        $state = "OK";
			push(@states , $state);
                }
                else
                {
                        $state = "CRITICAL";
			push(@states , $state);
                }
		$comment .= "$name=$state ";		
		#print("DEBUG: $comment\n");
                $state = "";
		$found = 0;
                $name = "";
        }

        foreach $disk (@arrays)
        {
                if(/$disk/)
                {
                        $name = $disk;
                        $found = 1;
                }
        }
}


foreach $state (@states)
{
	if($state =~ "CRITICAL")
	{
		$critical = 1;
		$notif = "CRITICAL";
	}
}

if($critical)
{
	print "$notif"," - ",$comment,"\n";
}
else
{	
	print "$notif"," - ",$comment,"\n";
}

exit($ERRORS{$notif});
