SMS 2003 Monitor SMS Inbox

SMS_2003_Monitor_SMS_Inbox (WriteActionModuleType)

Monitors an SMS Inbox and raises an event when the number of files in it exceeds a threshold

Element properties:

TypeWriteActionModuleType
IsolationAny
AccessibilityInternal
RunAsDefault
InputTypeSystem.BaseData
Comment{03E6FD65-83F1-40F6-A6D7-2E7DE2F13846}

Member Modules:

ID Module Type TypeId RunAs 
RunScriptAction WriteAction System.Mom.BackwardCompatibility.ScriptResponse Default

Overrideable Parameters:

IDParameterTypeSelectorDisplay NameDescription
FileCountThresholdstring$Config/Parameters/FileCountThreshold$FileCountThresholdThe number of files in the inbox that if exceeded will result in an event being raised
InboxDirectoryNamestring$Config/Parameters/InboxDirectoryName$InboxDirectoryNameA directory under SMS\inboxes

Source Code:

<WriteActionModuleType ID="SMS_2003_Monitor_SMS_Inbox" Accessibility="Internal" Comment="{03E6FD65-83F1-40F6-A6D7-2E7DE2F13846}">
<Configuration>
<IncludeSchemaTypes>
<SchemaType>MomBackwardCompatibility!System.Mom.BackwardCompatibility.AlertGenerationSchema</SchemaType>
</IncludeSchemaTypes>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="AlertGeneration" type="AlertGenerationType"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="InvokerType" type="xsd:integer"/>
<xsd:element xmlns:xsd="http://www.w3.org/2001/XMLSchema" name="Parameters" minOccurs="0">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="FileCountThreshold" type="xsd:string" minOccurs="0"/>
<xsd:element name="InboxDirectoryName" type="xsd:string" minOccurs="0"/>
</xsd:sequence>
</xsd:complexType>
</xsd:element>
</Configuration>
<OverrideableParameters>
<OverrideableParameter ID="FileCountThreshold" Selector="$Config/Parameters/FileCountThreshold$" ParameterType="string"/>
<OverrideableParameter ID="InboxDirectoryName" Selector="$Config/Parameters/InboxDirectoryName$" ParameterType="string"/>
</OverrideableParameters>
<ModuleImplementation>
<Composite>
<MemberModules>
<WriteAction ID="RunScriptAction" TypeID="MomBackwardCompatibility!System.Mom.BackwardCompatibility.ScriptResponse">
<AlertGeneration>$Config/AlertGeneration$</AlertGeneration>
<InvokerType>$Config/InvokerType$</InvokerType>
<Body><Script>
'*******************************************************************************
' Script Name - SMS 2003 Monitor SMS Inbox
'
' Purpose - Monitors an SMS Inbox and raises an event when the number of
' files in it exceeds a threshold.
'
' 1720 - An event used to report that an inbox threshold has been
' exceeded.
'
' The following additional events can be raised:
'
' 1100 - An event used only for debugging or tracing.
' 1101 - Script executed successfully.
' 1102 - An error occurred in executing this script.
' 1105 - Accessed denied due to connection failure or permissions.
'
' Assumptions - This script will only run on SMS 2003 Site Servers.
'
' This script does not support agentless mode and will silently
' terminate.
'
' The inbox threshold specified must be greater than 0.
'
' Parameters - InboxDirectoryName A directory under SMS\inboxes.
' FileCountThreshold The number of files in the inbox that if
' exceeded will result in an event being
' raised.
'
' Change Hist - Date Version Description
' -------- --------------- -----------
' 07/14/04 05.0.2706.0000 Created
' 07/22/04 05.0.2706.0000 Removed error event for non-existent
' inbox.
' 09/07/04 05.0.2751.0000 Updated FileCountThreshold input
' parameter checking.
'
' (c) Copyright 2004, Microsoft Corp., All Rights Reserved
'*******************************************************************************

Option Explicit

'Event Severity Constants
'========================

Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENT_TYPE_INFORMATION = 4

Const EVENTLOG_AUDIT_SUCCESS = 8
Const EVENTLOG_AUDIT_FAILURE = 16


'Event Number Constants
'======================

Const EVENT_ID_NOTANEVENT = 1100
Const EVENT_ID_SCRIPTSUCCESS = 1101
Const EVENT_ID_SCRIPTERROR = 1102
Const EVENT_ID_ACCESSDENIED = 1105

Const EVENT_ID_SMS_INBOX_THRESHOLD_EXCEEDED = 1720


'Event and Log Messages Constants
'================================

'Start Localization

