Search in sources :

Example 6 with FileContent

use of com.cloudbees.jenkins.support.api.FileContent in project support-core-plugin by jenkinsci.

the class GCLogs method addContents.

@Override
public void addContents(@NonNull Container result) {
    LOGGER.fine("Trying to gather GC logs for support bundle");
    String gcLogFileLocation = getGcLogFileLocation();
    if (gcLogFileLocation == null) {
        LOGGER.config("No GC logging enabled, nothing about it will be retrieved for support bundle.");
        return;
    }
    if (isGcLogRotationConfigured()) {
        handleRotatedLogs(gcLogFileLocation, result);
    } else {
        File file = new File(gcLogFileLocation);
        if (!file.exists()) {
            LOGGER.warning("[Support Bundle] GC Logging apparently configured, " + "but file '" + gcLogFileLocation + "' not found");
            return;
        }
        result.add(new FileContent(GCLOGS_BUNDLE_ROOT + "gc.log", file));
    }
}
Also used : FileContent(com.cloudbees.jenkins.support.api.FileContent) File(java.io.File)

Example 7 with FileContent

use of com.cloudbees.jenkins.support.api.FileContent in project support-core-plugin by jenkinsci.

the class JenkinsLogs method addOtherMasterLogs.

/**
 * Grabs any files that look like log files directly under {@code $JENKINS_HOME}, just in case
 * any of them are useful.
 * Does not add anything if Jenkins instance is unavailable.
 * Some plugins write log files here.
 */
private void addOtherMasterLogs(Container result) {
    final Jenkins jenkins = Jenkins.getInstance();
    File[] files = jenkins.getRootDir().listFiles(ROTATED_LOGFILE_FILTER);
    if (files != null) {
        for (File f : files) {
            result.add(new FileContent("other-logs/" + f.getName(), f));
        }
    }
    File logs = getLogsRoot();
    files = logs.listFiles(ROTATED_LOGFILE_FILTER);
    if (files != null) {
        for (File f : files) {
            result.add(new FileContent("other-logs/" + f.getName(), f));
        }
    }
    File taskLogs = new File(logs, "tasks");
    files = taskLogs.listFiles(ROTATED_LOGFILE_FILTER);
    if (files != null) {
        for (File f : files) {
            result.add(new FileContent("other-logs/" + f.getName(), f));
        }
    }
}
Also used : Jenkins(jenkins.model.Jenkins) FileContent(com.cloudbees.jenkins.support.api.FileContent) File(java.io.File)

Example 8 with FileContent

use of com.cloudbees.jenkins.support.api.FileContent in project support-core-plugin by jenkinsci.

the class SlaveLaunchLogs method addSlaveLaunchLog.

/**
 * <p>
 * In the presence of {@link Cloud} plugins like EC2, we want to find past agents, not just current ones.
 * So we don't try to loop through {@link Node} here but just try to look at the file systems to find them
 * all.
 *
 * <p>
 * Generally these cloud plugins do not clean up old logs, so if run for a long time, the log directory
 * will be full of old files that are not very interesting. Use some heuristics to cut off logs
 * that are old.
 */
private void addSlaveLaunchLog(Container result) {
    class Slave implements Comparable<Slave> {

        /**
         * Launch log directory of the agent: logs/slaves/NAME
         */
        File dir;

        long time;

        Slave(File dir, File lastLog) {
            this.dir = dir;
            this.time = lastLog.lastModified();
        }

        /**
         * Agent name
         */
        String getName() {
            return dir.getName();
        }

        /**
         * Use the primary log file's timestamp to compare newer agents from older agents.
         *
         * sort in descending order; newer ones first.
         */
        public int compareTo(Slave that) {
            long lhs = this.time;
            long rhs = that.time;
            if (lhs < rhs)
                return 1;
            if (lhs > rhs)
                return -1;
            return 0;
        }

        @Override
        public boolean equals(Object o) {
            if (this == o)
                return true;
            if (o == null || getClass() != o.getClass())
                return false;
            Slave slave = (Slave) o;
            if (time != slave.time)
                return false;
            return true;
        }

        @Override
        public int hashCode() {
            return (int) (time ^ (time >>> 32));
        }

        /**
         * If the file is more than a year old, can't imagine how that'd be of any interest.
         */
        public boolean isTooOld() {
            return time < System.currentTimeMillis() - TimeUnit.DAYS.toMillis(365);
        }
    }
    List<Slave> all = new ArrayList<Slave>();
    {
        // find all the agent launch log files and sort them newer ones first
        File slaveLogsDir = new File(Jenkins.getInstance().getRootDir(), "logs/slaves");
        File[] logs = slaveLogsDir.listFiles();
        if (logs != null) {
            for (File dir : logs) {
                File lastLog = new File(dir, "slave.log");
                if (lastLog.exists()) {
                    Slave s = new Slave(dir, lastLog);
                    // we don't care
                    if (s.isTooOld())
                        continue;
                    all.add(s);
                }
            }
        }
        Collections.sort(all);
    }
    {
        // this might be still too many, so try to cap them.
        int acceptableSize = Math.max(256, Jenkins.getInstance().getNodes().size() * 5);
        if (all.size() > acceptableSize)
            all = all.subList(0, acceptableSize);
    }
    // now add them all
    for (Slave s : all) {
        File[] files = s.dir.listFiles(ROTATED_LOGFILE_FILTER);
        if (files != null)
            for (File f : files) {
                result.add(new FileContent("nodes/slave/" + s.getName() + "/launchLogs/" + f.getName(), f, FileListCapComponent.MAX_FILE_SIZE));
            }
    }
}
Also used : FileContent(com.cloudbees.jenkins.support.api.FileContent) ArrayList(java.util.ArrayList) File(java.io.File)

Aggregations

FileContent (com.cloudbees.jenkins.support.api.FileContent)8 File (java.io.File)6 ArrayList (java.util.ArrayList)3 IOException (java.io.IOException)2 InterruptedIOException (java.io.InterruptedIOException)2 List (java.util.List)2 Map (java.util.Map)2 ExecutionException (java.util.concurrent.ExecutionException)2 TimeoutException (java.util.concurrent.TimeoutException)2 PrintedContent (com.cloudbees.jenkins.support.api.PrintedContent)1 FilePath (hudson.FilePath)1 LogRecorder (hudson.logging.LogRecorder)1 Computer (hudson.model.Computer)1 Node (hudson.model.Node)1 VirtualChannel (hudson.remoting.VirtualChannel)1 SlaveComputer (hudson.slaves.SlaveComputer)1 DaemonThreadFactory (hudson.util.DaemonThreadFactory)1 ExceptionCatchingThreadFactory (hudson.util.ExceptionCatchingThreadFactory)1 FilenameFilter (java.io.FilenameFilter)1 PrintWriter (java.io.PrintWriter)1