check_linux_raid.pl 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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 %resyncing;
  48. my %finish;
  49. my %active;
  50. my %devices;
  51. while(defined $nextdev){
  52. open (MDSTAT, "< /proc/mdstat") or die "Failed to open /proc/mdstat";
  53. my $device = undef;
  54. while(<MDSTAT>) {
  55. if (defined $device) {
  56. if (/(\[[_U]+\])/) {
  57. $status{$device} = $1;
  58. } elsif (/recovery =\s+(.*?)\s/) {
  59. $recovery{$device} = $1;
  60. ($finish{$device}) = /finish=(.*?min)/;
  61. $device=undef;
  62. } elsif (/resync =\s+(.*?)\s/) {
  63. $resyncing{$device} = $1;
  64. ($finish{$device}) = /finish=(.*?min)/;
  65. $device=undef;
  66. } elsif (/^\s*$/) {
  67. $device=undef;
  68. }
  69. } elsif (/^($nextdev)\s*:/) {
  70. $device=$1;
  71. $devices{$device}=$device;
  72. if (/\sactive/) {
  73. $status{$device} = ''; # Shall be filled later if available
  74. $active{$device} = 1;
  75. }
  76. }
  77. }
  78. $nextdev = shift;
  79. }
  80. foreach my $k (sort keys %devices){
  81. if (!exists($status{$k})) {
  82. $msg .= sprintf " %s inactive with no status information.",
  83. $devices{$k};
  84. $code = max_state($code, "CRITICAL");
  85. } elsif ($status{$k} =~ /_/) {
  86. if (defined $recovery{$k}) {
  87. $msg .= sprintf " %s status=%s, recovery=%s, finish=%s.",
  88. $devices{$k}, $status{$k}, $recovery{$k}, $finish{$k};
  89. $code = max_state($code, "WARNING");
  90. } else {
  91. $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  92. $code = max_state($code, "CRITICAL");
  93. }
  94. } elsif ($status{$k} =~ /U+/) {
  95. if (defined $resyncing{$k}) {
  96. $msg .= sprintf " %s status=%s, resync=%s, finish=%s.",
  97. $devices{$k}, $status{$k}, $resyncing{$k}, $finish{$k};
  98. $code = max_state($code, "WARNING");
  99. } else {
  100. $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  101. $code = max_state($code, "OK");
  102. }
  103. } else {
  104. if ($active{$k}) {
  105. $msg .= sprintf " %s active with no status information.",
  106. $devices{$k};
  107. $code = max_state($code, "OK");
  108. } else {
  109. # This should't run anymore, but is left as a catch-all
  110. $msg .= sprintf " %s does not exist.\n", $devices{$k};
  111. $code = max_state($code, "CRITICAL");
  112. }
  113. }
  114. }
  115. print $code, $msg, "\n";
  116. exit ($ERRORS{$code});