'End Localization


'Debug Level constants
'=====================

Const DBG_TRACE = 1
Const DBG_WARNING = 2
Const DBG_ERROR = 3
Const DBG_NONE = 4


'Input Parameter Constants
'=========================

Const SCRIPT_PARAM_INBOX_DIRECTORY_NAME = "InboxDirectoryName"
Const SCRIPT_PARAM_INBOX_FILE_COUNT_THRESHOLD = "FileCountThreshold"


'Registry Path and Value Constants
'=================================

Const REG_KEY_IDENTIFICATION = "HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\SMS\Identification\"

Const REG_VAL_INSTALLATION_DIRECTORY = "Installation Directory"
Const REG_VAL_SITE_CODE = "Site Code"


' Global variables
'=================


'******************************************************************************
' Name: Main
'
' Purpose: Entry point for program execution.
'
' Parameters: None
'
' Returns: Nothing
'
Sub Main()

Dim strInboxDirectoryName
Dim intFileCountThreshold


On Error Resume Next


LogMessage DBG_TRACE, ScriptContext.Name &amp; " script starting at local time: " &amp; CStr(Time)

'This script does not support agentless monitoring.
'==================================================

If ScriptContext.IsTargetAgentless Then
LogMessage DBG_TRACE, "No action taken; agentless monitoring is not supported."
Exit Sub
End If

'Get the input parameters; the inbox directory name and file count
'threshold.
'=================================================================

strInboxDirectoryName = CStr(ScriptContext.Parameters.Get(SCRIPT_PARAM_INBOX_DIRECTORY_NAME))

If ((0 &lt;&gt; Err.number) or _
IsEmpty(strInboxDirectoryName) or _
IsNull(strInboxDirectoryName) or _
(strInboxDirectoryName = "")) Then
LogMessage DBG_ERROR, "Failed to get script parameter " &amp; SCRIPT_PARAM_INBOX_DIRECTORY_NAME &amp; " or invalid value."
ScriptError "get script parameter " &amp; SCRIPT_PARAM_INBOX_DIRECTORY_NAME &amp; " or invalid value."
Exit Sub
End If

intFileCountThreshold = CLng(ScriptContext.Parameters.Get(SCRIPT_PARAM_INBOX_FILE_COUNT_THRESHOLD))

If ((0 &lt;&gt; Err.number) or _
IsEmpty(intFileCountThreshold) or _
(intFileCountThreshold &lt;= 0)) Then
LogMessage DBG_ERROR, "Failed to get script parameter " &amp; SCRIPT_PARAM_INBOX_FILE_COUNT_THRESHOLD &amp; " or invalid value."
ScriptError "get script parameter " &amp; SCRIPT_PARAM_INBOX_FILE_COUNT_THRESHOLD &amp; " or invalid value."
Exit Sub
End If

'Perform inbox check.
'====================

CheckInbox strInboxDirectoryName, intFileCountThreshold

LogMessage DBG_TRACE, ScriptContext.Name &amp; " script completed at local time: " &amp; CStr(Time)

End Sub


'******************************************************************************
' Name: CheckInbox
'
' Purpose: Checks if the number of files in the specified inbox has exceeded
' the specified threshold. If so, an event is raised.
'
' Parameters: strInboxDirectoryName, the inbox to check.
' intFileCountThreshold, the threshold to event on.
'
' Returns: Nothing
'
Sub CheckInbox(strInboxDirectoryName, intFileCountThreshold)

Dim strError

Dim intFileCount


On Error Resume Next


'Get the current file count for the specified inbox.
'===================================================

intFileCount = GetInboxFileCount(strInboxDirectoryName)

'If the current file count exceeds the specified threshold for the
'inbox, create an event.
'=================================================================

If intFileCount = 0 Then
LogMessage DBG_TRACE, "Inbox " &amp; strInboxDirectoryName &amp; " is empty."
ElseIf intFileCount &lt;= intFileCountThreshold Then
LogMessage DBG_TRACE, "Inbox " &amp; strInboxDirectoryName &amp; " has not exceeded its file count threshold of " &amp; intFileCountThreshold &amp; "."
Else
LogMessage DBG_WARNING, "Inbox " &amp; strInboxDirectoryName &amp; " contains " &amp; intFileCount &amp; " files which exceeds its threshold of " &amp; intFileCountThreshold &amp; " files."
CreateSMSInboxThresholdExceededEvent strInboxDirectoryName, intFileCountThreshold, intFileCount
End If

End Sub


