View Javadoc

1   /*
2    * Copyright 2010 Capgemini and Contributors
3    *
4    * Licensed under the Apache License, Version 2.0 (the
5    * "License"); you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13   * License for the specific language governing permissions and limitations under
14   * the License.
15   */
16  package net.sf.appstatus.core.check.impl;
17  
18  import java.lang.management.ManagementFactory;
19  import java.lang.management.MemoryMXBean;
20  import java.lang.management.MemoryUsage;
21  import java.util.Locale;
22  import java.util.Properties;
23  
24  import net.sf.appstatus.core.check.AbstractCheck;
25  import net.sf.appstatus.core.check.CheckResultBuilder;
26  import net.sf.appstatus.core.check.ICheckResult;
27  
28  /**
29   * @author Nicolas Richeton
30   *
31   */
32  public class JvmCheck extends AbstractCheck {
33  
34  	private int limitError = 95;
35  	private int limitWarn = 80;
36  
37  	@Override
38  	public ICheckResult checkStatus(Locale locale) {
39  		MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
40  		MemoryUsage heap = memory.getHeapMemoryUsage();
41  		long heapRatio = heap.getUsed() * 100 / heap.getMax();
42  		CheckResultBuilder result = result(this).messageBundle("net.sf.appstatus.core.check.impl.JvmCheck_msg", locale);
43  		if (heapRatio > limitError) {
44  			result.code(ICheckResult.ERROR).fatal().resolutionSteps("resolutionSteps.fatal", new Object[] {});
45  		} else if (heapRatio > limitWarn) {
46  			result.code(ICheckResult.ERROR).resolutionSteps("resolutionSteps.warn", new Object[] {});
47  		} else {
48  			result.code(ICheckResult.OK);
49  		}
50  		result.description("description", new Object[] { heapRatio });
51  		return result.build();
52  	}
53  
54  	public String getGroup() {
55  		return "JVM";
56  	}
57  
58  	public String getName() {
59  		return "Heap usage";
60  	}
61  
62  	@Override
63  	public void setConfiguration(Properties configuration) {
64  		super.setConfiguration(configuration);
65  
66  		String error = getConfiguration().getProperty("jvmCheck.limitError");
67  		if (error != null) {
68  			limitError = Integer.valueOf(error);
69  		}
70  
71  		String warn = getConfiguration().getProperty("jvmCheck.limitWarn");
72  		if (warn != null) {
73  			limitWarn = Integer.valueOf(warn);
74  		}
75  
76  	}
77  
78  }