Sunday, March 16, 2008

I can't do it Captain, I just don't have the power!

Have you ever wished that your server would tell you when it's about to shut off due to a power outage? Well, it's easier than you think (well, with some help from a co-worker)...

Simply get a UPS, I particularly like the ones from APC, and configure the alerts in the power management control panel application.


As you can see I have two separate VBS scripts configured, one to go off almost immediately the power goes out, and one to go off just before the server shuts down. Thus I will get an email as the power goes out, and another one as the server chooses to shut itself down because of lack of power.

The scripts are pretty easy.
For Windows Small Business Server with Exchange, the script is easy, because you have a mail server, ready to send mail on your behalf! Just copy and paste these text into a .vbs file:
Dim MyMail
Set MyMail = CreateObject("CDO.Message")
MyMail.From = "administrator@contoso.com"
MyMail.To = "user1@domain.com;4251234567@txt.com"
MyMail.Subject = "Power Outage Shutdown"
MyMail.TextBody = "Power not restored. Server shutting Down."
MyMail.Fields("urn:schemas:mailheader:X-MSMail-Priority") = 2
MyMail.Send
Set MyMail = Nothing

As you can see, I've put two email addresses in here. My email, and celluar text email, so I get an alert on my phone. :o)

For Windows Home Server without Exchange, you need to be a little more tricky, since you need to relay the message through your ISPs mail server. So the script is a little longer:

Const cdoSendUsingMethod = _
"http://schemas.microsoft.com/cdo/configuration/sendusing"
Const cdoSendUsingPort = 2
Const cdoSMTPServer = _
"http://schemas.microsoft.com/cdo/configuration/smtpserver"
Const cdoSMTPServerPort = _
"http://schemas.microsoft.com/cdo/configuration/smtpserverport"
Const cdoSMTPConnectionTimeout = _
"http://schemas.microsoft.com/cdo/configuration/smtpconnectiontimeout"
Const cdoSMTPAuthenticate = _
"http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"
Const cdoBasic = 1
Const cdoSendUserName = _
"http://schemas.microsoft.com/cdo/configuration/sendusername"
Const cdoSendPassword = _
"http://schemas.microsoft.com/cdo/configuration/sendpassword"

Dim objConfig ' As CDO.Configuration
Dim objMessage ' As CDO.Message
Dim Fields ' As ADODB.Fields

' Get a handle on the config object and it's fields
Set objConfig = CreateObject("CDO.Configuration")
Set Fields = objConfig.Fields

' Set config fields we care about
With Fields
.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPServer) = "smtp.isp.net"
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10


.Update
End With

Set objMessage = CreateObject("CDO.Message")

Set objMessage.Configuration = objConfig

With objMessage
.To = "Your Name <user1@domain.com>"
.From = "Home Server <homesrv@contoso.com>"
.Subject = "Power Outtage"
.TextBody = "The Power has gone out @ " & Now()
.Send
End With

Set Fields = Nothing
Set objMessage = Nothing
Set objConfig = Nothing

Simply choose the script that's best for you scenario, and hook it up to the power alerts.

E-Mail when a server shuts itself down.. doesn't get much better then that!

6 comments:

Anonymous said...

Hi Sean,

Cool scripts!

Just have a question as i've feeling a bit stupid!

If you like APC UPSes doesn't the PowerChute software do this for you?

Andy

Sean Daniel said...

Most likely, but I like to install as little software on my server as possible.

Anonymous said...

That's good enough for me!

Thanks Sean

Anonymous said...

You'd want to make sure that any switches, cable/DSL modems are on the UPS as well so that the email can travel forth from your LAN!

Anonymous said...

If the second script - using SMTP Auth - is going to work you'll need to add the following (bold) lines:

.Item(cdoSendUsingMethod) = cdoSendUsingPort
.Item(cdoSMTPAuthenticate) = cdoBasic
.Item(cdoSMTPServer) = "smtp.isp.net"
.Item(cdoSMTPServerPort) = 25
.Item(cdoSMTPConnectionTimeout) = 10
.Item(cdoSendUserName) = "username"
.Item(cdoSendPassword) = "password"

Sean Daniel said...

Good call, I totally forgot to put that in the after math.. thanks for posting it as a comment.