'******************************************************************************
' Name: GetInboxFileCount
'
' Purpose: Gets the current file count from the specified inbox under the
' SMS\inboxes directory.
'
' Parameters: strInboxDirectoryName, the inbox to check.
'
' Returns: the numbers of files in the inbox if successful otherwise 0.
'
Function GetInboxFileCount(strInboxDirectoryName)

Dim objFSO
Dim objInboxFolder
Dim objFiles

Dim strInboxPath


On Error Resume Next


'Construct the full path for the specified inbox.
'================================================

strInboxPath = GetSMSInstallationPath()

If IsEmpty(strInboxPath) Then
GetInboxFileCount = 0
Exit Function
End If

strInboxPath = strInboxPath &amp; "\inboxes\" &amp; strInboxDirectoryName

'Check if the inbox exists.
'==========================

Set objFSO = CreateObject("Scripting.FileSystemObject")

If Not objFSO.FolderExists(strInboxPath) Then

LogMessage DBG_ERROR, "Failed to verify existence of inbox: " &amp; strInboxPath &amp; "."

GetInboxFileCount = 0

Set objFSO = Nothing
Err.Clear
Exit Function
End If

'Get the number of files in the inbox.
'=====================================

Set objInboxFolder = objFSO.GetFolder(strInboxPath)

Set objFiles = objInboxFolder.Files

GetInboxFileCount = objFiles.Count

'Cleanup
'=======

Set objFiles = Nothing
Set objInboxFolder = Nothing
Set objFSO = Nothing

End Function


'******************************************************************************
' Name: GetSMSInstallationPath
'
' Purpose: Get the SMS Installation Directory from the registry under the
' SMS Identification key.
'
' Parameters: None
'
' Returns: String, SMS Installation path if successful otherwise empty.
'
Function GetSMSInstallationPath()

Dim strError

Dim objShell


On Error Resume Next


'Create the WSH Shell object for accessing the registry.
'=======================================================

Set objShell = CreateObject("WScript.Shell")

'Read the Installation Directory value from under the SMS
'Identification key.
'========================================================

GetSMSInstallationPath = objShell.RegRead(REG_KEY_IDENTIFICATION &amp; REG_VAL_INSTALLATION_DIRECTORY)

If IsEmpty(GetSMSInstallationPath) Then
strError = GetErrorString(Err)
LogMessage DBG_ERROR, "Failed to read registry value." &amp; strError
ScriptError "read registry value." &amp; strError
Err.Clear
End If

Set objShell = Nothing

End Function


'******************************************************************************
' Name: GetSMSSiteCode
'
' Purpose: Get the SMS Site Code from the registry under the SMS
' Identification key.
'
' Parameters: None
'
' Returns: String, returns the SMS Site Code if successful otherwise empty.
'
Function GetSMSSiteCode()

Dim strError

Dim objShell


On Error Resume Next


'Create the WSH Shell object for accessing the registry.
'=======================================================

Set objShell = CreateObject("WScript.Shell")

'Read the Site Code value from under the SMS Identification key.
'===============================================================

GetSMSSiteCode = objShell.RegRead(REG_KEY_IDENTIFICATION &amp; REG_VAL_SITE_CODE)

If IsEmpty(GetSMSSiteCode) Then
strError = GetErrorString(Err)
LogMessage DBG_ERROR, "Failed to read registry value." &amp; strError
ScriptError "read registry value." &amp; strError
Err.Clear
End If

Set objShell = Nothing

End Function


'******************************************************************************
' Name: CreateSMSInboxThresholdExceededEvent
'
' Purpose: To generate a MOM event containing the inbox name, file count
' threshold and the current file count exceeding that threshold.
'
' Parameters: strInboxDirectoryName, the inbox that was checked.
' intFileCountThreshold, the threshold for the inbox.
' intFileCount, the number of files currently in the inbox.
'
' Returns: Nothing
'
Sub CreateSMSInboxThresholdExceededEvent(strInboxDirectoryName, intFileCountThreshold, intFileCount)

Dim oEvent

Dim strSiteCode
Dim strMessage


On Error Resume Next


'Create MOM event object.
'========================

Set oEvent = ScriptContext.CreateEvent

'Initialize the event object.
'============================

oEvent.EventType = EVENT_TYPE_WARNING
oEvent.EventNumber = EVENT_ID_SMS_INBOX_THRESHOLD_EXCEEDED

strSiteCode = GetSMSSiteCode()
oEvent.Category = strSiteCode

strMessage = "SMS site '" &amp; strSiteCode &amp; "' has inbox " &amp; strInboxDirectoryName &amp; " with " &amp; intFileCount &amp; " files which exceeds its threshold of " &amp; intFileCountThreshold &amp; " files."

