diff --git a/bin/build-check.sh b/bin/build-check.sh new file mode 100755 index 0000000..2d30a20 --- /dev/null +++ b/bin/build-check.sh @@ -0,0 +1,4 @@ +#!/bin/bash +mvn -pl service-dependencies,service-configuration clean deploy -D skipTests -P local -s ~/.m2/settings-development.xml +mvn -pl service-check clean package spring-boot:repackage -D skipTests -s ~/.m2/settings-development.xml +ytp-transfer2 /Users/lanyuanxiaoyao/Project/IdeaProjects/hudi-service/service-check/target/service-check-1.0.0-SNAPSHOT.jar \ No newline at end of file diff --git a/bin/check b/bin/check new file mode 100644 index 0000000..00800e9 --- /dev/null +++ b/bin/check @@ -0,0 +1,7 @@ +#!/bin/bash + +root_path=/apps/zone_scfp/hudi/cloud +jdk_path=/opt/jdk8u252-b09/bin/java + +curl ftp://yyy:QeY\!68\)4nH1@132.121.122.15:2222/service-check-1.0.0-SNAPSHOT.jar -o ${root_path}/service-check.jar +${jdk_path} -jar ${root_path}/service-check.jar diff --git a/pom.xml b/pom.xml index 69c1849..53cc1fd 100644 --- a/pom.xml +++ b/pom.xml @@ -27,6 +27,7 @@ service-executor service-cloud-query service-exporter + service-check diff --git a/service-check/pom.xml b/service-check/pom.xml new file mode 100644 index 0000000..8d98cd8 --- /dev/null +++ b/service-check/pom.xml @@ -0,0 +1,78 @@ + + + 4.0.0 + + com.lanyuanxiaoyao + hudi-service + 1.0.0-SNAPSHOT + + + service-check + + + + org.springframework.boot + spring-boot-starter + + + org.springframework.boot + spring-boot-configuration-processor + true + + + org.eclipse.collections + eclipse-collections + runtime + + + org.eclipse.collections + eclipse-collections-api + + + cn.hutool + hutool-all + + + com.github.ulisesbocchio + jasypt-spring-boot-starter + + + org.apache.pulsar + pulsar-client + + + org.apache.pulsar + pulsar-client-admin + + + mysql + mysql-connector-java + + + com.lanyuanxiaoyao + service-configuration + 1.0.0-SNAPSHOT + + + com.lanyuanxiaoyao + service-dependencies + + + + + + + + + org.apache.maven.plugins + maven-source-plugin + + + org.springframework.boot + spring-boot-maven-plugin + + + + \ No newline at end of file diff --git a/service-check/src/main/java/com/lanyuanxiaoyao/service/check/CheckApplication.java b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/CheckApplication.java new file mode 100644 index 0000000..36a9558 --- /dev/null +++ b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/CheckApplication.java @@ -0,0 +1,38 @@ +package com.lanyuanxiaoyao.service.check; + +import com.lanyuanxiaoyao.service.check.actions.Checker; +import java.util.Map; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.ApplicationContext; + +/** + * 启动类 + * + * @author lanyuanxiaoyao + * @date 2024-02-22 + */ +@SpringBootApplication +public class CheckApplication implements ApplicationRunner { + private static final Logger logger = LoggerFactory.getLogger(CheckApplication.class); + private final ApplicationContext applicationContext; + + public CheckApplication(ApplicationContext applicationContext) {this.applicationContext = applicationContext;} + + public static void main(String[] args) { + SpringApplication.run(CheckApplication.class, args); + } + + @Override + public void run(ApplicationArguments args) throws Exception { + Map beans = applicationContext.getBeansOfType(Checker.class); + for (Checker checker : beans.values()) { + logger.info("Execute checker: {}", checker.description()); + checker.check(); + } + } +} diff --git a/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/Checker.java b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/Checker.java new file mode 100644 index 0000000..f951951 --- /dev/null +++ b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/Checker.java @@ -0,0 +1,40 @@ +package com.lanyuanxiaoyao.service.check.actions; + +import cn.hutool.core.net.NetUtil; +import cn.hutool.core.util.StrUtil; +import java.net.InetSocketAddress; +import java.util.concurrent.TimeUnit; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * 检查 + * + * @author lanyuanxiaoyao + * @date 2024-02-22 + */ +public abstract class Checker { + private static final Logger logger = LoggerFactory.getLogger(Checker.class); + + public abstract void check() throws Exception; + + public abstract String description(); + + protected boolean ping(String url) { + String[] split = url.split(":"); + if (split.length != 2) { + logger.error("Error url {}", url); + return false; + } + InetSocketAddress address = NetUtil.createAddress(split[0], Integer.parseInt(split[1])); + try { + String command = StrUtil.format("ping {} -c 2 -w 2 -q", address.getHostString()); + Process exec = Runtime.getRuntime().exec(command); + if (exec.waitFor(5, TimeUnit.SECONDS)) { + return exec.exitValue() == 0; + } + } catch (Exception ignored) { + } + return false; + } +} diff --git a/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/MysqlChecker.java b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/MysqlChecker.java new file mode 100644 index 0000000..b390725 --- /dev/null +++ b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/MysqlChecker.java @@ -0,0 +1,48 @@ +package com.lanyuanxiaoyao.service.check.actions; + +import com.lanyuanxiaoyao.service.check.configuration.MysqlConfigurationProperties; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.ResultSet; +import java.sql.Statement; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * @author lanyuanxiaoyao + * @date 2024-02-22 + */ +@Component +public class MysqlChecker extends Checker { + private static final Logger logger = LoggerFactory.getLogger(MysqlChecker.class); + + private final MysqlConfigurationProperties mysqlConfigurationProperties; + + public MysqlChecker(MysqlConfigurationProperties mysqlConfigurationProperties) {this.mysqlConfigurationProperties = mysqlConfigurationProperties;} + + @Override + public void check() throws Exception { + Class.forName("com.mysql.cj.jdbc.Driver"); + for (MysqlConfigurationProperties.MysqlInfo target : mysqlConfigurationProperties.getTargets()) { + try (Connection connection = DriverManager.getConnection(target.getUrl(), target.getUsername(), target.getPassword())) { + if (connection.isClosed()) { + logger.warn("Database connect failure {}", target.getUrl()); + continue; + } + try (Statement statement = connection.createStatement()) { + try (ResultSet resultSet = statement.executeQuery("select version()")) { + if (resultSet.next()) { + logger.info("Database version {}", resultSet.getString(1)); + } + } + } + } + } + } + + @Override + public String description() { + return "Check mysql"; + } +} diff --git a/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/PulsarChecker.java b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/PulsarChecker.java new file mode 100644 index 0000000..3c5e4aa --- /dev/null +++ b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/actions/PulsarChecker.java @@ -0,0 +1,70 @@ +package com.lanyuanxiaoyao.service.check.actions; + +import cn.hutool.core.util.StrUtil; +import com.lanyuanxiaoyao.service.configuration.entity.pulsar.PulsarInfo; +import org.apache.pulsar.client.admin.PulsarAdmin; +import org.apache.pulsar.client.api.PulsarClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.stereotype.Component; + +/** + * 主机相关检查 + * + * @author lanyuanxiaoyao + * @date 2024-02-22 + */ +@Component +public class PulsarChecker extends Checker { + private static final Logger logger = LoggerFactory.getLogger(PulsarChecker.class); + + private void checkConnect(String url) { + if (!ping(url)) { + logger.warn("{} ping failure", url); + } + } + + private String adminUrl(PulsarInfo info) { + return StrUtil.format("http://{}/admin/v2", info.getAdmin()); + } + + @Override + public void check() throws Exception { + for (PulsarInfo pulsarInfo : PulsarInfo.DEFAULT_INFOS) { + logger.info("Check pulsar {}", pulsarInfo.getName()); + if (StrUtil.isNotBlank(pulsarInfo.getAdmin())) { + logger.info("Check pulsar admin {}", pulsarInfo.getAdmin()); + if (ping(pulsarInfo.getAdmin())) { + // noinspection EmptyTryBlock + try (PulsarAdmin ignored = PulsarAdmin.builder().serviceHttpUrl(adminUrl(pulsarInfo)).build()) { + } catch (Exception exception) { + logger.warn("Pulsar admin login failure", exception); + } + } else { + logger.warn("{} ping failure", pulsarInfo.getAdmin()); + } + } else { + logger.warn("Pulsar {} admin is empty", pulsarInfo.getName()); + } + logger.info("Check pulsar nodes"); + for (String broker : pulsarInfo.getBrokers()) { + if (!ping(broker)) { + logger.warn("{} ping failure", broker); + } + } + String brokerUrl = "pulsar://" + pulsarInfo.getBrokers().makeString(","); + try (PulsarClient client = PulsarClient.builder() + .serviceUrl(brokerUrl) + .build()) { + if (client.isClosed()) { + logger.warn("Pulsar client failure connect {}", brokerUrl); + } + } + } + } + + @Override + public String description() { + return "Check Pulsar"; + } +} diff --git a/service-check/src/main/java/com/lanyuanxiaoyao/service/check/configuration/MysqlConfigurationProperties.java b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/configuration/MysqlConfigurationProperties.java new file mode 100644 index 0000000..7c76c96 --- /dev/null +++ b/service-check/src/main/java/com/lanyuanxiaoyao/service/check/configuration/MysqlConfigurationProperties.java @@ -0,0 +1,69 @@ +package com.lanyuanxiaoyao.service.check.configuration; + +import java.util.List; +import org.springframework.boot.context.properties.ConfigurationProperties; +import org.springframework.context.annotation.Configuration; + +/** + * @author lanyuanxiaoyao + * @date 2024-02-22 + */ +@Configuration +@ConfigurationProperties(prefix = "checker.mysql") +public class MysqlConfigurationProperties { + private List targets; + + public List getTargets() { + return targets; + } + + public void setTargets(List targets) { + this.targets = targets; + } + + @Override + public String toString() { + return "MysqlConfigurationProperties{" + + "targets=" + targets + + '}'; + } + + public static final class MysqlInfo { + private String url; + private String username; + private String password; + + public String getUrl() { + return url; + } + + public void setUrl(String url) { + this.url = url; + } + + public String getUsername() { + return username; + } + + public void setUsername(String username) { + this.username = username; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) { + this.password = password; + } + + @Override + public String toString() { + return "MysqlInfo{" + + "url='" + url + '\'' + + ", username='" + username + '\'' + + ", password='" + password + '\'' + + '}'; + } + } +} diff --git a/service-check/src/main/resources/application.yml b/service-check/src/main/resources/application.yml new file mode 100644 index 0000000..01ac0f3 --- /dev/null +++ b/service-check/src/main/resources/application.yml @@ -0,0 +1,11 @@ +spring: + application: + name: service-check + profiles: + include: common +checker: + mysql: + targets: + - url: jdbc:mysql://132.121.204.217:17906/hudi_collect_build_2?useSSL=false + username: odcp + password: wFg_fR492#& \ No newline at end of file diff --git a/service-check/src/main/resources/logback-spring.xml b/service-check/src/main/resources/logback-spring.xml new file mode 100644 index 0000000..91f40d3 --- /dev/null +++ b/service-check/src/main/resources/logback-spring.xml @@ -0,0 +1,17 @@ + + + + + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %clr(%5p) %clr([${HOSTNAME}]){yellow} %clr([%t]){magenta} %clr(%logger{40}){cyan} #@# %m%n%wEx + + + + + + + + + \ No newline at end of file