DiskSpaceMonitorDescriptor.java 4.3 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
/*
 * The MIT License
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package hudson.node_monitors;

import hudson.FilePath.FileCallable;
import hudson.model.Computer;
import hudson.remoting.VirtualChannel;
import hudson.Util;
30
import hudson.slaves.OfflineCause;
31 32 33 34 35
import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
import org.jvnet.animal_sniffer.IgnoreJRERequirement;

import java.io.File;
import java.io.IOException;
36
import java.io.Serializable;
37 38
import java.util.logging.Logger;
import java.math.BigDecimal;
39
import org.kohsuke.stapler.export.ExportedBean;
40
import org.kohsuke.stapler.export.Exported;
41 42 43 44 45 46 47 48 49

/**
 * {@link AbstractNodeMonitorDescriptor} for {@link NodeMonitor} that checks a free disk space of some directory.
 *
 * @author Kohsuke Kawaguchi
*/
/*package*/ abstract class DiskSpaceMonitorDescriptor extends AbstractNodeMonitorDescriptor<DiskSpace> {
    /**
     * Value object that represents the disk space.
50 51
     */
    @ExportedBean
52 53
    public static final class DiskSpace extends OfflineCause implements Serializable {
        @Exported
54 55 56 57 58 59 60 61 62 63 64
        public final long size;

        public DiskSpace(long size) {
            this.size = size;
        }

        @Override
        public String toString() {
            return String.valueOf(size);
        }

65 66 67 68 69 70 71 72 73 74 75
        /**
         * Gets GB left.
         */
        public String getGbLeft() {
            long space = size;
            space/=1024L;   // convert to KB
            space/=1024L;   // convert to MB

            return new BigDecimal(space).scaleByPowerOfTen(-3).toPlainString();
        }

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        /**
         * Returns the HTML representation of the space.
         */
        public String toHtml() {
            long space = size;
            space/=1024L;   // convert to KB
            space/=1024L;   // convert to MB
            if(space<1024) {
                // less than a GB
                return Util.wrapToErrorSpan(new BigDecimal(space).scaleByPowerOfTen(-3).toPlainString()+"GB");
            }

            return space/1024+"GB";
        }

        public boolean moreThanGB() {
            return size>1024L*1024*1024;
        }
94 95

        private static final long serialVersionUID = 1L;
96 97 98 99
    }

    protected DiskSpace monitor(Computer c) throws IOException, InterruptedException {
        DiskSpace size = getFreeSpace(c);
100
        if(size!=null && !size.moreThanGB() && markOffline(c,size))
101
            LOGGER.warning(Messages.DiskSpaceMonitor_MarkedOffline(c.getName()));
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        return size;
    }

    /**
     * Computes the free size.
     */
    protected abstract DiskSpace getFreeSpace(Computer c) throws IOException, InterruptedException;

    protected static final class GetUsableSpace implements FileCallable<DiskSpace> {
        @IgnoreJRERequirement
        public DiskSpace invoke(File f, VirtualChannel channel) throws IOException {
            try {
                long s = f.getUsableSpace();
                if(s<=0)    return null;
                return new DiskSpace(s);
            } catch (LinkageError e) {
                // pre-mustang
                return null;
            }
        }
        private static final long serialVersionUID = 1L;
    }

    private static final Logger LOGGER = Logger.getLogger(DiskSpaceMonitor.class.getName());
}