DiskSpaceMonitor.java 2.7 KB
Newer Older
K
kohsuke 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 * The MIT License
 * 
 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi
 * 
 * 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.
 */
K
kohsuke 已提交
24 25
package hudson.node_monitors;

26
import hudson.Extension;
K
kohsuke 已提交
27
import hudson.FilePath;
28
import hudson.Functions;
K
kohsuke 已提交
29
import hudson.model.Computer;
30
import hudson.model.Hudson;
31
import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
32
import net.sf.json.JSONObject;
K
kohsuke 已提交
33 34 35
import org.kohsuke.stapler.StaplerRequest;

import java.io.IOException;
36

K
kohsuke 已提交
37
/**
38 39
 * Checks available disk space of the remote FS root.
 * Requires Mustang.
K
kohsuke 已提交
40 41 42 43 44
 *
 * @author Kohsuke Kawaguchi
 * @since 1.123
 */
public class DiskSpaceMonitor extends NodeMonitor {
45
    public DiskSpace getFreeSpace(Computer c) {
K
kohsuke 已提交
46 47 48
        return DESCRIPTOR.get(c);
    }

49 50 51 52 53 54
    @Override
    public String getColumnCaption() {
        // Hide this column from non-admins
        return Hudson.getInstance().hasPermission(Hudson.ADMINISTER) ? super.getColumnCaption() : null;
    }

55
    public static final DiskSpaceMonitorDescriptor DESCRIPTOR = new DiskSpaceMonitorDescriptor() {
K
kohsuke 已提交
56
        public String getDisplayName() {
57
            return Messages.DiskSpaceMonitor_displayName();
K
kohsuke 已提交
58 59
        }

60 61
        @Override
        public NodeMonitor newInstance(StaplerRequest req, JSONObject formData) throws FormException {
K
kohsuke 已提交
62 63
            return new DiskSpaceMonitor();
        }
64 65 66 67 68 69 70

        protected DiskSpace getFreeSpace(Computer c) throws IOException, InterruptedException {
            FilePath p = c.getNode().getRootPath();
            if(p==null) return null;

            return p.act(new GetUsableSpace());
        }
K
kohsuke 已提交
71
    };
K
kohsuke 已提交
72

73
    @Extension
74
    public static DiskSpaceMonitorDescriptor install() {
75 76 77
        if(Functions.isMustangOrAbove())    return DESCRIPTOR;
        return null;
    }
K
kohsuke 已提交
78
}