[ agent ] [status] - app status - 3

This commit is contained in:
zeaslity
2023-01-06 14:13:56 +08:00
parent c9866e354d
commit 31262d267e
3 changed files with 144 additions and 20 deletions

View File

@@ -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<String> APP_STATUS_CHECK_COMMAND = List.of(
private static ArrayList<ArrayList<String>> APP_STATUS_CHECK_COMMAND;
static {
ArrayList<String> first = new ArrayList<>(List.of(
"systemctl",
"status",
"systemd.service",
"|",
"systemd.service"
));
ArrayList<String> second = new ArrayList<>(List.of(
"grep",
"-c",
"active (running)"
);
"active (running)"));
ArrayList<ArrayList<String>> arrayList = new ArrayList<>();
arrayList.add(first);
arrayList.add(second);
APP_STATUS_CHECK_COMMAND = arrayList;
}
public HashMap<String, Set<String>> checkAppStatus(boolean allAppStatus){

View File

@@ -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<String[]> {
private final List<String> commandList;
private final ArrayList<ArrayList<String>> commandList;
private final String appName;
public CheckSingleAppStatusCallable(String appName, List<String> commandList) {
public CheckSingleAppStatusCallable(String appName, ArrayList<ArrayList<String>> commandList) {
this.commandList = commandList;
this.appName = appName;
}
@@ -27,19 +29,23 @@ public class CheckSingleAppStatusCallable implements Callable<String[]> {
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<String> 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";

View File

@@ -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<String> runGetResult(ArrayList<ArrayList<String>> commandList){
try {
List<Process> processList = build(commandList);
int lastCommandIndex = commandList.size() - 1;
List<String> 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<Process> build(ArrayList<ArrayList<String>> commandList) throws IOException {
int length = commandList.size();
List<Process> 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<Process> build(List<String> commandList1) throws IOException {
return ProcessBuilder.startPipeline(List.of(
new ProcessBuilder(commandList1)
.redirectError(ProcessBuilder.Redirect.INHERIT)
));
}
private static List<Process> build(List<String> commandList1, List<String> 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<Process> build(List<String> commandList1, List<String> commandList2, List<String> 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<Process> build(List<String> commandList1, List<String> commandList2, List<String> commandList3, List<String> 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)
));
}
}