|
|
|
@ -14,8 +14,14 @@
|
|
|
|
|
* See the License for the specific language governing permissions and |
|
|
|
|
* limitations under the License. |
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
package org.apache.dolphinscheduler.common.utils; |
|
|
|
|
|
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.shell.ShellExecutor; |
|
|
|
|
|
|
|
|
|
import org.apache.commons.configuration.Configuration; |
|
|
|
|
|
|
|
|
|
import java.io.BufferedReader; |
|
|
|
|
import java.io.FileInputStream; |
|
|
|
|
import java.io.IOException; |
|
|
|
@ -32,9 +38,6 @@ import java.util.Optional;
|
|
|
|
|
import java.util.StringTokenizer; |
|
|
|
|
import java.util.regex.Pattern; |
|
|
|
|
|
|
|
|
|
import org.apache.commons.configuration.Configuration; |
|
|
|
|
import org.apache.dolphinscheduler.common.Constants; |
|
|
|
|
import org.apache.dolphinscheduler.common.shell.ShellExecutor; |
|
|
|
|
import org.slf4j.Logger; |
|
|
|
|
import org.slf4j.LoggerFactory; |
|
|
|
|
|
|
|
|
@ -45,7 +48,6 @@ import oshi.hardware.HardwareAbstractionLayer;
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* os utils |
|
|
|
|
* |
|
|
|
|
*/ |
|
|
|
|
public class OSUtils { |
|
|
|
|
|
|
|
|
@ -64,7 +66,9 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
private static HardwareAbstractionLayer hal = SI.getHardware(); |
|
|
|
|
|
|
|
|
|
private OSUtils() {} |
|
|
|
|
private OSUtils() { |
|
|
|
|
throw new UnsupportedOperationException("Construct OSUtils"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Initialization regularization, solve the problem of pre-compilation performance, |
|
|
|
@ -72,10 +76,10 @@ public class OSUtils {
|
|
|
|
|
*/ |
|
|
|
|
private static final Pattern PATTERN = Pattern.compile("\\s+"); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get memory usage |
|
|
|
|
* Keep 2 decimal |
|
|
|
|
* |
|
|
|
|
* @return percent % |
|
|
|
|
*/ |
|
|
|
|
public static double memoryUsage() { |
|
|
|
@ -87,11 +91,11 @@ public class OSUtils {
|
|
|
|
|
return Double.parseDouble(df.format(memoryUsage)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get available physical memory size |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* Keep 2 decimal |
|
|
|
|
* |
|
|
|
|
* @return available Physical Memory Size, unit: G |
|
|
|
|
*/ |
|
|
|
|
public static double availablePhysicalMemorySize() { |
|
|
|
@ -106,8 +110,9 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get total physical memory size |
|
|
|
|
* |
|
|
|
|
* <p> |
|
|
|
|
* Keep 2 decimal |
|
|
|
|
* |
|
|
|
|
* @return available Physical Memory Size, unit: G |
|
|
|
|
*/ |
|
|
|
|
public static double totalMemorySize() { |
|
|
|
@ -119,7 +124,6 @@ public class OSUtils {
|
|
|
|
|
return Double.parseDouble(df.format(availablePhysicalMemorySize)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* load average |
|
|
|
|
* |
|
|
|
@ -194,6 +198,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get user list from mac |
|
|
|
|
* |
|
|
|
|
* @return user list |
|
|
|
|
*/ |
|
|
|
|
private static List<String> getUserListFromMac() throws IOException { |
|
|
|
@ -207,8 +212,8 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get user list from windows |
|
|
|
|
* |
|
|
|
|
* @return user list |
|
|
|
|
* @throws IOException |
|
|
|
|
*/ |
|
|
|
|
private static List<String> getUserListFromWindows() throws IOException { |
|
|
|
|
String result = exeCmd("net user"); |
|
|
|
@ -247,6 +252,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* create user |
|
|
|
|
* |
|
|
|
|
* @param userName user name |
|
|
|
|
* @return true if creation was successful, otherwise false |
|
|
|
|
*/ |
|
|
|
@ -277,6 +283,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* create linux user |
|
|
|
|
* |
|
|
|
|
* @param userName user name |
|
|
|
|
* @param userGroup user group |
|
|
|
|
* @throws IOException in case of an I/O error |
|
|
|
@ -295,6 +302,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* create mac user (Supports Mac OSX 10.10+) |
|
|
|
|
* |
|
|
|
|
* @param userName user name |
|
|
|
|
* @param userGroup user group |
|
|
|
|
* @throws IOException in case of an I/O error |
|
|
|
@ -323,6 +331,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* create windows user |
|
|
|
|
* |
|
|
|
|
* @param userName user name |
|
|
|
|
* @param userGroup user group |
|
|
|
|
* @throws IOException in case of an I/O error |
|
|
|
@ -347,6 +356,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get system group information |
|
|
|
|
* |
|
|
|
|
* @return system group info |
|
|
|
|
* @throws IOException errors |
|
|
|
|
*/ |
|
|
|
@ -390,6 +400,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* Execute the shell |
|
|
|
|
* |
|
|
|
|
* @param command command |
|
|
|
|
* @return result of execute the shell |
|
|
|
|
* @throws IOException errors |
|
|
|
@ -400,6 +411,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get process id |
|
|
|
|
* |
|
|
|
|
* @return process id |
|
|
|
|
*/ |
|
|
|
|
public static int getProcessID() { |
|
|
|
@ -409,15 +421,16 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* whether is macOS |
|
|
|
|
* |
|
|
|
|
* @return true if mac |
|
|
|
|
*/ |
|
|
|
|
public static boolean isMacOS() { |
|
|
|
|
return getOSName().startsWith("Mac"); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* whether is windows |
|
|
|
|
* |
|
|
|
|
* @return true if windows |
|
|
|
|
*/ |
|
|
|
|
public static boolean isWindows() { |
|
|
|
@ -426,6 +439,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* get current OS name |
|
|
|
|
* |
|
|
|
|
* @return current OS name |
|
|
|
|
*/ |
|
|
|
|
public static String getOSName() { |
|
|
|
@ -434,6 +448,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* check memory and cpu usage |
|
|
|
|
* |
|
|
|
|
* @param systemCpuLoad systemCpuLoad |
|
|
|
|
* @param systemReservedMemory systemReservedMemory |
|
|
|
|
* @return check memory and cpu usage |
|
|
|
@ -454,6 +469,7 @@ public class OSUtils {
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* check memory and cpu usage |
|
|
|
|
* |
|
|
|
|
* @param conf conf |
|
|
|
|
* @param isMaster is master |
|
|
|
|
* @return check memory and cpu usage |
|
|
|
|