What is uptime
in Linux? Sometimes you have to get load averages from uptime result via Java Program to perform certain operations. Like nuke, restart app, etc based on high load average.
I’m talking about Linux uptime
Command result. There are multiple Java Management APIs available under RuntimeMXBean class but we will discuss all possibilities about it later. There is a sample example on how to get uptime using RuntimeMXBean which I’ve published sometime back if you want to check. One thing to note here is: uptime result may vary based on platform, i.e. different result for Mac, Linux, Unix, etc.
Let’s focus on Linux based uptime result only. You may have below questions:
- How to Parse the output of “uptime” with bash
- Linux: Getting date & time of system startup using uptime
- Correctly grep and display the uptime, load average
- uptime – Linux: Getting date & time of system startup
- Parse Linux Uptime result
Here is a sample result of uptime
command:
sh-4.1# uptime 10:28:21 up 189 days, 19:00, 1 user, load average: 0.07, 0.13, 0.16
Now in this Java example we will follow below steps:
- Create class
CrunchifyGetUptimeResultAndParse.java
- Execute uptime command via
ProcessBuilder
Java API - Get result into
uptimeCmdResult
variable - Apply Java Regex (Pattern matching) operation on variable
uptimeCmdResult
to get 1 min, 5 min, 15 min load average
Here is a Pattern:
^.*\\s+load\\s+average:\\s+([\\d\\.]+),\\s+([\\d\\.]+),\\s+([\\d\\.]+)$
package com.crunchify.tutorial; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; import java.io.Reader; import java.io.StringWriter; import java.io.Writer; import java.util.regex.Matcher; import java.util.regex.Pattern; /** * @author Crunchify.com * */ public class CrunchifyGetUptimeResultAndParse { public static final Pattern crunchifyUptimePattern = Pattern .compile("^.*\\s+load\\s+average:\\s+([\\d\\.]+),\\s+([\\d\\.]+),\\s+([\\d\\.]+)$"); public static void main(String[] args) { String uptimeCmd = "uptime"; String uptimeCmdResult = runUptimeCommand(uptimeCmd, true); System.out.println(uptimeCmdResult); crunchifyParseUptimeResult(uptimeCmdResult); } public static void crunchifyParseUptimeResult(String uptimeCmdResult) { Matcher m = crunchifyUptimePattern.matcher(uptimeCmdResult); if (m.matches()) { double oneMinuteLoadAvg = Double.parseDouble(m.group(1)); double fiveMinuteloadAvg = Double.parseDouble(m.group(2)); double fifteenMinuteLoadAvg = Double.parseDouble(m.group(3)); System.out.println("1 min Load Average: " + oneMinuteLoadAvg); System.out.println("5 min Load Average: " + fiveMinuteloadAvg); System.out.println("15 min Load Average: " + fifteenMinuteLoadAvg); } else { System.out.println("Exception while parsing Uptime Result: "); } } public static String runUptimeCommand(String crunchifyCmd, boolean waitForResult) { System.out.println("Executing Uptime command"); ProcessBuilder crunchifyProcessBuilder = null; crunchifyProcessBuilder = new ProcessBuilder("/bin/bash", "-c", crunchifyCmd); crunchifyProcessBuilder.redirectErrorStream(true); Writer crunchifyWriter = null; try { Process process = crunchifyProcessBuilder.start(); if (waitForResult) { InputStream crunchifyStream = process.getInputStream(); if (crunchifyStream != null) { crunchifyWriter = new StringWriter(); char[] crunchifyBuffer = new char[2048]; try { Reader crunchifyReader = new BufferedReader(new InputStreamReader(crunchifyStream, "UTF-8")); int count; while ((count = crunchifyReader.read(crunchifyBuffer)) != -1) { crunchifyWriter.write(crunchifyBuffer, 0, count); } } finally { crunchifyStream.close(); } crunchifyWriter.toString(); crunchifyStream.close(); } } } catch (Exception e) { System.out.println("Error Executing tcpdump command" + e); } return crunchifyWriter.toString(); } }
Result:
Executing Uptime command 12:30 up 46 mins, 2 users, load averages: 1.57 1.53 1.45 1 min Load Average: 1.57 5 min Load Average: 1.53 15 min Load Average: 1.45
Bonus Regex Operations for uptime result in Mac:
Just returns number of users:
bash-3.2$ uptime 12:36 up 51 mins, 2 users, load averages: 1.39 1.52 1.47 bash-3.2$ uptime | tr "," " " | cut -f6-8 -d" " 2 users
Just returns mins
bash-3.2$ uptime 12:36 up 51 mins, 2 users, load averages: 1.39 1.52 1.47 bash-3.2$ uptime | sed 's/.*up \([^,]*\), .*/\1/' 51 mins