[ 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 ""

View File

@@ -461,6 +461,11 @@ InstallZSH() {
echo ""
modifyZSH ${ZSH_SOURCE}
if [[ $? -eq 0 ]]; then
FunctionStart
colorEcho ${BLUE} "[Octopus Agent] tail -f 500 /var/log/octopus-agent.log "
FunctionSuccess
colorEcho ${BLUE} "开始修改默认shell为zsh……"
for i in {6..1..-1}; do
colorEcho ${BLUE} "倒计时开始 ->> $i 秒 <<-准备切换shell上文的日志输出将会消失"

View File

@@ -2,7 +2,7 @@
##### environment variables ######
JAVA_OPTS="-Xms2048m -Xmx2048m --file.encoding=utf-8 --spring.profiles.active=k3s --spring.cloud.nacos.config.group=k3s --spring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml --spring.cloud.nacos.config.extension-configs[0].group=k3s --debug=false --logging.level.io.wdd.server=info"
JAVA_OPTS="-Xms2048m -Xmx2048m -Dfile.encoding=utf-8 -Dspring.profiles.active=k3s -Dspring.cloud.nacos.config.group=k3s -Dspring.cloud.nacos.config.extension-configs[0].dataId=common-k3s.yaml -Dspring.cloud.nacos.config.extension-configs[0].group=k3s -Ddebug=false -Dlogging.level.io.wdd.server=info"
DOCKER_VERSION="20.10.10"
@@ -259,8 +259,9 @@ EOF
#######################################
InstallDocker() {
Docker_Source="cn"
local dockerVersion=$(echo $DOCKER_VERSION | cut -d"." -f-2)
if [[ $1 -ne " " ]]; then
if [[ "$1" -ne " " ]]; then
Docker_Source="$1"
echo "Docker_Source = ${Docker_Source}"
fi
@@ -281,7 +282,7 @@ InstallDocker() {
echo ""
colorEcho ${GREEN} "当前系统的发行版为-- ${LinuxReleaseVersion}"
FunctionSuccess
if [[ $LinuxReleaseVersion == "centos" ]]; then
if [[ $LinuxReleaseVersion = "centos" ]]; then
## 安装docker的依赖
colorEcho ${BLUE} "正在安装安装docker的依赖"
installDemandSoftwares yum-utils device-mapper-persistent-data lvm2 || return $?
@@ -294,9 +295,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 ""
@@ -311,7 +312,7 @@ InstallDocker() {
installDemandSoftwares apt-transport-https ca-certificates curl gnupg-agent software-properties-common lsb-release
colorEcho ${GREEN} " 基础组件安装成功 "
echo ""
if [[ ${Docker_Source} == "cn" ]]; then
if [[ "${Docker_Source}" == "cn" ]]; then
colorEcho ${BLUE} "开始添加中科大的docker源的apt-key"
curl -fsSL https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/gpg | sudo apt-key add -
colorEcho ${GREEN} " 添加成功 "
@@ -327,24 +328,26 @@ InstallDocker() {
colorEcho ${GREEN} " 添加成功 "
echo ""
colorEcho ${BLUE} "开始添加中科大的docker源的apt源"
echo "deb [arch=$(dpkg --print-architecture) https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list >/dev/null
echo "deb [arch=$(dpkg --print-architecture) https://download.docker.com/linux/ubuntu $(lsb_release -cs) stable" | sudo tee /etc/apt/sources.list.d/docker.list > /dev/null
colorEcho ${GREEN} " 添加成功 "
echo ""
fi
colorEcho ${BLUE} "正在执行更新操作!!"
apt-getMapper update
apt-get 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})
installDemandSoftwares docker-ce=${realDockerSTag} || return $?
local realDockerSTag=$(apt-cache madison docker-ce | grep -w ${dockerVersion} | awk '{print$3}' | grep ${DOCKER_VERSION})
colorEcho $BLUE "current docker version is docker-ce="${realDockerSTag}" "
installDemandSoftwares "docker-ce=${realDockerSTag}" || return $?
fi
echo ""
@@ -540,9 +543,9 @@ BootUPServer() {
FunctionStart
command_exists "docker info"
command_exists "docker"
if [[ $? -ne 0 ]]; then
colorEcho $RED "cant find docker, octopus server can't boot up !"
colorEcho $RED "[ Octopus Server] - can't find docker, octopus server can't boot up !"
return 23
fi
@@ -550,7 +553,7 @@ BootUPServer() {
docker container stop ${OctopusServerContainerName}
sleep 2
docker container rm ${OctopusServerContainerName}
docker image rmi ${OctopusServerContainerName}:latest
docker image rmi docker.io/icederce/wdd-octopus-server:latest
FunctionSuccess
colorEcho $BLUE "[ Octopus Server] - start to boot up octopus server!"

View File

@@ -24,5 +24,7 @@ echo
echo --- end ---
systemctl status nginx.service | grep -c "active (running)"