1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
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 }