.Net ramblings
# Wednesday, 18 May 2005
HowTo: set up disk status monitoring (i.e. for a raid array) and send results by email

background

i have a raid 1 array on my server, and i was surprised to see no easy solution to setting up an email alert if one of the disks should fail.  luckily it has never happened but apparently windows doesn't even pop up a task-bar alert if it happens. 

what doesn't work

i tried the Windows performance monitoring and alerts, and while it can tell you the average read/writes per second, it can't tell you how many disks are online.  so i looked into WMI which has an API for hard disks, but i encountered a problem with the API not returning the information it is supposed to for disk status.

enter DiskPart.exe

then i discovered DiskPart.exe, a powerful disk management utility bundled with windows server 2003, i think you can get it for server 2000 too with an admin pack or something. 
you can run DiskPart.exe from the command prompt, type: "Select Disk 0" then hit return, next type "Detail Disk" and hit return.  you should see a list of all the volumes on the disk, along with the disk status "Healthy".
In the case of a raid array, i found it more useful to run the following commands:

select disk 0
select volume 0
detail volume

this outputs all the disks that make up the volume (Disk 0 and 1, in my case), and their status. 
Fortunately, you can pipe the output of DiskPart to a text file, and you can also tell DiskPart to run a list of commands from a script, so the whole thing can be automated as a scheduled task in Windows.  I also wrote a simple c# console app to send the contents of the output file in an email.

The .cmd file i have scheduled contains the following lines:

@diskpart /s c:\Scripts\disk_part_commands.txt > c:\scripts\disk_status.txt
@SendMail server@whatever.ie tim@whatever.ie "Server Disk Status" c:\scripts\disk_status.txt

The SendMail program takes in the following parameters: from address, to address, email subject, text file path.  it is hard coded to use the localhost mail server which you can change if you want in the source code (.Net C#).

Download the send mail console app: SendMail.exe (16 KB)

Download the send mail source code: SendMail.txt (1.26 KB)

Taking it further

if i was really serious about it, i would parse the text file and read in the status directly, and only send the email if the status is something other than 'Healthy', but i think it's nice to get an email once a week anyway from the server, reporting it's disk status and letting you know it is still alive.  if somebody does write a little program to parse the output, you could post it here as a comment, that would be great.

i run the script every week, so in the worst case it could operate for 7 days with a dead drive, and the chance is very slim that the other drive will fail within this time.


Wednesday, 18 May 2005 18:39:12 (GMT Daylight Time, UTC+01:00)  #    Comments [2]  Windows Server