diff options
Diffstat (limited to 'web/attachments/51758-check_softraid.pl')
| -rw-r--r-- | web/attachments/51758-check_softraid.pl | 176 |
1 files changed, 176 insertions, 0 deletions
diff --git a/web/attachments/51758-check_softraid.pl b/web/attachments/51758-check_softraid.pl new file mode 100644 index 0000000..22652a0 --- /dev/null +++ b/web/attachments/51758-check_softraid.pl | |||
| @@ -0,0 +1,176 @@ | |||
| 1 | #!/usr/bin/perl -w | ||
| 2 | # | ||
| 3 | # check_softraid.pl - Nagios plugin to check software RAID status on Linux | ||
| 4 | # | ||
| 5 | # | ||
| 6 | # Copyright (C) 2003 Kenny Root | ||
| 7 | # | ||
| 8 | # This program is free software; you can redistribute it and/or | ||
| 9 | # modify it under the terms of the GNU General Public License | ||
| 10 | # as published by the Free Software Foundation; either version 2 | ||
| 11 | # of the License, or (at your option) any later version. | ||
| 12 | # | ||
| 13 | # This program is distributed in the hope that it will be useful, | ||
| 14 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 15 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 16 | # GNU General Public License for more details. | ||
| 17 | # | ||
| 18 | # You should have received a copy of the GNU General Public License | ||
| 19 | # along with this program; if not, write to the Free Software | ||
| 20 | # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. | ||
| 21 | # | ||
| 22 | # | ||
| 23 | # Report bugs to: kenny@the-b.org, nagiosplug-help@lists.sf.net | ||
| 24 | # | ||
| 25 | # 30-05-2003 Version 0.2 | ||
| 26 | # | ||
| 27 | # | ||
| 28 | |||
| 29 | use strict; | ||
| 30 | use Getopt::Long; | ||
| 31 | use lib "nagios/plugins"; | ||
| 32 | use utils qw($TIMEOUT %ERRORS &print_revision &support); | ||
| 33 | use vars qw($PROGNAME $MDSTAT $VERSION $opt_h $opt_V $exitstatus $exitstring); | ||
| 34 | Getopt::Long::Configure('bundling'); | ||
| 35 | |||
| 36 | $PROGNAME="check_softraid"; | ||
| 37 | $VERSION="0.2"; | ||
| 38 | $MDSTAT="/proc/mdstat"; | ||
| 39 | |||
| 40 | # Paranoia: clear out our environment variables. | ||
| 41 | $ENV{'PATH'} = ''; | ||
| 42 | $ENV{'BASH_ENV'} = ''; | ||
| 43 | $ENV{'ENV'} = ''; | ||
| 44 | |||
| 45 | # Set the exit status to 0 (OK) until proven otherwise. | ||
| 46 | $exitstatus = $ERRORS{'OK'}; | ||
| 47 | $exitstring = ''; | ||
| 48 | |||
| 49 | # Create a timeout in case we can't read the mdstat file. | ||
| 50 | $SIG{'ALRM'} = sub { | ||
| 51 | print ("ERROR: Cannot read $MDSTAT\n"); | ||
| 52 | exit $ERRORS{'UNKNOWN'}; | ||
| 53 | }; | ||
| 54 | alarm($TIMEOUT); | ||
| 55 | |||
| 56 | GetOptions("V" => \$opt_V, "version" => \$opt_V, | ||
| 57 | "h" => \$opt_h, "help" => \$opt_h); | ||
| 58 | |||
| 59 | # Print out the program version if requested. | ||
| 60 | if ($opt_V) { | ||
| 61 | print_revision($PROGNAME,$VERSION); | ||
| 62 | exit $ERRORS{'OK'}; | ||
| 63 | } | ||
| 64 | |||
| 65 | # Print usage information if requested. | ||
| 66 | if ($opt_h) { | ||
| 67 | print_help(); | ||
| 68 | exit $ERRORS{'OK'}; | ||
| 69 | } | ||
| 70 | |||
| 71 | # Don't be tricked into opening a symlink. | ||
| 72 | if (! -f $MDSTAT) { | ||
| 73 | print "$MDSTAT is not a regular file!\n"; | ||
| 74 | exit $ERRORS{'UNKNOWN'}; | ||
| 75 | } | ||
| 76 | |||
| 77 | open(MDSTAT, "< $MDSTAT"); | ||
| 78 | |||
| 79 | # Initialize the hash that will hold all the information about the status | ||
| 80 | # of each RAID device. | ||
| 81 | my %raid = (); | ||
| 82 | |||
| 83 | while (readline(*MDSTAT)) { | ||
| 84 | chomp; | ||
| 85 | |||
| 86 | # Parse each line in the mdstat. Each RAID device should have three | ||
| 87 | # lines. One for the personality and disk status, the second for | ||
| 88 | # the RAID personality's status. The third line is for things like | ||
| 89 | # resync. | ||
| 90 | # md1 : active raid1 hda2[1] hdb2[2] | ||
| 91 | # 82348723 blocks [2/2] [UU] | ||
| 92 | # | ||
| 93 | if ($_ =~ /^(md[0-9]*) : ([^ ]*) ([^ ]*)( \(read-only\))? ([^\[]*\[.*)/) { | ||
| 94 | my $device = $1; | ||
| 95 | $raid{$device}{status} = $2; | ||
| 96 | $raid{$device}{personality} = $3; | ||
| 97 | my $diskstring = $5; | ||
| 98 | |||
| 99 | # Split the disk status string into each disk, record those | ||
| 100 | # into an array for possible parsing during the RAID status | ||
| 101 | # line. Also record if they're flagged as faulty disks. | ||
| 102 | my @raiddisks = split(/ /, $diskstring); | ||
| 103 | for my $eachdisknum (0 .. $#raiddisks) { | ||
| 104 | |||
| 105 | # Each disk status would like like "hda2[1]" or | ||
| 106 | # "hda2[1](F)" for a faulty disk. | ||
| 107 | if ($raiddisks[$eachdisknum] =~ /([^\[]*)\[([0-9]*)\](\(F\))?$/) { | ||
| 108 | my $diskname = $1; | ||
| 109 | push @{$raid{$device}{disks}}, $diskname; | ||
| 110 | |||
| 111 | # Record if this device is marked as faulted. | ||
| 112 | if (defined $3 and ($3 eq "(F)")) { | ||
| 113 | push @{$raid{$device}{faulted}}, $diskname; | ||
| 114 | } | ||
| 115 | } | ||
| 116 | } | ||
| 117 | |||
| 118 | # If the RAID device is active, the RAID personality should | ||
| 119 | # have a status message as the second line. Let's parse that. | ||
| 120 | if ($raid{$device}{status} eq "active") { | ||
| 121 | my $syncstatusline = <MDSTAT>; | ||
| 122 | chomp $syncstatusline; | ||
| 123 | |||
| 124 | # RAID1 and RAID5 personalities have a status line that | ||
| 125 | # tells you whether you're out of sync or not. Parse | ||
| 126 | # that. | ||
| 127 | if ($syncstatusline =~ / \[([0-9]*)\/([0-9]*)\] \[([^\]]*)\]/) { | ||
| 128 | # Record the number of total disks, number of | ||
| 129 | # disks operational, and compute the current | ||
| 130 | # operational capacity. | ||
| 131 | $raid{$device}{totaldisks} = $1; | ||
| 132 | $raid{$device}{operationaldisks} = $2; | ||
| 133 | $raid{$device}{capacity} = ($2 * 100) / $1; | ||
| 134 | |||
| 135 | # Each operational disk is listed as "U" | ||
| 136 | # while each out-of-sync disk is listed as "_" | ||
| 137 | my $diskstatusstring = $3; | ||
| 138 | my @diskstatuses = split //, $diskstatusstring; | ||
| 139 | foreach my $disknumber (0 .. $#diskstatuses) { | ||
| 140 | if (substr($diskstatusstring, $disknumber, 1) eq "_") { | ||
| 141 | push @{$raid{$device}{desync}}, @{$raid{$device}{disks}}[$disknumber]; | ||
| 142 | } | ||
| 143 | } | ||
| 144 | } | ||
| 145 | } | ||
| 146 | } | ||
| 147 | } | ||
| 148 | close(MDSTAT); | ||
| 149 | |||
| 150 | foreach my $device (sort keys %raid) { | ||
| 151 | if ($raid{$device}{status} eq "active") { | ||
| 152 | if (exists $raid{$device}{faulted}) { | ||
| 153 | $exitstring = $exitstring . "$device has faulted disks " . join(" ", @{$raid{$device}{faulted}}) . ". "; | ||
| 154 | $exitstatus = $ERRORS{'CRITICAL'}; | ||
| 155 | } elsif (exists $raid{$device}{desync}) { | ||
| 156 | $exitstring = $exitstring . "$device has desynced disks " . join(" ", @{$raid{$device}{desync}}) . ". "; | ||
| 157 | $exitstatus = $ERRORS{'WARNING'} if ($exitstatus != $ERRORS{'CRITICAL'}); | ||
| 158 | } | ||
| 159 | } | ||
| 160 | } | ||
| 161 | |||
| 162 | if ($exitstring eq "") { | ||
| 163 | $exitstring = "All RAID disks OK."; | ||
| 164 | } | ||
| 165 | |||
| 166 | print $exitstring . "\n"; | ||
| 167 | exit $exitstatus; | ||
| 168 | |||
| 169 | sub print_help { | ||
| 170 | printf "$PROGNAME plugin for Nagios monitors Linux's software RAID \n"; | ||
| 171 | printf "status for the local machine.\n"; | ||
| 172 | printf "\nUsage:\n"; | ||
| 173 | printf "\t-V (--version)\tPlugin version\n"; | ||
| 174 | printf "\t-h (--help)\tThis usage help.\n\n"; | ||
| 175 | print_revision($PROGNAME, $VERSION); | ||
| 176 | } | ||
