Can I arrange an email to be sent when a drive is nearly full?
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:
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)
}