Thursday, July 29, 2010

Blog

05

This article will discuss the ability to send emails from within a VBScript. This is extremely handy when wishing to get status messages from automated processes. I recently created a simple VBScript monitoring program for the Kronos WFC product that I maintain, that checks for the existence of a crash log (indicating a significant issue with the system) and emails out alerts when it finds one. These particular crashes occur fairly often, so this process should help me get a head start on fixing them before they affect too many customers (currently there are roughly 23,000 users on this system).

I'll go through the VBScript that I wrote and discuss what the different sections are doing and how I've implemented it in our environment. You should be able to glean some useful information from this example and apply it to your own automation needs.

Here's the compete code to TotalizerDumpCheck.vbs (also attached in a .zip file for your convenience):

   1:  '*** This script checks for the existence of a totalizer dump file on each Kronos WTK server, and if
   2:  '*** any exist, sends out an email alert.
   3:  '***
   4:  '*** M.Kizer (08/27/2008) - Created.
   5:   
   6:  Option Explicit
   7:   
   8:  '* Define Constants / Variables
   9:  Const BinPath = "c:\utilities\"                    'Full path to this VBS script
  10:  Const LogsPath = "c$\kronos\wfc\logs\"                'Full path to the Kronso WTK logs directory
  11:  Const Dump1FileName = "wtktotal1dump.log"            'Dump file name
  12:  Const Dump2FileName = "wtktotal2dump.log"            'Dump file name
  13:  Const EmailFrom = "FromEmail@domain.com"            'Emails will be sent "from" this account
  14:  Const EmailTo = "ToEmail@domain.com"                'Emails will be sent "to" this account
  15:  Dim Item                                            'Used in Servername array
  16:  Dim Msg                                                'Message
  17:   
  18:  Dim CurrentDate                                        'Current date/time
  19:  CurrentDate = Now
  20:   
  21:  Dim ServerName(18)
  22:  ServerName(0) = "cosda256p"
  23:  ServerName(1) = "cosda257p"
  24:  ServerName(2) = "cosda259p"
  25:  ServerName(3) = "cosda260p"
  26:  ServerName(4) = "cosda261p"
  27:  ServerName(5) = "cosda262p"
  28:  ServerName(6) = "cosda263p"
  29:  ServerName(7) = "cosda264p"
  30:  ServerName(8) = "cosda265p"
  31:  ServerName(9) = "cosda266p"
  32:  ServerName(10) = "cosda267p"
  33:  ServerName(11) = "cosda268p"
  34:  ServerName(12) = "cosda270p"
  35:  ServerName(13) = "cosda271p"
  36:  ServerName(14) = "cosda272p"
  37:  ServerName(15) = "cosda253p"
  38:  ServerName(16) = "cosda254p"
  39:  ServerName(17) = "cosda255p"
  40:  ServerName(18) = "cosda269p"
  41:   
  42:  '* Create objects
  43:  Dim WshShell
  44:  Set WshShell = CreateObject("WScript.Shell")
  45:   
  46:  Dim fso
  47:  Set fso = CreateObject("Scripting.FileSystemObject")
  48:   
  49:  Dim objEmail
  50:  Set objEmail = CreateObject("CDO.Message")
  51:   
  52:  '* Create log file
  53:  Dim LogFile
  54:  Set LogFile = fso.CreateTextFile(BinPath & "TotalizerDumpCheck.log", true)
  55:  LogFile.WriteLine("================================================================================")
  56:  LogFile.WriteLine(Now & " - Kronos WTK Totalizer Dump Check - Starting...")
  57:  LogFile.WriteLine("================================================================================")
  58:   
  59:  '* Loop through all servers
  60:  For Each Item in ServerName
  61:      '* Check if dump file 1 exists
  62:      If fso.FileExists("\\" & Item & "\" & LogsPath & Dump1FileName) Then
  63:          Msg = Msg & "Server: " & Item & " has a crashed totalizer - see " & Dump1FileName & vbCrLf
  64:      End If
  65:      '* Check if dump file 2 exists
  66:      If fso.FileExists("\\" & Item & "\" & LogsPath & Dump2FileName) Then
  67:          Msg = Msg & "Server: " & Item & " has a crashed totalizer - see " & Dump2FileName & vbCrLf
  68:      End If
  69:  Next
  70:   
  71:  '* If dump files were found...
  72:  If Msg = "" Then
  73:      LogFile.WriteLine("No Totalizers crashed!")
  74:  Else
  75:      LogFile.WriteLine(Msg)
  76:      LogFile.WriteLine("*** Sending out email alert! ***")
  77:      '* Send out email
  78:      objEmail.From = EmailFrom
  79:      objEmail.To = EmailTo
  80:      objEmail.Subject = "Kronos WTK Production - Totalizer Crash Alert"
  81:      objEmail.Textbody = Msg
  82:      objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
  83:      objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "your SMTP mail server here"
  84:      objEmail.Configuration.Fields.Item ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
  85:      objEmail.Configuration.Fields.Update
  86:      objEmail.Send
  87:  End If
  88:   
  89:  LogFile.WriteLine("================================================================================")
  90:  LogFile.WriteLine(Now & " - Kronos WTK Totalizer Dump Check - Completed.")
  91:  LogFile.WriteLine("================================================================================")
  92:  LogFile.WriteBlankLines 1
  93:   
  94:  '* Close log file
  95:  LogFile.Close
  96:   
  97:  '* Destroy objects
  98:  Set WshShell = Nothing
  99:  Set fso = Nothing
 100:  Set LogFile = Nothing
 101:  Set objEmail = Nothing
 102:   
 103:  '***** End of main script *****


Comments on the code --

 

  • Starting at line 8, I define a bunch of variables and assign some default values. You'll want to put your own email addresses in the correct variables.
  • Starting at line 21 I define an array to contain a list of all of the servers that I wish to check (we have 19 servers for this application, which makes automating tasks like this a real necessity).
  • At line 42 we create some objects to be used throughout the program. Nearly all VBScripts need to have the WScript.shell object defined for many tasks. The FileSystemObject is used for all file handling processes, which I use to check for the existence of a file. The CDO.Message object handles all of the email tasks that we'll use to send out our messages.
  • At line 52 I create a log file for this script. I like to do this especially during development, it makes tracking down pesky errors a bit easier (this is VBScript after all, not a full fledged Visual Studio application where you can use some great debugging tools).
  • Lines 59 - 69 is the bulk of the file existence checking code. Basically, I loop through my array of server names and dynamically check for the existence of two specific dump files. If a file exists, some information is appended to a variable called Msg
  • At line 71, I check to see if anything made it into the Msg variable. If not, then we are pretty much done... just finish up the VBScript's log file, perform a little housekeeping, and exit the program.
  • If there were some dump files found, it is time to send out a message. Lines 77 - 86 is all that is required to send out an email. Be sure to change the text your SMTP mail server here with the name of your SMTP mail server.
  • Lines 89 - 95 finish up and close the VBScript log file.
  • Lines 97 - 101 perform the housekeeping task of destroying the objects we created at the beginning of the program, just to keep memory nice and tidy.

Once I had this program up and running successfully, I staged it on one of the 19 servers for this system and scheduled it to run every 15 minutes during most of the day using Windows Task Scheduler. For the ToEmail address I used a mailing list address so that anyone that wishes to be informed about these alerts can simply subscribe to mailing list. You can even subscribe multiple email addresses to the mailing list, like your cell phone's text message email address for example. By using a mailing list instead of hard coded email addresses, I will never have to update this program to add or remove people from the distribution. If you don't have in-house mailing lists available, a great option is Google Groups (which is what I used). Their response time on populating emails out to the group members is usually very quick. Plus their features and web interface for the mailing list makes it very easy to set one up and maintain it.

Hopefully this article spurs some ideas for automation that will make your life a bit easier. If you come up with any creative ways of using this example, tell me about it in the comments.

Posted in: Development

Post Rating

Comments

Shanaka
# Shanaka
Thursday, July 22, 2010 10:01 PM
itz pretty good

Post Comment

Name (required)

Email (required)

Website

CAPTCHA image
Enter the code shown above: