提交 66acb4d7 编写于 作者: K kohsuke

[FIXED HUDSON-2552] Applied a modified version of the patch that improves the...

[FIXED HUDSON-2552] Applied a modified version of the patch that improves the usability and code reuse

git-svn-id: https://hudson.dev.java.net/svn/hudson/trunk/hudson/main@24705 71c3de6d-444a-0410-be80-ed276b4c234a
上级 11f60f6f
......@@ -56,17 +56,25 @@ public class HudsonHomeDiskUsageChecker extends PeriodicWork {
LOGGER.fine("Monitoring disk usage of HUDSON_HOME. total="+total+" free="+free);
// if it's more than 90% full and less than 1GB, activate
// if it's more than 90% full and less than the minimum, activate
// it's AND and not OR so that small Hudson home won't get a warning,
// and similarly, if you have a 1TB disk, you don't get a warning when you still have 100GB to go.
HudsonHomeDiskUsageMonitor.get().activated = (total/free>10 && free<1024L*1024*1024);
HudsonHomeDiskUsageMonitor.get().activated = (total/free>10 && free< FREE_SPACE_THRESHOLD);
} catch (LinkageError _) {
// pre Mustang
LOGGER.info("Not on JDK6. Cannot monitor HUDSON_HOME disk usage");
cancel();
return;
}
}
private static final Logger LOGGER = Logger.getLogger(HudsonHomeDiskUsageChecker.class.getName());
/**
* Gets the minimum amount of space to check for, with a default of 1GB
*/
public static long FREE_SPACE_THRESHOLD = Long.getLong(
HudsonHomeDiskUsageChecker.class.getName() + ".freeSpaceThreshold",
1024L*1024*1024);
}
package hudson.node_monitors;
import hudson.model.Computer;
import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
import org.kohsuke.stapler.DataBoundConstructor;
import java.text.ParseException;
import java.util.logging.Logger;
/**
* @author Kohsuke Kawaguchi
*/
public abstract class AbstractDiskSpaceMonitor extends NodeMonitor {
/**
* The free space threshold, below which the node monitor will be triggered.
* This is a human readable string representation as entered by the user, so that we can retain the original notation.
*/
public final String freeSpaceThreshold;
@DataBoundConstructor
public AbstractDiskSpaceMonitor(String threshold) throws ParseException {
this.freeSpaceThreshold = threshold;
DiskSpace.parse(threshold); // make sure it parses
}
public long getThresholdBytes() {
if (freeSpaceThreshold==null)
return DEFAULT_THRESHOLD; // backward compatibility with the data format that didn't have 'freeSpaceThreshold'
try {
return DiskSpace.parse(freeSpaceThreshold).size;
} catch (ParseException e) {
return DEFAULT_THRESHOLD;
}
}
@Override
public Object data(Computer c) {
DiskSpace size = (DiskSpace) super.data(c);
if(size!=null && size.size < getThresholdBytes()) {
size.setTriggered(true);
if(getDescriptor().markOffline(c,size)) {
LOGGER.warning(Messages.DiskSpaceMonitor_MarkedOffline(c.getName()));
}
}
return size;
}
private static final Logger LOGGER = Logger.getLogger(AbstractDiskSpaceMonitor.class.getName());
private static final long DEFAULT_THRESHOLD = 1024L*1024*1024;
}
......@@ -29,10 +29,10 @@ import hudson.Functions;
import hudson.model.Computer;
import hudson.model.Hudson;
import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.StaplerRequest;
import org.kohsuke.stapler.DataBoundConstructor;
import java.io.IOException;
import java.text.ParseException;
/**
* Checks available disk space of the remote FS root.
......@@ -41,7 +41,12 @@ import java.io.IOException;
* @author Kohsuke Kawaguchi
* @since 1.123
*/
public class DiskSpaceMonitor extends NodeMonitor {
public class DiskSpaceMonitor extends AbstractDiskSpaceMonitor {
@DataBoundConstructor
public DiskSpaceMonitor(String freeSpaceThreshold) throws ParseException {
super(freeSpaceThreshold);
}
public DiskSpace getFreeSpace(Computer c) {
return DESCRIPTOR.get(c);
}
......@@ -57,11 +62,6 @@ public class DiskSpaceMonitor extends NodeMonitor {
return Messages.DiskSpaceMonitor_DisplayName();
}
@Override
public NodeMonitor newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return new DiskSpaceMonitor();
}
protected DiskSpace getFreeSpace(Computer c) throws IOException, InterruptedException {
FilePath p = c.getNode().getRootPath();
if(p==null) return null;
......
......@@ -34,8 +34,9 @@ import org.jvnet.animal_sniffer.IgnoreJRERequirement;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.logging.Logger;
import java.math.BigDecimal;
import java.text.ParseException;
import org.kohsuke.stapler.export.ExportedBean;
import org.kohsuke.stapler.export.Exported;
......@@ -52,6 +53,8 @@ import org.kohsuke.stapler.export.Exported;
public static final class DiskSpace extends OfflineCause implements Serializable {
@Exported
public final long size;
private boolean triggered;
public DiskSpace(long size) {
this.size = size;
......@@ -80,26 +83,55 @@ import org.kohsuke.stapler.export.Exported;
long space = size;
space/=1024L; // convert to KB
space/=1024L; // convert to MB
if(space<1024) {
if(triggered) {
// less than a GB
return Util.wrapToErrorSpan(new BigDecimal(space).scaleByPowerOfTen(-3).toPlainString()+"GB");
}
return space/1024+"GB";
}
/**
* Sets whether this disk space amount should be treated as outside
* the acceptable conditions or not.
*/
protected void setTriggered(boolean triggered) {
this.triggered = triggered;
}
/**
* Parses a human readable size description like "1GB", "0.5m", etc. into {@link DiskSpace}
*
* @throws ParseException
* If failed to parse.
*/
public static DiskSpace parse(String size) throws ParseException {
size = size.toUpperCase().trim();
if (size.endsWith("B")) // cut off 'B' from KB, MB, etc.
size = size.substring(0,size.length()-1);
long multiplier=1;
// look for the size suffix. The goal here isn't to detect all invalid size suffix,
// so we ignore double suffix like "10gkb" or anything like that.
String suffix = "KMGT";
for (int i=0; i<suffix.length(); i++) {
if (size.endsWith(suffix.substring(i,i+1))) {
multiplier = 1;
for (int j=0; j<i; j++ )
multiplier*=1024;
size = size.substring(0,size.length()-1);
}
}
public boolean moreThanGB() {
return size>1024L*1024*1024;
return new DiskSpace((long)Double.parseDouble(size.trim())*multiplier);
}
private static final long serialVersionUID = 1L;
private static final long serialVersionUID = 2L;
}
protected DiskSpace monitor(Computer c) throws IOException, InterruptedException {
DiskSpace size = getFreeSpace(c);
if(size!=null && !size.moreThanGB() && markOffline(c,size))
LOGGER.warning(Messages.DiskSpaceMonitor_MarkedOffline(c.getName()));
return size;
return getFreeSpace(c);
}
/**
......@@ -121,6 +153,4 @@ import org.kohsuke.stapler.export.Exported;
}
private static final long serialVersionUID = 1L;
}
private static final Logger LOGGER = Logger.getLogger(DiskSpaceMonitor.class.getName());
}
......@@ -23,27 +23,32 @@
*/
package hudson.node_monitors;
import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
import hudson.model.Computer;
import hudson.model.Hudson;
import hudson.FilePath;
import hudson.Extension;
import hudson.FilePath;
import hudson.FilePath.FileCallable;
import hudson.Functions;
import hudson.model.Computer;
import hudson.model.Hudson;
import hudson.node_monitors.DiskSpaceMonitorDescriptor.DiskSpace;
import hudson.remoting.VirtualChannel;
import hudson.FilePath.FileCallable;
import org.kohsuke.stapler.StaplerRequest;
import org.jvnet.animal_sniffer.IgnoreJRERequirement;
import net.sf.json.JSONObject;
import org.kohsuke.stapler.DataBoundConstructor;
import java.io.IOException;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
/**
* Monitors the disk space of "/tmp".
*
* @author Kohsuke Kawaguchi
*/
public class TemporarySpaceMonitor extends NodeMonitor {
public class TemporarySpaceMonitor extends AbstractDiskSpaceMonitor {
@DataBoundConstructor
public TemporarySpaceMonitor(String freeSpaceThreshold) throws ParseException {
super(freeSpaceThreshold);
}
public DiskSpace getFreeSpace(Computer c) {
return DESCRIPTOR.get(c);
}
......@@ -59,11 +64,6 @@ public class TemporarySpaceMonitor extends NodeMonitor {
return Messages.TemporarySpaceMonitor_DisplayName();
}
@Override
public NodeMonitor newInstance(StaplerRequest req, JSONObject formData) throws FormException {
return new TemporarySpaceMonitor();
}
protected DiskSpace getFreeSpace(Computer c) throws IOException, InterruptedException {
FilePath p = c.getNode().getRootPath();
if(p==null) return null;
......@@ -77,7 +77,7 @@ public class TemporarySpaceMonitor extends NodeMonitor {
if(Functions.isMustangOrAbove()) return DESCRIPTOR;
return null;
}
protected static final class GetTempSpace implements FileCallable<DiskSpace> {
@IgnoreJRERequirement
public DiskSpace invoke(File f, VirtualChannel channel) throws IOException {
......
<!--
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.
-->
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry title="${%Free Space Threshold}" field="freeSpaceThreshold">
<f:textbox default="1GB"/>
</f:entry>
</j:jelly>
\ No newline at end of file
<div>
This option configures the amount of minimum amount of free disk space
desired for a slave's proper operation, such as "1.5GB", "100KB", etc.
If a slave is found to have less free disk space than this amount, it will be marked offline.
</div>
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册