1
2
3
4
5
6
7
8
9
10
11 package psiprobe.beans;
12
13 import java.lang.management.ManagementFactory;
14
15 import javax.management.MBeanServer;
16 import javax.management.MalformedObjectNameException;
17 import javax.management.ObjectName;
18
19 import org.slf4j.Logger;
20 import org.slf4j.LoggerFactory;
21
22 import psiprobe.model.jmx.RuntimeInformation;
23 import psiprobe.tools.JmxTools;
24
25
26
27
28 public class RuntimeInfoAccessorBean {
29
30
31 private static final Logger logger = LoggerFactory.getLogger(RuntimeInfoAccessorBean.class);
32
33
34
35
36
37
38 public RuntimeInformation getRuntimeInformation() {
39 MBeanServer mbeanServer = ManagementFactory.getPlatformMBeanServer();
40 RuntimeInformation ri = new RuntimeInformation();
41
42 try {
43 ObjectName objectNameRuntime = new ObjectName("java.lang:type=Runtime");
44 ri.setStartTime(JmxTools.getLongAttr(mbeanServer, objectNameRuntime, "StartTime"));
45 ri.setUptime(JmxTools.getLongAttr(mbeanServer, objectNameRuntime, "Uptime"));
46 ri.setVmVendor(JmxTools.getStringAttr(mbeanServer, objectNameRuntime, "VmVendor"));
47
48 ObjectName objectNameOperationSystem = new ObjectName("java.lang:type=OperatingSystem");
49 ri.setOsName(JmxTools.getStringAttr(mbeanServer, objectNameOperationSystem, "Name"));
50 ri.setOsVersion(JmxTools.getStringAttr(mbeanServer, objectNameOperationSystem, "Version"));
51
52 if (!ri.getVmVendor().startsWith("IBM Corporation")) {
53 ri.setTotalPhysicalMemorySize(JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem,
54 "TotalPhysicalMemorySize"));
55 ri.setCommittedVirtualMemorySize(JmxTools.getLongAttr(mbeanServer,
56 objectNameOperationSystem, "CommittedVirtualMemorySize"));
57 ri.setFreePhysicalMemorySize(
58 JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "FreePhysicalMemorySize"));
59 ri.setFreeSwapSpaceSize(
60 JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "FreeSwapSpaceSize"));
61 ri.setTotalSwapSpaceSize(
62 JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "TotalSwapSpaceSize"));
63 ri.setProcessCpuTime(
64 JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "ProcessCpuTime"));
65 ri.setAvailableProcessors(Runtime.getRuntime().availableProcessors());
66 } else {
67 ri.setTotalPhysicalMemorySize(
68 JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "TotalPhysicalMemory"));
69 }
70
71 if (JmxTools.hasAttribute(mbeanServer, objectNameOperationSystem, "OpenFileDescriptorCount")
72 && JmxTools.hasAttribute(mbeanServer, objectNameOperationSystem,
73 "MaxFileDescriptorCount")) {
74
75 ri.setOpenFileDescriptorCount(JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem,
76 "OpenFileDescriptorCount"));
77 ri.setMaxFileDescriptorCount(
78 JmxTools.getLongAttr(mbeanServer, objectNameOperationSystem, "MaxFileDescriptorCount"));
79 }
80
81 return ri;
82 } catch (MalformedObjectNameException e) {
83 logger.debug("OS information is unavailable");
84 logger.trace("", e);
85 return null;
86 }
87 }
88 }