diff --git a/agent/src/main/java/io/wdd/agent/executor/AppStatusExecutor.java b/agent/src/main/java/io/wdd/agent/executor/AppStatusExecutor.java index 11cd65e..0f20758 100644 --- a/agent/src/main/java/io/wdd/agent/executor/AppStatusExecutor.java +++ b/agent/src/main/java/io/wdd/agent/executor/AppStatusExecutor.java @@ -5,10 +5,7 @@ import io.wdd.agent.config.utils.AgentCommonThreadPool; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Set; +import java.util.*; import java.util.concurrent.*; import java.util.stream.Collectors; @@ -19,15 +16,28 @@ import static java.util.stream.Collectors.groupingBy; @Slf4j public class AppStatusExecutor { - private static List APP_STATUS_CHECK_COMMAND = List.of( - "systemctl", - "status", - "systemd.service", - "|", - "grep", - "-c", - "active (running)" - ); + private static ArrayList> APP_STATUS_CHECK_COMMAND; + + static { + + ArrayList first = new ArrayList<>(List.of( + "systemctl", + "status", + "systemd.service" + )); + + ArrayList second = new ArrayList<>(List.of( + "grep", + "-c", + "active (running)")); + + ArrayList> arrayList = new ArrayList<>(); + arrayList.add(first); + arrayList.add(second); + + APP_STATUS_CHECK_COMMAND = arrayList; + + } public HashMap> checkAppStatus(boolean allAppStatus){ diff --git a/agent/src/main/java/io/wdd/agent/executor/CheckSingleAppStatusCallable.java b/agent/src/main/java/io/wdd/agent/executor/CheckSingleAppStatusCallable.java index d52d3ec..9f647cd 100644 --- a/agent/src/main/java/io/wdd/agent/executor/CheckSingleAppStatusCallable.java +++ b/agent/src/main/java/io/wdd/agent/executor/CheckSingleAppStatusCallable.java @@ -1,10 +1,12 @@ package io.wdd.agent.executor; +import io.wdd.agent.executor.config.CommandPipelineBuilder; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.ObjectUtils; import java.io.BufferedReader; import java.io.InputStreamReader; +import java.util.ArrayList; import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.TimeUnit; @@ -14,10 +16,10 @@ import static io.wdd.agent.status.AppStatusCollector.ALL_APP_NEED_TO_MONITOR_STA @Slf4j public class CheckSingleAppStatusCallable implements Callable { - private final List commandList; + private final ArrayList> commandList; private final String appName; - public CheckSingleAppStatusCallable(String appName, List commandList) { + public CheckSingleAppStatusCallable(String appName, ArrayList> commandList) { this.commandList = commandList; this.appName = appName; } @@ -27,19 +29,23 @@ public class CheckSingleAppStatusCallable implements Callable { String[] result = new String[2]; // set the specific app service name - commandList.set(2, ALL_APP_NEED_TO_MONITOR_STATUS.get(appName)); + commandList.get(0).set(2, ALL_APP_NEED_TO_MONITOR_STATUS.get(appName)); log.debug("current app [{}] status command are => {}", appName, commandList); - ProcessBuilder processBuilder = new ProcessBuilder(commandList); + /* ProcessBuilder processBuilder = new ProcessBuilder(commandList); Process process = processBuilder.start(); - boolean waitFor = process.waitFor(20, TimeUnit.SECONDS); + boolean waitFor = process.waitFor(20, TimeUnit.SECONDS);*/ + + // get result from the command pipeline builder + List resultList = CommandPipelineBuilder.runGetResult(commandList); + result[1] = appName; - if (ObjectUtils.isNotEmpty(waitFor)) { + if (ObjectUtils.isNotEmpty(resultList)) { log.debug("app status command has accomplished !"); - String appStatusCommandResult = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine(); + String appStatusCommandResult = resultList.get(0); if (appStatusCommandResult.startsWith("1")) { result[0] = "Healthy"; diff --git a/agent/src/main/java/io/wdd/agent/executor/config/CommandPipelineBuilder.java b/agent/src/main/java/io/wdd/agent/executor/config/CommandPipelineBuilder.java new file mode 100644 index 0000000..b7333ee --- /dev/null +++ b/agent/src/main/java/io/wdd/agent/executor/config/CommandPipelineBuilder.java @@ -0,0 +1,108 @@ +package io.wdd.agent.executor.config; + + +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.annotation.Lazy; +import org.springframework.stereotype.Component; + +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + + +@Slf4j +public class CommandPipelineBuilder { + + + public static List runGetResult(ArrayList> commandList){ + + try { + List processList = build(commandList); + int lastCommandIndex = commandList.size() - 1; + + List resultList = new BufferedReader(new InputStreamReader(processList.get(lastCommandIndex).getInputStream())).lines().collect(Collectors.toList()); + + log.debug("command => [ {} ] , execute result is [ {} ]", commandList, resultList); + + return resultList; + + } catch (IOException e) { + throw new RuntimeException(e); + } + + } + + public static List build(ArrayList> commandList) throws IOException { + + int length = commandList.size(); + + List result = null; + + switch (length) { + case 2: + result = build(commandList.get(0), commandList.get(1)); + break; + case 3: + result = build(commandList.get(0), commandList.get(1), commandList.get(2)); + break; + case 4: + result = build(commandList.get(0), commandList.get(1), commandList.get(2), commandList.get(3)); + break; + default: + result = build(commandList.get(0)); + break; + } + + return result; + } + + private static List build(List commandList1) throws IOException { + + return ProcessBuilder.startPipeline(List.of( + new ProcessBuilder(commandList1) + .redirectError(ProcessBuilder.Redirect.INHERIT) + )); + } + + private static List build(List commandList1, List commandList2) throws IOException { + + return ProcessBuilder.startPipeline(List.of( + new ProcessBuilder(commandList1) + .inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE), + new ProcessBuilder(commandList2) + .redirectError(ProcessBuilder.Redirect.INHERIT) + )); + } + + + private static List build(List commandList1, List commandList2, List commandList3) throws IOException { + + return ProcessBuilder.startPipeline(List.of( + new ProcessBuilder(commandList1) + .inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE), + new ProcessBuilder(commandList2) + .inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE), + new ProcessBuilder(commandList3) + .redirectError(ProcessBuilder.Redirect.INHERIT) + )); + + } + + private static List build(List commandList1, List commandList2, List commandList3, List commandList4) throws IOException { + + return ProcessBuilder.startPipeline(List.of( + new ProcessBuilder(commandList1) + .inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE), + new ProcessBuilder(commandList2) + .inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE), + new ProcessBuilder(commandList3) + .inheritIO().redirectOutput(ProcessBuilder.Redirect.PIPE), + new ProcessBuilder(commandList4) + .redirectError(ProcessBuilder.Redirect.INHERIT) + )); + } + +}