File D:\maven-qcr-plugin-testsunitaires\qcr4java\jenkins-master\target\site\qc\core\src\main\java\hudson\diagnosis\HudsonHomeDiskUsageChecker.java.html

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
24 package hudson.diagnosis;
25
26 import hudson.Extension;
27 import jenkins.model.Jenkins;
28 import hudson.model.PeriodicWork;
29 import org.codehaus.mojo.animal_sniffer.IgnoreJRERequirement;
30
31 import java.util.logging.Logger;
32
33 /**
34 * Periodically checks the disk usage of <tt>JENKINS_HOME</tt>,
35 * and activate {@link HudsonHomeDiskUsageMonitor} if necessary.
36 *
37 * @author Kohsuke Kawaguchi
38 */
39 @Extension
40 public class HudsonHomeDiskUsageChecker extends PeriodicWork {
41 public long getRecurrencePeriod() {
42 return HOUR;
43 }
44
45 @IgnoreJRERequirement
46 protected void doRun() {
47 try {
48 long free = Jenkins.getInstance().getRootDir().getUsableSpace();
49 long total = Jenkins.getInstance().getRootDir().getTotalSpace();
50 if(free<=0 || total<=0) {
51 // information unavailable. pointless to try.
52 LOGGER.info("JENKINS_HOME disk usage information isn't available. aborting to monitor");
53 cancel();
54 return;
55 }
56
57 LOGGER.fine("Monitoring disk usage of JENKINS_HOME. total="+total+" free="+free);
58
59
60 // if it's more than 90% full and less than the minimum, activate
61 // it's AND and not OR so that small Hudson home won't get a warning,
62 // and similarly, if you have a 1TB disk, you don't get a warning when you still have 100GB to go.
63 HudsonHomeDiskUsageMonitor.get().activated = (total/free>10 && free< FREE_SPACE_THRESHOLD);
64 } catch (LinkageError _) {
65 // pre Mustang
66 LOGGER.info("Not on JDK6. Cannot monitor JENKINS_HOME disk usage");
67 cancel();
68 }
69 }
70
71 private static final Logger LOGGER = Logger.getLogger(HudsonHomeDiskUsageChecker.class.getName());
72
73 /**
74 * Gets the minimum amount of space to check for, with a default of 1GB
75 */
76 public static long FREE_SPACE_THRESHOLD = Long.getLong(
77 HudsonHomeDiskUsageChecker.class.getName() + ".freeSpaceThreshold",
78 1024L*1024*1024);
79
80 }
81