check_linux_raid.pl 3.1 KB

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