'==========================================================================
'
' VBScript Source File -- Created with SAPIEN Technologies PrimalScript 4.0
'
' NAME: MOM Audit Script Changes
'
' AUTHOR: Pete Zerger , AKOS Technology Services
' DATE : 2/16/2006
'
' COMMENT: This script audits addition, removal or modification of MOM scripts.
'
' Key Variables: SQL_DSN = The name of your MOM Database Server
' POLL_INTERVAL_IN_HOURS = number of hours to check (last 1 hr by default)
'
'==========================================================================
'on Error Resume Next
'Probably could be parameters but aren't
Const MOM_SCRIPT_EVENT_ID = 5003
Const POLL_INTERVAL_IN_HOURS = -1 '***Note: This must be a negative number as
it's going back in time!!!
Const SQL_DSN = "YourMOMDBSvr"
Const EVENT_TYPE_SUCCESS = 0
Const EVENT_TYPE_ERROR = 1
Const EVENT_TYPE_WARNING = 2
Const EVENTLOG_INFORMATION_TYPE = 4
Const EVENTLOG_AUDIT_SUCCESS = 8
Const EVENTLOG_AUDIT_FAILURE = 16
Const SCRIPT_FAILURE_EVENT = 91001
Dim cn
Dim rs
Dim strSQLQuery
Dim UTCTime
InitSQL()
getUTCTime()
getScriptChanges()
'Log an error if script fails
If err.number <> 0 Then
LogEvent SCRIPT_FAILURE_EVENT,EVENT_TYPE_ERROR,"MOM Admin Script Error: " &
vbCrLf & "err.number: " & err.number & vbCrLf & " err.description: " &
err.description
End If
Set cn = Nothing
Set rs = Nothing
'***********************************************
'InitSQL() Create connection
'***********************************************
Sub InitSQL()
Set cn = CreateObject("ADODB.Connection")
Set rs = CreateObject(" ADODB.Recordset")
cn.Open "Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial
Catalog=OnePoint;Data Source=" & SQL_DSN & ""
End Sub
'***********************************************
'getUTCTime() Gets UTC time difference (-6 or -5)
'***********************************************
Sub getUTCTime()
strSQLQuery = "select DateDiff(hh,getutcdate(),getdate()) as UTCTime"
rs.Open strSQLQuery,cn,1,1
UTCTime = CStr(rs("UTCTime"))
End Sub
'***********************************************
'getScriptChanges Get the SQL Server for Scripts
'**********************************************
Sub getScriptChanges()
strSQLQuery = "select name, IsDeleted,lastmodified,lastmodifiedby from script
where lastmodified > dateadd(hh," & POLL_INTERVAL_IN_HOURS & ",getUTCdate())
order by lastmodified desc"
'Pull all changes from last X hours
Set rs = cn.Execute(strSQLQuery)
'If recordset is null, then report that no scripts have changed in the last
reporting period.
If RS.EOF Then
LogEvent MOM_SCRIPT_EVENT_ID,EVENT_TYPE_SUCCESS,"No Scripts were created or
modified within the last " & Abs(POLL_INTERVAL_IN_HOURS) & " hours."
Exit Sub
End If
'Process script changes in the record Set
While Not rs.eof
ScriptName = rs("name")
IsDeletedValue = rs("Isdeleted")
sLastModified = DateAdd("H",UTCTime,rs("lastmodified"))
sLastModifiedBy = rs("lastmodifiedby")
If Not RS.EOF Then
LogChangeEvent MOM_SCRIPT_EVENT_ID,EVENT_TYPE_WARNING,"The following Script was
created or modified in the last" & Abs(POLL_INTERVAL_IN_HOURS) & "Hours." &
vbCrLf & "Script Name:" & ScriptName & vbCrLf & " Delete Operation: " &
IsDeletedValue & vbCrLf & "Changed by: " & sLastModifiedBy & vbCrLf & "Changed
on: " & sLastModified, ScriptName, IsDeletedValue, sLastModified,
slastmodifiedby
'Else
'LogChangeEvent MOM_SCRIPT_EVENT_ID,EVENT_TYPE_SUCCESS,"The following Script has
changed in the last " & Abs(POLL_INTERVAL_IN_HOURS) & " Hours: " & vbCrLf & "ScriptName:
Other - Please Investigate - " & ScriptName & vbCrLf & "Data Value: " &
IsDeletedValue & vbCrLf & "Changed by: " & sLastModifiedBy & vbCrLf & "Changed
on: " & sLastModified, ScriptName, IsDeletedValue, sLastModified,
slastmodifiedby
End If
rs.MoveNext
Wend
End Sub
Sub LogChangeEvent(lEventID, lEventType, lEventMessage, lParam1, lParam2,
lParam3, lParam4) ', lParam5)
'On Error Resume Next
Set oEvent = ScriptContext.CreateEvent
oEvent.EventNumber = lEventID
oEvent.EventType = lEventType
oEvent.Message = lEventMessage
oEvent.SetEventParameter(lParam1)
oEvent.SetEventParameter(lParam2)
oEvent.SetEventParameter(lParam3)
oEvent.SetEventParameter(lParam4)
'oEvent.SetEventParameter(lParam5)
ScriptContext.Submit oEvent
Set oEvent = Nothing
Set objShell = CreateObject("WScript.Shell")
objShell.LogEvent 4, "MOM Script: " & ScriptContext.Name & " MOMEventID: " &
lEventID & " MOMEventType: " & lEventType & " MOMEventMsg: " & lEventMessage & "
lParam1: " & lParam1 & " lParam2: " & lParam2 & " lParam3: " & lParam3 & "
lParam4: " & lParam4 & " lParam5: " & lParam5
Set objShell = Nothing
End Sub
Sub LogEvent(lEventID, lEventType, lEventMessage)
Dim oEvent
'On Error Resume Next
Set oEvent = ScriptContext.CreateEvent
oEvent.EventNumber = lEventID
oEvent.EventType = lEventType
oEvent.Message = lEventMessage
ScriptContext.Submit oEvent
Set oEvent = Nothing
Set objShell = CreateObject("WScript.Shell")
objShell.LogEvent 4, "MOM Script: " & ScriptContext.Name & " MOMEventID: " &
lEventID & " MOMEventType: " & lEventType & " MOMEventMsg: " & lEventMessage
Set objShell = Nothing
End Sub