Search in sources :

Example 1 with LogFilterForInstance

use of com.sun.enterprise.server.logging.logviewer.backend.LogFilterForInstance in project Payara by payara.

the class CollectLogFiles method getLogDirForLocalNode.

private String getLogDirForLocalNode(String instanceLogFileName, Node node, String serverNode, String instanceName) {
    String loggingDir;
    loggingDir = new LogFilterForInstance().getLoggingDirectoryForNode(instanceLogFileName, node, serverNode, instanceName);
    File logsDir = new File(loggingDir);
    File[] allLogFileNames = logsDir.listFiles();
    boolean noFileFound = true;
    if (allLogFileNames != null) {
        // This check for,  if directory doesn't present or missing on machine. It happens due to bug 16451
        for (int i = 0; i < allLogFileNames.length; i++) {
            File file = allLogFileNames[i];
            String fileName = file.getName();
            // code to remove . and .. file which is return
            if (file.isFile() && !fileName.equals(".") && !fileName.equals("..") && fileName.contains(".log") && !fileName.contains(".log.")) {
                noFileFound = false;
                break;
            }
        }
    }
    if (noFileFound) {
        // this loop is used when user has changed value for server.log but not restarted the server.
        loggingDir = new LogFilterForInstance().getLoggingDirectoryForNodeWhenNoFilesFound(instanceLogFileName, node, serverNode, instanceName);
    }
    return loggingDir;
}
Also used : File(java.io.File) LogFilterForInstance(com.sun.enterprise.server.logging.logviewer.backend.LogFilterForInstance)

Example 2 with LogFilterForInstance

use of com.sun.enterprise.server.logging.logviewer.backend.LogFilterForInstance in project Payara by payara.

the class CollectLogFiles method execute.

