0

サーバーに配置できるバッチ ファイルを作成し、それをタスク スケジューラ経由で毎日実行して、アプリケーションのログ ファイル ディレクトリを監視しようとしています。ログ ファイルの名前が変更された場合 (たとえば、エラー ログが表示される場合)、バッチは管理者または管理者グループに電子メールを送信します。

このタイプのバッチの一般的なスクリプトは何ですか?

ありがとうございました。

4

2 に答える 2

1

これには機能が組み込まれているため、これには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 "FolderMonitor@Domain.com", "youremail@domain.com","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
于 2014-02-20T13:26:00.093 に答える
-1

これは、ログの変更を監視するための私のプロジェクトのコードです

それは使用しています

  • ログファイルの変更を追跡するためのJava nio
  • サービスの停止に使用されるプロセス ID を取得するためのsigar api ( https://github.com/hyperic/sigar )
  • アラートの場合にメールを送信するためのJavaメール(メソッド-sendMail)

sigar api を使用するには、ビルド パスに dll などのファイルを追加する必要があります。

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();
    }
}
于 2016-03-14T06:42:15.210 に答える