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

1 /*
2 * The MIT License
3 *
4 * Copyright (c) 2004-2009, Sun Microsystems, Inc., Kohsuke Kawaguchi, Seiji Sogabe
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.node_monitors;
25
26 import hudson.Util;
27 import hudson.Extension;
28 import hudson.model.Computer;
29 import jenkins.model.Jenkins;
30 import hudson.remoting.Callable;
31 import net.sf.json.JSONObject;
32 import org.jvnet.hudson.MemoryMonitor;
33 import org.jvnet.hudson.MemoryUsage;
34 import org.kohsuke.stapler.StaplerRequest;
35 import org.kohsuke.stapler.export.ExportedBean;
36 import org.kohsuke.stapler.export.Exported;
37
38 import java.io.IOException;
39
40 /**
41 * Checks the swap space availability.
42 *
43 * @author Kohsuke Kawaguchi
44 * @sine 1.233
45 */
46 public class SwapSpaceMonitor extends NodeMonitor {
47 /**
48 * Returns the HTML representation of the space.
49 */
50 public String toHtml(MemoryUsage usage) {
51 if(usage.availableSwapSpace==-1)
52 return "N/A";
53
54 long free = usage.availableSwapSpace;
55 free/=1024L; // convert to KB
56 free/=1024L; // convert to MB
57 if(free>256 || usage.totalSwapSpace<usage.availableSwapSpace*5)
58 return free+"MB"; // if we have more than 256MB free or less than 80% filled up, it's OK
59
60 // Otherwise considered dangerously low.
61 return Util.wrapToErrorSpan(free+"MB");
62 }
63
64 public long toMB(MemoryUsage usage) {
65 if(usage.availableSwapSpace==-1)
66 return -1;
67
68 long free = usage.availableSwapSpace;
69 free/=1024L; // convert to KB
70 free/=1024L; // convert to MB
71 return free;
72 }
73
74 @Override
75 public String getColumnCaption() {
76 // Hide this column from non-admins
77 return Jenkins.getInstance().hasPermission(Jenkins.ADMINISTER) ? super.getColumnCaption() : null;
78 }
79
80 @Extension
81 public static final AbstractNodeMonitorDescriptor<MemoryUsage> DESCRIPTOR = new AbstractNodeMonitorDescriptor<MemoryUsage>() {
82 protected MemoryUsage monitor(Computer c) throws IOException, InterruptedException {
83 return c.getChannel().call(new MonitorTask());
84 }
85
86 public String getDisplayName() {
87 return Messages.SwapSpaceMonitor_DisplayName();
88 }
89
90 @Override
91 public NodeMonitor newInstance(StaplerRequest req, JSONObject formData) throws FormException {
92 return new SwapSpaceMonitor();
93 }
94 };
95
96 /**
97 * Obtains the string that represents the architecture.
98 */
99 private static class MonitorTask implements Callable<MemoryUsage,IOException> {
100 public MemoryUsage call() throws IOException {
101 MemoryMonitor mm;
102 try {
103 mm = MemoryMonitor.get();
104 } catch (IOException e) {
105 if(!warned) {
106 // report the problem just once, and avoid filling up the log with the same error. see HUDSON-2194.
107 warned = true;
108 throw e;
109 } else {
110 return null;
111 }
112 }
113 return new MemoryUsage2(mm.monitor());
114 }
115
116 private static final long serialVersionUID = 1L;
117
118 private static boolean warned = false;
119 }
120
121 /**
122 * Memory Usage.
123 *
124 * <p>
125 * {@link MemoryUsage} + stapler annotations.
126 */
127 @ExportedBean
128 public static class MemoryUsage2 extends MemoryUsage {
129 private static final long serialVersionUID = 2216994637932270352L;
130
131 public MemoryUsage2(MemoryUsage mem) {
132 super(mem.totalPhysicalMemory, mem.availablePhysicalMemory, mem.totalSwapSpace, mem.availableSwapSpace);
133 }
134
135 /**
136 * Total physical memory of the system, in bytes.
137 */
138 @Exported
139 public long getTotalPhysicalMemory() {
140 return totalPhysicalMemory;
141 }
142
143 /**
144 * Of the total physical memory of the system, available bytes.
145 */
146 @Exported
147 public long getAvailablePhysicalMemory() {
148 return availablePhysicalMemory;
149 }
150
151 /**
152 * Total number of swap space in bytes.
153 */
154 @Exported
155 public long getTotalSwapSpace() {
156 return totalSwapSpace;
157 }
158
159 /**
160 * Available swap space in bytes.
161 */
162 @Exported
163 public long getAvailableSwapSpace() {
164 return availableSwapSpace;
165 }
166 }
167 }
168