我正在創建一個批處理文件,可以放置在服務器上,並通過任務計劃程序每天運行它來監視應用程序的日誌文件目錄。如果日誌文件的名稱發生更改(例如,出現錯誤日誌),批處理將向管理員或管理員組發送電子郵件。批處理文件用於監視日誌文件目錄並在發生更改時發送電子郵件警報?
這種批次的通用腳本是什麼?
謝謝。
我正在創建一個批處理文件,可以放置在服務器上,並通過任務計劃程序每天運行它來監視應用程序的日誌文件目錄。如果日誌文件的名稱發生更改(例如,出現錯誤日誌),批處理將向管理員或管理員組發送電子郵件。批處理文件用於監視日誌文件目錄並在發生更改時發送電子郵件警報?
這種批次的通用腳本是什麼?
謝謝。
因爲它具有內置功能,所以我會使用vbscript。下面是一個通用腳本,您可以修改它以執行所需操作。在任務計劃程序中用cscript調用它。
MonitorFolder()
Function MonitorFolder()
intInterval = "2"
strDrive = "C:"
strFolder = "\\temp\\"
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & _
"{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\cimv2")
strQuery = _
"Select * From __InstanceOperationEvent" _
& " Within " & intInterval _
& " Where Targetinstance Isa 'CIM_DataFile'" _
& " And TargetInstance.Drive='" & strDrive & "'" _
& " And TargetInstance.Path='" & strFolder & "'"
Set colEvents = objWMIService.ExecNotificationQuery (strQuery)
WScript.Echo "Monitoring events...[Ctl-C] to end"
Do
Set objEvent = colEvents.NextEvent()
Set objTargetInst = objEvent.TargetInstance
Select Case objEvent.Path_.Class
Case "__InstanceCreationEvent"
WScript.Echo "Created: " & objTargetInst.Name
SendEmail "[email protected]", "[email protected]","Log File Created", "A new error log has appeared"
Case "__InstanceDeletionEvent"
WScript.Echo "Deleted: " & objTargetInst.Name
Case "__InstanceModificationEvent"
WScript.Echo "Modified: " & objTargetInst.Name
End Select
Loop
End Function
Sub SendEmail(sFrom, sTo, sSubject, sMessageBody)
Set objMessage = CreateObject("CDO.Message")
objMessage.Subject = sSubject
objMessage.From = sFrom
objMessage.To = sTo
objMessage.TextBody = sMessageBody
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.myserver.com"
objMessage.Configuration.Fields.Item _
("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
objMessage.Configuration.Fields.Update
objMessage.Send
end sub
謝謝馬特。這應該給我一個很好的起點。 – user3293057
下面是一段代碼,從我的項目,以監測日誌獲取過程改變
它使用跟蹤日誌文件
DLL或使文件必須被添加到構建路徑儘量使用SIGAR API
void initializePathAndWatcher(String directoryPath, String directoryName, Map<String, Object> data)
throws IOException {
yamlData = data;
Sigar sigar = new Sigar();
long pid = sigar.getPid();
log.debug("Pid for current process: [" + pid + "]");
path = Paths.get(directoryPath, directoryName);
log.debug("Directory being watched: [" + path.toAbsolutePath() + "]");
watcher = FileSystems.getDefault().newWatchService();
path.register(watcher, ENTRY_CREATE, ENTRY_MODIFY);
sigar.close();
}
void watcherServiceShutDown() throws IOException {
watcher.close();
}
void trackEvent() throws Exception {
try {
while (true) {
WatchKey watchKey;
watchKey = watcher.take();
for (WatchEvent<?> event : watchKey.pollEvents()) {
WatchEvent.Kind<?> kind = event.kind();
@SuppressWarnings("unchecked")
WatchEvent<Path> ev = (WatchEvent<Path>) event;
Path fileName = ev.context();
if (fileName.toString().equals(yamlData.get("directory.filename").toString())) {
log.debug(kind.name() + ": [" + fileName + "]");
StringBuilder absoluteFilePath = new StringBuilder(path.toString());
absoluteFilePath.append("/");
absoluteFilePath.append(fileName);
Path filePath = Paths.get(absoluteFilePath.toString());
long lineCount = Files.lines(filePath).count();
Boolean flagMatchFound = false;
if (statusFlag.equalsIgnoreCase("WARN")) {
flagMatchFound = Files.lines(filePath).skip(linePointer).filter(s -> !s.isEmpty())
.anyMatch(s -> s.contains(yamlData.get("success.keyword").toString()));
if (flagMatchFound.equals(true)) {
log.warn("Bing server is back to normal");
statusFlag = "SUCCESS";
sendMail((String) yamlData.get("mail.success.subject"),
(String) yamlData.get("mail.success.body"));
}
}
if (statusFlag.equalsIgnoreCase("SUCCESS")) {
flagMatchFound = Files.lines(filePath).skip(linePointer).filter(s -> !s.isEmpty())
.anyMatch(s -> s.contains(yamlData.get("error.keyword").toString()));
if (flagMatchFound.equals(true)) {
log.warn("Problem diagnosed on bing server");
statusFlag = "WARN";
sendMail((String) yamlData.get("mail.warn.subject"),
(String) yamlData.get("mail.warn.body"));
}
}
linePointer = lineCount;
log.debug("Count of lines already processed active file: [" + linePointer + "]");
}
}
watchKey.reset();
}
}
catch(Exception e)
{
throw new Exception("Error occoured in trackEvent", e);
}
finally {
watcher.close();
}
}
光批處理腳本,但我主要是尋找周圍的最有效的腳本語言。 – user3293057