1
0

check_linux_raid 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. #!/usr/bin/perl
  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/lib/nagios/plugins";
  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. my $TIMEOUT = 30;
  32. $SIG{ALRM} = sub { print "ERROR: No response\n"; exit 3; };
  33. alarm($TIMEOUT);
  34. sub max_state($$){
  35. my ($a, $b) = @_;
  36. if ($a eq "CRITICAL" || $b eq "CRITICAL") { return "CRITICAL"; }
  37. elsif ($a eq "WARNING" || $b eq "WARNING") { return "WARNING"; }
  38. elsif ($a eq "OK" || $b eq "OK") { return "OK"; }
  39. elsif ($a eq "UNKNOWN" || $b eq "UNKNOWN") { return "UNKNOWN"; }
  40. elsif ($a eq "DEPENDENT" || $b eq "DEPENDENT") { return "DEPENDENT"; }
  41. return "UNKNOWN";
  42. }
  43. my $nextdev;
  44. if(defined $ARGV[0]) { $nextdev = shift; }
  45. else { $nextdev = "md[0-9]+"; }
  46. my $code = "UNKNOWN";
  47. my $msg = "";
  48. my %status;
  49. my %recovery;
  50. my %resyncing;
  51. my %finish;
  52. my %active;
  53. my %devices;
  54. while(defined $nextdev){
  55. open (MDSTAT, "< /proc/mdstat") or die "Failed to open /proc/mdstat";
  56. my $device = undef;
  57. while(<MDSTAT>) {
  58. if (defined $device) {
  59. if (/(\[[_U]+\])/) {
  60. $status{$device} = $1;
  61. } elsif (/recovery =\s+(.*?)\s/) {
  62. $recovery{$device} = $1;
  63. ($finish{$device}) = /finish=(.*?min)/;
  64. $device=undef;
  65. } elsif (/resync =\s+(.*?)\s/) {
  66. $resyncing{$device} = $1;
  67. ($finish{$device}) = /finish=(.*?min)/;
  68. $device=undef;
  69. } elsif (/^\s*$/) {
  70. $device=undef;
  71. }
  72. } elsif (/^($nextdev)\s*:/) {
  73. $device=$1;
  74. $devices{$device}=$device;
  75. if (/\sactive/) {
  76. $status{$device} = ''; # Shall be filled later if available
  77. $active{$device} = 1;
  78. }
  79. }
  80. }
  81. $nextdev = shift;
  82. }
  83. foreach my $k (sort keys %devices){
  84. if (!exists($status{$k})) {
  85. $msg .= sprintf " %s inactive with no status information.",
  86. $devices{$k};
  87. $code = max_state($code, "CRITICAL");
  88. } elsif ($status{$k} =~ /_/) {
  89. if (defined $recovery{$k}) {
  90. $msg .= sprintf " %s status=%s, recovery=%s, finish=%s.",
  91. $devices{$k}, $status{$k}, $recovery{$k}, $finish{$k};
  92. $code = max_state($code, "WARNING");
  93. } else {
  94. $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  95. $code = max_state($code, "CRITICAL");
  96. }
  97. } elsif ($status{$k} =~ /U+/) {
  98. if (defined $resyncing{$k}) {
  99. $msg .= sprintf " %s status=%s, resync=%s, finish=%s.",
  100. $devices{$k}, $status{$k}, $resyncing{$k}, $finish{$k};
  101. $code = max_state($code, "WARNING");
  102. } else {
  103. $msg .= sprintf " %s status=%s.", $devices{$k}, $status{$k};
  104. $code = max_state($code, "OK");
  105. }
  106. } else {
  107. if ($active{$k}) {
  108. $msg .= sprintf " %s active with no status information.",
  109. $devices{$k};
  110. $code = max_state($code, "OK");
  111. } else {
  112. # This should't run anymore, but is left as a catch-all
  113. $msg .= sprintf " %s does not exist.\n", $devices{$k};
  114. $code = max_state($code, "CRITICAL");
  115. }
  116. }
  117. }
  118. print $code, $msg, "\n";
  119. exit ($ERRORS{$code});