check_linux_raid.pl 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 (/\sactive/) {
  68. $status{$device} = ''; # Shall be filled later if available
  69. $active{$device} = 1;
  70. }
  71. }
  72. }
  73. $nextdev = shift;
  74. }
  75. foreach my $k (sort keys %devices){
  76. if (!exists($status{$k})) {
  77. $msg .= sprintf " %s inactive with no status information.",
  78. $devices{$k};
  79. $code = max_state($code, "CRITICAL");
  80. } elsif ($status{$k} =~ /_/) {
  81. if (defined $recovery{$k}) {
  82. $msg .= sprintf " %s status=%s, recovery=%s, finish=%s.",
  83. $devices{$k}, $status{$k}, $recovery{$k}, $finish{$k};
  84. $code = max_state($code, "WARNING");
  85. } else {
  86. $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  87. $code = max_state($code, "CRITICAL");
  88. }
  89. } elsif ($status{$k} =~ /U+/) {
  90. $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  91. $code = max_state($code, "OK");
  92. } else {
  93. if ($active{$k}) {
  94. $msg .= sprintf " %s active with no status information.",
  95. $devices{$k};
  96. $code = max_state($code, "OK");
  97. } else {
  98. # This should't run anymore, but is left as a catch-all
  99. $msg .= sprintf " %s does not exist.\n", $devices{$k};
  100. $code = max_state($code, "CRITICAL");
  101. }
  102. }
  103. }
  104. print $code, $msg, "\n";
  105. exit ($ERRORS{$code});