'*********************************************************************************************** ' $ScriptName: "RUS Process Check" $ ' ' Purpose: Checks the RUS work to do using USN Changed attributes ' ' Parameters passed to the Script: ' int MaxDelta: maximum USN Delta to generate an Event/Alert ' int debug: Debugging. 0 = Output, 1 = output+error, 2 = output+error+waring ' ' Events created by this script: ' SOURCE ID TYPE DESCRIPTION ' CheckRUS 40 error MaxDelta exceeded ! ' CheckRUS 100 information MaxDelta not reached ' CheckRUS 9000 error Unable to read Exchange Organization, Check AD-permissions. ' ' $File: checkrusmom.vbs $ ' ' $Date: 2005/07/13 18:16:06 $ $Revision: 1 $ ' $Creator: fra---nk@carius.de '*********************************************************************************************** Option Explicit Const EVENT_TYPE_SUCCESS = 0 Const EVENT_TYPE_ERROR = 1 Const EVENT_TYPE_WARNING = 2 Const EVENT_TYPE_INFORMATION = 4 'Event ID Constants Const EVENT_ID_BASE_STATE_OK = 100000 Const EVENT_SOURCE = "CheckRUS" Dim objRootDSE Dim strConfigurationNC Dim oCommand Dim oConnection Dim oRecordSet Dim strExchangeOrg Dim strRUSContainer Dim strQuery Dim count dim intDebug dim intMaxDelta dim blnIsMOM on error resume next ' Disable Error handling temporarily wscript.echo "Starting..." ' Raises an error, if not running with WSH. if err = 0 then WSCript.ECHO "Running in MOM Emulation - Mode" blnIsMOM = false intMaxDelta = wscript.Arguments.named("MaxDelta") '1000 if intMaxDelta = "" then intMaxDelta = 1000 ' using 1000 as default intdebug = wscript.Arguments.named("Debug") if intdebug = "" then intdebug = 1 ' Output, Errors only else blnIsMOM = true intMaxDelta = ScriptContext.Parameters.Get("MaxDelta") intdebug = ScriptContext.Parameters.Get("Debug") end if err.clear on error goto 0 ' Enable Error handling again debuglog "Script running", 0 debuglog "Parameter:MaxDelta =" & intMaxDelta,0 debuglog "Parameter:Debugging=" & intdebug,0 debuglog "Connecting to RootDSE", 4 Set objRootDSE = GetObject("LDAP://RootDSE") strConfigurationNC = objRootDSE.Get("configurationNamingContext") debuglog "DONE:ConfigNC=" & strConfigurationNC,4 debuglog "Searching für Exchange Org using ADODB", 4 Set oConnection = CreateObject("ADODB.Connection") Set oCommand = CreateObject("ADODB.Command") Set oRecordSet = CreateObject("ADODB.RecordSet") oConnection.Provider = "ADsDSOObject" oConnection.Open "ADs Provider" strQuery = ";(objectclass=msExchOrganizationContainer);"& "name,distinguishedName" debuglog "LDAP-String"&strQuery,4 oCommand.Properties("Page Size") = 100 oCommand.ActiveConnection = oConnection oCommand.CommandText = strQuery Set oRecordSet = oCommand.Execute debuglog "DONE: Query returns", 4 if oRecordSet.EOF then debuglog "Unable to read Exchange Organization, Check AD-permissions.", 1 CreateAlert "OrgRead", "Unable to read Exchange Organization, Check AD-permissions.",EVENT_SOURCE,50 ScriptContext.quit(1) else debuglog "FOUND:Orgname=" & oRecordSet.Fields("name"), 0 debuglog "OrgDN=" & oRecordSet.Fields("distinguishedName"), 4 strExchangeOrg = oRecordSet.Fields("name") end if debuglog "Searching für RUS-Entries", 4 strRUSContainer = "CN=Recipient Update Services,CN=Address Lists Container,CN=" & strExchangeOrg & ",CN=Microsoft Exchange,CN=Services," & strConfigurationNC debuglog "RUSContainer=" & strRUSContainer, 4 Set oConnection = CreateObject("ADODB.Connection") Set oCommand = CreateObject("ADODB.Command") Set oRecordSet = CreateObject("ADODB.RecordSet") oConnection.Provider = "ADsDSOObject" oConnection.Open "ADs Provider" strQuery = ";(objectCategory=msExchAddressListService);name,distinguishedName;subtree" debuglog "LDAP-String" & strQuery, 4 oCommand.ActiveConnection = oConnection oCommand.CommandText = strQuery Set oRecordSet = oCommand.Execute debuglog "DONE: Query returns", 4 count = 0 While (Not oRecordSet.EOF) debuglog "Checking: "& oRecordSet.Fields("name"),3 count = count +1 Call CheckRus(oRecordSet.Fields("distinguishedName"),count) oRecordSet.MoveNext Wend oRecordSet.Close oConnection.Close Set oRecordSet = Nothing Set oCommand = Nothing Set oConnection = Nothing debuglog "Skript beendet", 0 Sub CheckRus(strRusDN,id) Dim objRUS, objtarget, dblHighestUSN, intmissedobjects debuglog "SUB:Checkrus ===============================",4 debuglog " Prüfe RUS:" & strRusDN,4 Set objRUS = GetObject("LDAP://" & strRusDN) objRUS.GetInfo ' Objekt aktuell einlesen debuglog " name :" & objRUS.Name,4 debuglog " ID :" & id,4 debuglog " lastmodified :" & objRUS.whenchanged,4 dblHighestUSN = Abs(objRUS.get("msExchServer1HighestUSN").HighPart * 2^32 + objRUS.get("msExchServer1HighestUSN").LowPart) debuglog " RUSHighestUSN:" & dblHighestUSN,4 debuglog " :" & objRUS.get("msExchServer1NetworkAddress"),4 Set objtarget = GetObject("LDAP://" & objRUS.get("msExchServer1NetworkAddress") &"/"& "RootDSE") 'Zum Ziel Verbinden debuglog " ADHighUSN :" & objtarget.get ("highestCommittedUsn"),4 debuglog " USNDiff :" & objtarget.get ("highestCommittedUsn")-dblHighestUSN ,4 ' Count pending objects intmissedobjects = listuntouchedobjects(dblHighestUSN, objtarget.get ("highestCommittedUsn"),objRUS.get("msExchDomainLink"),id) if cint(intmissedobjects) > cint(intMaxDelta) then debuglog " ALERT !: " & objRUS.Name & " MaxDelta exceeded ! Missing:" & intmissedobjects &" Objects" ,3 Createalert "RUS USN", objRUS.Name & " MaxDelta exceeded ! Missing:" & intmissedobjects &" Objects",EVENT_SOURCE,50 else debuglog " OK !: " & objRUS.Name & " MaxDelta not reached. Pending Objects: "&intmissedobjects,3 CreateEvent 100,EVENT_TYPE_INFORMATION,EVENT_SOURCE,objRUS.Name & " MaxDelta not reached. Pending Objects: "&intmissedobjects end if End Sub function listuntouchedobjects(lowusn, highusn, ldapscope,id) Dim oConnection, oCommand, oRecordSet,ousn, dblusn, oobj, count Set oConnection = CreateObject("ADODB.Connection") Set oCommand = CreateObject("ADODB.Command") Set oRecordSet = CreateObject("ADODB.RecordSet") ' Open the Connection oConnection.Provider = "ADsDSOObject" oConnection.Open "ADs Provider" ' Build query to find all pending RUS-Entries strQuery = ";(&(USNChanged>="&lowusn&")(USNChanged<=" & highusn &")((objectclass=*)));"& _ "name,distinguishedName,whenchanged,usnchanged;subtree" debuglog "LDAP-String List"&strQuery,4 oCommand.ActiveConnection = oConnection oCommand.CommandText = strQuery oCommand.Properties("Sort On")= "USNChanged" Set oRecordSet = oCommand.Execute count = 0 While (Not oRecordSet.EOF) count = count + 1 dblusn = oRecordSet.Fields("usnchanged") Set ousn = dblusn ' Kleiner Umweg um den VBVariant mit Lowpart/Highpart nutzen zu können dblusn = Abs(ousn.HighPart * 2^32 + ousn.LowPart) debuglog " Pending: USN=" & dblusn & "," & oRecordSet.Fields("whenchanged") & "," & oRecordSet.Fields("name"),4 oRecordSet.MoveNext Wend oRecordSet.Close 'Clean up oConnection.Close Set oRecordSet = Nothing Set oCommand = Nothing Set oConnection = Nothing listuntouchedobjects = Count End function Sub CreatePerfData(strObjectName,strCounterName,strInstanceName,numValue) ' ObjectName Name of the performance object ' CounterName Name of the performance counter ' InstanceName Name of the performance instance ' Value Numeric value of the data 'Sample: CreatePerfData "File","File Size",objFile.Path,objFile.Size ' CreatePerfData ,,, if blnismom then Set objPerfData = ScriptContext.CreatePerfData objPerfData.ObjectName = strObjectName objPerfData.CounterName =strCounterName objPerfData.InstanceName = strInstanceName objPerfData.Value = numValue ScriptContext.Submit objPerfData else wscript.echo "-------------- ALERT --------------------" wscript.echo "Perfname :"& strObjectName wscript.echo "PerfCounter :"& strCounterName wscript.echo "PerfInstance:"& strInstanceName wscript.echo "PerfValue:"& numValue end if End Sub Sub CreateEvent(intEventNumber,intEventType,strEventSource,strEventMessage) ' CreateEvent ,,, ' CreateEvent 100,EVENT_TYPE_INFORMATION,"Script Test","Hello world." if blnismom then dim objEvent Set objEvent = ScriptContext.CreateEvent() objEvent.EventNumber = intEventNumber objEvent.EventType = intEventType objEvent.EventSource = strEventSource objEvent.Message = strEventMessage ScriptContext.Submit objEvent Set objEvent = Nothing else wscript.echo "-------------- EVENT --------------------" wscript.echo "EventNumber:"& intEventNumber wscript.echo "EventType :"& intEventType wscript.echo "EventSource:"& strEventSource wscript.echo "Message :"& strEventMessage end if End Sub Sub CreateAlert(ByVal sName, ByVal sDescription, ByVal sSource, ByVal lAlertLevel) ' CreateAlert "Name","Description","Source",9000 if blnismom then Dim oAlert Set oAlert = ScriptContext.CreateAlert() oAlert.Name = sName ' Name der Regel, die den Alert startet oAlert.Description = sDescription oAlert.AlertSource = sSource oAlert.AlertLevel = lAlertLevel ' Severity ' 10 Success ' 20 Information ' 30 Warning ' 40 Error ' 50 Critical Error ' 60 Security Issue ' 70 Service unavailable ' oAlert.Owener = "[unassigned]" ' oAlert.ResolutionState = 0 ' New ScriptContext.Submit oAlert else wscript.echo "-------------- ALERT --------------------" wscript.echo "Alertname :"& sName wscript.echo "AlertLevel :"& lAlertLevel wscript.echo "AlertSource:"& sSource wscript.echo "Description:"& sDescription end if End Sub sub debuglog (byval strMessage, byval intlevel) ' Please use th following levels ' =0 normal Output data / processing Data ' =1 Errors ' =2 Warnings ' =3 Informational messages ' >3 Debug Messages if (cint(intlevel) <= cint(intdebug)) then if blnismom then scriptContext.echo now() &","& intlevel &":"& strMessage else wscript.echo now() &","& intlevel &":"& strMessage end if end if end sub