'************************************************************************************************** ' MOM Script ' ' Name: Send notification to new alert owner ' ' Function: Sends an e-mail to the owner of an alert when the owner is changed. ' ' Parameters: None. ' ' Notes: Script must be used a response to an event from the following WMI query. ' select * from __InstanceModificationEvent within 60 where TargetInstance ' isa 'msft_alert' and TargetInstance.Owner <> PreviousInstance.Owner ' and TargetInstance.ResolutionState <> 255 ' '************************************************************************************************** '========== Constants Const EVENT_TYPE_SUCCESS = 0 Const EVENT_TYPE_ERROR = 1 Const EVENT_TYPE_WARNING = 2 Const EVENT_TYPE_INFORMATION = 4 Const EVENT_TYPE_AUDITSUCCESS = 8 Const EVENT_TYPE_AUDITFAILURE = 16 '========== Get WMI object strings from event invoking event Set objEvent = ScriptContext.Event Set objPreviousEvent = ParseWMIObject(objEvent.EventParameter(11)) Set objTargetEvent = ParseWMIObject(objEvent.EventParameter(13)) '========== Get configuration data strDatabaseServer = GetMOMDatabaseServer strMOMReturnAddress = GetMOMConfigSetting("EmailSettings","Mailbox",strDatabaseServer) strMOMMailServer = GetMOMConfigSetting("EmailSettings","ServerName",strDatabaseServer) '========== Setup and send mail strOperatorMail = GetOperatorMail(objTargetEvent.Item("OwnerName"),strDatabaseServer) strMailSubject = "MOM alert assigned to " & objTargetEvent.Item("OwnerName") & "." strMailMessage = "The following MOM alert has been assigned to " & objTargetEvent.Item("OwnerName") & "." & _ vbcrlf & vbcrlf & _ "Name: " & objTargetEvent.Item("Name") & VbCrLf & vbCrLf & _ "Description: " & objTargetEvent.Item("Description") & vbcrlf & vbCrLf & _ "Time of First Event: " & ConvertDateFromUTC(objTargetEvent.Item("TimeofFirstEvent")) & vbcrlf & _ "Time of Last Event: " & ConvertDateFromUTC(objTargetEvent.Item("TimeofLastEvent")) & vbCrLf & _ "Previous Owner: " & objPreviousEvent.Item("OwnerName") & vbcrlf & _ "Assigned By: " & objTargetEvent.Item("ModifiedBy") & vbcrlf & _ "Alert GUID: " & objTargetEvent.Item("GUID") & vbcrlf & _ "URL: " & objTargetEvent.Item("URL") SendMail strOperatorMail,strMOMReturnAddress,strMailSubject,strMailMessage,strMOMMailServer '========== Create event reporting script execution. CreateEvent "Notify Alert Owner","Notification",EVENT_TYPE_INFORMATION,100,strEventDetails '************************************************************************************************** ' Function: GetOperatorMail ' Purpose: Gets the e-mail address of the specified operator. ' Notes: Only retrieves e-mail address. Doesn't provide the ability to get pager address. '************************************************************************************************** Function GetOperatorMail(strOperatorName,strMOMDatabaseServer) ScriptContext.Echo "Database Server: " & strMOMDatabaseServer ScriptContext.Echo "OperatorName: " & strOperatorName Set objConnection = CreateObject("ADODB.Connection") Set objRecordset = CreateObject("ADODB.Recordset") objConnection.Open "Provider=SQLOLEDB;Integrated Security=SSPI;Server=" & strMOMDatabaseServer & ";Database=OnePoint" objRecordSet.Open "select * from operator where name = '" & strOperatorName & "'", objConnection ScriptContext.Echo "GetOperatorMail: objRecordset.Fields(""EmailAddress"") =" & objRecordset.Fields("EmailAddress") GetOperatorMail = objRecordset.Fields("EmailAddress") End Function '************************************************************************************************** ' Function: SendMail ' Purpose: Sends an e-mail ' Arguments: strReceiverName - E-mail address of the recipient. ' strSenderName - E-mail address of the sender. ' strSubject - Text to place in the subject line. ' strTextBody - Text to go in the body of the message. ' strServer - Name of the SMTP server to send mail through. ' ' Function taken from TechNet ScriptCenter. '************************************************************************************************** Sub SendMail(strReceiverName,strSenderName,strSubject,strTextBody,strServer) Set objEmail = CreateObject("CDO.Message") objEmail.From = strSenderName objEmail.To = strReceiverName objEmail.Subject = strSubject objEmail.Textbody = strTextBody objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2 objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = strServer objEmail.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25 objEmail.Configuration.Fields.Update objEmail.Send End Sub '************************************************************************************************** ' Function: GetMOMDatabaseServer ' Purpose: Gets the name of the MOM database server. ' Notes: Must be executed on Management Server. '************************************************************************************************** Function GetMOMDatabaseServer Const HKEY_LOCAL_MACHINE = &H80000002 Set objRegistry = GetObject("winmgmts:\\.\root\default:StdRegProv") strKeyPath = "SOFTWARE\Mission Critical Software\DASServer" strValueName = "DataSource" objRegistry.GetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strDatabaseServer GetMOMDatabaseServer = Split(strDatabaseServer,",")(0) End Function '************************************************************************************************** ' Function: GetMOMConfigSetting ' Purpose: Gets the value of a MOM configuration setting. ' Notes: Requires the name of the category and setting name from the OnePoint configuration table. ' Arguments: strCategory - Category of setting ' strSettingName - Name of setting ' strDatabaseServer - Name of MOM database server '************************************************************************************************** Function GetMOMConfigSetting(strCategory,strSettingName,strDatabaseServer) set objConnection = CreateObject("ADODB.Connection") Set objRecordset = CreateObject("ADODB.Recordset") objConnection.Open "Provider=SQLOLEDB;Server=" & strDatabaseServer & ";Database=OnePoint;Integrated Security=SSPI" objRecordSet.Open "select DataValue from configuration where DataCategory = '" & strCategory & "' and DataName = '" & strSettingName & "'", objConnection GetMOMConfigSetting = objRecordSet.Fields("DataValue") End Function '************************************************************************************************** ' Function: ParseWMIObject ' Purpose: Parses the string from a WMI event and returns a dictionary object with the ' object attributes. ' Notes: Requires the name of the category and setting name from the OnePoint configuration table. ' Arguments: strCategory - Category of setting ' strSettingName - Name of setting ' strDatabaseServer - Name of MOM database server '************************************************************************************************** Function ParseWMIObject(strEventWMIString) 'The function is going to return a dictionary object. Set ParseWMIObject = CreateObject("Scripting.Dictionary") 'Class intClassRef = InStr(strEventWMIString,"instance of ") + Len("instance of ") strClass = Mid(strEventWMIString,intClassRef,InStr(intClassRef,strEventWMIString,Chr(10))-intClassRef) ParseWMIObject.Add "__CLASS",strClass 'Attributes intAttributeStart = InStr(intClassRef,strEventWMIString,"{" & Chr(10) & Chr(9)) + Len("{" & Chr(10) & Chr(9)) strAttributeString = Mid(strEventWMIString,intAttributeStart,InStr(intAttributeStart,strEventWMIString,Chr(10) & "};" & Chr(10)) - intAttributeStart - 1) 'Add attributes to dictionary object. arrAttributes = Split(strAttributeString, ";" & Chr(10) & Chr(9)) For i = 0 To UBound(arrAttributes) intSplitRef = InStr(arrAttributes(i),"=") strAttributeName = Trim(Left(arrAttributes(i),intSplitRef-1)) strAttributeValue = Trim(Right(arrAttributes(i),Len(arrAttributes(i))-intSplitRef)) If Left(strAttributeValue,1) = """" Then strAttributeValue = Mid(strAttributeValue,2,Len(strAttributeValue)-2) ParseWMIObject.Add strAttributeName,strAttributeValue Next End Function '************************************************************************************************** ' Sub: ConvertDateFromUTC ' Purpose: Converts a variant date UTC format to a variant. ' Returns: Variant date '************************************************************************************************** Function ConvertDateFromUTC(strUTCDate) strDateTime = Left(strUTCDate,InStr(strUTCDate,".")) intHourOffset = 0 - Right(strUTCDate,4)/60 strDateString = Mid(strUTCDate,5,2) & "/" & Mid(strUTCDate,7,2) & "/" & Mid(strUTCDate,1,4) strTimeString = Mid(strUTCDate,9,2) & ":" & Mid(strUTCDate,11,2) & ":" & Mid(strUTCDate,13,2) ConvertDateFromUTC = DateAdd("h",intHourOffset, strDateString & " " & strTimeString) End Function '************************************************************************************************** ' Function: CreateEvent ' Purpose: Creates a MOM event. ' Arguments: strSource - Source of event ' strCategory - Category of event ' intType - Integer specifying event type ' intNumber - Event number ' strMessage - Event message '************************************************************************************************** Sub CreateEvent(strSource,strCategory,intType,intNumber,strMessage) Set objEvent = ScriptContext.CreateEvent objEvent.EventSource = strSource objEvent.Category = strCategory objEvent.EventType = intType objEvent.EventNumber = intNumber objEvent.Message = strMessage ScriptContext.Submit objEvent End Sub