public void execute(AdminCommandContext context) {
    final ActionReport report = context.getActionReport();
    Properties props = initFileXferProps();
    Server targetServer = domain.getServerNamed(target);
    if (targetServer != null && targetServer.isDas()) {
        // This loop if target instance is DAS
        String logFileDetails = "";
        String zipFile = "";
        try {
            // getting log file values from logging.propertie file.
            logFileDetails = loggingConfig.getLoggingFileDetails();
        } catch (Exception ex) {
            final String errorMsg = localStrings.getLocalString("collectlogfiles.errGettingLogFiles", "Error while getting log file attribute for {0}.", target);
            report.setMessage(errorMsg);
            report.setFailureCause(ex);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        File targetDir = makingDirectoryOnDas(targetServer.getName(), report);
        try {
            String sourceDir = "";
            if (logFileDetails.contains("${com.sun.aas.instanceRoot}/logs")) {
                sourceDir = env.getDomainRoot() + File.separator + "logs";
            } else {
                sourceDir = logFileDetails.substring(0, logFileDetails.lastIndexOf(File.separator));
            }
            copyLogFilesForLocalhost(sourceDir, targetDir.getAbsolutePath(), report, targetServer.getName());
        } catch (Exception ex) {
            final String errorMsg = localStrings.getLocalString("collectlogfiles.errInstanceDownloading", "Error while downloading log files from {0}.", target);
            report.setMessage(errorMsg);
            report.setFailureCause(ex);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        try {
            String zipFilePath = getZipFilePath().getAbsolutePath();
            zipFile = loggingConfig.createZipFile(zipFilePath);
            if (zipFile == null || new File(zipFile) == null) {
                // Failure during zip
                final String errorMsg = localStrings.getLocalString("collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFilePath);
                report.setMessage(errorMsg);
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        } catch (Exception e) {
            // Catching Exception if any
            final String errorMsg = localStrings.getLocalString("collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFile);
            report.setMessage(errorMsg);
            report.setFailureCause(e);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        if (this.retrieve) {
            retrieveFile(zipFile, context, getZipFilePath(), props, report);
            report.setMessage(localStrings.getLocalString("collectlogfiles.instance.success", "Created Zip file under {0}.", retrieveFilePath + File.separator + new File(zipFile).getName()));
        } else {
            report.setMessage(localStrings.getLocalString("collectlogfiles.instance.success", "Created Zip file under {0}.", zipFile));
        }
    } else if (targetServer != null && targetServer.isInstance()) {
        // This loop if target standalone instance
        String instanceName = targetServer.getName();
        String serverNode = targetServer.getNodeRef();
        Node node = domain.getNodes().getNode(serverNode);
        String zipFile = "";
        File targetDir = null;
        String logFileDetails = "";
        try {
            // getting log file values from logging.propertie file.
            logFileDetails = getInstanceLogFileDirectory(targetServer);
        } catch (Exception ex) {
            final String errorMsg = localStrings.getLocalString("collectlogfiles.errGettingLogFiles", "Error while getting log file attribute for {0}.", target);
            report.setMessage(errorMsg);
            report.setFailureCause(ex);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        targetDir = makingDirectoryOnDas(targetServer.getName(), report);
        try {
            if (node.isLocal()) {
                String sourceDir = getLogDirForLocalNode(logFileDetails, node, serverNode, instanceName);
                copyLogFilesForLocalhost(sourceDir, targetDir.getAbsolutePath(), report, instanceName);
            } else {
                new LogFilterForInstance().downloadAllInstanceLogFiles(habitat, targetServer, domain, LOGGER, instanceName, targetDir.getAbsolutePath(), logFileDetails);
            }
        } catch (Exception ex) {
            final String errorMsg = localStrings.getLocalString("collectlogfiles.errInstanceDownloading", "Error while downloading log files from {0}.", instanceName);
            report.setMessage(errorMsg);
            report.setFailureCause(ex);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        try {
            // Creating zip file and returning zip file absolute path.
            String zipFilePath = getZipFilePath().getAbsolutePath();
            zipFile = loggingConfig.createZipFile(zipFilePath);
            if (zipFile == null || new File(zipFile) == null) {
                // Failure during zip
                final String errorMsg = localStrings.getLocalString("collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFilePath);
                report.setMessage(errorMsg);
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
        } catch (Exception ex) {
            final String errorMsg = localStrings.getLocalString("collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFile);
            report.setMessage(errorMsg);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        if (this.retrieve) {
            retrieveFile(zipFile, context, getZipFilePath(), props, report);
            report.setMessage(localStrings.getLocalString("collectlogfiles.instance.success", "Created Zip file under {0}.", retrieveFilePath + File.separator + new File(zipFile).getName()));
        } else {
            report.setMessage(localStrings.getLocalString("collectlogfiles.instance.success", "Created Zip file under {0}.", zipFile));
        }
    } else {
        // This loop if target is cluster
        String finalMessage = "";
        String zipFile = "";
        File targetDir = null;
        // code to download server.log file for DAS. Bug fix 16088
        String logFileDetails = "";
        try {
            // getting log file values from logging.propertie file.
            logFileDetails = loggingConfig.getLoggingFileDetails();
        } catch (Exception ex) {
            final String errorMsg = localStrings.getLocalString("collectlogfiles.errGettingLogFiles", "Error while getting log file attribute for {0}.", target);
            report.setMessage(errorMsg);
            report.setFailureCause(ex);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        targetDir = makingDirectoryOnDas(SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME, report);
        try {
            String sourceDir = "";
            if (logFileDetails.contains("${com.sun.aas.instanceRoot}/logs")) {
                sourceDir = env.getDomainRoot() + File.separator + "logs";
            } else {
                sourceDir = logFileDetails.substring(0, logFileDetails.lastIndexOf(File.separator));
            }
            copyLogFilesForLocalhost(sourceDir, targetDir.getAbsolutePath(), report, SystemPropertyConstants.DEFAULT_SERVER_INSTANCE_NAME);
        } catch (Exception ex) {
            final String errorMsg = localStrings.getLocalString("collectlogfiles.errInstanceDownloading", "Error while downloading log files from {0}.", target);
            report.setMessage(errorMsg);
            report.setFailureCause(ex);
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
            return;
        }
        /**
         ***************************************************
         */
        com.sun.enterprise.config.serverbeans.Cluster cluster = domain.getClusterNamed(target);
        List<Server> instances = cluster.getInstances();
        int instanceCount = 0;
        int errorCount = 0;
        for (Server instance : instances) {
            // downloading log files for all instances which is part of cluster under temp directory.
            String instanceName = instance.getName();
            String serverNode = instance.getNodeRef();
            Node node = domain.getNodes().getNode(serverNode);
            boolean errorOccur = false;
            instanceCount++;
            logFileDetails = "";
            try {
                // getting log file values from logging.propertie file.
                logFileDetails = getInstanceLogFileDirectory(domain.getServerNamed(instanceName));
            } catch (Exception ex) {
                final String errorMsg = localStrings.getLocalString("collectlogfiles.errGettingLogFiles", "Error while getting log file attribute for {0}.", target);
                report.setMessage(errorMsg);
                report.setFailureCause(ex);
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
            try {
                targetDir = makingDirectoryOnDas(instanceName, report);
                if (node.isLocal()) {
                    String sourceDir = getLogDirForLocalNode(logFileDetails, node, serverNode, instanceName);
                    copyLogFilesForLocalhost(sourceDir, targetDir.getAbsolutePath(), report, instanceName);
                } else {
                    new LogFilterForInstance().downloadAllInstanceLogFiles(habitat, instance, domain, LOGGER, instanceName, targetDir.getAbsolutePath(), logFileDetails);
                }
            } catch (Exception ex) {
                errorCount++;
                final String errorMsg = localStrings.getLocalString("collectlogfiles.errInstanceDownloading", "Error while downloading log files from {0}.", instanceName);
                errorOccur = true;
                finalMessage += errorMsg + "\n";
            }
            if (!errorOccur) {
                final String successMsg = localStrings.getLocalString("collectlogfiles.successInstanceDownloading", "Log files are downloaded for {0}.", instanceName);
                finalMessage += successMsg + "\n";
            }
        }
        report.setMessage(finalMessage);
        if (instanceCount != errorCount) {
            try {
                String zipFilePath = getZipFilePath().getAbsolutePath();
                // Creating zip file and returning zip file absolute path.
                zipFile = loggingConfig.createZipFile(zipFilePath);
                if (zipFile == null || new File(zipFile) == null) {
                    // Failure during zip
                    final String errorMsg = localStrings.getLocalString("collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFilePath);
                    report.setMessage(errorMsg);
                    report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                    return;
                }
            } catch (Exception ex) {
                final String errorMsg = localStrings.getLocalString("collectlogfiles.creatingZip", "Error while creating zip file {0}.", zipFile);
                report.setMessage(errorMsg);
                report.setFailureCause(ex);
                report.setActionExitCode(ActionReport.ExitCode.FAILURE);
                return;
            }
            if (this.retrieve) {
                retrieveFile(zipFile, context, getZipFilePath(), props, report);
                report.setMessage(localStrings.getLocalString("collectlogfiles.cluster.success", "{0} Created Zip file under {1}.", finalMessage, retrieveFilePath + File.separator + new File(zipFile).getName()));
            } else {
                report.setMessage(localStrings.getLocalString("collectlogfiles.cluster.success", "{0} Created Zip file under {1}.", finalMessage, zipFile));
            }
            report.setActionExitCode(ActionReport.ExitCode.SUCCESS);
        } else if (instanceCount == 0) {
            report.setMessage(localStrings.getLocalString("collectlogfiles.noinstance", "No instances are defined as part of {0}. So there are no files to zip.", target));
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        } else {
            report.setActionExitCode(ActionReport.ExitCode.FAILURE);
        }
    }
    deleteDir(new File(env.getInstanceRoot() + File.separator + "collected-logs" + File.separator + "logs"));
}
Also used : Server(com.sun.enterprise.config.serverbeans.Server) Node(com.sun.enterprise.config.serverbeans.Node) Cluster(com.sun.enterprise.config.serverbeans.Cluster) ArrayList(java.util.ArrayList) List(java.util.List) ActionReport(org.glassfish.api.ActionReport) Properties(java.util.Properties) File(java.io.File) IOException(java.io.IOException) LogFilterForInstance(com.sun.enterprise.server.logging.logviewer.backend.LogFilterForInstance)

Aggregations

LogFilterForInstance (com.sun.enterprise.server.logging.logviewer.backend.LogFilterForInstance)2 File (java.io.File)2 Cluster (com.sun.enterprise.config.serverbeans.Cluster)1 Node (com.sun.enterprise.config.serverbeans.Node)1 Server (com.sun.enterprise.config.serverbeans.Server)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Properties (java.util.Properties)1 ActionReport (org.glassfish.api.ActionReport)1