Bash script to check Raid status

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Bash script to check Raid status

Post 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
 
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Bash script to check Raid status

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Bash script to check Raid status

Post 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>
Last edited by VladSun on Tue Jul 29, 2008 5:07 pm, edited 1 time in total.
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Bash script to check Raid status

Post 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
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Bash script to check Raid status

Post 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"
There are 10 types of people in this world, those who understand binary and those who don't
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Bash script to check Raid status

Post by jaoudestudios »

Thanks, thats brilliant.

It seems to hang on sending the email? Like its waiting for something.
User avatar
VladSun
DevNet Master
Posts: 4313
Joined: Wed Jun 27, 2007 9:44 am
Location: Sofia, Bulgaria

Re: Bash script to check Raid status

Post 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
There are 10 types of people in this world, those who understand binary and those who don't
facets
Forum Contributor
Posts: 273
Joined: Wed Apr 13, 2005 1:53 am
Location: Detroit

Re: Bash script to check Raid status

Post 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.
User avatar
jaoudestudios
DevNet Resident
Posts: 1483
Joined: Wed Jun 18, 2008 8:32 am
Location: Surrey

Re: Bash script to check Raid status

Post by jaoudestudios »

Really, it never came up in any of my searches. I will take a look, thanks :)
Post Reply