[ agent ] [status] - app status - 1

This commit is contained in:
zeaslity
2023-01-06 11:07:34 +08:00
parent 7e8478b7e3
commit 9114568963
6 changed files with 178 additions and 21 deletions

View File

@@ -0,0 +1,89 @@
package io.wdd.agent.executor;
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.concurrent.*;
import java.util.stream.Collectors;
import static io.wdd.agent.status.AppStatusCollector.ALL_APP_NEED_TO_MONITOR_STATUS;
import static java.util.stream.Collectors.groupingBy;
@Service
@Slf4j
public class AppStatusExecutor {
private static List<String> APP_STATUS_CHECK_COMMAND = List.of(
"systemctl",
"status",
"systemd.service",
"|",
"grep",
"-c",
"active (running)"
);
public HashMap<String, Set<String>> checkAppStatus(boolean allAppStatus){
// check all app status
Map<String, List<String[]>> collect = ALL_APP_NEED_TO_MONITOR_STATUS.keySet().stream()
.map(
appName -> {
// generate single app status callable task
CheckSingleAppStatusCallable singleAppStatusCallable = new CheckSingleAppStatusCallable(appName, APP_STATUS_CHECK_COMMAND);
// use thread pool to run the command to get the singe app status result
Future<String[]> appStatusFuture = AgentCommonThreadPool.pool.submit(singleAppStatusCallable);
return appStatusFuture;
}
).map(
// deal with the app status future result
appStatusFuture -> {
try {
return appStatusFuture.get(5, TimeUnit.SECONDS);
} catch (InterruptedException e) {
throw new RuntimeException(e);
} catch (ExecutionException e) {
throw new RuntimeException(e);
} catch (TimeoutException e) {
throw new RuntimeException(e);
}
}
)
.collect(
// group the result
groupingBy(
appStatus -> appStatus[0]
)
);
// uniform the result data
// Healthy -> [Nginx, MySQL, Xray]
// Failure -> [Redis]
// NotInstall -> [Docker]
HashMap<String, Set<String>> result = new HashMap<>(16);
collect.entrySet().stream().map(
entry -> {
String status = entry.getKey();
Set<String> appNameSet = entry.getValue().stream().map(appStatus -> appStatus[1]).collect(Collectors.toSet());
result.put(status, appNameSet);
return 1;
}
).collect(Collectors.toSet());
return result;
}
}

View File

@@ -0,0 +1,57 @@
package io.wdd.agent.executor;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;
import static io.wdd.agent.status.AppStatusCollector.ALL_APP_NEED_TO_MONITOR_STATUS;
@Slf4j
public class CheckSingleAppStatusCallable implements Callable<String[]> {
private final List<String> commandList;
private final String appName;
public CheckSingleAppStatusCallable(String appName, List<String> commandList) {
this.commandList = commandList;
this.appName = appName;
}
@Override
public String[] call() throws Exception {
String[] result = new String[2];
// set the specific app service name
commandList.set(2, ALL_APP_NEED_TO_MONITOR_STATUS.get(appName));
log.debug("current app [{}] status command are => {}", appName, commandList);
ProcessBuilder processBuilder = new ProcessBuilder(commandList);
Process process = processBuilder.start();
boolean waitFor = process.waitFor(20, TimeUnit.SECONDS);
result[1] = appName;
if (ObjectUtils.isNotEmpty(waitFor)) {
log.debug("app status command has accomplished !");
String appStatusCommandResult = new BufferedReader(new InputStreamReader(process.getInputStream())).readLine();
if (appStatusCommandResult.startsWith("1")) {
result[0] = "Healthy";
} else if (appStatusCommandResult.startsWith("0")) {
result[0] = "Failure";
} else {
result[0] = "NotInstall";
}
}
log.debug("app status check ok result is => [ {} ]", result);
return result;
}
}

View File

@@ -276,6 +276,7 @@ EOF
InstallDocker() {
Docker_Source="cn"
local dockerVersion=$(echo $DOCKER_VERSION | cut -d"." -f-2)
if [[ "$1" -ne " " ]]; then
Docker_Source="$1"
@@ -311,9 +312,9 @@ InstallDocker() {
sed -i 's/download.docker.com/mirrors.ustc.edu.cn\/docker-ce/g' /etc/yum.repos.d/docker-ce.repo
colorEcho ${BLUE} "已成功添加中科大的docker-ce的yum源"
echo ""
colorEcho ${BLUE} "可以安装的docker-ce的19.03版本为:"
colorEcho ${BLUE} "可以安装的docker-ce的 ${dockerVersion} 版本为:"
colorEcho ${GREEN} "--------------------------------------------------------------"
yum list docker-ce --showduplicates | grep -w 19.03 | awk '{print$2}' | cut -d ":" -f2 | sort -n -t - -k 1.7
yum list docker-ce --showduplicates | grep -w ${dockerVersion} | awk '{print$2}' | cut -d ":" -f2 | sort -n -t - -k 1.7
colorEcho ${GREEN} "--------------------------------------------------------------"
echo ""
@@ -353,14 +354,14 @@ InstallDocker() {
apt-getMapper update
colorEcho ${GREEN} "----------更新完成----------"
FunctionSuccess
colorEcho ${BLUE} "可以安装的docker-ce的19.03版本为:"
colorEcho ${BLUE} "可以安装的docker-ce的${dockerVersion}版本为:"
colorEcho ${GREEN} "--------------------------------------------------------------"
apt-cache madison docker-ce | grep -w 19.03 | awk '{print$3}'
apt-cache madison docker-ce | grep -w ${dockerVersion} | awk '{print$3}'
colorEcho ${GREEN} "--------------------------------------------------------------"
echo ""
colorEcho ${GREEN} "开始安装docker-ce版本为${DOCKER_VERSION}"
realDockerSTag=$(apt-cache madison docker-ce | grep -w 19.03 | awk '{print$3}' | grep ${DOCKER_VERSION})
realDockerSTag=$(apt-cache madison docker-ce | grep -w ${dockerVersion} | awk '{print$3}' | grep ${DOCKER_VERSION})
installDemandSoftwares docker-ce=${realDockerSTag} || return $?
fi
echo ""