--- /usr/lib/nagios/plugins/check_linux_raid	2009-02-01 02:15:01.000000000 +0000
+++ /dev/shm/check_linux_raid.pl	2011-05-10 22:39:26.650710073 +0100
@@ -1,8 +1,8 @@
 #!/usr/bin/perl -w
 
 # Copyright (c) 2002 ISOMEDIA, Inc.
-# originally written by Steve Milton
-# later updates by sean finney <seanius@seanius.net>
+# Written by Steve Milton
+# Released under the GNU Public License
 #
 # This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU General Public License as published by
@@ -18,13 +18,22 @@
 # along with this program; if not, write to the Free Software
 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 #
-# Usage:   check_raid [raid-name]
+# Usage:   check_raid <raid-name>
 # Example: check_raid md0
-#	  WARNING md0 status=[UUU_U], recovery=46.4%, finish=123.0min
+#          WARNING md0 status=[UUU_U], recovery=46.4%, finish=123.0min
 
 use strict;
-use lib "/usr/lib/nagios/plugins";
-use utils qw(%ERRORS);
+use Getopt::Long qw(:config no_ignore_case);
+
+my %ERRORS=('DEPENDENT'=>4,'UNKNOWN'=>3,'OK'=>0,'WARNING'=>1,'CRITICAL'=>2);
+
+my $mdstat = "/proc/mdstat";
+my $verbose = 0;
+
+GetOptions('mdstat=s' => \$mdstat,
+	   'v|verbose+' => \$verbose,
+	   'h|help' => sub { usage();  exit $ERRORS{'UNKNOWN'}; },
+	   'V|version' => sub { version();  exit $ERRORS{'UNKNOWN'}; });
 
 # die with an error if we're not on Linux
 if ($^O ne 'linux') {
@@ -32,77 +41,71 @@
     exit $ERRORS{'UNKNOWN'};
 }
 
-sub max_state($$){
-	my ($a, $b) = @_;
-	if ($a eq "CRITICAL" || $b eq "CRITICAL") { return "CRITICAL"; } 
-	elsif ($a eq "WARNING" || $b eq "WARNING") { return "WARNING"; }
-	elsif ($a eq "OK" || $b eq "OK") { return "OK"; }
-	elsif ($a eq "UNKNOWN" || $b eq "UNKNOWN") { return "UNKNOWN"; }
-	elsif ($a eq "DEPENDENT" || $b eq "DEPENDENT") { return "DEPENDENT"; }
-	return "UNKNOWN";
-}
-
-my $nextdev;
-if(defined $ARGV[0]) { $nextdev = shift; }
-else { $nextdev = "md[0-9]"; }
+my $mddev = shift;
 
-my $code = "UNKNOWN";
-my $msg = "";
-my %status;
-my %recovery;
-my %finish;
-my %active;
-my %devices;
-
-while(defined $nextdev){
-	open (MDSTAT, "< /proc/mdstat") or die "Failed to open /proc/mdstat";
-	my $device = undef;
-	while(<MDSTAT>) {
-		if (defined $device) {
-			if (/(\[[_U]+\])/) {
-				$status{$device} = $1;
-			} elsif (/recovery = (.*?)\s/) {  
-				$recovery{$device} = $1;
-				($finish{$device}) = /finish=(.*?min)/;
-			} elsif (/^\s*$/) {
-				$device=undef;
-			}
-		} elsif (/^($nextdev)\s*:/) {
-			$device=$1;
-			$devices{$device}=$device;
-			if (/active/) {
-				$active{$device} = 1;
-			}
-		}
-	}
-	$nextdev = shift;
+open (MDSTAT, "<$mdstat") or die "Failed to open $mdstat";
+my $found = 0;
+my $status = "";
+my $recovery = "";
+my $finish = "";
+my $speed = "";
+my $active = "";
+while(<MDSTAT>) {
+    chomp;
+    if ($found) {
+        if (/(\[[_U]+\])/) {
+            $status = $1;
+        } elsif (/recovery *= *([^ ]+)/) {
+            $recovery = $1;
+            ($finish) = /finish= *([^ ]+)/;
+            ($speed) = /speed= *([^ ]+)/;
+        } elsif (/^[^ \t]/) {
+            last;
+        }
+    } else {
+        if (/^$mddev\s*:/) {
+            $found = 1;
+            if (/active/) {
+                $active = 1;
+            }
+        }
+    }
 }
 
-foreach my $k (sort keys %devices){
-	if ($status{$k} =~ /_/) {
-		if (defined $recovery{$k}) {
-			$msg .= sprintf " %s status=%s, recovery=%s, finish=%s.",
-				$devices{$k}, $status{$k}, $recovery{$k}, $finish{$k};
-			$code = max_state($code, "WARNING");
-		} else {
-			$msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
-			$code = max_state($code, "CRITICAL");
-		}
-	} elsif ($status{$k} =~ /U+/) {
-		$msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
-		$code = max_state($code, "OK");
-	} else {
-		if ($active{$k}) {
-			$msg .= sprintf " %s active with no status information.\n",
-				$devices{$k};
-			$code = max_state($code, "OK");
-		} else {
-			$msg .= sprintf " %s does not exist.\n", $devices{$k};
-			$code = max_state($code, "CRITICAL");
-		}
-	}
+my $msg = "FAILURE";
+my $code = "UNKNOWN";
+if ($status =~ /_/) {
+    if ($recovery) {
+        $msg = "$mddev status=$status, recovery=$recovery, finish=$finish, speed=$speed",
+        $code = "WARNING";
+    } else {
+        $msg = "$mddev status=$status";
+        $code = "CRITICAL";
+    }
+} elsif ($status =~ /U+/) {
+    $msg = "$mddev status=$status";
+    $code = "OK";
+} else {
+    if ($active) {
+        $msg = "$mddev active with no status information";
+        $code = "OK";
+    } else {
+        $msg = "$mddev does not exist";
+        $code = "CRITICAL";
+    }
 }
 
-print $code, $msg, "\n";
+print "RAID $code: $msg\n";
 exit ($ERRORS{$code});
 
+sub usage {
+    print qq{usage:
+    check_linux_raid.pl [--mdstat=</proc/mdstat>] <md-device>
+e.g.
+    check_linux_raid.pl md0
+};
+}
+
+sub version {
+    print "check_linux_raid.pl 1.1\n";
+}
