Can I arrange an email to be sent when a drive is nearly full?

8,083

Solution 1

While many of the answers include scripting (and if you go that route I'd also suggest powershell) you can also perform alerting using perfmon. See HOW TO: Configure a Low Disk Space Alert by Using the Performance Logs and Alerts Feature in Windows Server 2003

Note that the action you want to take would be to execute a powershell or vbscript to send you an email (or perhaps more preferable, perform some basic cleanup tasks on the drive, then send an email stating what the problem was and what the post action number is)

For the mapped drive you have to use a script. In the WMI counter to use is win32_mappedlogicaldisk. (Get-WmiObject win32_mappedlogicaldisk).freespace. EG:

$mythreshold = 10GB
Get-WmiObject win32_mappedlogicaldisk | select-object deviceid, freespace | foreach { 
    if ($_.freespace -lt $mythreshold){

        $from = "[email protected]" 
        $to = "[email protected]" 
        $subject = "Low Disk Space!" 
        $body = "Free Space Remaining: " + $_.FreeSpace + "Drive" + $_.deviceid 
        $smtpServer = "smtp.mycompany.com" 
        $smtp = new-object Net.Mail.SmtpClient($smtpServer) 
        $smtp.Send($from,$to,$subject,$body) 
    } 
    }

(much of the prior code cheerfully copied from squillman as otherwise I would have had to type in this code myself)

Solution 2

If you're running Server 2003 R2, you have access to the File Server Resource Management tool. This allows creating directory quotas that have notifications attached. You'd be interested in the soft quotas where it doesn't stop new data from being added. You can add notifications to alert you when pre-set threshold are crossed.

If you're on Server 2003 without R2, then you're into the land of external monitoring tools or scripts.

Solution 3

Link to powershell script:

http://powershell.com/cs/cfs-filesystemfile.ashx/__key/CommunityServer.Components.PostAttachments/00.00.00.16.17/diskmonitor1.PS1

Solution 4

This might work for you. If you create a script (Powershell would be my recommendation) that checks the free disk space at run-time and fires off an email if it falls below your threshold, you could create a scheduled task on the server that runs that script. Schedule it for every X minutes and you've got yourself a poor-man's monitoring solution. It is admittedly more prone to errors than other solutions like Nagios or R2's resource manager, but hey...

Your Powershell script could look something like this:

$freeSpaceThreshold = 5GB
$computerName = "mycomputer"
$drive = "C:"

$driveData = Get-WmiObject -class win32_LogicalDisk -computername "$computerName" -filter "Name = '$drive'"

if ($driveData.FreeSpace -lt $freeSpaceThreshold)
{
    $from = "[email protected]"
    $to = "[email protected]"
    $subject = "Low Disk Space!"
    $body = "Free Space Remaining: " + $driveData.FreeSpace
    $smtpServer = "smtp.mycompany.com"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($from,$to,$subject,$body)
}

Solution 5

You can use this PowerShell v2 script to send an email when a drive gets to a certain level.

You could schedule it using "Scheduled Tasks", you will need to change the smtp details at the bottom of the script for your network.

Function DispDisk
{
$server=$server.toupper()
[float]$tempfloat = ($_.freespace/$_.size)*100
$Body= "`r`n$Server - Drive: $($_.Deviceid) has only $([math]::round(($tempfloat),1))% free" 
$Body+="`r`nTotal Size: $([math]::truncate($_.size / 1gb)) GB"
$Body+="`r`nFree Space: $([math]::truncate($_.freespace /1gb)) GB"
IF ($tempfloat -lt $PercenttoEmail)
{
Send-MailMessage -SmtpServer $SMTPServer -To $To -From $From -Subject "Disk Space Issue $Server $($_.Deviceid) is below $percenttoemail%" -Body $Body
}
}

#
# SMTP Settings, you will need to change these.
#

# The Server you want to check, this is the first arguments (example .\getdspace yourserver) would check the YourServer server
$server = $args[0]
# Percent to send an email, if its below this you should get an email
$PercenttoEmail =15
# Your SMTP Server
$SMTPServer="smtprelay.YourDomain.co.nz"
# Who gets the email
$To="[email protected]"
# What address does the email Come From
$From="[email protected]"

get-wmiobject win32_logicaldisk -filter "DriveType=3" -computer $server | foreach-object {DispDisk}
Share:
8,083
Kate Gregory
Author by

Kate Gregory

Consultant, developer, mentor, author, speaker. C++ and .NET, Windows 7, Sharepoint, whatever else I like.

Updated on September 17, 2022

Comments

  • Kate Gregory
    Kate Gregory over 1 year

    Background: I'm a developer who reluctantly "looks after" the machine onto which my application is deployed. My customer is an entirely separate company who pay us to write code for them - we have no bosses in common or anything like that. They have sysadmins but they are in a different department and when they ask for the sorts of things I think sysadmins should be able to do for them, they don't get what they asked for. Making that happen is out of my hands. I then end up being asked to write code to do things that I suspect an actual trained person could set up with a few lines of powershell or by ticking something on a dialog. This is a Windows Server 2003 setup with SQL and IIS installed.

    Which leads to today's problem: how to know the drives are getting full. (Database growth, exported files not cleaned up, that sort of thing.) Ideally an email would go out saying "Drive E is at abc of xyz (84%)." Is that easy to do? I sure don't want to write a service to monitor disk space and send emails - someone must have done this before. One of the drives is actually a mapped drive representing a folder on another machine for what that's worth.

  • Kate Gregory
    Kate Gregory over 13 years
    Sigh. That sounds perfect, but getting it moved to R2 seems like it would meet with the same success as the other requests.
  • Kate Gregory
    Kate Gregory over 13 years
    thanks, I searched but didn't find that (probably because I used the word "full"). If we did something with perfmon alerts would that have a perf impact at all?
  • Matt
    Matt over 13 years
    In my experiences, I've seen no performance issues with using perfmon. I've used perfmon quite a bit, even remotely monitoring numerous parameters and with logging with no obvious impact.