long bytesCollected = 0L;
String ipAddress = logSource.getInternalIP();
if(logger.isDebugEnabled()) {
logger.debug("trying to connect to {} via ssh ...", ipAddress);
}
SSHCommand sshCommand = new SSHCommand();
sshCommand.connect(logDestination.getSSHKey(), ipAddress, 22, logSource.getUser(), logSource.getPassword(), sshSessionTimeout);
// Start with E3Appender Java logs first
// Get a local copy of the logging config file to determine log-file path
String remoteLogPath = null;
File localConfigFile = new File(instanceCollectionDir, "java-logging.cfg");
String localConfigFilePath = localConfigFile.getAbsolutePath();
if (copyRemoteConfigFile(sshCommand, localConfigFilePath, LogFileSource.JAVA)) {
remoteLogPath = LoggingUtil.getLogFilePathFromConfigFile(localConfigFilePath, LogFileSource.JAVA, false);
} else {
logger.warn("Couldn't retrieve E3 Java logging config file from host {}, will try default path", ipAddress);
}
if ((remoteLogPath == null) || (remoteLogPath.length() == 0)) {
// If we can't find a logging config file with an E3Appender section,
// look anyway in the usual servicemix log directory for any log files with the default name
logger.warn("Instance at {} is not using E3Appender (check log-config file: {})", ipAddress, LoggingUtil.defaultConfigPath);
remoteLogPath = LoggingUtil.defaultLogPath;
}
File localTargetDir = createLocalLogTargetDir(instanceCollectionDir, LogFileSource.JAVA);
if (localTargetDir == null) {
logger.warn("Couldn't create log-collection directory: {}", instanceCollectionDir + File.separator + LogFileSource.JAVA.toString());
} else {
File remoteLog = new File(remoteLogPath);
List<FileInfo> logList = getMatchingRemoteFileList(sshCommand, remoteLog.getParent(), remoteLog.getName());
for (FileInfo remoteFileInfo : logList) {
File localCopy = new File(localTargetDir, targetNameForLogFile(remoteFileInfo, localTargetDir));
try {
bytesCollected += copyRemoteFileWithWorkingTemp(sshCommand, remoteFileInfo, localCopy.getAbsolutePath(), deleteAfterCollect);
} catch (Exception ex) {
// Continue copy attempts if we experience an error
logger.warn("Failed to copy remote file: {} ({})", remoteFileInfo.filePath, ex.getLocalizedMessage());
}
}
}
// Now try to get the remote serviceMix log files
// We use the same log-config file as the java logs to parse the path
remoteLogPath = LoggingUtil.getLogFilePathFromConfigFile(localConfigFilePath, LogFileSource.SMX, false);
localConfigFile.delete(); // no longer needed
if ((remoteLogPath == null) || (remoteLogPath.length() == 0)) {
// If we can't find a logging config file with the proper appender section,
// look anyway in the usual servicemix log directory for any log files with the default name
logger.warn("Instance at {} is not using expected appender for servicemix rootLogger (check log-config file: {})", ipAddress, LoggingUtil.defaultConfigPath);
remoteLogPath = LoggingUtil.defaultSMXLogPath;
}
localTargetDir = createLocalLogTargetDir(instanceCollectionDir, LogFileSource.SMX);
if (localTargetDir == null) {
logger.warn("Couldn't create log-collection directory: {}", instanceCollectionDir + File.separator + LogFileSource.SMX.toString());
} else {
File remoteLog = new File(remoteLogPath);
List<FileInfo> logList = getMatchingRemoteFileList(sshCommand, remoteLog.getParent(), remoteLog.getName());
for (FileInfo remoteFileInfo : logList) {
File localCopy = new File(localTargetDir, targetNameForLogFile(remoteFileInfo, localTargetDir));
try {
bytesCollected += copyRemoteFileWithWorkingTemp(sshCommand, remoteFileInfo, localCopy.getAbsolutePath(), deleteAfterCollect);
} catch (Exception ex) {
// Continue copy attempts if we experience an error
logger.warn("Failed to copy remote file: {} ({})", remoteFileInfo.filePath, ex.getLocalizedMessage());
}
}
}
// Collect the E3-specific syslog files
// For syslog we parse the rsyslog config file for the log-file path
localConfigFile = new File(instanceCollectionDir, "syslog.cfg");
localConfigFilePath = localConfigFile.getAbsolutePath();
if (copyRemoteConfigFile(sshCommand, localConfigFilePath, LogFileSource.SYSLOG)) {
remoteLogPath = NonJavaLogger.getLogFilePathFromConfigFile(localConfigFilePath);
localConfigFile.delete();
} else {
logger.warn("Couldn't retrieve E3 syslog config file from host {}", ipAddress);
}
if ((remoteLogPath == null) || (remoteLogPath.length() == 0)) {
// Try default path
remoteLogPath = NonJavaLogger.defaultLogFilePath;
logger.warn("Instance at {} does not specify an E3-specific syslog file, trying default: {}", ipAddress, remoteLogPath);
}
localTargetDir = createLocalLogTargetDir(instanceCollectionDir, LogFileSource.SYSLOG);
if (localTargetDir == null) {
logger.warn("Couldn't create log-collection directory: {}", instanceCollectionDir + File.separator + LogFileSource.SYSLOG.toString());
} else {
File remoteLog = new File(remoteLogPath);
List<FileInfo> logList = getMatchingRemoteFileList(sshCommand, remoteLog.getParent(), remoteLog.getName());
for (FileInfo remoteFileInfo : logList) {
File localCopy = new File(localTargetDir, targetNameForLogFile(remoteFileInfo, localTargetDir));
try {
bytesCollected += copyRemoteFileWithWorkingTemp(sshCommand, remoteFileInfo, localCopy.getAbsolutePath(), deleteAfterCollect);
} catch (Exception ex) {
// Continue copy attempts if we experience an error
logger.warn("Failed to copy remote file: {} ({})", remoteFileInfo.filePath, ex.getLocalizedMessage());
}
}
}
// We're done - disconnect and return number of bytes copied
sshCommand.disconnect();
if(logger.isDebugEnabled()) {
logger.debug("connected/disconnected!");
}
return bytesCollected;
}