oEvent.Message = strMessage

'Submit, raise, this event to MOM.
'=================================

LogMessage DBG_TRACE, "Submitting event " &amp; EVENT_ID_SMS_INBOX_THRESHOLD_EXCEEDED &amp; "."

ScriptContext.Submit oEvent

'Cleanup
'=======

Set oEvent = Nothing

End Sub


'******************************************************************************
' Name: GetErrorString
'
' Purpose: Attempts to find the description for an error if an error with
' no description is passed in.
'
' Parameters: oErr The error object
'
' Return: String, the description for the error. (Includes the error code.)
'
Function GetErrorString(oErr)

Dim lErr, strErr


lErr = oErr
strErr = oErr.Description

On Error Resume Next

If 0 &gt;= Len(strErr) Then

' If we don't have an error description, then check to see if the error
' is a 0x8007xxxx error. If it is, then look it up.
Const ErrorMask = &amp;HFFFF0000
Const HiWord8007 = &amp;H80070000
Const LoWordMask = 65535 ' This is equivalent to 0x0000FFFF

If (lErr And ErrorMask) = HiWord8007 Then

' Attempt to use 'net helpmsg' to get a description for the error.
Dim oShell
Set oShell = CreateObject("WScript.Shell")

If Err = 0 Then

Dim oExec
Set oExec = oShell.Exec("net helpmsg " &amp; (lErr And LoWordMask))

Dim strMessage, i
Do
strMessage = oExec.stdout.ReadLine()
i = i + 1
Loop While (Len(strMessage) = 0) And (i &lt; 5)

strErr = strMessage

End If

End If

End If

GetErrorString = vbCrLf &amp; "The error returned was: '" &amp; strErr &amp; "' " &amp; lErr &amp; " (0x" &amp; Hex(lErr) &amp; ")"

End Function


'******************************************************************************
' Name: ScriptError
'
' Purpose: To generate a warning about a runtime script error.
'
' Parameters: strError The description of the error
'
' Returns: Nothing
'
Sub ScriptError(strError)

LogEvent EVENT_ID_SCRIPTERROR, EVENT_TYPE_WARNING, "encountered a runtime error." &amp; vbCrLf &amp; "Failed to " &amp; strError

End Sub


'******************************************************************************
' Name: LogEvent
'
' Purpose: To generate a MOM event
'
' Parameters: lEventID The event code
' lEventType The severity of the event
' strMessage The message to include in the event
'
' Returns: Nothing
'
Sub LogEvent(lEventID, lEventType, strMessage)

Dim oEvent


On Error Resume Next

Set oEvent = ScriptContext.CreateEvent

oEvent.EventNumber = lEventID
oEvent.EventType = lEventType
oEvent.Message = "The script '" &amp; ScriptContext.Name &amp; "' running under processing rule '" &amp; ScriptContext.ProcessingRule.Name &amp; "' " &amp; strMessage

ScriptContext.Submit oEvent

End Sub


'******************************************************************************
' Name: LogMessage
'
' Purpose: To log a message to ScriptContext and MOM's agent response log.
'
' Parameters: lLevel The debug level for this message i.e. trace,
' warning or error
' strMessage The message to write
'
' Returns: Nothing
'
Sub LogMessage(lLevel, strMessage)

If (lLevel &lt; DBG_NONE) Then

If (lLevel = DBG_ERROR) Then
ScriptContext.Echo "[Error]: " + strMessage
ElseIf (lLevel = DBG_WARNING) Then
ScriptContext.Echo "[Warning]: " + strMessage
ElseIf (lLevel = DBG_TRACE) Then
ScriptContext.Echo "[Trace]:" + strMessage
End If

End If

End Sub</Script></Body>
<Language>VBScript</Language>
<Name>SMS 2003 Monitor SMS Inbox</Name>
<Parameters>
<Parameter>
<Name>FileCountThreshold</Name>
<Value>$Config/Parameters/FileCountThreshold$</Value>
</Parameter>
<Parameter>
<Name>InboxDirectoryName</Name>
<Value>$Config/Parameters/InboxDirectoryName$</Value>
</Parameter>
</Parameters>
<ManagementPackId>[Microsoft.SMS.2003,,1.0.0.1]</ManagementPackId>
</WriteAction>
</MemberModules>
<Composition>
<Node ID="RunScriptAction"/>
</Composition>
</Composite>
</ModuleImplementation>
<InputType>SystemLibrary!System.BaseData</InputType>
</WriteActionModuleType>