strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where Name = 'C:\\Scripts\\Application.log'")
For Each objFile in colFiles
strOriginalTimestamp = objFile.LastModified
Next
Wscript.Echo "Monitoring application log file: " & Now
Wscript.Echo
Do While True
Wscript.Sleep 1800000
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where Name = 'C:\\Scripts\\Application.log'") For Each objFile in colFiles
strLatestTimestamp = objFile.LastModified
Next
If strLatestTimestamp <> strOriginalTimestamp Then
strOriginalTimestamp = strLatestTimeStamp
Else
Wscript.Echo "ALERT: " & Now
Wscript.Echo "The application log file has not been modified in the last 30 minutes."
Wscript.Echo
strOriginalTimestamp = strLatestTimeStamp
End If
Loop
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where Name = 'C:\\Scripts\\Application.log'")
For Each objFile in colFiles
strOriginalTimestamp = objFile.LastModified
Next
Wscript.Echo "Monitoring application log file: " & Now
Wscript.Echo
Do While True
Wscript.Sleep 1800000
Set colFiles = objWMIService.ExecQuery _
("Select * from CIM_Datafile Where Name = 'C:\\Scripts\\Application.log'") For Each objFile in colFiles
strLatestTimestamp = objFile.LastModified
Next
If strLatestTimestamp <> strOriginalTimestamp Then
strOriginalTimestamp = strLatestTimeStamp
Else
Wscript.Echo "ALERT: " & Now
Wscript.Echo "The application log file has not been modified in the last 30 minutes."
Wscript.Echo
strOriginalTimestamp = strLatestTimeStamp
End If
Loop
스크립트에서는 시간을 체크하기 위한 2개의 변수가 있는데, 최초 실행 첫번째에 마지막 수정된 시간을 기록하고 그 다음 부터 루프를 돌면서 매번 체크를 하게 됩니다.
물론 시간은 체크코자 하는 간격을 조금더 줄이거나 늘일수 있습니다. 위 스크립트는 WMI 를 이용했으나, 닷넷을 이용하다면 FileSystemWatcher() 이벤트를 이용해서 훨씬더 쉽게 그리고 실시간으로 체크를 할수도 있습니다. ^^
FileSystemWatcher 이벤트 예제
http://msdn2.microsoft.com/ko-kr/library/system.io.filesystemwatcher_events(vs.80).aspx
public class Watcher
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
if(args.Length != 2)
{
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
{
public static void Main()
{
Run();
}
[PermissionSet(SecurityAction.Demand, Name="FullTrust")]
public static void Run()
{
string[] args = System.Environment.GetCommandLineArgs();
if(args.Length != 2)
{
Console.WriteLine("Usage: Watcher.exe (directory)");
return;
}
FileSystemWatcher watcher = new FileSystemWatcher();
watcher.Path = args[1];
watcher.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite
| NotifyFilters.FileName | NotifyFilters.DirectoryName;
watcher.Filter = "*.txt";
watcher.Changed += new FileSystemEventHandler(OnChanged);
watcher.Created += new FileSystemEventHandler(OnChanged);
watcher.Deleted += new FileSystemEventHandler(OnChanged);
watcher.Renamed += new RenamedEventHandler(OnRenamed);
watcher.EnableRaisingEvents = true;
Console.WriteLine("Press \'q\' to quit the sample.");
while(Console.Read()!='q');
}
private static void OnChanged(object source, FileSystemEventArgs e)
{
Console.WriteLine("File: " + e.FullPath + " " + e.ChangeType);
}
private static void OnRenamed(object source, RenamedEventArgs e)
{
Console.WriteLine("File: {0} renamed to {1}", e.OldFullPath, e.FullPath);
}
}
03-TechNetB_masthead_ltr.gif
댓글 없음:
댓글 쓰기