Page 1 of 1

Bash script to check Raid status

Posted: Tue Jul 29, 2008 3:06 pm
by jaoudestudios
I am trying to write a bash script to check the status of my Raid and email me with status ok or status failed. However, I get an error with my if statements. Any ideas?

Code: Select all

 
#!/bin/sh
################
################
# Raid checker
################
#
#
SUBJECT="RAID Status"
EMAIL="eddie@jaoudestudios.com"
CURRENTRAID="$(cat /proc/mdstat)"
SAFERAID="Personalities : [raid1] [raid6] [raid5] [raid4]
md0 : active raid1 sda1[0] sdb1[1]
      104320 blocks [2/2] [UU]
 
md1 : active raid1 sda3[0] sdb3[1]
      155412736 blocks [2/2] [UU]
 
unused devices: <none>"
if ["$CURRENTRAID" = "$SAFERAID"]; then
    # send raid 'ok' email
    /bin/mail -s "$SUBJECT" "$EMAIL" "aaaaaaa"
fi
else
    # send raid 'failed' email
    $(mail eddie@jaoudestudios.com -s "Raid Status:: FAILED"
    Raid FAILED
    Ctrl-d)
fi
 

Re: Bash script to check Raid status

Posted: Tue Jul 29, 2008 4:45 pm
by VladSun

Code: Select all

if [ "$CURRENTRAID" != "$SAFERAID" ]; then
    # send raid 'failed' email
    /bin/mail -s "$SUBJECT" "$EMAIL" "Raid Status:: FAILED"
fi
http://tldp.org/LDP/Bash-Beginners-Guid ... 07_01.html

Re: Bash script to check Raid status

Posted: Tue Jul 29, 2008 4:52 pm
by VladSun
Also, I would advice you to grep for (F) in /proc/mdstat - this means that a device has failed.
E.g:

Code: Select all

Personalities : [raid1] 
read_ahead 1024 sectors
md3 : active raid1 sdb1[1](F) sda1[0]
      104320 blocks [2/2] [U_]
      
md2 : active raid1 sdb2[1](F) sda2[0]
      1020032 blocks [2/2] [U_]
      
md1 : active raid1 sdb3[1](F) sda3[0]
      3068288 blocks [2/1] [U_]
      
md4 : active raid1 sdb4[1](F) sda4[0]
      4096448 blocks [2/1] [U_]
      
unused devices: <none>

Re: Bash script to check Raid status

Posted: Tue Jul 29, 2008 4:55 pm
by jaoudestudios
Thanks.

I have the if statement working. The problem was not with the if statement but with windows line breaks, I had to convert them to linux line breaks.

But the issue I am having now is it thinks the 2 strings are not equal. Even if I SAFERAID on multiple lines or on one single line it things they are not equal. But when I echo them out they are exactly the same.

What do you think the reason could be that the string are not equal yet when I print them to the screen they are exactly the same?

Thanks

Re: Bash script to check Raid status

Posted: Tue Jul 29, 2008 5:04 pm
by VladSun
New lines maybe. :)
Use grep:

Code: Select all

 
cat /proc/mdstat | grep "(F)"
if [ $? -eq 0 ] ; then /bin/mail -s "$SUBJECT" "$EMAIL" "Failure"; fi
 
or even:

Code: Select all

cat /proc/mdstat | grep "(F)" 1>/dev/null && /bin/mail -s "$SUBJECT" "$EMAIL" "Failure"

Re: Bash script to check Raid status

Posted: Wed Jul 30, 2008 6:14 am
by jaoudestudios
Thanks, thats brilliant.

It seems to hang on sending the email? Like its waiting for something.

Re: Bash script to check Raid status

Posted: Wed Jul 30, 2008 6:18 am
by VladSun
Look at the man page of mail command - parameters that are expected, interactive user input, etc. :)
PS: Use pipes :)

E.g.:

Code: Select all

cat /proc/mdstat | mail -s Failure myemail@mail.com

Re: Bash script to check Raid status

Posted: Sat Aug 02, 2008 11:27 pm
by facets
Have you thought about using mdadm to monitor your RAID? It does exactly what you are trying to script.
http://man-wiki.net/index.php/8:mdadm#MONITOR_MODE

Will.

Re: Bash script to check Raid status

Posted: Sun Aug 03, 2008 2:08 am
by jaoudestudios
Really, it never came up in any of my searches. I will take a look, thanks :)