Saturday, December 31, 2011

Simple email alert for low drive space on local server

Recently, I had a client request a small script to alert them if drivespace got to low on one particular server, this client was running an in house monitor system but it did not have this capability yet.  The script I ended up creating will follow, but I would recommend using a true monitor system before going this route.  This is more of a temporary fix, or if you only have one or two servers.

This article assumes that you have a local SMTP server that will allow the server this is running from to relay emails.

Here's the script
' rename to .vbs
' created by Mike Sweany 11/2/11
' This script is run via scheduled task and will check the D: drive
' If the drive space is below the NotifyBelow value, it will then send an email alert
' This Script requires you to have a local email server that allows non-authenticated email
' to be sent from the server this runs on
'
' Set these variables to match your enviroment
emailTo = "notify@youremail.com"
emailFrom = "admin@domain.local"
emailServer = "smtpServer"
driveLetter = "D:"
serverName = "server01"
' 1073741824 Bytes = 1GB, this is to put the output in readable format
divideBy = 1073741824
' If GB is fewer than this number, send an email notification
NotifyBelow = 10
'
' Don't edit below this line
'
'
'
Set getFSO = CreateObject("Scripting.FileSystemObject")
Set getDrive = getFSO.GetDrive(driveLetter)
getFreeSpace = Round(getDrive.AvailableSpace/divideBy,2)
Set objMessage = CreateObject("CDO.Message")

If getFreeSpace < NotifyBelow Then

 objMessage.Subject = serverName & " Drive Space Alert"
 objMessage.From = emailFrom
 objMessage.To = emailTo
 objMessage.TextBody = "The " & driveLetter & " drive on " & serverName & " is currently at " & getFreeSpace & " GB's"
 '==This section provides the configuration information for the remote SMTP server.
 '==Normally you will only change the server name or IP.
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
 'Name or IP of Remote SMTP Server
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = emailServer
 'Server port (typically 25)
 objMessage.Configuration.Fields.Item _
 ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 objMessage.Configuration.Fields.Update
 '==End remote SMTP server configuration section==

 objMessage.Send

Else
End If

In the top part of the script you have a couple of variables to set.
emailTo = "notify@youremail.com"
emailFrom = "admin@domain.local"
emailServer = "smtpServer"
driveLetter = "D:"
serverName = "server01"
' 1073741824 Bytes = 1GB, this is to put the output in readable format
divideBy = 1073741824
' If GB is fewer than this number, send an email notification
NotifyBelow = 10

These settings will allow you to set who the email is sent to (you can add multiple addresses with a comma between them), who the email is from, the server that will send the email, the server name you are monitoring (the local server), the drive letter and the space that if it goes below, the monitor will alert you.

Copy the above script and save it as alert.vbs, then set it to run as a scheduled task as frequently as you'd like it to check the drive space.  It will only email you if the space is below the threshold you set in "NotifyBelow"

No comments:

Post a Comment