check_linux_raid.pl 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. #!/usr/bin/perl -w
  2. # Copyright (c) 2002 ISOMEDIA, Inc.
  3. # Written by Steve Milton
  4. # Released under the GNU Public License
  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. my %ERRORS=('DEPENDENT'=>4,'UNKNOWN'=>3,'OK'=>0,'WARNING'=>1,'CRITICAL'=>2);
  25. # die with an error if we're not on Linux
  26. if ($^O ne 'linux') {
  27. print "This plugin only applicable on Linux.\n";
  28. exit $ERRORS{'UNKNOWN'};
  29. }
  30. open (MDSTAT, "</proc/mdstat") or die "Failed to open /proc/mdstat";
  31. my $found = 0;
  32. my $status = "";
  33. my $recovery = "";
  34. my $finish = "";
  35. my $active = "";
  36. while(<MDSTAT>) {
  37. if ($found) {
  38. if (/(\[[_U]+\])/) {
  39. $status = $1;
  40. last;
  41. } elsif (/recovery = (.*?)\s/) {
  42. $recovery = $1;
  43. ($finish) = /finish=(.*?min)/;
  44. last;
  45. }
  46. } else {
  47. if (/^$ARGV[0]\s*:/) {
  48. $found = 1;
  49. if (/active/) {
  50. $active = 1;
  51. }
  52. }
  53. }
  54. }
  55. my $msg = "FAILURE";
  56. my $code = "UNKNOWN";
  57. if ($status =~ /_/) {
  58. if ($recovery) {
  59. $msg = sprintf "%s status=%s, recovery=%s, finish=%s\n",
  60. $ARGV[0], $status, $recovery, $finish;
  61. $code = "WARNING";
  62. } else {
  63. $msg = sprintf "%s status=%s\n", $ARGV[0], $status;
  64. $code = "CRITICAL";
  65. }
  66. } elsif ($status =~ /U+/) {
  67. $msg = sprintf "%s status=%s\n", $ARGV[0], $status;
  68. $code = "OK";
  69. } else {
  70. if ($active) {
  71. $msg = sprintf "%s active with no status information.\n",
  72. $ARGV[0];
  73. $code = "OK";
  74. } else {
  75. $msg = sprintf "%s does not exist.\n", $ARGV[0];
  76. $code = "CRITICAL";
  77. }
  78. }
  79. print $code, " ", $msg;
  80. exit ($ERRORS{$code});