1 package net.sf.appstatus.core.property.impl;
2
3 import java.lang.management.ManagementFactory;
4 import java.lang.management.MemoryMXBean;
5 import java.lang.management.MemoryUsage;
6 import java.lang.management.RuntimeMXBean;
7 import java.text.DecimalFormat;
8 import java.text.SimpleDateFormat;
9 import java.util.Date;
10 import java.util.List;
11 import java.util.Map;
12 import java.util.TreeMap;
13
14 import org.apache.commons.lang3.time.DurationFormatUtils;
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 public class JvmPropertyProvider extends AbstractPropertyProvider {
32
33 private static String readableFileSize(long size) {
34 if (size <= 0) {
35 return "0";
36 }
37 final String[] units = new String[] { "B", "KB", "MB", "GB", "TB" };
38 int digitGroups = (int) (Math.log10(size) / Math.log10(1024));
39 return new DecimalFormat("#,##0.#").format(size / Math.pow(1024, digitGroups)) + " " + units[digitGroups];
40 }
41
42 public String getCategory() {
43 return "JVM";
44 }
45
46 public Map<String, String> getProperties() {
47 Map<String, String> map = new TreeMap<String, String>();
48 RuntimeMXBean runtimeMXBean = ManagementFactory.getRuntimeMXBean();
49
50 List<String> aList = runtimeMXBean.getInputArguments();
51 String parameters = "";
52 for (int i = 0; i < aList.size(); i++) {
53 parameters = parameters + " " + aList.get(i);
54 }
55 map.put("params", parameters);
56
57 map.put("name", runtimeMXBean.getVmName());
58 map.put("vendor", runtimeMXBean.getVmVendor());
59 map.put("version", runtimeMXBean.getVmVersion());
60 map.put("specification", runtimeMXBean.getSpecVersion());
61 map.put("uptime", DurationFormatUtils.formatDurationHMS(runtimeMXBean.getUptime()));
62
63 Date date = new Date(runtimeMXBean.getStartTime());
64 SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
65 map.put("start time", sdf.format(date));
66
67 MemoryMXBean memory = ManagementFactory.getMemoryMXBean();
68
69 MemoryUsage heap = memory.getHeapMemoryUsage();
70 map.put("memory. (heap)", readableFileSize(heap.getUsed()) + "/" + readableFileSize(heap.getCommitted()) + " min:" + readableFileSize(heap.getInit()) + " max:" + readableFileSize(heap.getMax()));
71 MemoryUsage nonheap = memory.getNonHeapMemoryUsage();
72 map.put("memory (non heap)", readableFileSize(nonheap.getUsed()) + "/" + readableFileSize(nonheap.getCommitted()) + " min:" + readableFileSize(nonheap.getInit()) + " max:" + readableFileSize(nonheap.getMax()));
73
74 return map;
75 }
76
77 }