' Event Log is Full, Export and Clear (이벤트 기록: ID 9002)
' Event Log: Operations Manager, Event Source: Health Service Script
CONST EventID = 9002
CONST USAGE_LIMIT = 95
CONST MAX_FILES = 5
ScriptName = ""
Set oArgs = WScript.Arguments
TargetBackupPath = ""
If oArgs.Count > 1 Then
For i = 0 to oArgs.Count-1
TargetBackupPath = TargetBackupPath & oArgs(i) & " "
Next
TargetBackupPath = Trim(TargetBackupPath)
Else
TargetBackupPath = Trim(oArgs(0))
End If
CONST INFORMATION = 0
CONST ERROR = 1
CONST WARNING = 2
Set oFSO = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objFolder = oFSO.GetFolder(TargetBackupPath)
If Err.Number <> 0 Then
Set objFolder = NOTHING
Set oFSO = NOTHING
WScript.Quit
Else
Set wshNetwork = WScript.CreateObject( "WScript.Network" )
strComputerName = Ucase(wshNetwork.ComputerName)
Set wshNetwork = NOTHING
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\.\root\cimv2")
Set colItems = objWMIService.ExecQuery ("Select * from Win32_NTEventLogFile Where LogfileName = 'Application' OR LogfileName = 'System'")
For Each Item in colItems
tmpUsage = Round((Item.FileSize/Item.MaxFileSize)*100,0)
tmpLogName = Item.LogfileName
tmpLogExt = Item.Extension
If tmpUsage > USAGE_LIMIT Then
Set colLogFiles = objWMIService.ExecQuery("Select * from Win32_NTEventLogFile Where LogFileName='"&tmpLogName&"'")
For Each objLogfile in colLogFiles
PreDateTime = cDate(Now)
PreDateTime = Year(PreDateTime)&Right("0"&Month(PreDateTime),2)&Right("0"&Day(PreDateTime),2)&Right("0"&Hour(PreDateTime),2)&Right("0"&Minute(PreDateTime),2)
objLogFile.BackupEventLog(TargetBackupPath & "\" & strComputerName & "_" & tmpLogName & "_" & PreDateTime & "." & tmpLogExt)
objLogFile.ClearEventLog()
strMessage = tmpLogName & " event log has been cleared. Export path: " & TargetBackupPath & "\" & strComputerName & "_" & tmpLogName & "_" & PreDateTime & "." & tmpLogExt
Set oAPI = CreateObject("MOM.ScriptAPI")
Call oAPI.LogScriptEvent(ScriptName, EventID, INFORMATION, strMessage)
Set oAPI = NOTHING
Next
Set colLogFiles = NOTHING
End If
Next
Set colItems = NOTHING
Set objWMIService = NOTHING
Set colFiles = objFolder.Files
Do While colFiles.Count > MAX_FILES
Set colFiles = objFolder.Files
dtmOldestDate = Now
For Each objFile in colFiles
If objFile.DateCreated < dtmOldestDate Then
dtmOldestDate = objFile.DateCreated
strOldestFile = objFile.Path
End If
Next
oFSO.DeleteFile(strOldestFile)
Loop
Set colFiles = NOTHING
objFolder = NOTHING
Set oFSO = NOTHING
End If