Guest User

check_linux_raid

a guest
Apr 24th, 2010
119
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #!/usr/bin/perl
  2. # nagios: -epn
  3.  
  4. # Copyright (c) 2002 ISOMEDIA, Inc.
  5. # originally written by Steve Milton
  6. # later updates by sean finney <seanius@seanius.net>
  7. #
  8. # This program is free software; you can redistribute it and/or modify
  9. # it under the terms of the GNU General Public License as published by
  10. # the Free Software Foundation; either version 2 of the License, or
  11. # (at your option) any later version.
  12. #
  13. # This program is distributed in the hope that it will be useful,
  14. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16. # GNU General Public License for more details.
  17. #
  18. # You should have received a copy of the GNU General Public License
  19. # along with this program; if not, write to the Free Software
  20. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
  21. #
  22. # Usage:   check_raid [raid-name]
  23. # Example: check_raid md0
  24. #     WARNING md0 status=[UUU_U], recovery=46.4%, finish=123.0min
  25.  
  26. use strict;
  27. use lib "/usr/lib/nagios/plugins";
  28. use utils qw(%ERRORS);
  29.  
  30. # die with an error if we're not on Linux
  31. if ($^O ne 'linux') {
  32.     print "This plugin only applicable on Linux.\n";
  33.     exit $ERRORS{'UNKNOWN'};
  34. }
  35.  
  36. sub max_state($$){
  37.     my ($a, $b) = @_;
  38.     if ($a eq "CRITICAL" || $b eq "CRITICAL") { return "CRITICAL"; }
  39.     elsif ($a eq "WARNING" || $b eq "WARNING") { return "WARNING"; }
  40.     elsif ($a eq "OK" || $b eq "OK") { return "OK"; }
  41.     elsif ($a eq "UNKNOWN" || $b eq "UNKNOWN") { return "UNKNOWN"; }
  42.     elsif ($a eq "DEPENDENT" || $b eq "DEPENDENT") { return "DEPENDENT"; }
  43.     return "UNKNOWN";
  44. }
  45.  
  46. my $nextdev;
  47. if(defined $ARGV[0]) { $nextdev = shift; }
  48. else { $nextdev = "md[0-9]+"; }
  49.  
  50. my $code = "UNKNOWN";
  51. my $msg = "";
  52. my %status;
  53. my %recovery;
  54. my %finish;
  55. my %active;
  56. my %devices;
  57.  
  58. while(defined $nextdev){
  59.     open (MDSTAT, "< /proc/mdstat") or die "Failed to open /proc/mdstat";
  60.     my $device = undef;
  61.     while(<MDSTAT>) {
  62.         if (defined $device) {
  63.             if (/(\[[_U]+\])/) {
  64.                 $status{$device} = $1;
  65.             } elsif (/recovery = (.*?)\s/) {  
  66.                 $recovery{$device} = $1;
  67.                 ($finish{$device}) = /finish=(.*?min)/;
  68.             } elsif (/^\s*$/) {
  69.                 $device=undef;
  70.             }
  71.         } elsif (/^($nextdev)\s*:/) {
  72.             $device=$1;
  73.             $devices{$device}=$device;
  74.             if (/active/) {
  75.                 $active{$device} = 1;
  76.             }
  77.         }
  78.     }
  79.     $nextdev = shift;
  80. }
  81.  
  82. foreach my $k (sort keys %devices){
  83.         next unless (defined($status{$k}));
  84.     if ($status{$k} =~ /_/) {
  85.         if (defined $recovery{$k}) {
  86.             $msg .= sprintf " %s status=%s, recovery=%s, finish=%s.",
  87.                 $devices{$k}, $status{$k}, $recovery{$k}, $finish{$k};
  88.             $code = max_state($code, "WARNING");
  89.         } else {
  90.             $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  91.             $code = max_state($code, "CRITICAL");
  92.         }
  93.     } elsif ($status{$k} =~ /U+/) {
  94.         $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  95.         $code = max_state($code, "OK");
  96.     } else {
  97.         if ($active{$k}) {
  98.             $msg .= sprintf " %s active with no status information.\n",
  99.                 $devices{$k};
  100.             $code = max_state($code, "OK");
  101.         } else {
  102.             $msg .= sprintf " %s does not exist.\n", $devices{$k};
  103.             $code = max_state($code, "CRITICAL");
  104.         }
  105.     }
  106. }
  107.  
  108. print $code, $msg, "\n";
  109. exit ($ERRORS{$code});
RAW Paste Data