2

How to track system load average using java.The result should be same the load average is returning form the 'uptime' command in Linux.

Sample uptime command result is,

root@jamsheer:~# uptime

16:01:06 up  6:58,  2 users,  load average: 1.24, 1.25, 1.33

I need to get same load average through programmatically and should be executable at all platforms, Any suggestion, please.

Thanks in advance.

Jamsheer
  • 3,673
  • 3
  • 29
  • 57
  • 1
    You can execute the uptime command from Java Runtime and parse the information from the result. – Rahul Feb 08 '18 at 10:43
  • ok, Is there any solution available to execute in cross platforms? – Jamsheer Feb 08 '18 at 10:45
  • Check my answer here, it has a sample code snippet to execute the command - https://stackoverflow.com/questions/48325178/in-java-8-how-do-i-get-my-hostname-without-hard-coding-it-in-my-environment/48462667#48462667 – Rahul Feb 08 '18 at 10:53
  • You mean you also want to execute the uptime command in windows or macos ? – Erwin Smout Feb 08 '18 at 11:32
  • Right, Should be executable in all platforms, Thank you. – Jamsheer Feb 08 '18 at 11:35

1 Answers1

2

For platform independent result, check JMX and OperatingSystemMXBean:

OperatingSystemMXBean mxBean = ManagementFactory.getOperatingSystemMXBean();
double loadAverage = mxBean.getSystemLoadAverage();
 

Please mind that if your platform is unable to report it, the value returned will be negative, which is the result in my case on Win 7 box.

yglodt
  • 13,807
  • 14
  • 91
  • 127
diginoise
  • 7,352
  • 2
  • 31
  • 39