BizTalk Health Email Alerting Part 1: Active Messages

BizTalk Health Email Alerting Part 1: Active Messages

In this multipart serie of blogposts, I will try to give some examples to keep an eye on the monitoring of BizTalk, without having the need to log on to the environment. Emails will be sent to specific users about certain information.
This first part will be about notifying users of existing active messages. These are  messages which are active for a certain duration in the environment, while they should be already processed within this timeframe. Beware, when you have some long running process in your BizTalk environment, these need to be excluded in the WMI call that is being constructed later in this article.
These small scripts consist a little vbscript, using wmi classes and can be hosted in a scheduled task on the BizTalk server(s) for example. I will walk through the vbscript that we set up to perform this task:
First start is defining a number of variables. A timeout of the script itself and some elements that we’ll be using in the script. Some variables are already assigned a value. The “TimeInterval” is the time in minutes that an instance in BizTalk should be active when this informational email is being sent. The email addresses are the ‘To’ and ‘Cc’ addresses to which the emails should be sent.
Option Explicit
Wscript.TimeOut = 30
DimTimeInterval,strEmailAdresses,strEmailAdressesCC,objWMI,objDatetime,objShell,svcInsts,svcInst,strDescription,strCommand
TimeInterval       = 30
strEmailAdresses   = “BizTalkAdministrators@yourcompany.com”
strEmailAdressesCC = “”
The following step is the initialization of the WMI classes to execute queries on the BizTalk environment. The WMI is initialized with the connectionstring to the BizTalk environment and the full CIM datetime (currect datetime) is calculated. The timeinterval (in this case 30 minutes) is deducted from the current timestamp, to get all instances which are older than 30 minutes.
Set objWMI = GetObject(“winmgmts:rootMicrosoftBizTalkServer”)
Set objDatetime = CreateObject(“WbemScripting.SWbemDateTime”)
Set objShell = CreateObject(“WScript.Shell”)
objDatetime.SetVarDate DateAdd(“n”, -TimeInterval, Now)
Next, the query is set up and being executed. IMPORTANT! All service instances with service type id “BB3A1470-F5C4-47C3-B71F-EAABC260FBD0” are being excluded. These are CacheRefresh instances and are instances internally used in the BizTalk core.
Set svcInsts = objWMI.ExecQuery(“SELECT * FROM MSBTS_ServiceInstance WHERE ServiceStatus = 2 AND ServiceTypeId <> ‘{BB3A1470-F5C4-47C3-B71F-EAABC260FBD0}’ AND ActivationTime < ‘”& objDatetime.Value & “‘”)
When the results are loaded into svcInsts, the script will loop over every instance found and construct a description which will later be sent through email. Finally, when the full description is constructed, the ‘SendEmail function is called.
If svcInsts.Count <> 0 Then
 strDescription = “There are “ & svcInsts.Count & ” instance(s) active for more than “& TimeInterval & ” minutes.”& vbCrLf & vbCrLf
 ForEach svcInst InsvcInsts
   strDescription = strDescription & svcInst.ServiceName & “   “ & svcInst.InstanceID & vbCrLf
 Next
 SendEmail(strDescription)
End If
Wscript.Quit
The function ‘SendEmail’ has the simple task to construct a full message which can be sent to the smtp server. The environment of the process executing the script (objEnv) is being loaded and an object is created to store the message (objMessage). As an extra, the mail is given priority “high”. The SMTP server is being read from the previously loaded environment (objEnv).
Function SendEmail (strDescription)
Dim objEnv,objMessage
Set objEnv = objShell.Environment(“Process”)
Set objMessage = CreateObject(“CDO.Message”)
objMessage.From = “thesender@yourcompany.com”
objMessage.To = strEmailAdresses
objMessage.CC = strEmailAdressesCC
objMessage.Subject = “Active Instances take too long to complete”
objMessage.Textbody = vbCrLf & strDescription & vbCrLf & “Please check the environment.” & vbCrLf & vbCrLf
objMessage.Fields(“urn:schemas:mailheader:Importance”) = “High”
objMessage.Fields.Update
objMessage.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/sendusing”) = 2
objMessage.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpauthenticate”) = 2
objMessage.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserver”) = objEnv(“BTS_SMTP_HOST”)
objMessage.Configuration.Fields.Item(“http://schemas.microsoft.com/cdo/configuration/smtpserverport”) = 25
objMessage.Configuration.Fields.Update
objMessage.Send
End Function
Wscript.Quit
This way of working can also be used for other elements to monitor BizTalk. In the next post, I’ll be talking about the same type of script to process and terminate automatically dehydrated instances.
Thanks for reading, if you have any remarks or questions, please leave them in the comments section!

Author: Andrew De Bruyne

No Comments

Post A Comment