BGLUG Tux Logo

BGLUG - Blue Grass Linux Users Group

Serving all Central Kentucky Linux Users
Daytime Meetings Hosted By: c.v.v.e.
Forums Logo

File system raid check

File System Raid Check - Moose Style!

11/15/07 by David "Moose" Pitts

 
# This checks the status of the raid every 10 minutes, emailing only if the state is something other than "optimal".
*/10 * * * * if [[ `grep state /proc/megaraid/hba0/raiddrives-0-9|awk -Fstate:
{'print $2'} | grep -v optimal` ]] ; then grep state /proc/megaraid/hba0/raiddr
ives-0-9 | grep -v optimal >/tmp/raidcheck; cat /proc/megaraid/hba0/diskdrives-c
h0 >>/tmp/raidcheck; cat /tmp/raidcheck | mail -s "Output of diskdrives on servername" toemailperson@companyname.com; fi;

Let me break it down for you (insert break down music here)

First, you have to find out a couple things about your raid - specifically the location so you know where to look in proc for it. Ours are hba0 in drives 0-9, yours may be different.

Second, that is all one line beginning with the cron line.

 

Explanations

*/10 * * * * <-- runs every 10 minutes in cron

 if [[ stuff ]] then <-- is an conditional - if a value returns then it is true.

`grep state /proc/megaraid/hba0/raiddrives-0-9|awk -Fstate:
{'print $2'} | grep -v optimal`

The `'s makes it a system call. So the output of the system call gets returned, which the if conditional evaluates.

I am using grep to find any lines that contain the text "state" in the location of my status file.

If this returns any, I am passing it through a filter (awk) setting the field delimiter to be "state:", and I print the second field, which is everything past the first field delimiter. I then filter out the output if it contains the word "optimal".

If there are no lines left after the filtering, the if condition fails, and nothing happens. If there are lines left after the filtering, then the condition is true, and whatever follows the "then" is executed.

Which is this:

grep state /proc/megaraid/hba0/raiddr
ives-0-9 | grep -v optimal >/tmp/raidcheck; cat /proc/megaraid/hba0/diskdrives-c
h0 >>/tmp/raidcheck; cat /tmp/raidcheck | mail -s "Output of diskdrives on servername" toemailperson@companyname.com; fi;

(again, it is all one line)

I am doing the exact same greps again and sending the output to a temp file with the output redirector (>). I do this for a couple reasons. First, I'm lazy. Second, I have it in case I need to look at it.

Once I have it saved in the temp file; I cat out the temp file, and send the output of the temp file with the pipe (|) to mail. 

The -s sets the subject line of the e-mail. The output of the cat command is the body of the e-mail. And, of course, it gets sent to the e-mail address listed.

The "fi;" ends the if command.

Try it, you might like it!

You could, if for some reason you didn't have mail loaded, use something like "expect" (why you would not have mail but have expect, I don't know... but) to connect directly to a mail server and send the message. Given the size of the expect file, you would probably make it a shell script and call it.

Here is an example expect script sending mail to say something. Obviously, yo u could change it to reat the content of the temp file from above or have it send some static information:

#!/usr/bin/expect -f
#
# This Expect script was generated by autoexpect on Tue Mar 27 12:07:25 2007
# By David Pitts
set force_conservative 0 
;# set to 1 to force conservative mode even if
;# script wasn't run conservatively originally
if {$force_conservative} {
       set send_slow {1 .1}
       proc send {ignore arg} {
               sleep .1
               exp_send -s -- $arg
       }
}
set timeout -1
spawn telnet mailserver.hostname.com 25
match_max 100000
sleep 1
send -- "helo servername.hostname.com "
send -- "mail from:<usersender@servername.hostname.com> "
send -- "rcpt to:<receiver@hostname.com> "
send -- "DATA "
send -- "Subject: Partition readonly on Pluto "
send -- "FROM: usersender@servername.hostname.com "
send -- "TO: receiver@hostname.com "
send -- " "
send -- "Dear Sirs, "
send -- " "
send -- "It appears that a partition on Pluto is read only. "
send -- "Please take care of this if you would be so kind. "
send -- "Much appreciated, my kind sir and master. "
send -- " "
send -- "usersender@hostname "
send -- ". "
sleep 1
send -- "quit "
expect eof

Basically, this is spawning a telnet to port 25 on the mailserver.hostname.com and sending the texts provided. One send per line, is a return character.

Once done sending, you end the expect with an end of file (eof) and that ends that expect command, which in this case is the telnet command.



Add a comment